mangrovedao/mangrove.js

Cannot declare a new token with the API

jkrivine opened this issue · 3 comments

Describe the bug
It is currently not possible to declare a new ERC20 token to the API.

Suppose you want to build a MgvToken instance of token 'A' at address '0xAA'. After
mgv.setAddress('A', '0xAA')
a call to new MgvToken('A') throws because the decimals of A are unkown. The method setDecimals() is not usable at this point.

Solutions

  1. In the constructor of MgvToken the function getDecimals is called but is A decimals are not set in tokenDecimal.json of the library, the call will throw. A simple solution would be to then try to call fetchDecimals that would query the contract itself.
  2. allow users to dynamicall set decimals (like setAddress)
  3. pass token decimals in the constructor

Why can't you do Mangrove.setDecimals at this point? If you could would it be a good enough solution?

OK found the problem, here are the steps that work to declare a new token:

  1. mgv.setAddress("tkn", "0x...")
  2. MgvToken.setDecimals("tkn", 10)
  3. mytkn = new MgvToken("tkn", mgv)

at step 2 I was trying mgv.setDecimals. I think it is a bit odd to use setAddress from the object mgv and that setDecimals is a static method of MgvToken and Mangrove. Maybe we could encapsulate the above steps into the already existing function mgv.token("tkn", {address: "0x..", decimals: 10})

You should be able to do Mangrove.setDecimals and also MgvToken.setDecimals. In fact I'm not sure MgvToken.setDecimals is great? For instance setDisplayedDecimals is only defined on Mangrove. Same for setAddress for tokens.

Also once #638 is auto-merged you will be able to do mgv.token("tkn",{address,decimals,displayedDecimals}); note that this updates globally the properties of "tkn".