Git Product home page Git Product logo

near-examples / ft Goto Github PK

View Code? Open in Web Editor NEW
119.0 5.0 99.0 1019 KB

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

Home Page: https://github.com/nearprotocol/NEPs/blob/master/specs/Standards/Tokens/FungibleToken.md

License: MIT License

Rust 76.05% Shell 0.51% Batchfile 0.50% JavaScript 0.73% TypeScript 22.22%
ready-to-use rust cli

ft's Introduction

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.

ft's People

Contributors

amgando avatar anaisurlichs avatar benkurrek avatar bucanero avatar chadoh avatar dependabot-preview[bot] avatar dzarezenko avatar frol avatar gagdiez avatar idea404 avatar janedegtiareva avatar jonathansampson avatar mattlockyer avatar mikedotexe avatar pablolion avatar thisisjoshford avatar vgrichina avatar volovyks avatar zavodil avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

ft's Issues

When call storage_balance_of() method, Unhandled Rejection (Error): [-32700] occured.

I'm developing Dapp on NEAR protocol.

And I want to cross call method.

I'm writing FT contract's storage_balance_of() method before call ross call method.
But Unhandled Rejection (Error): [-32700] occured.

What are the possible causes and countermeasures?

Unhandled Rejection (Error): [-32700] Parse error: Failed parsing args: missing field `account_id`

スクリーンショット 2022-11-06 14 34 07

frontend source code is here.

/**
 * get storage balance of account id function
 */
export async function storage_balance_of(account_id) {  
  let balance = await window.ftContract.storage_balance_of({
    account_id: account_id,
  });
  return balance;
}

reporting error in AssemblyScript build

via km1tz on Discord:

"Error is thrown in build time inside near-sdk-core package."

Error Message

ERROR TS2304: Cannot find name 'encode'.
       this.setBytes(key, encode<T>(value));
 in ~lib/near-sdk-core/storage.ts(158,26)

ERROR TS2304: Cannot find name 'decode'.
     return decode<T>(bytes);
 in ~lib/near-sdk-core/util.ts(46,12)

"While inspecting further, I can see that encode and decode functions are defined inside near-sdk-core\assembly\as_types.d.ts and cannot figure out where is the problem. I've tried updating near-sdk-core and typescript compiler to latest version to latest version but it did not make any difference."

Replicated Locally

yarn build:as
yarn run v1.22.5
$ ./contracts/assemblyscript/build.js

compiling contract [ nep21-basic/main.ts ] to [ out/nep21-basic-as.wasm ]
 asconfig.js is deprecated use `asb`to build files.  NOTE: output now in ./build/release/ 
asc /Users/jlwaugh/Desktop/FT/contracts/assemblyscript/nep21-basic/main.ts -O3z --debug --measure --runPasses inlining-optimizing,dce --optimizeLevel 3 --shrinkLevel 3 --target release --binaryFile build/release/main.wasm
ERROR TS2304: Cannot find name 'encode'.

       this.setBytes(key, encode<T>(value));
                          ~~~~~~
 in ~lib/near-sdk-core/storage.ts(158,26)

ERROR TS2304: Cannot find name 'decode'.

     return decode<T>(bytes);
            ~~~~~~
 in ~lib/near-sdk-core/util.ts(46,12)

/Users/jlwaugh/Desktop/FT/node_modules/near-sdk-bindgen/compiler.js:19
        throw new Error(err);
        ^

Error: Error: 2 compile error(s)
    at cb (/Users/jlwaugh/Desktop/FT/node_modules/near-sdk-bindgen/compiler.js:19:15)
    at Object.main (/Users/jlwaugh/Desktop/FT/node_modules/assemblyscript/cli/asc.js:717:12)
    at compileProject (/Users/jlwaugh/Desktop/FT/node_modules/asbuild/dist/cli.js:191:9)
    at main (/Users/jlwaugh/Desktop/FT/node_modules/asbuild/dist/cli.js:92:9)
    at module.exports.compile (/Users/jlwaugh/Desktop/FT/node_modules/near-sdk-bindgen/compiler.js:42:3)
    at compileContract (/Users/jlwaugh/Desktop/FT/contracts/assemblyscript/build.js:23:3)
    at Array.map (<anonymous>)
    at Object.<anonymous> (/Users/jlwaugh/Desktop/FT/contracts/assemblyscript/build.js:6:16)
    at Module._compile (internal/modules/cjs/loader.js:1015:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1035:10)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

