Commit e0885355 authored by vicotor's avatar vicotor

update code

parent 71b8af45
package main
import (
"code.wuban.net.cn/luxueqian/rpcproxy/contracts/blacklist"
"context"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"strings"
"sync"
"time"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
)
......@@ -40,46 +39,23 @@ func IsInBlacklist(client *ethclient.Client, contract common.Address, addr commo
if client == nil {
return false, fmt.Errorf("nil ethclient")
}
parsed, err := abi.JSON(strings.NewReader(blacklistABI))
contraction, err := blacklist.NewBlacklist(contract, client)
if err != nil {
return false, fmt.Errorf("failed to parse ABI: %w", err)
return false, fmt.Errorf("failed to bind contract: %w", err)
}
data, err := parsed.Pack("inBlacklist", addr)
if err != nil {
return false, fmt.Errorf("failed to pack params: %w", err)
opts := &bind.CallOpts{
From: addr,
BlockNumber: nil,
Context: context.Background(),
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res, err := client.CallContract(ctx, ethereum.CallMsg{
To: &contract,
Data: data,
}, nil)
results, err := contraction.InBlackList(opts, []common.Address{addr})
if err != nil {
return false, fmt.Errorf("call contract failed: %w", err)
return false, fmt.Errorf("contract call failed: %w", err)
}
// Unpack into a slice of interfaces
var out []interface{}
if err := parsed.UnpackIntoInterface(&out, "inBlacklist", res); err != nil {
return false, fmt.Errorf("failed to unpack result: %w", err)
}
if len(out) == 0 {
return false, fmt.Errorf("empty result from contract")
}
b, ok := out[0].(bool)
if !ok {
// Some ABI versions may return type *big.Int for booleans encoded as uint8; handle that just in case
if bi, ok2 := out[0].(*big.Int); ok2 {
return bi.Cmp(big.NewInt(0)) != 0, nil
}
return false, fmt.Errorf("unexpected result type: %T", out[0])
if len(results) > 0 && results[0] {
return true, nil
}
return b, nil
return false, nil
}
// CachedIsInBlacklist checks an in-memory cache before calling the contract.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment