Which token contract I should to use?

Hey everyone
I have an issue
I’m tried to fork token contract from swayswap, but my compiler started give me error like Expected an identifier right here

pub fn get_b256(key: b256) -> b256 {
    asm(r1: key, r2) {
        move r2 sp;
        cfei i32;
        srwq r2 r1; //👈🏻 err "Expected an identifier" right here
        r2: b256

I noticed that swayswap contract use only get_msg_sender_address_or_panic func and replaced it on my token contract and after forc build && cargo test
I see output like that

Compiled library "core".
  Compiled library "std".
  Compiled library "token_abi".
  Compiled contract "token_contract".
  Bytecode size is 13312 bytes.
    Finished test [unoptimized + debuginfo] target(s) in 0.27s
     Running tests/harness.rs (target/debug/deps/tests-7993d6f5897492f9)

running 1 test
test token_contract ... FAILED


---- token_contract stdout ----
thread 'token_contract' panicked at 'error: Could not connect to fuel core server.', /Users/alexey/.cargo/registry/src/github.com-1ecc6299db9ec823/fuels-test-helpers-0.23.0/src/node.rs:321:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread 'token_contract' panicked at 'called `Result::unwrap()` on an `Err` value: RecvError(())', /Users/alexey/.cargo/registry/src/github.com-1ecc6299db9ec823/fuels-test-helpers-0.23.0/src/node.rs:307:14


test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.52s

error: test failed, to rerun pass '--test tests'

What I’m doing wrong?

Here is a question: Is it ok to use token contract like that or maybe you have basic token contract?


The test was refactored and fixed, swayswap used old versions of packages
Token contract
Token contract test

Btw I still don’t understand is it ok to use token contract from swayswap

1 Like

This mostly depends on what you’re trying to achieve.
If you need a Non-Fungible Token I recommend you check out sway-libs/sway_libs/src/nft at master · FuelLabs/sway-libs · GitHub.

On the other hand, if you want to create a Fungible Token (similar to an erc-20 token), it’s actually very simple, but also very different from how it’s done on Ethereum.

On Fuel, any contract can be a token contract. Contracts on Fuel have access to the mint opcode, and tokens minted by a contract automatically have an AssetId equivalent to the ContractId of the minting contract.

In practice, you’ll want to use the sway standard library for this. The std::token module has the helper functions needed for managing tokens, including minting, burning and transferring functionality.

At a bare minimum, you will need to add a function to your contract abi to allow some authorized user to mint tokens, and when you implement this function in your contract simply import and use the stdlib function you need. For example:


use std::token::mint_to;

impl MyTokenAbi for Contract {
    fn mint(amount: u64, to: Identity) {
    mint_to(amount, to);

For reference: sway/token.sw at a036dc5a5b6c604f6374b040559ededd4161240e · FuelLabs/sway · GitHub

Hope that helps!


Thanks for the detailed answer, everything is now clear with tokens :heart:

1 Like

Hey, I’ve started drafting a specification for a Sway token standard, seems relevant here:

Would love feedback!


This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.