Add methods to get ERC20 constants

This commit is contained in:
Eduard S
2020-10-06 11:39:15 +02:00
parent 15a122a6a0
commit 650911cda3
7 changed files with 408 additions and 5 deletions

View File

@@ -176,6 +176,7 @@ type EthereumBlock struct {
Time int64
Hash ethCommon.Hash
ParentHash ethCommon.Hash
Tokens map[ethCommon.Address]eth.ERC20Consts
// state ethState
}
@@ -200,14 +201,15 @@ func (b *Block) Next() *Block {
blockNext := b.copy()
blockNext.Rollup.Events = eth.NewRollupEvents()
blockNext.Auction.Events = eth.NewAuctionEvents()
blockNext.Eth = &EthereumBlock{
BlockNum: b.Eth.BlockNum + 1,
ParentHash: b.Eth.Hash,
}
blockNext.Eth.BlockNum = b.Eth.BlockNum + 1
blockNext.Eth.ParentHash = b.Eth.Hash
blockNext.Rollup.Constants = b.Rollup.Constants
blockNext.Auction.Constants = b.Auction.Constants
blockNext.Rollup.Eth = blockNext.Eth
blockNext.Auction.Eth = blockNext.Eth
return blockNext
}
@@ -360,6 +362,7 @@ func NewClient(l bool, timer Timer, addr *ethCommon.Address, setup *ClientSetup)
Time: timer.Time(),
Hash: hasher.Next(),
ParentHash: ethCommon.Hash{},
Tokens: make(map[ethCommon.Address]eth.ERC20Consts),
},
}
blockCurrent.Rollup.Eth = blockCurrent.Eth
@@ -508,6 +511,23 @@ func (c *Client) EthTransactionReceipt(ctx context.Context, txHash ethCommon.Has
return nil, nil
}
// CtlAddERC20 adds an ERC20 token to the blockchain.
func (c *Client) CtlAddERC20(tokenAddr ethCommon.Address, constants eth.ERC20Consts) {
nextBlock := c.nextBlock()
e := nextBlock.Eth
e.Tokens[tokenAddr] = constants
}
// EthERC20Consts returns the constants defined for a particular ERC20 Token instance.
func (c *Client) EthERC20Consts(tokenAddr ethCommon.Address) (*eth.ERC20Consts, error) {
currentBlock := c.currentBlock()
e := currentBlock.Eth
if constants, ok := e.Tokens[tokenAddr]; ok {
return &constants, nil
}
return nil, fmt.Errorf("tokenAddr not found")
}
// func newHeader(number *big.Int) *types.Header {
// return &types.Header{
// Number: number,

View File

@@ -62,6 +62,19 @@ func TestClientEth(t *testing.T) {
require.Nil(t, err)
assert.Equal(t, int64(2), block.EthBlockNum)
assert.Equal(t, time.Unix(2, 0), block.Timestamp)
// Add a token
tokenAddr := ethCommon.HexToAddress("0x44021007485550008e0f9f1f7b506c7d970ad8ce")
constants := eth.ERC20Consts{
Name: "FooBar",
Symbol: "FOO",
Decimals: 4,
}
c.CtlAddERC20(tokenAddr, constants)
c.CtlMineBlock()
tokenConstants, err := c.EthERC20Consts(tokenAddr)
require.Nil(t, err)
assert.Equal(t, constants, *tokenConstants)
}
func TestClientAuction(t *testing.T) {