• Stars
    star
    117
  • Rank 301,828 (Top 6 %)
  • Language
    Rust
  • License
    MIT License
  • Created over 4 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Example implementations of money-like tokens, where one token is the same as any other, using the NEP-141 spec (similar to ERC-20)

Fungible Token (FT)

Example implementation of a Fungible Token contract which uses near-contract-standards and simulation tests. This is a contract-only example.

Prerequisites

If you're using Gitpod, you can skip this step.

  1. Make sure Rust is installed per the prerequisites in near-sdk-rs
  2. Ensure near-cli is installed by running near --version. If not installed, install with: npm install -g near-cli

Building

To build run:

./scripts/build.sh

Using this contract

Quickest deploy

You can build and deploy this smart contract to a development account. Dev Accounts are auto-generated accounts to assist in developing and testing smart contracts. Please see the Standard deploy section for creating a more personalized account to deploy to.

near dev-deploy --wasmFile res/fungible_token.wasm --helperUrl https://near-contract-helper.onrender.com

Behind the scenes, this is creating an account and deploying a contract to it. On the console, notice a message like:

Done deploying to dev-1234567890123

In this instance, the account is dev-1234567890123. A file has been created containing a key pair to the account, located at neardev/dev-account. To make the next few steps easier, we're going to set an environment variable containing this development account id and use that when copy/pasting commands. Run this command to the environment variable:

source neardev/dev-account.env

You can tell if the environment variable is set correctly if your command line prints the account name after this command:

echo $CONTRACT_NAME

The next command will initialize the contract using the new method:

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

To get the fungible token metadata:

near view $CONTRACT_NAME ft_metadata

Standard deploy

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.

Switch to mainnet. You can skip this step to use testnet as a default network.

export NEAR_ENV=mainnet

In the project root, log in to your newly created account with near-cli by following the instructions after this command:

near login

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:

ID=MY_ACCOUNT_NAME

You can tell if the environment variable is set correctly if your command line prints the account name after this command:

echo $ID

Now we can deploy the compiled contract in this example to your account:

near deploy --wasmFile res/fungible_token.wasm --accountId $ID

FT contract should be initialized before usage. You can read more about metadata at 'nomicon.io'. Modify the parameters and create a token:

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

Get metadata:

near view $ID ft_metadata

Transfer Example

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.

near create-account bob.$ID --masterAccount $ID --initialBalance 1

Add storage deposit for Bob's account:

near call $ID storage_deposit '' --accountId bob.$ID --amount 0.00125

Check balance of Bob's account, it should be 0 for now:

near view $ID ft_balance_of '{"account_id": "'bob.$ID'"}'

Transfer tokens to Bob from the contract that minted these fungible tokens, exactly 1 yoctoNEAR of deposit should be attached:

near call $ID ft_transfer '{"receiver_id": "'bob.$ID'", "amount": "19"}' --accountId $ID --amount 0.000000000000000000000001

Check the balance of Bob again with the command from before and it will now return 19.

Testing

As with many Rust libraries and contracts, there are tests in the main fungible token implementation at ft/src/lib.rs.

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.

You can run unit tests with the following command:

cd ft && cargo test -- --nocapture --color=always

You can run integration tests with the following commands: Rust

cd integration-tests/rs && cargo run --example integration-tests

TypeScript

cd integration-tests/ts && yarn && yarn test

Notes

  • The maximum balance value is limited by U128 (2**128 - 1).
  • JSON calls should pass U128 as a base-10 string. E.g. "100".
  • 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.

No AssemblyScript?

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.

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.

Contributing

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.

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.

More Repositories

1

NFT

Example implementations of tokens to represent unique assets, such as collectibles or deeds, using the NEP-171 spec (similar to ERC-721)
Rust
346
star
2

nft-market

NFT & Marketplace Contracts with royalties and fungible token support. Sample React app included.
Rust
144
star
3

nft-tutorial

Rust
98
star
4

guest-book-as

Sign in with NEAR and add a message to the guest book!
JavaScript
76
star
5

