Closed bitbeb closed 3 years ago
There's not enough information to locate your issue.
According to the error message "net/http: nil Context", I think you should check the usage of your Token
struct. Do you missing a context.Context
object for your Transfer
method?
您好!我部署了合约都是使用MetaMask和web3交互都是正常,但是使用GO语言始终无法成功交互
const key = {"address":"66f696ed8ec0938333d18a62c1a389440aeed9db","crypto":{"cipher":"aes-128-ctr","ciphertext":"0222aa46d6c91e0efe11c1f2b645a6ea1c58b64084c8c24dea1eda499e5b491c","cipherparams":{"iv":"315e48935b59a6c6d93b8dd449882009"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,"salt":"40bf920a0079cd38ef652f5dc5f762d1ba4b15a2549d377d12d82ea00492338d"},"mac":"730dfd292e224088bacf2ffeb9806a050ca145fe68f8057efe9671f07425be24"},"id":"74febe30-3169-4a8c-9b0a-529af214cf81","version":3}
func main() {
conn, err := ethclient.Dial("http://127.0.0.1:8545")
fmt.Println("connect to local geth node...", conn)
if err != nil {
log.Fatalf("could not connect to local node: %v", err)
}
token, err := NewMain(common.HexToAddress("0xa5f5F48BDBB50C22e8Cf56a471cD6E7eD5b41526"), conn)
if err != nil {
log.Fatalf("Failed to instantiate a Token contract: %v", err)
}
fmt.Println("contract token======>:", token)
//解锁对应账户
auth, err := bind.NewTransactor(strings.NewReader(key), "123456")
auth.Value=big.NewInt(21000000000)
if err != nil {
log.Fatalf("could not create auth: %v", auth)
}
fmt.Println("contract auth======>:", auth)
tmp, err := token.MaxBonus(nil)
//time.Sleep(time.Second * 10)
if err != nil {
log.Fatalf("transaction produce fail: %v", err)
}
fmt.Println("=========start put data61=========",tmp)
time.Sleep(time.Second * 10)
ts, err := token.Vote(auth)
fmt.Println("=========start put data61=========")
if err != nil {
log.Fatalf("transaction produce fail: %v", err)
}
fmt.Println(ts.Hash().Hex())
结果 2021/07/15 10:21:06 Failed to request token transfer: net/http: nil Context
您好!我部署了合约都是使用MetaMask和web3交互都是正常,但是使用GO语言始终无法成功交互 const key =
{"address":"66f696ed8ec0938333d18a62c1a389440aeed9db","crypto":{"cipher":"aes-128-ctr","ciphertext":"0222aa46d6c91e0efe11c1f2b645a6ea1c58b64084c8c24dea1eda499e5b491c","cipherparams":{"iv":"315e48935b59a6c6d93b8dd449882009"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,"salt":"40bf920a0079cd38ef652f5dc5f762d1ba4b15a2549d377d12d82ea00492338d"},"mac":"730dfd292e224088bacf2ffeb9806a050ca145fe68f8057efe9671f07425be24"},"id":"74febe30-3169-4a8c-9b0a-529af214cf81","version":3}
func main() {
conn, err := ethclient.Dial("http://127.0.0.1:8545") fmt.Println("connect to local geth node...", conn) if err != nil { log.Fatalf("could not connect to local node: %v", err) } token, err := NewMain(common.HexToAddress("0xa5f5F48BDBB50C22e8Cf56a471cD6E7eD5b41526"), conn) if err != nil { log.Fatalf("Failed to instantiate a Token contract: %v", err) } fmt.Println("contract token======>:", token) //解锁对应账户 auth, err := bind.NewTransactor(strings.NewReader(key), "123456") auth.Value=big.NewInt(21000000000) if err != nil { log.Fatalf("could not create auth: %v", auth) } fmt.Println("contract auth======>:", auth) tmp, err := token.MaxBonus(nil) //time.Sleep(time.Second * 10) if err != nil { log.Fatalf("transaction produce fail: %v", err) } fmt.Println("=========start put data61=========",tmp) time.Sleep(time.Second * 10) ts, err := token.Vote(auth) fmt.Println("=========start put data61=========") if err != nil { log.Fatalf("transaction produce fail: %v", err) } fmt.Println(ts.Hash().Hex())
结果 2021/07/15 10:21:06 Failed to request token transfer: net/http: nil Context
These codes are not relevant to your issue.
Can you show the source code of the token.go
?
// Code generated - DO NOT EDIT. // This file is a generated binding and any manual changes will be lost.
package main
import ( "math/big" "strings"
ethereum "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/event"
)
// Reference imports to suppress errors if they are not otherwise used. var ( = big.NewInt = strings.NewReader = ethereum.NotFound = bind.Bind = common.Big1 = types.BloomLookup _ = event.NewSubscription )
// MainABI is the input ABI used to generate the binding from. const MainABI = "[{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"comeFrom\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"to\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"time\",\"type\":\"uint256\"}],\"name\":\"LogBonus\",\"type\":\"event\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"\",\"type\":\"address\"}],\"name\":\"BebSwapContractAddress\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"MaxBonus\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"MinBonus\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"\",\"type\":\"address\"}],\"name\":\"TotalBonusOf\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"addresspayable\",\"name\":\"to\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"}],\"name\":\"sendBEB\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"addresspayable\",\"name\":\"addr\",\"type\":\"address\"}],\"name\":\"setBebSwapContractAddress\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"vete\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"stateMutability\":\"payable\",\"type\":\"receive\"}]"
// MainBin is the compiled bytecode used for deploying new contracts. var MainBin = "0x60806040527314f98bb04a2b305a210ce6496399d0b490a9fca2600460006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555034801561006557600080fd5b506105fc806100756000396000f3fe6080604052600436106100745760003560e01c80636b09f01a1161004e5780636b09f01a146101355780638582789d14610160578063cda089db146101b1578063d898e3e3146102165761007b565b80631b5d09b514610080578063572c0dc9146100f35780636a59fc8d1461010a5761007b565b3661007b57005b600080fd5b34801561008c57600080fd5b506100d9600480360360408110156100a357600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291908035906020019092919050505061027f565b604051808215151515815260200191505060405180910390f35b3480156100ff57600080fd5b5061010861040f565b005b34801561011657600080fd5b5061011f610422565b6040518082815260200191505060405180910390f35b34801561014157600080fd5b5061014a610428565b6040518082815260200191505060405180910390f35b34801561016c57600080fd5b506101af6004803603602081101561018357600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919050505061042e565b005b3480156101bd57600080fd5b50610200600480360360208110156101d457600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919050505061058e565b6040518082815260200191505060405180910390f35b34801561022257600080fd5b506102656004803603602081101561023957600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291905050506105a6565b604051808215151515815260200191505060405180910390f35b6000600260003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060009054906101000a900460ff16610340576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252601d8152602001807f556e617574686f72697a656420636f6e7472616374206164647265737300000081525060200191505060405180910390fd5b814710610404578273ffffffffffffffffffffffffffffffffffffffff166108fc839081150290604051600060405180830381858888f1935050505015801561038d573d6000803e3d6000fd5b508273ffffffffffffffffffffffffffffffffffffffff163073ffffffffffffffffffffffffffffffffffffffff167f0225b780b9dd86fca91b0cf7339324b06f52e503469ba9e92d7fc6d42fce41fb8442604051808381526020018281526020019250505060405180910390a360019050610409565b600090505b92915050565b6000808154809291906001019190505550565b60005481565b60015481565b3373ffffffffffffffffffffffffffffffffffffffff16600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16146104f1576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260148152602001807f4e6f7420616e2061646d696e6973747261746f7200000000000000000000000081525060200191505060405180910390fd5b6001600260008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060006101000a81548160ff0219169083151502179055506000600460006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050565b60036020528060005260406000206000915090505481565b60026020528060005260406000206000915054906101000a900460ff168156fea2646970667358221220376b85e897e5f95bbe9bb92d56b3731a369d106ffd749aeb81a9b45b986cb02464736f6c63430006000033"
// DeployMain deploys a new Ethereum contract, binding an instance of Main to it. func DeployMain(auth bind.TransactOpts, backend bind.ContractBackend) (common.Address, types.Transaction, *Main, error) { parsed, err := abi.JSON(strings.NewReader(MainABI)) if err != nil { return common.Address{}, nil, nil, err }
address, tx, contract, err := bind.DeployContract(auth, parsed, common.FromHex(MainBin), backend)
if err != nil {
return common.Address{}, nil, nil, err
}
return address, tx, &Main{MainCaller: MainCaller{contract: contract}, MainTransactor: MainTransactor{contract: contract}, MainFilterer: MainFilterer{contract: contract}}, nil
}
// Main is an auto generated Go binding around an Ethereum contract. type Main struct { MainCaller // Read-only binding to the contract MainTransactor // Write-only binding to the contract MainFilterer // Log filterer for contract events }
// MainCaller is an auto generated read-only Go binding around an Ethereum contract. type MainCaller struct { contract *bind.BoundContract // Generic contract wrapper for the low level calls }
// MainTransactor is an auto generated write-only Go binding around an Ethereum contract. type MainTransactor struct { contract *bind.BoundContract // Generic contract wrapper for the low level calls }
// MainFilterer is an auto generated log filtering Go binding around an Ethereum contract events. type MainFilterer struct { contract *bind.BoundContract // Generic contract wrapper for the low level calls }
// MainSession is an auto generated Go binding around an Ethereum contract, // with pre-set call and transact options. type MainSession struct { Contract *Main // Generic contract binding to set the session for CallOpts bind.CallOpts // Call options to use throughout this session TransactOpts bind.TransactOpts // Transaction auth options to use throughout this session }
// MainCallerSession is an auto generated read-only Go binding around an Ethereum contract, // with pre-set call options. type MainCallerSession struct { Contract *MainCaller // Generic contract caller binding to set the session for CallOpts bind.CallOpts // Call options to use throughout this session }
// MainTransactorSession is an auto generated write-only Go binding around an Ethereum contract, // with pre-set transact options. type MainTransactorSession struct { Contract *MainTransactor // Generic contract transactor binding to set the session for TransactOpts bind.TransactOpts // Transaction auth options to use throughout this session }
// MainRaw is an auto generated low-level Go binding around an Ethereum contract. type MainRaw struct { Contract *Main // Generic contract binding to access the raw methods on }
// MainCallerRaw is an auto generated low-level read-only Go binding around an Ethereum contract. type MainCallerRaw struct { Contract *MainCaller // Generic read-only contract binding to access the raw methods on }
// MainTransactorRaw is an auto generated low-level write-only Go binding around an Ethereum contract. type MainTransactorRaw struct { Contract *MainTransactor // Generic write-only contract binding to access the raw methods on }
// NewMain creates a new instance of Main, bound to a specific deployed contract. func NewMain(address common.Address, backend bind.ContractBackend) (*Main, error) { contract, err := bindMain(address, backend, backend, backend) if err != nil { return nil, err } return &Main{MainCaller: MainCaller{contract: contract}, MainTransactor: MainTransactor{contract: contract}, MainFilterer: MainFilterer{contract: contract}}, nil }
// NewMainCaller creates a new read-only instance of Main, bound to a specific deployed contract. func NewMainCaller(address common.Address, caller bind.ContractCaller) (*MainCaller, error) { contract, err := bindMain(address, caller, nil, nil) if err != nil { return nil, err } return &MainCaller{contract: contract}, nil }
// NewMainTransactor creates a new write-only instance of Main, bound to a specific deployed contract. func NewMainTransactor(address common.Address, transactor bind.ContractTransactor) (*MainTransactor, error) { contract, err := bindMain(address, nil, transactor, nil) if err != nil { return nil, err } return &MainTransactor{contract: contract}, nil }
// NewMainFilterer creates a new log filterer instance of Main, bound to a specific deployed contract. func NewMainFilterer(address common.Address, filterer bind.ContractFilterer) (*MainFilterer, error) { contract, err := bindMain(address, nil, nil, filterer) if err != nil { return nil, err } return &MainFilterer{contract: contract}, nil }
// bindMain binds a generic wrapper to an already deployed contract. func bindMain(address common.Address, caller bind.ContractCaller, transactor bind.ContractTransactor, filterer bind.ContractFilterer) (*bind.BoundContract, error) { parsed, err := abi.JSON(strings.NewReader(MainABI)) if err != nil { return nil, err } return bind.NewBoundContract(address, parsed, caller, transactor, filterer), nil }
// Call invokes the (constant) contract method with params as input values and // sets the output to result. The result type might be a single field for simple // returns, a slice of interfaces for anonymous returns and a struct for named // returns. func (_Main MainRaw) Call(opts bind.CallOpts, result *[]interface{}, method string, params ...interface{}) error { return _Main.Contract.MainCaller.contract.Call(opts, result, method, params...) }
// Transfer initiates a plain transaction to move funds to the contract, calling // its default method if one is available. func (_Main MainRaw) Transfer(opts bind.TransactOpts) (*types.Transaction, error) { return _Main.Contract.MainTransactor.contract.Transfer(opts) }
// Transact invokes the (paid) contract method with params as input values. func (_Main MainRaw) Transact(opts bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, error) { return _Main.Contract.MainTransactor.contract.Transact(opts, method, params...) }
// Call invokes the (constant) contract method with params as input values and // sets the output to result. The result type might be a single field for simple // returns, a slice of interfaces for anonymous returns and a struct for named // returns. func (_Main MainCallerRaw) Call(opts bind.CallOpts, result *[]interface{}, method string, params ...interface{}) error { return _Main.Contract.contract.Call(opts, result, method, params...) }
// Transfer initiates a plain transaction to move funds to the contract, calling // its default method if one is available. func (_Main MainTransactorRaw) Transfer(opts bind.TransactOpts) (*types.Transaction, error) { return _Main.Contract.contract.Transfer(opts) }
// Transact invokes the (paid) contract method with params as input values. func (_Main MainTransactorRaw) Transact(opts bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, error) { return _Main.Contract.contract.Transact(opts, method, params...) }
// BebSwapContractAddress is a free data retrieval call binding the contract method 0xd898e3e3. // // Solidity: function BebSwapContractAddress(address ) view returns(bool) func (_Main MainCaller) BebSwapContractAddress(opts bind.CallOpts, arg0 common.Address) (bool, error) { var out []interface{} err := _Main.contract.Call(opts, &out, "BebSwapContractAddress", arg0)
if err != nil {
return *new(bool), err
}
out0 := *abi.ConvertType(out[0], new(bool)).(*bool)
return out0, err
}
// BebSwapContractAddress is a free data retrieval call binding the contract method 0xd898e3e3. // // Solidity: function BebSwapContractAddress(address ) view returns(bool) func (_Main *MainSession) BebSwapContractAddress(arg0 common.Address) (bool, error) { return _Main.Contract.BebSwapContractAddress(&_Main.CallOpts, arg0) }
// BebSwapContractAddress is a free data retrieval call binding the contract method 0xd898e3e3. // // Solidity: function BebSwapContractAddress(address ) view returns(bool) func (_Main *MainCallerSession) BebSwapContractAddress(arg0 common.Address) (bool, error) { return _Main.Contract.BebSwapContractAddress(&_Main.CallOpts, arg0) }
// MaxBonus is a free data retrieval call binding the contract method 0x6a59fc8d. // // Solidity: function MaxBonus() view returns(uint256) func (_Main MainCaller) MaxBonus(opts bind.CallOpts) (*big.Int, error) { var out []interface{} err := _Main.contract.Call(opts, &out, "MaxBonus")
if err != nil {
return *new(*big.Int), err
}
out0 := *abi.ConvertType(out[0], new(*big.Int)).(**big.Int)
return out0, err
}
// MaxBonus is a free data retrieval call binding the contract method 0x6a59fc8d. // // Solidity: function MaxBonus() view returns(uint256) func (_Main MainSession) MaxBonus() (big.Int, error) { return _Main.Contract.MaxBonus(&_Main.CallOpts) }
// MaxBonus is a free data retrieval call binding the contract method 0x6a59fc8d. // // Solidity: function MaxBonus() view returns(uint256) func (_Main MainCallerSession) MaxBonus() (big.Int, error) { return _Main.Contract.MaxBonus(&_Main.CallOpts) }
// MinBonus is a free data retrieval call binding the contract method 0x6b09f01a. // // Solidity: function MinBonus() view returns(uint256) func (_Main MainCaller) MinBonus(opts bind.CallOpts) (*big.Int, error) { var out []interface{} err := _Main.contract.Call(opts, &out, "MinBonus")
if err != nil {
return *new(*big.Int), err
}
out0 := *abi.ConvertType(out[0], new(*big.Int)).(**big.Int)
return out0, err
}
// MinBonus is a free data retrieval call binding the contract method 0x6b09f01a. // // Solidity: function MinBonus() view returns(uint256) func (_Main MainSession) MinBonus() (big.Int, error) { return _Main.Contract.MinBonus(&_Main.CallOpts) }
// MinBonus is a free data retrieval call binding the contract method 0x6b09f01a. // // Solidity: function MinBonus() view returns(uint256) func (_Main MainCallerSession) MinBonus() (big.Int, error) { return _Main.Contract.MinBonus(&_Main.CallOpts) }
// TotalBonusOf is a free data retrieval call binding the contract method 0xcda089db. // // Solidity: function TotalBonusOf(address ) view returns(uint256) func (_Main MainCaller) TotalBonusOf(opts bind.CallOpts, arg0 common.Address) (*big.Int, error) { var out []interface{} err := _Main.contract.Call(opts, &out, "TotalBonusOf", arg0)
if err != nil {
return *new(*big.Int), err
}
out0 := *abi.ConvertType(out[0], new(*big.Int)).(**big.Int)
return out0, err
}
// TotalBonusOf is a free data retrieval call binding the contract method 0xcda089db. // // Solidity: function TotalBonusOf(address ) view returns(uint256) func (_Main MainSession) TotalBonusOf(arg0 common.Address) (big.Int, error) { return _Main.Contract.TotalBonusOf(&_Main.CallOpts, arg0) }
// TotalBonusOf is a free data retrieval call binding the contract method 0xcda089db. // // Solidity: function TotalBonusOf(address ) view returns(uint256) func (_Main MainCallerSession) TotalBonusOf(arg0 common.Address) (big.Int, error) { return _Main.Contract.TotalBonusOf(&_Main.CallOpts, arg0) }
// SendBEB is a paid mutator transaction binding the contract method 0x1b5d09b5. // // Solidity: function sendBEB(address to, uint256 amount) returns(bool) func (_Main MainTransactor) SendBEB(opts bind.TransactOpts, to common.Address, amount big.Int) (types.Transaction, error) { return _Main.contract.Transact(opts, "sendBEB", to, amount) }
// SendBEB is a paid mutator transaction binding the contract method 0x1b5d09b5. // // Solidity: function sendBEB(address to, uint256 amount) returns(bool) func (_Main MainSession) SendBEB(to common.Address, amount big.Int) (*types.Transaction, error) { return _Main.Contract.SendBEB(&_Main.TransactOpts, to, amount) }
// SendBEB is a paid mutator transaction binding the contract method 0x1b5d09b5. // // Solidity: function sendBEB(address to, uint256 amount) returns(bool) func (_Main MainTransactorSession) SendBEB(to common.Address, amount big.Int) (*types.Transaction, error) { return _Main.Contract.SendBEB(&_Main.TransactOpts, to, amount) }
// SetBebSwapContractAddress is a paid mutator transaction binding the contract method 0x8582789d. // // Solidity: function setBebSwapContractAddress(address addr) returns() func (_Main MainTransactor) SetBebSwapContractAddress(opts bind.TransactOpts, addr common.Address) (*types.Transaction, error) { return _Main.contract.Transact(opts, "setBebSwapContractAddress", addr) }
// SetBebSwapContractAddress is a paid mutator transaction binding the contract method 0x8582789d. // // Solidity: function setBebSwapContractAddress(address addr) returns() func (_Main MainSession) SetBebSwapContractAddress(addr common.Address) (types.Transaction, error) { return _Main.Contract.SetBebSwapContractAddress(&_Main.TransactOpts, addr) }
// SetBebSwapContractAddress is a paid mutator transaction binding the contract method 0x8582789d. // // Solidity: function setBebSwapContractAddress(address addr) returns() func (_Main MainTransactorSession) SetBebSwapContractAddress(addr common.Address) (types.Transaction, error) { return _Main.Contract.SetBebSwapContractAddress(&_Main.TransactOpts, addr) }
// Vete is a paid mutator transaction binding the contract method 0x572c0dc9. // // Solidity: function vete() returns() func (_Main MainTransactor) Vete(opts bind.TransactOpts) (*types.Transaction, error) { return _Main.contract.Transact(opts, "vete") }
// Vete is a paid mutator transaction binding the contract method 0x572c0dc9. // // Solidity: function vete() returns() func (_Main MainSession) Vete() (types.Transaction, error) { return _Main.Contract.Vete(&_Main.TransactOpts) }
// Vete is a paid mutator transaction binding the contract method 0x572c0dc9. // // Solidity: function vete() returns() func (_Main MainTransactorSession) Vete() (types.Transaction, error) { return _Main.Contract.Vete(&_Main.TransactOpts) }
// Receive is a paid mutator transaction binding the contract receive function. // // Solidity: receive() payable returns() func (_Main MainTransactor) Receive(opts bind.TransactOpts) (*types.Transaction, error) { return _Main.contract.RawTransact(opts, nil) // calldata is disallowed for receive function }
// Receive is a paid mutator transaction binding the contract receive function. // // Solidity: receive() payable returns() func (_Main MainSession) Receive() (types.Transaction, error) { return _Main.Contract.Receive(&_Main.TransactOpts) }
// Receive is a paid mutator transaction binding the contract receive function. // // Solidity: receive() payable returns() func (_Main MainTransactorSession) Receive() (types.Transaction, error) { return _Main.Contract.Receive(&_Main.TransactOpts) }
// MainLogBonusIterator is returned from FilterLogBonus and is used to iterate over the raw logs and unpacked data for LogBonus events raised by the Main contract. type MainLogBonusIterator struct { Event *MainLogBonus // Event containing the contract specifics and raw log
contract *bind.BoundContract // Generic contract to use for unpacking event data
event string // Event name to use for unpacking event data
logs chan types.Log // Log channel receiving the found contract events
sub ethereum.Subscription // Subscription for errors, completion and termination
done bool // Whether the subscription completed delivering logs
fail error // Occurred error to stop iteration
}
// Next advances the iterator to the subsequent event, returning whether there // are any more events found. In case of a retrieval or parsing error, false is // returned and Error() can be queried for the exact failure. func (it *MainLogBonusIterator) Next() bool { // If the iterator failed, stop iterating if it.fail != nil { return false } // If the iterator completed, deliver directly whatever's available if it.done { select { case log := <-it.logs: it.Event = new(MainLogBonus) if err := it.contract.UnpackLog(it.Event, it.event, log); err != nil { it.fail = err return false } it.Event.Raw = log return true
default:
return false
}
}
// Iterator still in progress, wait for either a data or an error event
select {
case log := <-it.logs:
it.Event = new(MainLogBonus)
if err := it.contract.UnpackLog(it.Event, it.event, log); err != nil {
it.fail = err
return false
}
it.Event.Raw = log
return true
case err := <-it.sub.Err():
it.done = true
it.fail = err
return it.Next()
}
}
// Error returns any retrieval or parsing error occurred during filtering. func (it *MainLogBonusIterator) Error() error { return it.fail }
// Close terminates the iteration process, releasing any pending underlying // resources. func (it *MainLogBonusIterator) Close() error { it.sub.Unsubscribe() return nil }
// MainLogBonus represents a LogBonus event raised by the Main contract. type MainLogBonus struct { ComeFrom common.Address To common.Address Amount big.Int Time big.Int Raw types.Log // Blockchain specific contextual infos }
// FilterLogBonus is a free log retrieval operation binding the contract event 0x0225b780b9dd86fca91b0cf7339324b06f52e503469ba9e92d7fc6d42fce41fb. // // Solidity: event LogBonus(address indexed comeFrom, address indexed to, uint256 amount, uint256 time) func (_Main MainFilterer) FilterLogBonus(opts bind.FilterOpts, comeFrom []common.Address, to []common.Address) (*MainLogBonusIterator, error) {
var comeFromRule []interface{}
for _, comeFromItem := range comeFrom {
comeFromRule = append(comeFromRule, comeFromItem)
}
var toRule []interface{}
for _, toItem := range to {
toRule = append(toRule, toItem)
}
logs, sub, err := _Main.contract.FilterLogs(opts, "LogBonus", comeFromRule, toRule)
if err != nil {
return nil, err
}
return &MainLogBonusIterator{contract: _Main.contract, event: "LogBonus", logs: logs, sub: sub}, nil
}
// WatchLogBonus is a free log subscription operation binding the contract event 0x0225b780b9dd86fca91b0cf7339324b06f52e503469ba9e92d7fc6d42fce41fb. // // Solidity: event LogBonus(address indexed comeFrom, address indexed to, uint256 amount, uint256 time) func (_Main MainFilterer) WatchLogBonus(opts bind.WatchOpts, sink chan<- *MainLogBonus, comeFrom []common.Address, to []common.Address) (event.Subscription, error) {
var comeFromRule []interface{}
for _, comeFromItem := range comeFrom {
comeFromRule = append(comeFromRule, comeFromItem)
}
var toRule []interface{}
for _, toItem := range to {
toRule = append(toRule, toItem)
}
logs, sub, err := _Main.contract.WatchLogs(opts, "LogBonus", comeFromRule, toRule)
if err != nil {
return nil, err
}
return event.NewSubscription(func(quit <-chan struct{}) error {
defer sub.Unsubscribe()
for {
select {
case log := <-logs:
// New log arrived, parse the event and forward to the user
event := new(MainLogBonus)
if err := _Main.contract.UnpackLog(event, "LogBonus", log); err != nil {
return err
}
event.Raw = log
select {
case sink <- event:
case err := <-sub.Err():
return err
case <-quit:
return nil
}
case err := <-sub.Err():
return err
case <-quit:
return nil
}
}
}), nil
}
// ParseLogBonus is a log parse operation binding the contract event 0x0225b780b9dd86fca91b0cf7339324b06f52e503469ba9e92d7fc6d42fce41fb. // // Solidity: event LogBonus(address indexed comeFrom, address indexed to, uint256 amount, uint256 time) func (_Main MainFilterer) ParseLogBonus(log types.Log) (MainLogBonus, error) { event := new(MainLogBonus) if err := _Main.contract.UnpackLog(event, "LogBonus", log); err != nil { return nil, err } event.Raw = log return event, nil }
你好!这个是自动生成的,应该不会有问题吧? ./abigen --sol=token.sol --pkg=main --out=token.go Hello! This is generated automatically. Should there be no problem?
Which version of go-ethereum are you using?
I think you should try:
context.Context
object to your auth
before using it, like auth.Context = context.Background()
Thank you. Add auth. Context = context. Background() to interact successfully!! Thank you.
使用GO语言直接与geth交互 func main() { // Create an IPC based RPC connection to a remote node and instantiate a contract binding conn, err := ethclient.Dial("http://127.0.0.1:8545") if err != nil { log.Fatalf("Failed to connect to the Ethereum client: %v", err) } token, err := NewToken(common.HexToAddress("0x21e6fc92f93c8a1bb41e2be64b4e1f88a54d3576"), conn) if err != nil { log.Fatalf("Failed to instantiate a Token contract: %v", err) } // Create an authorized transactor and spend 1 unicorn auth, err := bind.NewTransactor(strings.NewReader(key), "my awesome super secret password") if err != nil { log.Fatalf("Failed to create authorized transactor: %v", err) } tx, err := token.Transfer(auth, common.HexToAddress("0x0000000000000000000000000000000000000000"), big.NewInt(1)) if err != nil { log.Fatalf("Failed to request token transfer: %v", err) } fmt.Printf("Transfer pending: 0x%x\n", tx.Hash()) }
==================无法交互====报错如下:
[root@iZt4n20guljct01a322mi6Z src]# go run main.go token.go 2021/07/15 10:21:06 Failed to request token transfer: net/http: nil Context
感谢帮助!