Skip to content

Commit

Permalink
complete auction
Browse files Browse the repository at this point in the history
  • Loading branch information
PiVortex committed Oct 10, 2024
1 parent b3b8274 commit 15251af
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 281 deletions.
37 changes: 19 additions & 18 deletions docs/3.tutorials/auction/0-intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@ We'll start from a simple auction contract and slowly build on top of it to crea

By the time you finish this tutorial, you will have learned several concepts and how to use many key privitives along the way:

- [Creating a simple smart contract](./1.1-basic.md)
- [Creating a simple smart contract](./1.1-basic.md#the-contracts-state)
- [Writing tests for a contract](./1.2-testing.md)
- [Deploying a contract to testnet](./1.3-deploy.md)

<!-- - [Locking a contract](./2-locking.md)
- [Making cross-contract calls](./3-nft.md#transferring-the-nft-to-the-winner)
- [Using Non-Fungible Tokens](./3-nft.md)
- [Using Fungible Tokens](./4-ft.md)
- [Creating a frontend to interact with the contract](./5-frontend.md)
- [Using an indexing API to keep track of the contract's activity](./6-indexing.md)
- [Modifying a contract factory to deploy your own contracts](./7-factory.md) -->
- [Locking a contract](./1.3-deploy.md#locking-the-contract)
- [Creating a frontend to interact with the contract](./2.1-frontend.md)
- [Using an indexing API to view historical bids](./6-indexing.md)
- [Making cross-contract calls](./3.1-nft.md#transferring-the-nft-to-the-winner)
- [Using Non-Fungible Tokens](./3.1-nft.md)
- [Using Fungible Tokens](./3.2-ft.md)
- [Modifying a factory contract to deploy your own contracts](./4-factory.md)

---

Expand Down Expand Up @@ -80,21 +79,23 @@ We will be using [NEAR CLI](../../4.tools/cli.md) to interact with the blockchai

This series will touch on different level of the NEAR tech stack. Each section will be independent of the previous one, so feel free to jump into the section that interests you the most.

#### 1. Smart Contract
#### 1. Smart contracts 101
1. [The Auction Contract](./1.1-basic.md): We cover a simple auction smart contract
2. [Testing the Contract](./1.2-testing.md): Learn how to test your contract in a realistic environment
3. [Deploying the Contract](./1.3-deploy.md): Deploy your contract to the NEAR blockchain
4. Updating and Locking a Contract (soon): Discover what it means to lock a contract
5. Giving an NFT to the Winner (soon) : Give the highest bidder an NFT to signal their win
6. Integrating Fungible Tokens (soon) : Allow people to use fungible tokens to bid (e.g. stable coins)

#### 2. Frontend
#### 2. Frontends 101

1. [Creating the frontend](./2.1-frontend.md): Lets learn how to connect a frontend with your smart contract
2. [indexing historical data](./2.2-indexing.md): Use APIs to keep track of historical bids

1. Creating the frontend (soon): Lets learn how to connect a frontend with your smart contract
2. Easily query on-chain data (soon): Use open APIs to keep track of the users and their bidding price
#### 3. Using Primitives
1. [Giving an NFT to the Winner](./3.1-nft.md): Give the highest bidder an NFT to signal their win
2. [Integrating Fungible Tokens](./3.2-ft.md): Allow people to use fungible tokens to bid (e.g. stable coins)
3. [Updating the frontend](./3.3-new-frontend.md): Update the frontend to use the extended functionality of the contract.

#### 3. Factory
1. Creating a factory (soon): Allow users to easily deploy and initialize their own auction contracts
#### 3. Auction Factory
1. [Creating a factory](./4-factory.md): Allow users to easily deploy and initialize their own auction contracts

---

Expand Down
4 changes: 2 additions & 2 deletions docs/3.tutorials/auction/1.2-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ When testing we should also check that the contract does not allow invalid calls
<TabItem value="rust" label="🦀 Rust">

<Github fname="test_basics.rs" language="rust"
url="https://github.com/near-examples/auctions-tutorial/blob/reorg-auction/contract-rs/01-basic-auction/tests/test_basics.rs"
url="https://github.com/near-examples/auctions-tutorial/blob/reorg-auction/contract-rs/01-basic-auction/tests/test_basics.rs#L77-L83"
start="77" end="83" />

</TabItem>
Expand All @@ -159,7 +159,7 @@ After which the auction can now be claimed. Once claimed the test checks that th
<TabItem value="rust" label="🦀 Rust">

<Github fname="test_basics.rs" language="rust"
url="https://github.com/near-examples/auctions-tutorial/blob/reorg-auction/contract-rs/01-basic-auction/tests/test_basics.rs#L96-L112"
url="https://github.com/near-examples/auctions-tutorial/blob/reorg-auction/contract-rs/01-basic-auction/tests/test_basics.rs#L95-L112"
start="95" end="112" />

</TabItem>
Expand Down
2 changes: 1 addition & 1 deletion docs/3.tutorials/auction/1.3-deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ We are now ready to start bidding by calling the `bid` function on the contract.

```bash
# call the contract to bid
near call <contractId> bid --accountId <bidderId> --amount 1
near call <contractId> bid --accountId <bidderId> --amount 1

# get the highest bid
near view <contractId> get_highest_bid
Expand Down
37 changes: 34 additions & 3 deletions docs/3.tutorials/auction/3.1-nft.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ No one will enter an auction if there's nothing to win, so let's add a prize. Wh

When we create an auction we need to list the NFT. To specify which NFT is being auctioned off we need the account ID of the NFT contract and the token ID of the NFT. We will specify these when the contract is initialized; amend `init` to add `nft_contract` and `token_id` as such:


<Tabs groupId="code-tabs">

<TabItem value="js" label="🌐 JavaScript">
Expand Down Expand Up @@ -85,6 +84,38 @@ In our contract, we perform no checks to verify whether the contract actually ow

---

## Displaying the contract object

Since we are now dealing with more information in our contract, instead of implementing a function to display each field we'll create a function to display the entire contract object. Since the contract doesn't include large complex data structures like a map displaying the contract state in its entirety is easily done.

<Tabs groupId="code-tabs">

<TabItem value="js" label="🌐 JavaScript">

<Github fname="main.ava.js" language="javascript"
url="https://github.com/near-examples/auctions-tutorial/blob/reorg-auction/contract-ts/02-winner-gets-nft/src/contract.ts#L73-L76"
start="73" end="76" />

</TabItem>

<TabItem value="rust" label="🦀 Rust">

<Github fname="lib.rs" language="rust"
url="https://github.com/near-examples/auctions-tutorial/blob/reorg-auction/contract-rs/02-winner-gets-nft/src/lib.rs#L17-L26"
start="17" end="26" />

We add the `serilizers` macro to enable json serialization so the object as a whole can easily be displayed to the frontend without having to output each field individually.

<Github fname="lib.rs" language="rust"
url="https://github.com/near-examples/auctions-tutorial/blob/reorg-auction/contract-rs/02-winner-gets-nft/src/lib.rs#L107-L109"
start="107" end="109" />


</TabItem>

</Tabs>


## Testing with multiple contracts

In our tests, we're now going to be using two contracts; the auction contract and an NFT contract. Sandbox testing is great as it allows us to test multiple contracts in a realistic environment.
Expand Down Expand Up @@ -171,8 +202,8 @@ After `claim` is called, the test should verify that the auction winner now owns

If you would like to interact with the new contract via the CLI you can mint an NFT from a pre-deployed NFT contract

```
near contract call-function as-transaction nft.examples.testnet nft_mint json-args '{"token_id": "TYPE_A_UNIQUE_VALUE_HERE", "receiver_id": "<accountId>", "metadata": { "title": "GO TEAM", "description": "The Team Goes", "media": "https://bafybeidl4hjbpdr6u6xvlrizwxbrfcyqurzvcnn5xoilmcqbxfbdwrmp5m.ipfs.dweb.link/", "copies": 1}}' prepaid-gas '100.0 Tgas' attached-deposit '0.1 NEAR' sign-as <accountId> network-config testnet
```bash
near call nft.examples.testnet nft_mint '{"token_id": "TYPE_A_UNIQUE_VALUE_HERE", "receiver_id": "<accountId>", "metadata": { "title": "GO TEAM", "description": "The Team Goes", "media": "https://bafybeidl4hjbpdr6u6xvlrizwxbrfcyqurzvcnn5xoilmcqbxfbdwrmp5m.ipfs.dweb.link/", "copies": 1}}' --accountId <accountId> --deposit 0.1
```

You can also just buy an NFT with testnet $NEAR on a testnet marketplace like [Mintbase](https://testnet.mintbase.xyz/explore/new/0).
Expand Down
24 changes: 12 additions & 12 deletions docs/3.tutorials/auction/3.2-ft.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ When we want to return the funds to the previous bidder we now make a cross-cont

<Language value="javascript" language="javascript">
<Github fname="call"
url="https://github.com/near-examples/auctions-tutorial/blob/reorg-auction/contract-ts/03-bid-with-fts/src/contract.ts#L69-L75"
start="69" end="75" />
url="https://github.com/near-examples/auctions-tutorial/blob/reorg-auction/contract-ts/03-bid-with-fts/src/contract.ts#L45-L51"
start="45" end="51" />
<Github fname="callback"
url="https://github.com/near-examples/auctions-tutorial/blob/reorg-auction/contract-ts/03-bid-with-fts/src/contract.ts#L78-L81"
start="78" end="81" />
start="68" end="71" />
</Language>

In JavaScript, we have to return the Promise to transfer the FTs but we also need to return how much to refund the user. So after transferring the FTs, we make a `callback` to our own contract to resume the contract flow. Note that the callback is private so it can only be called by the contract. We return 0 because the method uses all the FTs in the call.
Expand Down Expand Up @@ -150,7 +150,7 @@ When we want to return the funds to the previous bidder we now make a cross-cont

</Tabs>

If the calls was to fail the FT contract will automatically refund the user their FTs.
If the call was to fail the FT contract will automatically refund the user their FTs.

<details>
Expand All @@ -173,8 +173,8 @@ When the auction is complete we need to send the fungible tokens to the auctione
<TabItem value="js" label="🌐 JavaScript">

<Github fname="contract.ts" language="javascript"
url="https://github.com/near-examples/auctions-tutorial/blob/reorg-auction/contract-ts/03-bid-with-fts/src/contract.ts#L49-L53"
start="49" end="53" />
url="https://github.com/near-examples/auctions-tutorial/blob/reorg-auction/contract-ts/03-bid-with-fts/src/contract.ts#L61-L65"
start="61" end="65" />

In JavaScript, since we need to return each cross-contract call we chain the NFT and FT transfer.

Expand Down Expand Up @@ -377,14 +377,14 @@ When deploying the contract make sure to specify the FT contract `dai.fakes.test

The auction contract will need to be registered as well, you could do this by sending it an arbitrary amount of $DAI from the faucet or you can just register it since it doesn't need any FTs. You should also register the auctioneer,

```
near contract call-function as-transaction dai.fakes.testnet storage_deposit json-args '{"account_id": "<auctionContractId>"}' prepaid-gas '100.0 Tgas' attached-deposit '0.1 NEAR'
```bash
near call dai.fakes.testnet storage_deposit '{"account_id": "<auctionContractId>"}' --accountId <accountId> --deposit 0.1
```

Now you can go ahead and place a bid. DAI has 18 decimals meaning that 1 $DAI is made up of 10^24 smallest units. To make a bid of 2 $DAI you can use the command:

```
near contract call-function as-transaction dai.fakes.testnet ft_transfer_call json-args '{"receiver_id": "<auctionContractId>", "amount": "2000000000000000000", "msg": ""}' prepaid-gas '100.0 Tgas' attached-deposit '1 yoctoNEAR'
```bash
near call dai.fakes.testnet ft_transfer_call '{"receiver_id": "<auctionContractId>", "amount": "2000000000000000000", "msg": ""}' --accountId <bidderId> --depositYocto 1
```

## Auction architecture
Expand Down Expand Up @@ -423,6 +423,6 @@ However, this architecture could be deemed less secure since if a bad actor were

In this section, you learned a lot about fungible tokens: how to send and receive FTs in a smart contract, and then in sandbox tests how to deploy and initialize an FT contract, how to register a user in an FT contract, and send them some tokens, how to attach FTs to a smart contract call and finally how to view the FT balance of a user. With that, we now have our completed auction smart contract!

Taking a further step back we've taken a very simple auction contract and transformed it into a more production contract with thorough testing. To improve the auction we learned how to make a contract more secure by locking it, added a prize by introducing NFTs, and enabled auctioneers to host auctions with FTs.
Taking a further step back we've taken a very simple auction contract and transformed it into a more production contract with thorough testing. To improve the auction we learned how to add a prize by introducing NFTs, and enabled auctioneers to host auctions with FTs.

Up to now, we've just interacted with the contract via the CLI. In the [next part](./5-frontend.md), we'll learn the basics of creating frontends for NEAR contracts by creating a simple frontend for our auction contract so users can seamlessly interact with it.
In the [next part of the tutorial](./3.3-new-frontend.md), we're going to update the frontend to interact with the new features of the contract.
Loading

0 comments on commit 15251af

Please sign in to comment.