beforeEach hook always returns failures when testing FT with ava,can anyone help?

When I use main.ava.js to test FT whit ava, the beforeEach hook always throws failures. Below is the relevant logging, hope someone can help me.

` ✖ beforeEach hook for Can close empty balance account Rejected promise returned by test
✖ beforeEach hook for Simple transfer Rejected promise returned by test
✖ beforeEach hook for Total supply Rejected promise returned by test
✖ beforeEach hook for Can force close non-empty balance account Rejected promise returned by test
✖ 4 tests remaining in tests/main.ava.ts

beforeEach hook for Can close empty balance account

Rejected promise returned by test. Reason:

Error (TransactionError) {
message: '{"result":{"receipts_outcome":[{"block_hash":"sqRkmRMXyr9ZntukqEwSbdoqkF83vvctKnHHCcET6NX","id":"EXi5BYRuLAtvgFutnHBFnp3sdWcX5qhSb1UmgyA8WJnz","outcome":{"executor_id":"fungible-token.test.near","gas_burnt":2478387861555,"logs":[],"metadata":{"gas_profile":[{"cost":"CONTRACT_COMPILE_BASE","cost_category":"WASM_HOST_COST","gas_used":"35445963"},{"cost":"CONTRACT_COMPILE_BYTES","cost_category":"WASM_HOST_COST","gas_used":"50290551750"}],"version":1},"receipt_ids":["B7aDDWkf8zF3gAiniFsTFtqArCz9G9XsUfDPvcX7zVCJ"],"status":{"Failure":{"ActionError":{"index":0,"kind":{"FunctionCallError":{"ExecutionError":"Link Error: Error while importing \"env\".\"promise_batch_action_function_call_weight\": unknown import. Expected Function(FunctionType { params: [I64, I64, I64, I64, I64, I64, I64, I64], results: [] })"}}}}},"tokens_burnt":"2478387861555000000000"},"proof":[{"direction":"Left","hash":"ESvfpBxCQGqxmUvfBKwHbAcdKpS349WBky8Hv7fP8i6z"}]},{"block_hash":"BqLFCroAX53xJguskiH9tbPLfqbbzh1Hxsx6MrgvTEzL","id":"B7aDDWkf8zF3gAiniFsTFtqArCz9G9XsUfDPvcX7zVCJ","outcome":{"executor_id":"fungible-token.test.near","gas_burnt":223182562500,"logs":[],"metadata":{"gas_profile":[],"version":1},"receipt_ids":[],"status":{"SuccessValue":""},"tokens_burnt":"0"},"proof":[]}],"status":{"Failure":{"ActionError":{"index":0,"kind":{"FunctionCallError":{"ExecutionError":"Link Error: Error while importing \"env\".\"promise_batch_action_function_call_weight\": unknown import. Expected Function(FunctionType { params: [I64, I64, I64, I64, I64, I64, I64, I64], results: [] })"}}}}},"transaction":{"actions":[{"FunctionCall":{"args":"eyJvd25lcl9pZCI6InRlc3QubmVhciIsInRvdGFsX3N1cHBseSI6IjEwMDAwIn0=","deposit":"0","gas":30000000000000,"method_name":"new_default_meta"}}],"hash":"4oWzC5qB4dsTznq1NFAaHHwRD6fPhVdPoHyKaKBFvVta","nonce":3000001,"public_key":"ed25519:448iYi2H8MehSfcG652X6quoxwdrHAcgtheyutKtVMng","receiver_id":"fungible-token.test.near","signature":"ed25519:iTv4KNxXRSfUajLXP2TNGPGuBSKmSP3bkYeWiET3Zm2cHKLzQC3bVhKREYRnmTs96UADgk6NnFNibw4w1ofiCzF","signer_id":"fungible-token.test.near"},"transaction_outcome":{"block_hash":"sqRkmRMXyr9ZntukqEwSbdoqkF83vvctKnHHCcET6NX","id":"4oWzC5qB4dsTznq1NFAaHHwRD6fPhVdPoHyKaKBFvVta","outcome":{"executor_id":"fungible-token.test.near","gas_burnt":2428061863842,"logs":[],"metadata":{"gas_profile":null,"version":1},"receipt_ids":["EXi5BYRuLAtvgFutnHBFnp3sdWcX5qhSb1UmgyA8WJnz"],"status":{"SuccessReceiptId":"EXi5BYRuLAtvgFutnHBFnp3sdWcX5qhSb1UmgyA8WJnz"},"tokens_burnt":"2428061863842000000000"},"proof":[{"direction":"Right","hash":"76FRpWKRPCoUUxzDV7kBjqtLTNK5iyHoaCMQmy8ZafQ3"}]}},"startMs":1658990180091,"endMs":1658990182129,"config":{"network":"sandbox","rootAccountId":"test.near","rpcAddr":"http://localhost:6360","initialBalance":"100000000000000000000000000","homeDir":"/tmp/sandbox/9dfdc3b0-cc36-48e9-9b1d-d7857eb318bd","port":6360,"rm":false,"refDir":null}}',
}
`


