1、调整gas费浮动比例为90%

This commit is contained in:
2025-05-17 09:10:14 +08:00
parent 38a5acface
commit 8113868cc0
19 changed files with 397 additions and 23 deletions

View File

@ -36,6 +36,30 @@ const erc20ABI = `[
}
]`
// GetBalance 查询 余额并转换为正常单位 (使用 decimal)
func GetBalance(client *ethclient.Client, tokenAddress, accountAddress string, decimals int) (decimal.Decimal, error) {
switch tokenAddress {
case "0":
return GetEthBalance(client, accountAddress, decimals)
default:
return GetERC20Balance(client, tokenAddress, accountAddress, decimals)
}
}
// GetEthBalance 查询以太坊余额并转换为正常单位 (使用 decimal)
func GetEthBalance(client *ethclient.Client, accountAddress string, decimals int) (decimal.Decimal, error) {
account := common.HexToAddress(accountAddress)
balance, err := client.BalanceAt(context.Background(), account, nil)
if err != nil {
return decimal.Zero, fmt.Errorf("查询余额失败 err:%v", err)
}
balanceDecimal := decimal.NewFromBigInt(balance, 0) // Create decimal from big.Int
decimalFactor := decimal.NewFromInt(10).Pow(decimal.NewFromInt(int64(decimals)))
readableBalance := balanceDecimal.Div(decimalFactor)
return readableBalance, nil
}
// GetERC20Balance 查询 ERC-20 代币余额并转换为正常单位 (使用 decimal)
// tokenAddress: 代币合约地址
// accountAddress: 账户地址

View File

@ -0,0 +1,25 @@
package ethbalanceofhelper
import (
"fmt"
"testing"
)
func TestBalanceOfHelper(t *testing.T) {
api := "https://stylish-cool-fire.ethereum-sepolia.quiknode.pro/17572db4c091accfa5dc6faa0c60a805e5173459"
proxy := "http://127.0.0.1:7890"
client, err := EthClientWithProxy(api, proxy)
if err != nil {
t.Error(err)
}
address := "0x5079a681a2a2344f4da79a21e187d6ed075ba90a"
balance, err := GetBalance(client, "0", address, 18)
if err != nil {
t.Error(err)
}
fmt.Println("余额:", balance)
}