Commit e0885355 authored by vicotor's avatar vicotor

update code

parent 71b8af45
package main package main
import ( import (
"code.wuban.net.cn/luxueqian/rpcproxy/contracts/blacklist"
"context" "context"
"fmt" "fmt"
"math/big" "github.com/ethereum/go-ethereum/accounts/abi/bind"
"strings" "strings"
"sync" "sync"
"time" "time"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient" "github.com/ethereum/go-ethereum/ethclient"
) )
...@@ -40,46 +39,23 @@ func IsInBlacklist(client *ethclient.Client, contract common.Address, addr commo ...@@ -40,46 +39,23 @@ func IsInBlacklist(client *ethclient.Client, contract common.Address, addr commo
if client == nil { if client == nil {
return false, fmt.Errorf("nil ethclient") return false, fmt.Errorf("nil ethclient")
} }
contraction, err := blacklist.NewBlacklist(contract, client)
parsed, err := abi.JSON(strings.NewReader(blacklistABI))
if err != nil { if err != nil {
return false, fmt.Errorf("failed to parse ABI: %w", err) return false, fmt.Errorf("failed to bind contract: %w", err)
} }
opts := &bind.CallOpts{
data, err := parsed.Pack("inBlacklist", addr) From: addr,
if err != nil { BlockNumber: nil,
return false, fmt.Errorf("failed to pack params: %w", err) Context: context.Background(),
} }
results, err := contraction.InBlackList(opts, []common.Address{addr})
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res, err := client.CallContract(ctx, ethereum.CallMsg{
To: &contract,
Data: data,
}, nil)
if err != nil { if err != nil {
return false, fmt.Errorf("call contract failed: %w", err) return false, fmt.Errorf("contract call failed: %w", err)
} }
if len(results) > 0 && results[0] {
// Unpack into a slice of interfaces return true, nil
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])
} }
return b, nil return false, nil
} }
// CachedIsInBlacklist checks an in-memory cache before calling the contract. // 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