diff --git a/docs/smart-contracts/quickstart.md b/docs/smart-contracts/quickstart.md index d95ef608871..0c83b982f91 100644 --- a/docs/smart-contracts/quickstart.md +++ b/docs/smart-contracts/quickstart.md @@ -5,14 +5,14 @@ sidebar_label: Quickstart description: "Create your first contract using your favorite language." --- -import {Github} from '@site/src/components/UI/Codetabs'; +import {Github, Language} from '@site/src/components/UI/Codetabs'; import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; import MovingForwardSupportSection from '@site/src/components/MovingForwardSupportSection'; Welcome! [NEAR accounts](../protocol/account-model.md) can store small apps known as smart contracts. In this quick tutorial, we will guide you in creating your first contract on the NEAR **testnet**! -Join us in creating a friendly contract that stores a greeting, and exposes functions to interact with it. +Join us in creating a friendly auction contract, which allows users to place bids, track the highest bidder and claim tokens at the end of the auction. :::tip Want to jump right into the code without setting up a local dev environment? @@ -72,7 +72,13 @@ curl --proto '=https' --tlsv1.2 -LsSf https://github.com/near/cargo-near/release -```bash +:::note +Python quickstart tutorial is coming soon! + +In the meantime, please check out the [hello-near](https://github.com/near-examples/hello-near-examples/tree/main/contract-py) example. +::: + + -# Linux AMD64 (x86_64) -wget https://github.com/tinygo-org/tinygo/releases/download/v0.37.0/tinygo_0.37.0_amd64.deb -sudo dpkg -i tinygo_0.37.0_amd64.deb - -# Linux ARM64 -wget https://github.com/tinygo-org/tinygo/releases/download/v0.37.0/tinygo_0.37.0_arm64.deb -sudo dpkg -i tinygo_0.37.0_arm64.deb - -# macOS -brew tap tinygo-org/tools -brew install tinygo - -# Install NEAR Go CLI to manage and interact with smart contracts easily -# Alternatively, download it from GitHub Releases and move it manually to your bin folder. - -curl -LO https://github.com/vlmoon99/near-cli-go/releases/latest/download/install.sh && bash install.sh - -``` @@ -218,6 +189,7 @@ hello-near ├── tests # sandbox testing │ └── test_basics.rs ├── Cargo.toml # package manager +├── Cargo.lock # package lock file ├── README.md └── rust-toolchain.toml ``` @@ -242,7 +214,13 @@ You can skip the interactive menu and create a new project with specific name ru -```bash +:::note +Python quickstart tutorial is coming soon! + +In the meantime, please check out the [hello-near](https://github.com/near-examples/hello-near-examples/tree/main/contract-py) example. +::: + + @@ -303,42 +262,38 @@ hello-world/contract ## The Contract -The `Hello World` smart contract stores a greeting in its state, and exposes two functions to interact with it: -1. `set_greeting`: to change the greeting -2. `get_greeting`: to fetch the greeting +The auction smart contract allows users to place bids, track the highest bidder and claim tokens at the end of the auction. Therefore it exposes following methods to interact with it: +1. `init`: to initialize the contract with auction parameters +2. `bid`: to place a bid in the auction +3. `claim`: to claim tokens after the auction ends +4. `get_highest_bid`: to fetch the highest bid and bidder information +5. `get_auction_end_time`: to retrieve the auction end time +6. `get_auctioneer`: to get the auctioneer's account ID +7. `get_claimed`: to check if a bidder has claimed their tokens - - + url="https://github.com/near/create-near-app/blob/V9/templates/contracts/auction/ts/src/contract.ts" + start="9" end="72" /> - - + url="https://github.com/near/cargo-near/blob/update-new-template/cargo-near/src/commands/new/new-project-template/src/lib.rs" + start="23" end="103" /> + :::note + Python quickstart tutorial is coming soon! - - - - - - Edit a `main.go` file for your contract: - - + In the meantime, please check out the [hello-near](https://github.com/near-examples/hello-near-examples/tree/main/contract-py) example. + ::: + @@ -379,7 +334,13 @@ Building and testing the contract is as simple as running the `test` command. Th - ```bash + :::note + Python quickstart tutorial is coming soon! + + In the meantime, please check out the [hello-near](https://github.com/near-examples/hello-near-examples/tree/main/contract-py) example. + ::: + + - In the background, these commands are calling the build tools for each language and using a [Sandbox](./testing/integration-test.md) to test the contract. @@ -499,7 +432,7 @@ When running the near account create-account command in a headless Linux environ When you are ready to create a build of the contract run a one-line command depending on your environment. - + ```bash @@ -525,7 +458,13 @@ When you are ready to create a build of the contract run a one-line command depe - ```bash + :::note + Python quickstart tutorial is coming soon! + + In the meantime, please check out the [hello-near](https://github.com/near-examples/hello-near-examples/tree/main/contract-py) example. + ::: + + - - ```bash - near-go build - ``` @@ -605,7 +538,13 @@ Having our account created, we can now deploy the contract: - + :::note + Python quickstart tutorial is coming soon! + + In the meantime, please check out the [hello-near](https://github.com/near-examples/hello-near-examples/tree/main/contract-py) example. + ::: + + - **Congrats**! Your contract now lives in the NEAR testnet network. @@ -657,47 +574,47 @@ To interact with your deployed smart contract, you can call its functions throug
-#### Get Greeting -Let's start by fetching the greeting stored in the contract. The `get_greeting` function only reads from the contract's state, and can thus be called for **free**. +#### Initialize the Contract +Let's start by initializing the contract with the auction parameters. The `init` method sets up the auction with an end time and the auctioneer's account ID. It can be called only by contract's account itself. ```bash - > near view get_greeting - # "Hello, NEAR world!" + > near call init '{"end_time": "", "auctioneer": ""}' --accountId ``` ```bash - > near contract call-function as-read-only get_greeting json-args {} network-config testnet now - # "Hello, NEAR world!" + > near contract call-function as-transaction init json-args '{"end_time": "", "auctioneer": ""}' prepaid-gas '30.0 Tgas' attached-deposit '0 NEAR' sign-as network-config testnet sign-with-keychain send ``` +:::note + `` should be replaced with the account ID of the auctioneer, which can be the same as `` or a different account. +::: +
-#### Set Greeting +#### Place a Bid -We can now change the greeting stored in the contract. The `set_greeting` method writes on the contract's [storage](./anatomy/storage.md), and thus requires a user to sign a transaction in order to be executed. +We can now place a bid in the auction using the `bid` method. This method allows users to place their bids by attaching a deposit. The highest bid and bidder information will be updated accordingly and will be stored on the contract's [storage](./anatomy/storage.md), and thus requires a user to sign a transaction in order to be executed (as well as attaching a deposit). ```bash - > near call set_greeting '{"greeting": "Hola"}' --accountId - # {"success": true} + > near call bid '{}' --deposit 0.01 --accountId ``` ```bash - > near contract call-function as-transaction set_greeting json-args '{"greeting": "Hola"}' prepaid-gas '100.0 Tgas' attached-deposit '0 NEAR' sign-as network-config testnet sign-with-keychain send - # {"success": true} + > near contract call-function as-transaction bid json-args {} prepaid-gas '30.0 Tgas' attached-deposit '0.01 NEAR' sign-as network-config testnet sign-with-keychain send ``` @@ -708,6 +625,115 @@ Notice that we are signing the transaction using ``, so in this ::: +
+ +#### Claim + +After the auction ends, the highest bidder can claim their tokens using the `claim` method. Actually, anyone can call this method on behalf of the highest bidder to transfer the tokens to them. This method requires a signed transaction but does not require any deposit. + + + + + ```bash + > near call claim '{}' --accountId + ``` + + + + + ```bash + > near contract call-function as-transaction claim json-args {} prepaid-gas '30.0 Tgas' attached-deposit '0 NEAR' sign-as network-config testnet sign-with-keychain send + ``` + + + +
+ +#### Get Highest Bid + +The `get_highest_bid` function only reads from the contract's state, and can thus be called for **free**. + + + + + ```bash + > near view get_highest_bid '{}' + ``` + + + + + ```bash + > near contract call-function as-read-only get_highest_bid json-args {} network-config testnet now + ``` + + + +
+ +#### Get Auction End Time + +Same as `get_highest_bid`, the `get_auction_end_time` function only reads from the contract's state, and can thus be called for **free**. + + + + + ```bash + > near view get_auction_end_time '{}' + ``` + + + + + ```bash + > near contract call-function as-read-only get_auction_end_time json-args {} network-config testnet now + ``` + + + +
+ +#### Get Auctioneer + +Same as `get_highest_bid`, the `get_auctioneer` function only reads from the contract's state, and can thus be called for **free**. + + + + + ```bash + > near view get_auctioneer '{}' + ``` + + + + + ```bash + > near contract call-function as-read-only get_auctioneer json-args {} network-config testnet now + ``` + + + +
+ +#### Get Claimed + +Same as `get_highest_bid`, the `get_claimed` function only reads from the contract's state, and can thus be called for **free**. + + + + + ```bash + > near view get_claimed '{}' + ``` + + + + + ```bash + > near contract call-function as-read-only get_claimed json-args {} network-config testnet now + ``` + + --- @@ -725,10 +751,10 @@ If you prefer to see more examples, check our [examples](/tutorials/examples/cou At the time of this writing, this example works with the following versions: -- node: `20.18.0` -- rustc: `1.81.0` -- near-cli-rs: `0.17.0` -- cargo-near: `0.13.2` +- node: `22.18.0` +- rustc: `1.86.0` +- near-cli-rs: `0.22.0` +- cargo-near: `0.16.1` - Python: `3.13` - near-sdk-py: `0.7.3` - uvx nearc: `0.9.2`