# Mint Native Tokens with SDK

**URL:** <https://forum.fuel.network/t/mint-native-tokens-with-sdk/6568>\
**Category:** TypeScript SDK\
**Created:** [September 5, 2024, 7:15pm UTC](https://forum.fuel.network/t/mint-native-tokens-with-sdk/6568 "2024-09-05T19:15:10Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![KITSCHO](https://avatars.discourse-cdn.com/v4/letter/k/b19c9b/32.png) [@KITSCHO](https://forum.fuel.network/u/KITSCHO)\
**Post date:** [September 5, 2024, 7:15pm UTC](https://forum.fuel.network/t/mint-native-tokens-with-sdk/6568/1 "2024-09-05T19:15:10Z")

</div>

I want to mint native asset but I keep getting an error

> FuelError: The transaction reverted with an unknown reason: 0

This is my function

```auto
const mintTokens = async () => {
        if (!provider || !account) {
            return;
        }
        const wallet = Wallet.fromAddress(account, provider);
        if (!wallet) {
            return;
        }
        console.log("mintTokens");
        const token = new Token(TOKEN_CONTRACT_ID, wallet);

        const subID = "0x0000000000000000000000000000000000000000000000000000000000000000";
        const mintAmount = bn(1000);
        const recipientAddress = Address.fromString(account);
        const recipientIdentity = { Address: { bits: recipientAddress.toB256() } };
        const { waitForResult } = await token.functions.mint(recipientIdentity, subID, mintAmount).call();
        console.log("waitForResult: ", waitForResult);
        const txResult = await waitForResult();

        console.log("txResult: ", txResult);

        const mintedAssetId = getMintedAssetId(token.id.toB256(), subID);
        console.log("mintedAssetId: ", mintedAssetId);
    }

```

My token contract

```auto
contract;

mod errors;
mod interface;

use standards::{src20::SRC20, src3::SRC3, src5::SRC5};
use std::{
    asset::{
        burn,
        mint_to,
    },
    call_frames::msg_asset_id,
    constants::DEFAULT_SUB_ID,
    context::msg_amount,
    string::String,
};

storage {
    DECIMALS: u8 = 9u8,
    NAME: str[8] = __to_str_array("FuelCoin"),
    SYMBOL: str[4] = __to_str_array("Fuel"),
    total_supply: u64 = 0,
}

impl SRC20 for Contract {
    #[storage(read)]
     fn total_assets() -> u64 {
        1
    }

    #[storage(read)]
   fn total_supply(asset: AssetId) -> Option<u64> {
        if asset == AssetId::default() {
            Some(storage.total_supply.read())
        } else {
            None
        }
    }

    #[storage(read)]
    fn name(asset: AssetId) -> Option<String> {
        if asset == AssetId::default() {
            Some(String::from_ascii_str(from_str_array(storage.NAME.read())))
        } else {
            None
        }
    }

    #[storage(read)]
    fn symbol(asset: AssetId) -> Option<String> {
        if asset == AssetId::default() {
            Some(String::from_ascii_str(from_str_array(storage.SYMBOL.read())))
        } else {
            None
        }
    }

    #[storage(read)]
    fn decimals(asset: AssetId) -> Option<u8> {
        if asset == AssetId::default() {
            Some(storage.DECIMALS.read())
        } else {
            None
        }
    }
}

impl SRC3 for Contract {
    #[storage(read, write)]
    fn mint(recipient: Identity, sub_id: Option<SubId>, amount: u64) {
         let sub_id = match sub_id {
            Some(s) => s,
            None => DEFAULT_SUB_ID,
        };
         require(sub_id == DEFAULT_SUB_ID, "incorrect-sub-id");
          storage
            .total_supply
            .write(amount + storage.total_supply.read());
        mint_to(recipient, DEFAULT_SUB_ID, amount);
     
    }
    #[payable]
    #[storage(read, write)]
    fn burn(sub_id: SubId, amount: u64) {
        require(sub_id == DEFAULT_SUB_ID, "incorrect-sub-id");
        require(msg_amount() >= amount, "incorrect-amount-provided");
        require(
            msg_asset_id() == AssetId::default(),
            "incorrect-asset-provided",
        );
 
        storage
            .total_supply
            .write(storage.total_supply.read() - amount);
        burn(DEFAULT_SUB_ID, amount);
    }
}

```

---

<div class="post-metadata">

**Author:** ![maschad](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/maschad/32/2710_2.png) [@maschad](https://forum.fuel.network/u/maschad)\
**Post date:** [September 5, 2024, 7:32pm UTC](https://forum.fuel.network/t/mint-native-tokens-with-sdk/6568/2 "2024-09-05T19:32:38Z")

</div>

Hi @KITSCHO thanks for reporting this.

Given this is a relatively complex interaction, and I don’t want to make assumptions about your setup, could you link to a repository that has a reproduction of the issue? We have a [bug template](https://github.com/FuelLabs/fuels-ts/issues/new?assignees=&labels=triage&projects=&template=1-bug.yaml) that you could include in the `README.md` to make debugging faster.

Also just for guidance, have you explored [this guide](https://docs.fuel.network/docs/fuels-ts/contracts/minted-token-asset-id/#minted-token-asset-id) for minting a token?

We also have a demo apps of [Native Assets](https://github.com/FuelLabs/sway-applications/tree/master/native-asset) that you could take a look at.

---

<div class="post-metadata">

**Author:** ![KITSCHO](https://avatars.discourse-cdn.com/v4/letter/k/b19c9b/32.png) [@KITSCHO](https://forum.fuel.network/u/KITSCHO)\
**Post date:** [September 5, 2024, 7:58pm UTC](https://forum.fuel.network/t/mint-native-tokens-with-sdk/6568/3 "2024-09-05T19:58:56Z")

</div>

Here is my project: [GitHub - KristjanBajuk/fuel-native-asset](https://github.com/KristjanBajuk/fuel-native-asset)  
I’m reading the docs and demo apps, but I can’t get it to work.

So i deployed the contract to testnet and now I want to mint the tokens to the wallet…

---

<div class="post-metadata">

**Author:** ![KITSCHO](https://avatars.discourse-cdn.com/v4/letter/k/b19c9b/32.png) [@KITSCHO](https://forum.fuel.network/u/KITSCHO)\
**Post date:** [September 6, 2024, 6:51am UTC](https://forum.fuel.network/t/mint-native-tokens-with-sdk/6568/4 "2024-09-06T06:51:59Z")

</div>

And when I run the app in dev mode

```auto
  const { wallet } = useWallet();

```

returns undefined. But if I build the app and run it with the command next start it is returning the wallet instance.

I’m connecting to the app with the browser wallet.

---

<div class="post-metadata">

**Author:** ![danielbate](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/danielbate/32/1617_2.png) [@danielbate](https://forum.fuel.network/u/danielbate)\
**Post date:** [September 6, 2024, 9:10am UTC](https://forum.fuel.network/t/mint-native-tokens-with-sdk/6568/5 "2024-09-06T09:10:42Z")

</div>

I’d reccomend checking out our [connectors app](https://github.com/FuelLabs/fuels-ts/blob/master/apps/demo-wallet-sdk-react/src/app/page.tsx) for info on using the various hooks.

I can see you’ve got 2 environment files, `.env` and `.env.local`. One sets your provider to local and one sets it to testnet. I assume when you run dev it is not connecting to a local wallet, but you do have a wallet configured to testnet in the browser extension.

---

<div class="post-metadata">

**Author:** ![KITSCHO](https://avatars.discourse-cdn.com/v4/letter/k/b19c9b/32.png) [@KITSCHO](https://forum.fuel.network/u/KITSCHO)\
**Post date:** [September 6, 2024, 4:25pm UTC](https://forum.fuel.network/t/mint-native-tokens-with-sdk/6568/6 "2024-09-06T16:25:15Z")

</div>

Ok I get the instance of the wallet from useWallet now.

But I still have this issue of  
`FuelError: The transaction reverted with an unknown reason: 0`

I took the same code for native asset from sway-playground. I deployed the contract succesfully. I call total\_assets function and it returns the value correctly. But when I want to initialize owner with constructor I get an error. The same error as before when i tried to call mint function.

I have this TS script

```auto
 import {Address, Provider, Wallet, TESTNET_NETWORK_URL} from 'fuels';
import { TokenFactory } from '@/sway-api/token';
import dotenv from 'dotenv';

// Load environment variables from .env file
dotenv.config();

const WALLET_PVT_KEY = process.env.NEXT_PUBLIC_WALLET_PVT_KEY;

async function deployContract() {
    try {
        // Create the provider
        const provider = await Provider.create(TESTNET_NETWORK_URL as string);
        console.log("Provider initialized successfully");

        // Create the wallet
        const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY as string, provider);
        if (!wallet) {
            return;
        }
        console.log("Wallet created successfully");

        const factory = new TokenFactory(wallet);
        console.log("Factory instance created");

        // Deploy the contract using the factory instance
        const deployResult = await factory.deploy();
        console.log("Deploy initiated");

        // Wait for deployment results
        const { contract, transactionResult } = await deployResult.waitForResult();

        console.log('Contract deployed successfully with ID:', contract.id.toString());
        console.log('Transaction ID:', transactionResult.id);

        const { waitForResult } = await contract.functions.total_assets().call();
        console.log("waitForResult");
        const txResult = await waitForResult();
        console.log("total Assets: ", txResult.value.toString());

        const recipientAddress = Address.fromString(wallet.address.toString());
        const recipientIdentity = { Address: { bits: recipientAddress.toB256() } };

        // Call the constructor function with your address
        console.log("Initializing contract with owner address...");
        const { waitForResult: waitForConstructor } = await contract.functions.constructor(recipientIdentity).call();
        await waitForConstructor();
        console.log("Contract initialized with owner successfully.");

    } catch (error) {
        console.error("Error deploying contract:", error);
    }

```

This are the logs when I execute this script

```auto
Provider initialized successfully
Wallet created successfully
Factory instance created
Deploy initiated
Contract deployed successfully with ID: fuel19xl73swuuwryn7dwjaw56g9d3r68xw6zq36kwqtcz22lrmagtlts30el80
Transaction ID: 0x5e02e3c7ea650c1fe85ca9f165dda36d624d5140ae8724de54cbc3dbd20bda54
waitForResult
total Assets: 1
Initializing contract with owner address...
Error deploying contract: _FuelError: The transaction reverted with an unknown reason: 0

```

---

<div class="post-metadata">

**Author:** ![KITSCHO](https://avatars.discourse-cdn.com/v4/letter/k/b19c9b/32.png) [@KITSCHO](https://forum.fuel.network/u/KITSCHO)\
**Post date:** [September 6, 2024, 5:19pm UTC](https://forum.fuel.network/t/mint-native-tokens-with-sdk/6568/7 "2024-09-06T17:19:44Z")

</div>

So if I remove the line where I’m updating total\_supply in mint function then it works. coins get minted correctly to the recipient. But I don’t know why writing to the storage is not working.

This code does not work

```auto
  #[storage(read, write)]
    fn mint(recipient: Identity, sub_id: Option<SubId>, amount: u64) {
          let sub_id = sub_id.unwrap_or(DEFAULT_SUB_ID);
        require(sub_id == DEFAULT_SUB_ID, "incorrect-sub-id");
        require_access_owner();
        
        storage.total_supply.write(amount + storage.total_supply.read());
            
        mint_to(recipient, DEFAULT_SUB_ID, amount);
    }

```

This works

```auto
  #[storage(read, write)]
    fn mint(recipient: Identity, sub_id: Option<SubId>, amount: u64) {
          let sub_id = sub_id.unwrap_or(DEFAULT_SUB_ID);
        require(sub_id == DEFAULT_SUB_ID, "incorrect-sub-id");
        require_access_owner();
            
        mint_to(recipient, DEFAULT_SUB_ID, amount);
    }

```

This also works

```auto
  #[storage(read, write)]
    fn mint(recipient: Identity, sub_id: Option<SubId>, amount: u64) {
          let sub_id = sub_id.unwrap_or(DEFAULT_SUB_ID);
        require(sub_id == DEFAULT_SUB_ID, "incorrect-sub-id");
        require_access_owner();

        storage.total_supply.write(100);
            
        mint_to(recipient, DEFAULT_SUB_ID, amount);
    }

```

---

<div class="post-metadata">

**Author:** ![KITSCHO](https://avatars.discourse-cdn.com/v4/letter/k/b19c9b/32.png) [@KITSCHO](https://forum.fuel.network/u/KITSCHO)\
**Post date:** [September 6, 2024, 5:44pm UTC](https://forum.fuel.network/t/mint-native-tokens-with-sdk/6568/8 "2024-09-06T17:44:10Z")

</div>

Upon further trial and error i guess that the reading from storage is the problem.

even if i try this I get the same error.

```auto
#[storage(read)]
    fn get_total_supply() -> u64 {
        storage.total_supply.read()
    }

```

---

<div class="post-metadata">

**Author:** ![maschad](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/maschad/32/2710_2.png) [@maschad](https://forum.fuel.network/u/maschad)\
**Post date:** [September 6, 2024, 8:20pm UTC](https://forum.fuel.network/t/mint-native-tokens-with-sdk/6568/9 "2024-09-06T20:20:37Z")

</div>

It seems as if there are some other errors unrelated to the original issue that you are now encountering, you may want to explore introducing [logs](https://docs.fuel.network/docs/sway-by-example-lib/logging/#logging) in your contract as well as looking at the transaction [receipts](https://docs.fuel.network/docs/specs/abi/receipts/#receipts) to better understand why it’s reverting. They are returned in the [Transaction Response](https://docs.fuel.network/docs/fuels-ts/transactions/transaction-response/#transaction-response) in the TS SDK.

We also have [methods](https://docs.fuel.network/docs/fuels-ts/account/#assemblereverterror) for getting the logs when a transaction is reverted

---

<div class="post-metadata">

**Author:** ![KITSCHO](https://avatars.discourse-cdn.com/v4/letter/k/b19c9b/32.png) [@KITSCHO](https://forum.fuel.network/u/KITSCHO)\
**Post date:** [September 7, 2024, 7:37am UTC](https://forum.fuel.network/t/mint-native-tokens-with-sdk/6568/10 "2024-09-07T07:37:04Z")

</div>

I have put logs in the smart contract and catch the error in the typescript. but All i get is unknown error.

 ![image](https://us1.discourse-cdn.com/flex022/uploads/fuel/original/2X/0/08eb1d8199dd959c9e98b16e7426ad1ed1f11a91.png)

smart contract

 ![image](https://us1.discourse-cdn.com/flex022/uploads/fuel/original/2X/b/b2a2276f20ea27281e62f0a38e0f7c59cbbf6bac.png)

Is it maybe a problem with a versions? I get this message before deploy.

```auto
The Fuel Node that you are trying to connect to is using fuel-core version 0.35.0,
which is not supported by the version of the TS SDK that you are using.
Things may not work as expected.
Supported fuel-core version: 0.34.0.

```

Here are my active versions

```auto
Installed toolchains
--------------------
beta-3-aarch64-apple-darwin
latest-aarch64-apple-darwin (default)
nightly-aarch64-apple-darwin
nightly-2024-05-28-aarch64-apple-darwin
testnet-aarch64-apple-darwin

active toolchain
----------------
latest-aarch64-apple-darwin (default)
  forc : 0.63.5
    - forc-client
      - forc-deploy : 0.63.5
      - forc-run : 0.63.5
    - forc-crypto : 0.63.5
    - forc-debug : 0.63.5
    - forc-doc : 0.63.5
    - forc-fmt : 0.63.5
    - forc-lsp : 0.63.5
    - forc-tx : 0.63.5
    - forc-wallet : 0.9.0
  fuel-core : 0.35.0
  fuel-core-keygen : 0.35.0

fuels versions
--------------
forc : 0.66.4
forc-wallet : 0.66.0

```

I’m deploying and testing this on the TESTNET not local fuel node.

---

<div class="post-metadata">

**Author:** ![KITSCHO](https://avatars.discourse-cdn.com/v4/letter/k/b19c9b/32.png) [@KITSCHO](https://forum.fuel.network/u/KITSCHO)\
**Post date:** [September 8, 2024, 4:56pm UTC](https://forum.fuel.network/t/mint-native-tokens-with-sdk/6568/11 "2024-09-08T16:56:20Z")

</div>

So the solution and the problem is the same as here: [Failed to deploy contract using typescript sdk - #17 by yuduan0220](https://forum.fuel.network/t/failed-to-deploy-contract-using-typescript-sdk/4440/17)

The problem is that storage variables are somehow uninitialized.

Here the variable total\_supply is somehow uninitialized.

```auto
storage {
    total_supply: u64 = 0,
    owner: State = State::Uninitialized,
}

```

I put this in the constructor

```auto
#[storage(read, write)]
    fn constructor(owner_: Identity) {
        storage.total_supply.write(0);
        storage.owner.write(State::Initialized(owner_));
    }

```

Now everything works fine. Mint function work as intended. Why is that?

---

<div class="post-metadata">

**Author:** ![maschad](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/maschad/32/2710_2.png) [@maschad](https://forum.fuel.network/u/maschad)\
**Post date:** [September 8, 2024, 5:47pm UTC](https://forum.fuel.network/t/mint-native-tokens-with-sdk/6568/12 "2024-09-08T17:47:22Z")

</div>

Hi @KITSCHO sorry for the delayed response but it seems what’s happening here is that your storage slots are not being loaded.

Our _Typegen_ tries to resolve, auto-load, and embed the [Storage Slots](https://docs.fuel.network/docs/fuels-ts/contracts/storage-slots/) for your contract so normally you shouldn’t have to override it for the variables to be initialized.

Could you try deploying the contract with the [storage slots in the constructor](https://docs.fuel.network/docs/fuels-ts/contracts/storage-slots/#storage-slots) and see if that removes the need for those lines in the contract’s constructor?

---

<div class="post-metadata">

**Author:** ![KITSCHO](https://avatars.discourse-cdn.com/v4/letter/k/b19c9b/32.png) [@KITSCHO](https://forum.fuel.network/u/KITSCHO)\
**Post date:** [September 8, 2024, 6:02pm UTC](https://forum.fuel.network/t/mint-native-tokens-with-sdk/6568/13 "2024-09-08T18:02:13Z")

</div>

yes this works. I added storageSlots to deploy function

```auto
  const deployResult = await factory.deploy({
            storageSlots
        });

```

And now it works without that workaround in the constructor.

---

<div class="post-metadata">

**Author:** ![maschad](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/maschad/32/2710_2.png) [@maschad](https://forum.fuel.network/u/maschad)\
**Post date:** [September 8, 2024, 6:41pm UTC](https://forum.fuel.network/t/mint-native-tokens-with-sdk/6568/14 "2024-09-08T18:41:02Z")

</div>

Thanks for reporting that @KITSCHO I created an [issue](https://github.com/FuelLabs/fuels-ts/issues/3130) as I believe this may be a bug.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex022/uploads/fuel/original/2X/5/57d5a345cc15a64b636e0d56e042857f8a0e80b1.png) [@system](https://forum.fuel.network/u/system)\
**Post date:** [September 29, 2024, 2:41pm UTC](https://forum.fuel.network/t/mint-native-tokens-with-sdk/6568/15 "2024-09-29T14:41:16Z")

</div>

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