counter-rust-deprecated

Simple counter in Rust. Increment, decrement, and reset. See branches for extended usage.
JavaScript
61
star
6

rust-status-message

Set and retrieve status messages per account with this simple smart contract.
JavaScript
46
star
7

wallet-example

This example demonstrates how to integrate your application with NEAR Wallet.
JavaScript
40
star
8

workshop--exploring-assemblyscript-contracts

Learn to develop contracts for NEAR Protocol using AssemblyScript
TypeScript
38
star
9

counter-as

Increment and decrement a counter with this simple smart contract via a web page.
JavaScript
35
star
10

rust-template

Rust
27
star
11

token-factory

Allows anyone to issue new fungible tokens
JavaScript
27
star
12

pow-faucet

Proof of Work Faucet for NEAR accounts to gain Ⓝ (NEAR denomination) from a React app.
JavaScript
23
star
13

token-contract-as

Implementation of token contract similar to ERC20.
JavaScript
22
star
14

nft-onboarding

A more detailed example of progressive onboarding using an NFT mint + sale shop example.
JavaScript
21
star
15

near-multichain

Examples for signing and executing transactions across multiple blockchain protocols from one NEAR account.
JavaScript
19
star
16

poker

Decentralized poker using NEAR blockchain as a backend.
Rust
18
star
17

workshop--exploring-near-apis

Learn to work with NEAR APIs
TypeScript
18
star
18

coin-flip-examples

A random coin-flip that lives in the NEAR blockchain
JavaScript
16
star
19

nft-tutorial-frontend

Rust
15
star
20

nft-tutorial-js

TypeScript
15
star
21

chainsig-script

Script to sign TX for Ethereum and Bitcoin, using Near Protocol MPC.
TypeScript
15
star
22

nearnames

Gift someone a named near account ID with a link. Rotates the main full access key and secures receiver of new account.
JavaScript
12
star
23

rust-fungible-token

Create a fungible token on a NEAR account. Additionally, use escrow functionality to send tokens on behalf of another account.
Rust
12
star
24

transaction-examples

Examples of how to construct, sign, and send transactions on the NEAR blockchain.
JavaScript
11
star
25

place

NEAR Place - draw with pixels to earn more pixels
JavaScript
11
star
26

crossword-tutorial-chapter-1

This repo covers the first chapter from the tutorial teaching Rust smart contract development on NEAR
JavaScript
11
star
27

near-account-utils

Small command line demo using near-api-js and JSON RPC API
JavaScript
10
star
28

near-pet-shop

Compile and migrate simple Solidity contracts using Truffle to the NEAR EVM
JavaScript
10
star
29

hello-near-js-deprecated

Hello NEAR in JavaScript!
JavaScript
8
star
30

staking-pool-factory

Frontend for the Staking Pool Factory on betanet
JavaScript
7
star
31

hello-near-rust-deprecated

Hello NEAR in Rust!
JavaScript
7
star
32

token-printer

Faucet using Proof of Work to distribute Ⓝ. The token printer goes BRRR…..
JavaScript
7
star
33

ft-tutorial

Rust
7
star
34

near-lake-accounts-watcher

