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
- In the constructor of
MgvToken
the functiongetDecimals
is called but isA
decimals are not set intokenDecimal.json
of the library, the call will throw. A simple solution would be to then try to callfetchDecimals
that would query the contract itself. - allow users to dynamicall set decimals (like
setAddress
) - 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".