mirror of
https://github.com/grassrootseconomics/cic-go.git
synced 2024-12-23 08:47:32 +01:00
Mohamed Sohail
79634affb8
module: - cic-go packages: - cic/meta - cic/net other fixes: - nonce is now a settable value - package init outside test runner scope
53 lines
1.2 KiB
Go
53 lines
1.2 KiB
Go
package net
|
|
|
|
import (
|
|
"context"
|
|
"github.com/ethereum/go-ethereum/common"
|
|
"github.com/lmittmann/w3"
|
|
"github.com/lmittmann/w3/module/eth"
|
|
"math/big"
|
|
)
|
|
|
|
type ERC20Token struct {
|
|
Name string
|
|
Symbol string
|
|
Decimals big.Int
|
|
}
|
|
|
|
func (c *CicNet) ERC20TokenInfo(ctx context.Context, tokenAddress common.Address) (ERC20Token, error) {
|
|
var (
|
|
tokenName string
|
|
tokenSymbol string
|
|
tokenDecimals big.Int
|
|
)
|
|
|
|
err := c.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),
|
|
)
|
|
if err != nil {
|
|
return ERC20Token{}, err
|
|
}
|
|
|
|
return ERC20Token{
|
|
Name: tokenName,
|
|
Symbol: tokenSymbol,
|
|
Decimals: tokenDecimals,
|
|
}, nil
|
|
}
|
|
func (c *CicNet) BalanceOf(ctx context.Context, tokenAddress common.Address, accountAddress common.Address) (big.Int, error) {
|
|
var balance big.Int
|
|
|
|
err := c.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
|
|
}
|