Skip to content

Commit

Permalink
Merge branch 'master' into near-api-section
Browse files Browse the repository at this point in the history
  • Loading branch information
bucanero authored Oct 16, 2024
2 parents 457ffb1 + b34ce57 commit 5628fa9
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 51 deletions.
40 changes: 20 additions & 20 deletions docs/2.build/2.smart-contracts/anatomy/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Smart contracts expose functions so users can interact with them. There are diff

<ExplainCode languages="js,rust">

<Block highlights='{"js": "14-17,20-39,42-44,47-49", "rust": "22-30,33-58,60-62,64-66"}' fname="auction">
<Block highlights='{"js": "16-20,23-42,45-50,53-55,58-60", "rust": "24-34,37-62,64-75,77-79,81-83"}' fname="auction">

### Contract's Interface

Expand Down Expand Up @@ -45,49 +45,49 @@ impl MyTrait for MyContractStructure {

</Block>

<Block highlights='{"js":"13-17", "rust": "22-30"}' fname="auction">
<Block highlights='{"js":"15-20", "rust": "22-34"}' fname="auction">

### Initialization Functions
A contract can opt to have an initialization function. If present, this function must be called before any other to [initialize the contract](./storage.md).

</Block>

<Block highlights='{"js": "13"}' fname="auction">
<Block highlights='{"js": "15"}' fname="auction">

#### `@initialize({ privateFunction: true })`
The initialization function is marked with the `@initialize` decorator.

</Block>

<Block highlights='{"rust": "20"}' fname="auction">
<Block highlights='{"rust": "22"}' fname="auction">

#### `#[init]`
Read-only functions are those that take an **immutable** reference to `self` in Rust.
The initialization function is marked with the `#[init]` macro.

</Block>

<Block highlights='{"js":"14-17", "rust": "33-58"}' fname="auction">
<Block highlights='{"js":"22-42,44-50", "rust": "37-62,64-75"}' fname="auction">

### State Changing Functions
The functions that modify the [state](./storage.md) or perform [actions](./actions.md) need to be called by a user with a NEAR account, since a transaction is required to execute them.

</Block>

<Block highlights='{"js": "19"}' fname="auction">
<Block highlights='{"js": "22,44"}' fname="auction">

#### `@call`
State changing functions are marked with the `@call` decorator.

</Block>

<Block highlights='{"rust": "33"}' fname="auction">
<Block highlights='{"rust": "37,64"}' fname="auction">

#### `mut &self`
State changing functions are those that take a **mutable** reference to `self` in Rust.

</Block>

<Block highlights='{"js": "22,26", "rust": "36,42"}' fname="auction" type='info'>
<Block highlights='{"js": "25,28,29", "rust": "40,45,46"}' fname="auction" type='info'>

:::tip

Expand All @@ -97,28 +97,28 @@ The SDK provides [contextual information](./environment.md), such as which accou

</Block>

<Block highlights='{"js":"42-44,47-49", "rust": "60-62,64-66"}' fname="auction">
<Block highlights='{"js":"52-55,57-60", "rust": "77-79,81-83"}' fname="auction">

### Read-Only Functions
Contract's functions can be read-only, meaning they don't modify the state. Calling them is free for everyone, and does not require to have a NEAR account.

</Block>

<Block highlights='{"js": "41,46"}' fname="auction">
<Block highlights='{"js": "52,57"}' fname="auction">

#### `@view`
Read-only functions are marked with the `@view` decorator in TS/JS.

</Block>

<Block highlights='{"rust": "60,64"}' fname="auction">
<Block highlights='{"rust": "77,81"}' fname="auction">

#### `&self`
Read-only functions are those that take an **immutable** reference to `self` in Rust.

</Block>

<Block highlights='{"js":"13", "rust": "21"}' fname="auction">
<Block highlights='{"js":"15", "rust": "23"}' fname="auction">

### Private Functions
Many times you will want to have functions that **are exposed** as part of the contract's interface, but **should not be called directly** by users.
Expand All @@ -129,21 +129,21 @@ These functions are marked as `private` in the contract's code, and can only be

</Block>

<Block highlights='{"js": "13"}' fname="auction">
<Block highlights='{"js": "15"}' fname="auction">

#### `decorator({privateFunction: true})`
Private functions are marked by setting `privateFunction: true` in the `@call` or `@initialization` decorators.

</Block>

<Block highlights='{"rust": "21"}' fname="auction">
<Block highlights='{"rust": "23"}' fname="auction">

#### [#private]
Private functions are marked using the `#[private]` macro in Rust.

</Block>

<Block highlights='{"js":"19,25", "rust": "32,41"}' fname="auction">
<Block highlights='{"js":"22,28", "rust": "36,45"}' fname="auction">

### Payable Functions
By default, functions will panic if the user attaches NEAR Tokens to the call. Functions that accept NEAR Tokens must be marked as `payable`.
Expand All @@ -152,14 +152,14 @@ Within the function, the user will have access to the [attached deposit](./envir

</Block>

<Block highlights='{"js": "19,25"}' fname="auction">
<Block highlights='{"js": "22,28"}' fname="auction">

#### `@call({payableFunction: true})`
Payable functions are marked by setting `payableFunction: true` in the `@call` decorator.

</Block>

<Block highlights='{"rust": "32,41"}' fname="auction">
<Block highlights='{"rust": "36,45"}' fname="auction">

#### [#payable]
Payable functions are marked using the `#[payable]` macro in Rust.
Expand Down Expand Up @@ -224,9 +224,9 @@ They are useful to return hardcoded values on the contract.

</Block>

<File language="js" fname="auction" url="https://github.com/near-examples/auction-examples/blob/main/contract-ts/01-basic-auction/src/contract.ts" start="2" end="51" />
<File language="js" fname="auction" url="https://github.com/near-examples/auctions-tutorial/blob/main/contract-ts/01-basic-auction/src/contract.ts" start="2" end="61" />

<File language="rust" fname="auction" url="https://github.com/near-examples/auction-examples/blob/main/contract-rs/01-basic-auction/src/lib.rs" start="2" end="72" />
<File language="rust" fname="auction" url="https://github.com/near-examples/auctions-tutorial/blob/main/contract-rs/01-basic-auction/src/lib.rs" start="2" end="84" />

<CodeBlock language="js" fname="example">

Expand Down
18 changes: 9 additions & 9 deletions docs/2.build/2.smart-contracts/anatomy/storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ It is important to know that the account's **code** and account's **storage** ar

<ExplainCode languages="js,rust" >

<Block highlights='{"js": "3-6,10-11"}' fname="auction">
<Block highlights='{"js": "3-6,10-13"}' fname="auction">

### Defining the State
The attributes of the `class` marked as the contract define the data that will be stored.
Expand All @@ -30,7 +30,7 @@ It is important to know that the account's **code** and account's **storage** ar

</Block>

<Block highlights='{"rust": "6-9,14,15"}' fname="auction">
<Block highlights='{"rust": "6-9,13-18"}' fname="auction">

### Defining the State
The attributes of the `struct` marked as the contract define the data that will be stored.
Expand Down Expand Up @@ -69,7 +69,7 @@ It currently costs ~**1Ⓝ** to store **100KB** of data.

</Block>

<Block highlights='{"js": "8,13-17"}' fname="auction">
<Block highlights='{"js": "8,15-20"}' fname="auction">

### I. Initialization Functions
An option to initialize the state is to create an `initialization` function, which needs to be called before executing any other function.
Expand All @@ -80,7 +80,7 @@ It currently costs ~**1Ⓝ** to store **100KB** of data.

</Block>

<Block highlights='{"js": "10-11"}' fname="auction" type='info'>
<Block highlights='{"js": "10-13"}' fname="auction" type='info'>

:::warning

Expand All @@ -90,7 +90,7 @@ In TS/JS you still **must** set default values for the attributes, so the SDK ca

</Block>

<Block highlights='{"rust": "12,22-30"}' fname="auction">
<Block highlights='{"rust": "12,22-34"}' fname="auction">

### I. Initialization Functions
An option to initialize the state is to create an `initialization` function, which needs to be called before executing any other function.
Expand Down Expand Up @@ -153,16 +153,16 @@ Make sure to read the [updating a contract](../release/upgrade.md) if you run in
</Block>

<File language="js" fname="auction"
url="https://github.com/near-examples/auction-examples/blob/main/contract-ts/01-basic-auction/src/contract.ts"
start="2" end="51" />
url="https://github.com/near-examples/auctions-tutorial/blob/main/contract-ts/01-basic-auction/src/contract.ts"
start="2" end="60" />

<File language="js" fname="hello"
url="https://github.com/near-examples/hello-near-examples/blob/main/contract-ts/src/contract.ts"
start="2" end="18" />

<File language="rust" fname="auction"
url="https://github.com/near-examples/auction-examples/blob/main/contract-rs/01-basic-auction/src/lib.rs"
start="2" end="72" />
url="https://github.com/near-examples/auctions-tutorial/blob/main/contract-rs/01-basic-auction/src/lib.rs"
start="2" end="83" />

<File language="rust" fname="hello"
url="https://github.com/near-examples/hello-near-examples/blob/main/contract-rs/src/lib.rs"
Expand Down
16 changes: 8 additions & 8 deletions docs/2.build/2.smart-contracts/anatomy/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Lets discuss which types smart contracts use to input and output data, as well a

</Block>

<Block highlights='{"rust": "1,15,22,64"}' fname="auction" type='info'>
<Block highlights='{"rust": "1,15,24,81"}' fname="auction" type='info'>

:::warning `U64/U128`

Expand Down Expand Up @@ -71,7 +71,7 @@ To simplify development, the SDK provides the `U64` and `U128` types which are a

</Block>

<Block highlights='{"js": "5,11,25,47"}' fname="auction">
<Block highlights='{"js": "5,10,28"}' fname="auction">

### Handling Tokens
`$NEAR` tokens are typed as `BigInt` in JS, and their values represented in `yoctonear`
Expand All @@ -80,7 +80,7 @@ To simplify development, the SDK provides the `U64` and `U128` types which are a

</Block>

<Block highlights='{"rust": "8,26,41"}' fname="auction">
<Block highlights='{"rust": "8,28,45"}' fname="auction">

### Handling Tokens
`$NEAR` tokens are handled through the `NearToken` struct, which exposes methods to represent the value in `yoctonear`, `milinear` and `near`
Expand All @@ -89,7 +89,7 @@ To simplify development, the SDK provides the `U64` and `U128` types which are a

</Block>

<Block highlights='{"js": "4", "rust": "7"}' fname="auction">
<Block highlights='{"js": "4,29", "rust": "7,46"}' fname="auction">

### Account
The SDK exposes a special type to handle NEAR Accounts, which automatically checks if the account address is valid
Expand All @@ -105,11 +105,11 @@ To simplify development, the SDK provides the `U64` and `U128` types which are a
start="2" end="32" />

<File language="js" fname="auction"
url="https://github.com/near-examples/auction-examples/blob/main/contract-ts/01-basic-auction/src/contract.ts"
start="2" end="51" />
url="https://github.com/near-examples/auctions-tutorial/blob/main/contract-ts/01-basic-auction/src/contract.ts"
start="2" end="61" />

<File language="rust" fname="auction"
url="https://github.com/near-examples/auction-examples/blob/main/contract-rs/01-basic-auction/src/lib.rs"
start="2" end="72" />
url="https://github.com/near-examples/auctions-tutorial/blob/main/contract-rs/01-basic-auction/src/lib.rs"
start="2" end="84" />

</ExplainCode>
16 changes: 8 additions & 8 deletions docs/3.tutorials/auction/1.3-deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,13 @@ To deploy the contract, you need to compile the contract code into WebAssembly (
npm run build

# deploy the contract
near deploy <contractId> ./build/auction.wasm
near deploy <contractId> ./build/auction-contract.wasm

# initialize the contract, it finishes in 2 minutes
MINUTE_FROM_NOW=$(date -v+2M +%s000000000)
near call <contractId> init '{"end_time": "'$MINUTE_FROM_NOW'", "auctioneer": "<auctioneerId>"}' --accountId <contractId>
TWO_MINUTES_FROM_NOW=$(date -v+2M +%s000000000)
near call <contractId> init '{"end_time": "'$TWO_MINUTES_FROM_NOW'", "auctioneer": "<auctioneerId>"}' --accountId <contractId>
```

Replace `<auctioneerId>` with the name of another account, this should not be the same as the contract account as we intend on removing its keys.

</TabItem>

<TabItem value="rust" label="🦀 Rust">
Expand All @@ -73,17 +71,19 @@ To deploy the contract, you need to compile the contract code into WebAssembly (
cargo near build

# deploy the contract
near deploy <contractId> ./target/near/contract_rs.wasm
near deploy <contractId> ./target/near/auction-contract.wasm

# initialize the contract, it finishes in 2 minutes
MINUTE_FROM_NOW=$(date -v+2M +%s000000000)
near call <contractId> init '{"end_time": "'$MINUTE_FROM_NOW'"}' --accountId <contractId>
TWO_MINUTES_FROM_NOW=$(date -v+2M +%s000000000)
near call <contractId> init '{"end_time": "'$TWO_MINUTES_FROM_NOW'", "auctioneer": "<auctioneerId>"}' --accountId <contractId>
```

</TabItem>

</Tabs>

Replace `<auctioneerId>` with the name of another account, this should not be the same as the contract account as we intend on removing its keys.

---

## Locking the contract
Expand Down
10 changes: 4 additions & 6 deletions docs/3.tutorials/auction/4-factory.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,17 @@ In this fork, we have also removed the option to add an access key to the contra
Build and deploy the factory like you would any other contract, this time without any initialization parameters.

```bash
# compile the contract using cargo-near
cargo near build
```

then

```bash
cargo near deploy <accountId> without-init-call network-config testnet sign-with-legacy-keychain send
# deploy the contract
near deploy <contractId> ./target/near/contract.wasm
```

You can now use the factory to deploy new auction contracts, here is an example command.

```bash
near call auction-factory.testnet deploy_new_auction '{"name": "new-auction", "end_time": "3000000000000000000", "auctioneer": "pivortex.testnet", "ft_contract": "dai.fakes.testnet", "nft_contract": "nft.examples.testnet", "token_id": "7777", "starting_price": "1000000000000000000"}' --accountId <accountId> --deposit '1.6 NEAR'
near call auction-factory.testnet deploy_new_auction '{"name": "new-auction", "end_time": "3000000000000000000", "auctioneer": "pivortex.testnet", "ft_contract": "dai.fakes.testnet", "nft_contract": "nft.examples.testnet", "token_id": "7777", "starting_price": "1000000000000000000"}' --accountId pivortex.testnet --deposit 1.6 --gas 100000000000000
```

:::info Deposit and storage costs
Expand Down

0 comments on commit 5628fa9

Please sign in to comment.