Toniq-Labs/extendable-token

Usage of tokens from within other canister

Opened this issue · 3 comments

I'm struggling to figure out how to use the token examples / standards within other canisters.

Scenario: I want to create a token not from a frontend, but another "backend" canister instead.

Expected behavior:

I can deploy my canister with the token standard as a dependency and create new tokens whenever I want.

I configured my test env like this:

...
"canisters": {
    "erc20": {
      "main": "src/token_test/assets/examples/erc20.mo",
      "type": "motoko"
    },
    "token_test": {
      "main": "src/token_test/main.mo",
      "type": "motoko",
      "dependencies": ["erc20"]
    }
  },
...

Actual behavior:

Dfinity fails with "Invalid data: Expected arguments but found none.".
To my understanding, this happens because the tokens are defined as actors so that they can be deployed on their own. But in my case, this throws an error because dfinity tries to deploy the erc20 dependency (which would create a token).

Now I understand I can remove the "actor" and just use the token as a class, but I don't know if that is the desired behavior here.

What do you suggest?

So remove the dep and remove erc20 as a canister - anything listed in the canister section defines one to be created. So your json should just be:

...
"canisters": {
    "token_test": {
      "main": "src/token_test/main.mo",
      "type": "motoko"
    }
  },
...

Then in main.mo include the erc20.mo file as an import, e.g.:

import ERC20 "./assets/examples/erc20";

and then to create the token in code you would do something like:

let name : Text = "My Token";
let symbol : Text = "MYT";
let decimals : Nat8 = 2;
let supply : Nat = 10000;
let token = await ERC20.erc20_token(name, symbol, decimals, supply, msg.caller);

We recommend using the multi-canister approach due to cost savings. Currently it cost about 1T cycles to originate a new canister, where as the multi-canister approach costs about 50M cycles per token.

I'll look to add an example of this soon too

Thank you! I got a couple of steps further with your help. But a full example with usage etc. would be very helpful. I think it would help a lot of new ic devs that are trying to get into motoko & tokens if they can kickstart without fighting through the motoko documentation.