Skip to content

Commit db5339b

Browse files
committed
update dependencies, code structure, tests, README
1 parent d77f507 commit db5339b

File tree

27 files changed

+913
-1155
lines changed

27 files changed

+913
-1155
lines changed

.cargo/config

Lines changed: 0 additions & 2 deletions
This file was deleted.

.gitpod.Dockerfile

Lines changed: 0 additions & 8 deletions
This file was deleted.

.gitpod.yml

Lines changed: 0 additions & 7 deletions
This file was deleted.

Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "fungible-token"
3+
version = "1.0.0"
4+
authors = ["Near Inc <[email protected]>"]
5+
edition = "2018"
6+
7+
[lib]
8+
crate-type = ["cdylib", "rlib"]
9+
10+
[dependencies]
11+
near-sdk = "5.5.0"
12+
near-contract-standards = "5.5.0"
13+
14+
[dev-dependencies]
15+
near-sdk = { version = "5.5.0", features = ["unit-testing"] }
16+
near-workspaces = { version = "0.14.1", features = ["unstable"] }
17+
anyhow = "1.0"
18+
tokio = { version = "1.41.0", features = ["full"] }

README-Windows.md

Lines changed: 0 additions & 112 deletions
This file was deleted.

README.md

Lines changed: 39 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,144 +1,58 @@
1-
Fungible Token (FT)
2-
===================
1+
Fungible Token (FT) Example 👋
32

