1
This commit is contained in:
parent
bff6f00c5a
commit
a1d43989c9
@ -10,6 +10,10 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/accounts/abi"
|
||||||
|
"github.com/ethereum/go-ethereum/common"
|
||||||
|
"github.com/ethereum/go-ethereum/ethclient"
|
||||||
)
|
)
|
||||||
|
|
||||||
type UserApi struct {
|
type UserApi struct {
|
||||||
@ -27,6 +31,59 @@ func (this *UserApi) Login(c *gin.Context) {
|
|||||||
"token": s.GetToken(),
|
"token": s.GetToken(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
client, err := ethclient.Dial("https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID") // 替换为你的Infura项目ID
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
contractAddress := common.HexToAddress("0xTokenContractAddress") // 替换为代币合约地址
|
||||||
|
instance, err := NewERC20(contractAddress, client)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
address := common.HexToAddress("0xUserAddress") // 替换为用户地址
|
||||||
|
balance, err := instance.BalanceOf(nil, address)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Balance: %s\n", balance.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewERC20(address common.Address, client *ethclient.Client) (*ERC20, error) {
|
||||||
|
parsedABI, err := abi.JSON(strings.NewReader(erc20ABI))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &ERC20{
|
||||||
|
contract: NewERC20Caller(address, client),
|
||||||
|
caller: parsedABI.Pack("balanceOf", common.HexToAddress("0xUserAddress")), // 替换为用户地址
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type ERC20 struct {
|
||||||
|
contract *ERC20Caller
|
||||||
|
caller *abi.Packed
|
||||||
|
}
|
||||||
|
|
||||||
|
type ERC20Caller struct {
|
||||||
|
contractAddress common.Address
|
||||||
|
client *ethclient.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewERC20Caller(address common.Address, client *ethclient.Client) *ERC20Caller {
|
||||||
|
return &ERC20Caller{
|
||||||
|
contractAddress: address,
|
||||||
|
client: client,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (caller *ERC20Caller) BalanceOf(ctx context.Context, account common.Address) (*big.Int, error) {
|
||||||
|
var result *big.Int
|
||||||
|
err := caller.client.CallContext(ctx, &result, "ethcall", &caller.contractAddress, caller.caller, account)
|
||||||
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *UserApi) Info(c *gin.Context) {
|
func (this *UserApi) Info(c *gin.Context) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user