github.com/ChainSafe/chainbridge-core@v1.4.2/chains/evm/calls/contracts/erc20/erc20.go (about) 1 package erc20 2 3 import ( 4 "github.com/ChainSafe/chainbridge-core/chains/evm/calls" 5 "github.com/ChainSafe/chainbridge-core/chains/evm/calls/contracts" 6 "github.com/ChainSafe/chainbridge-core/chains/evm/calls/transactor" 7 "math/big" 8 "strings" 9 10 "github.com/ChainSafe/chainbridge-core/chains/evm/calls/consts" 11 "github.com/ethereum/go-ethereum/accounts/abi" 12 "github.com/ethereum/go-ethereum/common" 13 "github.com/rs/zerolog/log" 14 ) 15 16 type ERC20Contract struct { 17 contracts.Contract 18 } 19 20 func NewERC20Contract( 21 client calls.ContractCallerDispatcher, 22 erc20ContractAddress common.Address, 23 transactor transactor.Transactor, 24 ) *ERC20Contract { 25 a, _ := abi.JSON(strings.NewReader(consts.ERC20PresetMinterPauserABI)) 26 b := common.FromHex(consts.ERC20PresetMinterPauserBin) 27 return &ERC20Contract{contracts.NewContract(erc20ContractAddress, a, b, client, transactor)} 28 } 29 30 func (c *ERC20Contract) GetBalance(address common.Address) (*big.Int, error) { 31 log.Debug().Msgf("Getting balance for %s", address.String()) 32 res, err := c.CallContract("balanceOf", address) 33 if err != nil { 34 return nil, err 35 } 36 b := abi.ConvertType(res[0], new(big.Int)).(*big.Int) 37 return b, nil 38 } 39 40 func (c *ERC20Contract) MintTokens( 41 to common.Address, 42 amount *big.Int, 43 opts transactor.TransactOptions, 44 ) (*common.Hash, error) { 45 log.Debug().Msgf("Minting %s tokens to %s", amount.String(), to.String()) 46 return c.ExecuteTransaction("mint", opts, to, amount) 47 } 48 49 func (c *ERC20Contract) ApproveTokens( 50 target common.Address, 51 amount *big.Int, 52 opts transactor.TransactOptions, 53 ) (*common.Hash, error) { 54 log.Debug().Msgf("Approving %s tokens for %s", target.String(), amount.String()) 55 return c.ExecuteTransaction("approve", opts, target, amount) 56 } 57 58 func (c *ERC20Contract) MinterRole() ([32]byte, error) { 59 res, err := c.CallContract("MINTER_ROLE") 60 if err != nil { 61 return [32]byte{}, err 62 } 63 out := *abi.ConvertType(res[0], new([32]byte)).(*[32]byte) 64 return out, nil 65 } 66 67 func (c *ERC20Contract) AddMinter( 68 minter common.Address, 69 opts transactor.TransactOptions, 70 ) (*common.Hash, error) { 71 log.Debug().Msgf("Adding new minter %s", minter.String()) 72 role, err := c.MinterRole() 73 if err != nil { 74 return nil, err 75 } 76 return c.ExecuteTransaction("grantRole", opts, role, minter) 77 }