4-
Example implementation of a [Fungible Token] contract which uses [near-contract-standards] and [simulation] tests. This is a contract-only example.
3+
[![](https://img.shields.io/badge/⋈%20Examples-Basics-green)](https://docs.near.org/tutorials/welcome)
4+
[![](https://img.shields.io/badge/Contract-Rust-red)](contract-rs)
55

6-
[Fungible Token]: https://nomicon.io/Standards/FungibleToken/Core
7-
[near-contract-standards]: https://github.com/near/near-sdk-rs/tree/master/near-contract-standards
8-
[simulation]: https://github.com/near/near-sdk-rs/tree/master/near-sdk-sim
6+
This repository contains an example implementation of a [fungible token] contract in Rust which uses [near-contract-standards] and workspaces-rs tests.
97

10-
Prerequisites
11-
=============
8+
[fungible token]: https://nomicon.io/Standards/FungibleToken/Core
9+
[near-contract-standards]: https://github.com/near/near-sdk-rs/tree/master/near-contract-standards
10+
[near-workspaces-rs]: https://github.com/near/near-workspaces-rs
1211

13-
If you're using Gitpod, you can skip this step.
12+
<br />
1413

15-
1. Make sure Rust is installed per the prerequisites in [`near-sdk-rs`](https://github.com/near/near-sdk-rs#pre-requisites)
16-
2. Ensure `near-cli` is installed by running `near --version`. If not installed, install with: `npm install -g near-cli`
14+
## How to Build Locally?
1715

18-
## Building
16+
Install [`cargo-near`](https://github.com/near/cargo-near) and run:
1917

20-
To build run:
2118
```bash
22-
./scripts/build.sh
19+
cargo near build
2320
```
2421

25-
Using this contract
26-
===================
27-
28-
### Quickest deploy
29-
30-
You can build and deploy this smart contract to a development account. [Dev Accounts](https://docs.near.org/concepts/basics/account#dev-accounts) are auto-generated accounts to assist in developing and testing smart contracts. Please see the [Standard deploy](#standard-deploy) section for creating a more personalized account to deploy to.
22+
## How to Test Locally?
3123

3224
```bash
33-
near dev-deploy --wasmFile res/fungible_token.wasm --helperUrl https://near-contract-helper.onrender.com
25+
cargo test
3426
```
3527

36-
Behind the scenes, this is creating an account and deploying a contract to it. On the console, notice a message like:
28+
## How to Deploy?
3729

38-
>Done deploying to dev-1234567890123
39-
40-
In this instance, the account is `dev-1234567890123`. A file has been created containing a key pair to
41-
the account, located at `neardev/dev-account`. To make the next few steps easier, we're going to set an
42-
environment variable containing this development account id and use that when copy/pasting commands.
43-
Run this command to the environment variable:
30+
To deploy manually, install [`cargo-near`](https://github.com/near/cargo-near) and run:
4431

4532
```bash
46-
source neardev/dev-account.env
47-
```
48-
49-
You can tell if the environment variable is set correctly if your command line prints the account name after this command:
50-
```bash
51-
echo $CONTRACT_NAME
52-
```
33+
# Create a new account
34+
cargo near create-dev-account
5335

54-
The next command will initialize the contract using the `new` method:
36+
# Deploy the contract on it
37+
cargo near deploy <contract-account-id>
5538

56-
```bash
57-
near call $CONTRACT_NAME new '{"owner_id": "'$CONTRACT_NAME'", "total_supply": "1000000000000000", "metadata": { "spec": "ft-1.0.0", "name": "Example Token Name", "symbol": "EXLT", "decimals": 8 }}' --accountId $CONTRACT_NAME
39+
# Initialize the contract
40+
near call <contract-account-id> new '{"owner_id": "<contract-account-id>", "total_supply": "1000000000000000", "metadata": { "spec": "ft-1.0.0", "name": "Example Token Name", "symbol": "EXLT", "decimals": 8 }}' --accountId <contract-account-id>
5841
```
5942

60-
To get the fungible token metadata:
61-
43+
## Basic methods
6244
```bash
63-
near view $CONTRACT_NAME ft_metadata
64-
```
65-
66-
### Standard deploy
67-
68-
This smart contract will get deployed to your NEAR account. For this example, please create a new NEAR account. Because NEAR allows the ability to upgrade contracts on the same account, initialization functions must be cleared. If you'd like to run this example on a NEAR account that has had prior contracts deployed, please use the `near-cli` command `near delete`, and then recreate it in Wallet. To create (or recreate) an account, please follow the directions on [NEAR Wallet](https://wallet.near.org/).
69-
70-
Switch to `mainnet`. You can skip this step to use `testnet` as a default network.
71-
72-
export NEAR_ENV=mainnet
73-
74-
In the project root, log in to your newly created account with `near-cli` by following the instructions after this command:
75-
76-
near login
77-
78-
To make this tutorial easier to copy/paste, we're going to set an environment variable for your account id. In the below command, replace `MY_ACCOUNT_NAME` with the account name you just logged in with, including the `.near`:
79-
80-
ID=MY_ACCOUNT_NAME
81-
82-
You can tell if the environment variable is set correctly if your command line prints the account name after this command:
83-
84-
echo $ID
85-
86-
Now we can deploy the compiled contract in this example to your account:
87-
88-
near deploy --wasmFile res/fungible_token.wasm --accountId $ID
89-
90-
FT contract should be initialized before usage. You can read more about metadata at ['nomicon.io'](https://nomicon.io/Standards/FungibleToken/Metadata.html#reference-level-explanation). Modify the parameters and create a token:
91-
92-
near call $ID new '{"owner_id": "'$ID'", "total_supply": "1000000000000000", "metadata": { "spec": "ft-1.0.0", "name": "Example Token Name", "symbol": "EXLT", "decimals": 8 }}' --accountId $ID
93-
94-
Get metadata:
95-
96-
near view $ID ft_metadata
45+
# View metadata
46+
near view <contract-account-id> ft_metadata
9747

48+
# Make a storage deposit
49+
near call <contract-account-id> storage_deposit '' --accountId <account-id> --amount 0.00125
9850

99-
Transfer Example
100-
---------------
51+
# View balance
52+
near view <contract-account-id> ft_balance_of '{"account_id": "<account-id>"}'
10153

102-
Let's set up an account to transfer some tokens to. These account will be a sub-account of the NEAR account you logged in with.
103-
104-
near create-account bob.$ID --masterAccount $ID --initialBalance 1
105-
106-
Add storage deposit for Bob's account:
107-
108-
near call $ID storage_deposit '' --accountId bob.$ID --amount 0.00125
109-
110-
111-
Check balance of Bob's account, it should be `0` for now:
112-
113-
near view $ID ft_balance_of '{"account_id": "'bob.$ID'"}'
114-
115-
Transfer tokens to Bob from the contract that minted these fungible tokens, exactly 1 yoctoNEAR of deposit should be attached:
116-
117-
near call $ID ft_transfer '{"receiver_id": "'bob.$ID'", "amount": "19"}' --accountId $ID --amount 0.000000000000000000000001
118-
119-
120-
Check the balance of Bob again with the command from before and it will now return `19`.
121-
122-
## Testing
123-
124-
As with many Rust libraries and contracts, there are tests in the main fungible token implementation at `ft/src/lib.rs`.
125-
126-
Additionally, this project has [simulation] tests in `tests/sim`. Simulation tests allow testing cross-contract calls, which is crucial to ensuring that the `ft_transfer_call` function works properly. These simulation tests are the reason this project has the file structure it does. Note that the root project has a `Cargo.toml` which sets it up as a workspace. `ft` and `test-contract-defi` are both small & focused contract projects, the latter only existing for simulation tests. The root project imports `near-sdk-sim` and tests interaction between these contracts.
127-
128-
You can run unit tests with the following command:
129-
130-
```bash
131-
cd ft && cargo test -- --nocapture --color=always
132-
```
133-
134-
You can run integration tests with the following commands:
135-
*Rust*
136-
```bash
137-
cd integration-tests/rs && cargo run --example integration-tests
138-
```
139-
*TypeScript*
140-
```bash
141-
cd integration-tests/ts && yarn && yarn test
54+
# Transfer tokens
55+
near call <contract-account-id> ft_transfer '{"receiver_id": "<account-id>", "amount": "19"}' --accountId <contract-account-id> --amount 0.000000000000000000000001
14256
```
14357

14458
## Notes
@@ -147,14 +61,13 @@ cd integration-tests/ts && yarn && yarn test
14761
- JSON calls should pass U128 as a base-10 string. E.g. "100".
14862
- This does not include escrow functionality, as `ft_transfer_call` provides a superior approach. An escrow system can, of course, be added as a separate contract or additional functionality within this contract.
14963

150-
## No AssemblyScript?
151-
152-
[near-contract-standards] is currently Rust-only. We strongly suggest using this library to create your own Fungible Token contract to ensure it works as expected.
153-
154-
Someday NEAR core or community contributors may provide a similar library for AssemblyScript, at which point this example will be updated to include both a Rust and AssemblyScript version.
155-
156-
## Contributing
157-
158-
When making changes to the files in `ft` or `test-contract-defi`, remember to use `./build.sh` to compile all contracts and copy the output to the `res` folder. If you forget this, **the simulation tests will not use the latest versions**.
64+
## Useful Links
15965

160-
Note that if the `rust-toolchain` file in this repository changes, please make sure to update the `.gitpod.Dockerfile` to explicitly specify using that as default as well.
66+
- [cargo-near](https://github.com/near/cargo-near) - NEAR smart contract development toolkit for Rust
67+
- [near CLI](https://near.cli.rs) - Iteract with NEAR blockchain from command line
68+
- [NEAR Rust SDK Documentation](https://docs.near.org/sdk/rust/introduction)
69+
- [NEAR Documentation](https://docs.near.org)
70+
- [NEAR StackOverflow](https://stackoverflow.com/questions/tagged/nearprotocol)
71+
- [NEAR Discord](https://near.chat)
72+
- [NEAR Telegram Developers Community Group](https://t.me/neardev)
73+
- NEAR DevHub: [Telegram](https://t.me/neardevhub), [Twitter](https://twitter.com/neardevhub)

ft/Cargo.toml

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)