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)
}

View File

@ -28,7 +28,7 @@ func TransferErc20(
tokenAmount decimal.Decimal,
tokenDecimals uint8) (*types.Transaction, error) {
switch tokenAddress {
case "":
case "0":
return TransferEth(client, fromPrivateKey, toAddress, tokenAmount)
default:
return TransferErc20Token(client, fromPrivateKey, tokenAddress, toAddress, tokenAmount, tokenDecimals)
@ -81,7 +81,7 @@ func TransferEth(client *ethclient.Client, fromPrivateKey string, toAddress stri
}
// 9. (预估gas+基础费用)*1.1 作为 GasLimit
gasLimit = (gasLimit + 21000) * 12 / 10 // 增加 20%
gasLimit = (gasLimit + 21000) * 19 / 10 // 增加 90%
if gasLimit < 23000 {
gasLimit = 23000 // 最小 Gas 限制
}
@ -182,7 +182,7 @@ func TransferErc20Token(
}
// 9. (预估gas+基础费用)*1.1 作为 GasLimit
gasLimit = (gasLimit + 21000) * 12 / 10 // 增加 20%
gasLimit = (gasLimit + 21000) * 19 / 10 // 增加 90%
if gasLimit < 23000 {
gasLimit = 23000 // 最小 Gas 限制
}
@ -274,5 +274,5 @@ func IsValidAddress(address string) bool {
}
checksumAddress := common.HexToAddress(address).String()
return address == checksumAddress
return strings.EqualFold(address, checksumAddress)
}

View File

@ -167,3 +167,46 @@ func MapExcelToStruct[T any](rows [][]string, headers []string) ([]T, error) {
return results, nil
}
// 获取上传文件的数据
func GetExcelContent(c *gin.Context) (dataRows [][]string, headers []string, err error) {
// 获取上传的文件
file, err := c.FormFile("file")
if err != nil {
err = errors.New("文件上传失败")
return
}
// 打开上传的文件
src, err := file.Open()
if err != nil {
err = errors.New("文件打开失败")
return
}
defer src.Close()
// 使用 excelize 读取 Excel 文件
xlFile, err := excelize.OpenReader(src)
if err != nil {
err = errors.New("读取 Excel 文件失败")
return
}
sheetName := xlFile.GetSheetName(0)
// 假设读取第一个工作表中的数据
rows, err := xlFile.GetRows(sheetName)
if err != nil {
err = errors.New("读取 Excel 行数据失败")
return
}
if len(rows) < 1 {
err = errors.New("没有数据内容")
return
}
headers = rows[0] // First row is the header
dataRows = rows[1:]
return
}