cic-go/net/erc20_token.go

58 lines
1.4 KiB
Go
Raw Normal View History

package net
import (
"context"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/lmittmann/w3"
"github.com/lmittmann/w3/module/eth"
)
type ERC20Token struct {
2022-05-23 11:47:10 +02:00
Name string
Symbol string
Decimals big.Int
TotalSupply big.Int
}
func (c *CicNet) ERC20TokenInfo(ctx context.Context, tokenAddress common.Address) (ERC20Token, error) {
var (
tokenName string
tokenSymbol string
tokenDecimals big.Int
2022-05-23 11:47:10 +02:00
totalSupply big.Int
)
err := c.provider.EthClient.CallCtx(
ctx,
eth.CallFunc(w3.MustNewFunc("name()", "string"), tokenAddress).Returns(&tokenName),
eth.CallFunc(w3.MustNewFunc("symbol()", "string"), tokenAddress).Returns(&tokenSymbol),
eth.CallFunc(w3.MustNewFunc("decimals()", "uint256"), tokenAddress).Returns(&tokenDecimals),
2022-05-23 11:47:10 +02:00
eth.CallFunc(w3.MustNewFunc("totalSupply()", "uint256"), tokenAddress).Returns(&totalSupply),
)
if err != nil {
return ERC20Token{}, err
}
return ERC20Token{
2022-05-23 11:47:10 +02:00
Name: tokenName,
Symbol: tokenSymbol,
Decimals: tokenDecimals,
TotalSupply: totalSupply,
}, nil
}
func (c *CicNet) BalanceOf(ctx context.Context, tokenAddress common.Address, accountAddress common.Address) (big.Int, error) {
var balance big.Int
err := c.provider.EthClient.CallCtx(
ctx,
eth.CallFunc(w3.MustNewFunc("balanceOf(address _account)", "uint256"), tokenAddress, accountAddress).Returns(&balance),
)
if err != nil {
return big.Int{}, err
}
return balance, nil
}