Skip to content

Commit

Permalink
Add Rust Tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
CapCap authored and aptos-bot committed Mar 13, 2022
1 parent 550688d commit 2d50bb5
Show file tree
Hide file tree
Showing 15 changed files with 1,826 additions and 14 deletions.
25 changes: 23 additions & 2 deletions developer-docs-site/docs/tutorials/first-move-module.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ Now we return to our application to deploy and interact with the module on the A

</TabItem>
<TabItem value="rust" label="Rust" default>

```rust
:!: static/examples/rust/hello_blockchain/src/lib.rs section_1
```

</TabItem>
<TabItem value="typescript" label="Typescript" default>

Expand All @@ -154,6 +159,11 @@ The module is published at an address. This is the `contract_address` below. Thi

</TabItem>
<TabItem value="rust" label="Rust" default>

```rust
:!: static/examples/rust/hello_blockchain/src/lib.rs section_2
```

</TabItem>
<TabItem value="typescript" label="Typescript" default>

Expand All @@ -180,6 +190,11 @@ Note: while the REST interface can display strings, due to limitations of JSON a

</TabItem>
<TabItem value="rust" label="Rust" default>

```rust
:!: static/examples/rust/hello_blockchain/src/lib.rs section_3
```

</TabItem>
<TabItem value="typescript" label="Typescript" default>

Expand All @@ -202,8 +217,14 @@ For Python3:
* Execute the example: `python3 hello_blockchain.py Message.mv`

</TabItem>
<TabItem value="rust" label="Rust">
</TabItem>
<TabItem value="rust" label="Rust">
For Rust:

* Download the [example project](https://github.com/aptos-labs/aptos-core/tree/main/developer-docs-site/static/examples/rust)
* Open your favorite terminal and navigate to where you downloaded the above example project
* Execute the example: `cargo run --bin hello-blockchain -- Message.mv`

</TabItem>
<TabItem value="typescript" label="Typescript">
For Typescript:

Expand Down
34 changes: 34 additions & 0 deletions developer-docs-site/docs/tutorials/first-transaction.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ Each Aptos account has a unique account address. The owner of that account hold
</TabItem>
<TabItem value="rust" label="Rust" default>

```rust
:!: static/examples/rust/first_transaction/src/lib.rs section_1
```

</TabItem>
<TabItem value="typescript" label="Typescript" default>

Expand All @@ -79,6 +83,11 @@ Aptos exposes a [REST interface][rest_spec] for interacting with the blockchain.

</TabItem>
<TabItem value="rust" label="Rust" default>

```rust
:!: static/examples/rust/first_transaction/src/lib.rs section_2
```

</TabItem>
<TabItem value="typescript" label="Typescript" default>

Expand All @@ -102,6 +111,11 @@ The following are wrappers for querying account data.

</TabItem>
<TabItem value="rust" label="Rust" default>

```rust
:!: static/examples/rust/first_transaction/src/lib.rs section_3
```

</TabItem>
<TabItem value="typescript" label="Typescript" default>

Expand All @@ -124,6 +138,11 @@ The following demonstrate the core functionality for constructing, signing, and

</TabItem>
<TabItem value="rust" label="Rust" default>

```rust
:!: static/examples/rust/first_transaction/src/lib.rs section_4
```

</TabItem>
<TabItem value="typescript" label="Typescript" default>

Expand All @@ -147,6 +166,11 @@ The following demonstrate how to read data from the blockchain and how to write

</TabItem>
<TabItem value="rust" label="Rust" default>

```rust
:!: static/examples/rust/first_transaction/src/lib.rs section_5
```

</TabItem>
<TabItem value="typescript" label="Typescript" default>

Expand All @@ -170,6 +194,11 @@ A blockchain faucet provides an account some amount of tokens that can be used f

</TabItem>
<TabItem value="rust" label="Rust" default>

```rust
:!: static/examples/rust/first_transaction/src/lib.rs section_6
```

</TabItem>
<TabItem value="typescript" label="Typescript" default>

Expand All @@ -191,6 +220,11 @@ A blockchain faucet provides an account some amount of tokens that can be used f

</TabItem>
<TabItem value="rust" label="Rust" default>

```rust
:!: static/examples/rust/first_transaction/src/main.rs section_7
```

</TabItem>
<TabItem value="typescript" label="Typescript" default>

Expand Down
3 changes: 2 additions & 1 deletion developer-docs-site/src/remark/code-injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ const plugin = (options) => {
if (!matches) {
throw new Error(`Could not find open/closing tags for section '${sectionName}' in ${filepath}`);
}
node.value = matches[1].trim();
// Remove line breaks from start/end, but not whitespace
node.value = matches[1].replace(/^[\r\n]+|[\r\n]+$/g, "");
}
});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def sign_transaction(self, account_from: Account, txn_request: Dict[str, Any]) -

res = requests.post(f"{self.url}/transactions/signing_message", json=txn_request)
assert res.status_code == 200, res.text
to_sign = bytes.fromhex(res.json()['message'][2:])
to_sign = bytes.fromhex(res.json()["message"][2:])
signature = account_from.signing_key.sign(to_sign).signature
txn_request["signature"] = {
"type": "ed25519_signature",
Expand Down Expand Up @@ -131,7 +131,7 @@ def account_balance(self, account_address: str) -> Optional[int]:
return int(resource["data"]["coin"]["value"])
return None

def transfer(self, account_from: Account, recipient: str, amount: int) -> (int, str):
def transfer(self, account_from: Account, recipient: str, amount: int) -> str:
"""Transfer a given coin amount from a given Account to the recipient's account address.
Returns the sequence number of the transaction used to transfer."""

Expand All @@ -147,7 +147,7 @@ def transfer(self, account_from: Account, recipient: str, amount: int) -> (int,
txn_request = self.generate_transaction(account_from.address(), payload)
signed_txn = self.sign_transaction(account_from, txn_request)
res = self.submit_transaction(signed_txn)
return int(signed_txn["sequence_number"]), str(res["hash"])
return str(res["hash"])
#<:!:section_5

#:!:>section_6
Expand Down Expand Up @@ -190,7 +190,7 @@ def fund_account(self, pub_key: str, amount: int) -> None:
print(f"Bob: {rest_client.account_balance(bob.address())}")

# Have Alice give Bob 10 coins
seq_no, tx_hash = rest_client.transfer(alice, bob.address(), 1_000)
tx_hash = rest_client.transfer(alice, bob.address(), 1_000)
rest_client.wait_for_transaction(tx_hash)

print("\n=== Final Balances ===")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Copyright (c) The Diem Core Contributors
# SPDX-License-Identifier: Apache-2.0

from typing import Any, Dict, Optional, Sequence
from typing import Optional
import sys

from first_transaction import Account, FaucetClient, RestClient, TESTNET_URL, FAUCET_URL
Expand Down
Loading

0 comments on commit 2d50bb5

Please sign in to comment.