My package.json in workspace:

{ "devDependencies": { "@types/bn.js": "^5.1.0", "ava": "^4.2.0", "ts-node": "^10.8.0", "bn": "^1.0.5", "bn.js": "^5.2.1", "near-workspaces": "^2.0.0" }, "dependencies": { } }

cleanup punchlist

example of querying balance in command line

Can we have in the readme an example for querying the balance of an account please?

Also the explorer doesn't support auto-detecting user's FT Tokens (like etherscan) right?

Error encountered when creating Token - formatting a string: Input string was not in a correct format

near call $ID new '{"owner_id": "'$ID'", "total_supply": "1000000000000000", "metadata": { "spec": "https://upload.wikimedia.org/wikipedia/commons/thumb/archive/7/76/20110911121639!Lazio_Coat_of_Arms.svg/120px-Lazio_Coat_of_Arms.svg.png", "name": "Vibe Project Tokens", "symbol": "VIBE", "decimals": 8 }}' --accountId $ID

Error formatting a string: Input string was not in a correct format..
At line:1 char:1

  • near call $ID new '{"owner_id": "'$ID'", "total_supply": "10000000000 ...
  •   + CategoryInfo          : InvalidOperation: ({0}", "total_su...decimals": 8 }}:String) [], RuntimeException
      + FullyQualifiedErrorId : FormatError
    
    

Flesh out Rust README

Currently the README in the project root has an empty section for Rust. While the tests are a decent step toward informing a user about usage, there are some aspects of the fungible token that are particularly difficult to grasp without being spelled out. For instance, after initializing the contract and sending X fungible tokens to an account, we receive this error:

Error: GuestPanic [Error]: Smart contract panicked: panicked at 'The required attached deposit is 36500000000000000000000, but the given attached deposit is is 0', src/lib.rs:254:13

This is because we need to make sure that state storage doesn't get exploited. Folks have to pay for the storage it will take, essentially. This ticket is for the DevX team to take a first stab at explaining how to use a fungible token, and then tag and pull in @evgenykuzyakov for review.

I believe there are two approaches to fleshing this out:

  1. Give instructions on how to use near-shell to build, deploy, initialize, and use the fungible token, demonstrating escrow and expected behavior.
  2. Use near-api-js and a simple web interface (like we have with Token Contract AS) where folks can add commands in the developer console of their browser and see the results.

Acceptance Criteria:

  • What is generally written above
  • Use the unit tests in the contracts in order to determine ways in which we can give example scenarios using near shell. For instance, walk the use through how to create and initialize a fungible token, send directly from Alice to Bob. Then have Charlie be an escrow between the two, incrementing allowance, etc. A user should be able to come to the README and run a few near shell commands and try out sending fungible tokens around.

tests / simulation fail

followed the tutorial until the tests. tried to run them

environment: ubuntu 21.04
rust: latest from 4 days ago

  thread 'main' panicked at 'Unable to find libclang: "couldn't find any valid shared libraries matching: ['libclang.so', 'libclang-*.so', 'libclang.so.*', 'libclang-*.so.*'], set the `LIBCLANG_PATH` environment variable to a path where one of these files can be found (invalid: [])"', /home/andrei/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.54.0/src/lib.rs:1959:31
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...

no cargo.toml file

hi, I tried to run script 'build' or cargo build - returns error
"error: could not find Cargo.toml in ...\FT or any parent directory"

for me it helped to add cargo.toml to main dir.

[profile.release]
codegen-units = 1
opt-level = "z"
lto = true
debug = false
panic = "abort"
overflow-checks = true

[workspace]
members = [
"ft",
"test-contract-defi",
]
exclude = [
"integration-tests"
]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.