A source code for a video tutorial on how to use the [NEAR Lake Framework](https://github.com/near/near-lake-framework)
Rust
6
star
35

guest-book-js-deprecated

About Sign in with NEAR and add a message to the guest book!
JavaScript
6
star
36

simulation-testing

Demonstration of simulation testing - a category of testing allowing (direct) cross-contract calls
Rust
5
star
37

nft-series

mint editions of an NFT series (with individual owner permissions) from a single contract on NEAR Protocol
Rust
5
star
38

bos-gateway-deprecated

The simplest gateway for BOS
TypeScript
5
star
39

collection-examples-rs

JavaScript
5
star
40

indexer-tx-watcher-example-lake

NEAR Lake Framework indexer example that watches for transaction for specified accounts/contracts
Rust
5
star
41

near-lake-raw-printer

An example of NEAR Lake Framework usage that prints the raw data from the stream
Rust
4
star
42

rust-high-level-cross-contract

Explore various cross-contract calls written in Rust using a localnet and the cli.
Rust
4
star
43

simple_counter_as

An example of creating a AssemblyScript NEAR contract by hand.
WebAssembly
4
star
44

storage-examples

Rust
4
star
45

indexer-tx-watcher-example

NEAR Indexer example that watches for transaction for specified accounts/contracts
Rust
4
star
46

crud-tutorial

JavaScript
4
star
47

near-lake-nft-indexer

Source code for the tutorial
TypeScript
4
star
48

escrow-js

A simple example of a NEAR escrow contract in JavaScript
JavaScript
4
star
49

evm-simple

Interact with the NEAR EVM in 42 lines of code.
HTML
3
star
50

cross-contract-hello-rust

An example on how to perform the simplest cross-contract call in NEAR
Rust
3
star
51

hello-near-examples

👋 Hello NEAR
JavaScript
3
star
52

near-dashboard

A toy project to produce a cool looking CLI dashboard for the NEAR platform.
Shell
3
star
53

chainlink-demo

This demonstrates how a smart contract on NEAR can access off-chain data via Chainlink's oracle solution.
JavaScript
3
star
54

update-migrate-rust

An example on how to perform self-updates and state migrations in NEAR Rust contracts
Rust
3
star
55

donation-js-deprecated

JavaScript donations contract seen in our documentation.
JavaScript
3
star
56

free-place

Unlimited NEAR Place - draw for free. We're paying for you
JavaScript
3
star
57

crossword-tutorial-chapter-2

Second chapter in the zero-to-hero guide for Rust smart contract development
JavaScript
3
star
58

collection-examples-as

Examples for using NEAR AssemblyScript collections
JavaScript
3
star
59

coingecko-oracle

Python
2
star
60

guest-book-rust-deprecated

A guest book that lives in the NEAR network
JavaScript
2
star
61

rust-linkdrop-app

JavaScript
2
star
62

guest-book-tutorial

Sign in with NEAR and add a message to the guest book!
JavaScript
2
star
63

cross-contract-calls

This repo contains examples for cross contract calls in Rust, JS, as well as an advanced cross contract call example
Rust
2
star
64

cross-contract-calls-tutorial

Zero to hero tutorial for cross-contract calls.
Rust
2
star
65

hackathon-starter-kit

This starter kit includes 3 project templates
JavaScript
2
star
66

crossword-tutorial-chapter-3

Adding cross-contract calls and an interesting use of access keys to our crossword puzzle.
JavaScript
2
star
67

jestnet

Run jest JS tests on testnet
Rust
2
star
68

donation-rust-deprecated

Donation example in rust
JavaScript
2
star
69

counter-js-deprecated

Simple counter in JavaScript. Increment, decrement, and reset.
JavaScript
2
star
70

donation-examples

JavaScript
1
star
71

rust-multisig-example

A multisig contract example app. Deploy the contract and contract the multisig via React App.
JavaScript
1
star
72

activity--TEMPLATE

A template for workshop activities using AssemblyScript contracts
JavaScript
1
star
73

rust-filehash

A map of file hashes to account ids
Rust
1
star
74

cross-contract-rust-counter

A simple extension to the rust counter with the addition of cross-contract calls
Rust
1
star
75

contract-that-deploys-contracts-rs

Rust
1
star
76

NEAR-Arweave-Tutorial

A tutorial on how to store information on Arweave and mint the ID on NEAR
Rust
1
star
77

oracle

1
star
78

BlockVote-JS-Edition

C
1
star
79

lake-indexer-start-options

The source code for the tutorial
Rust
1
star
80

indexer-tutorials

A code related to indexer tutorials from official documentation
Rust
1
star
81

NEAR-SIA-SKYNET-Tutorial

Quick tutorial on how to integrate Sia'
Rust
1
star
82

webrtc-chat

[WIP] WebRTC based video calls using NEAR to establish connection
JavaScript
1
star
83

bos-components

A template for teams to collaborate on BOS components
JavaScript
1
star