gitlab.com/yannislg/go-pulse@v0.0.0-20210722055913-a3e24e95638d/mobile/bind.go (about) 1 // Copyright 2016 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 // Contains all the wrappers from the bind package. 18 19 package geth 20 21 import ( 22 "math/big" 23 "strings" 24 25 "github.com/ethereum/go-ethereum/accounts/abi" 26 "github.com/ethereum/go-ethereum/accounts/abi/bind" 27 "github.com/ethereum/go-ethereum/accounts/keystore" 28 "github.com/ethereum/go-ethereum/common" 29 "github.com/ethereum/go-ethereum/core/types" 30 ) 31 32 // Signer is an interface defining the callback when a contract requires a 33 // method to sign the transaction before submission. 34 type Signer interface { 35 Sign(*Address, *Transaction) (tx *Transaction, _ error) 36 } 37 38 type MobileSigner struct { 39 sign bind.SignerFn 40 } 41 42 func (s *MobileSigner) Sign(addr *Address, unsignedTx *Transaction) (signedTx *Transaction, _ error) { 43 sig, err := s.sign(types.EIP155Signer{}, addr.address, unsignedTx.tx) 44 if err != nil { 45 return nil, err 46 } 47 return &Transaction{sig}, nil 48 } 49 50 // CallOpts is the collection of options to fine tune a contract call request. 51 type CallOpts struct { 52 opts bind.CallOpts 53 } 54 55 // NewCallOpts creates a new option set for contract calls. 56 func NewCallOpts() *CallOpts { 57 return new(CallOpts) 58 } 59 60 func (opts *CallOpts) IsPending() bool { return opts.opts.Pending } 61 func (opts *CallOpts) GetGasLimit() int64 { return 0 /* TODO(karalabe) */ } 62 63 // GetContext cannot be reliably implemented without identity preservation (https://github.com/golang/go/issues/16876) 64 // Even then it's awkward to unpack the subtleties of a Go context out to Java. 65 // func (opts *CallOpts) GetContext() *Context { return &Context{opts.opts.Context} } 66 67 func (opts *CallOpts) SetPending(pending bool) { opts.opts.Pending = pending } 68 func (opts *CallOpts) SetGasLimit(limit int64) { /* TODO(karalabe) */ } 69 func (opts *CallOpts) SetContext(context *Context) { opts.opts.Context = context.context } 70 func (opts *CallOpts) SetFrom(addr *Address) { opts.opts.From = addr.address } 71 72 // TransactOpts is the collection of authorization data required to create a 73 // valid Ethereum transaction. 74 type TransactOpts struct { 75 opts bind.TransactOpts 76 } 77 78 // NewTransactOpts creates a new option set for contract transaction. 79 func NewTransactOpts() *TransactOpts { 80 return new(TransactOpts) 81 } 82 83 // NewKeyedTransactOpts is a utility method to easily create a transaction signer 84 // from a single private key. 85 func NewKeyedTransactOpts(keyJson []byte, passphrase string) (*TransactOpts, error) { 86 key, err := keystore.DecryptKey(keyJson, passphrase) 87 if err != nil { 88 return nil, err 89 } 90 return &TransactOpts{*bind.NewKeyedTransactor(key.PrivateKey)}, nil 91 } 92 93 func (opts *TransactOpts) GetFrom() *Address { return &Address{opts.opts.From} } 94 func (opts *TransactOpts) GetNonce() int64 { return opts.opts.Nonce.Int64() } 95 func (opts *TransactOpts) GetValue() *BigInt { return &BigInt{opts.opts.Value} } 96 func (opts *TransactOpts) GetGasPrice() *BigInt { return &BigInt{opts.opts.GasPrice} } 97 func (opts *TransactOpts) GetGasLimit() int64 { return int64(opts.opts.GasLimit) } 98 99 // GetSigner cannot be reliably implemented without identity preservation (https://github.com/golang/go/issues/16876) 100 // func (opts *TransactOpts) GetSigner() Signer { return &signer{opts.opts.Signer} } 101 102 // GetContext cannot be reliably implemented without identity preservation (https://github.com/golang/go/issues/16876) 103 // Even then it's awkward to unpack the subtleties of a Go context out to Java. 104 //func (opts *TransactOpts) GetContext() *Context { return &Context{opts.opts.Context} } 105 106 func (opts *TransactOpts) SetFrom(from *Address) { opts.opts.From = from.address } 107 func (opts *TransactOpts) SetNonce(nonce int64) { opts.opts.Nonce = big.NewInt(nonce) } 108 func (opts *TransactOpts) SetSigner(s Signer) { 109 opts.opts.Signer = func(signer types.Signer, addr common.Address, tx *types.Transaction) (*types.Transaction, error) { 110 sig, err := s.Sign(&Address{addr}, &Transaction{tx}) 111 if err != nil { 112 return nil, err 113 } 114 return sig.tx, nil 115 } 116 } 117 func (opts *TransactOpts) SetValue(value *BigInt) { opts.opts.Value = value.bigint } 118 func (opts *TransactOpts) SetGasPrice(price *BigInt) { opts.opts.GasPrice = price.bigint } 119 func (opts *TransactOpts) SetGasLimit(limit int64) { opts.opts.GasLimit = uint64(limit) } 120 func (opts *TransactOpts) SetContext(context *Context) { opts.opts.Context = context.context } 121 122 // BoundContract is the base wrapper object that reflects a contract on the 123 // Ethereum network. It contains a collection of methods that are used by the 124 // higher level contract bindings to operate. 125 type BoundContract struct { 126 contract *bind.BoundContract 127 address common.Address 128 deployer *types.Transaction 129 } 130 131 // DeployContract deploys a contract onto the Ethereum blockchain and binds the 132 // deployment address with a wrapper. 133 func DeployContract(opts *TransactOpts, abiJSON string, bytecode []byte, client *EthereumClient, args *Interfaces) (contract *BoundContract, _ error) { 134 // Deploy the contract to the network 135 parsed, err := abi.JSON(strings.NewReader(abiJSON)) 136 if err != nil { 137 return nil, err 138 } 139 addr, tx, bound, err := bind.DeployContract(&opts.opts, parsed, common.CopyBytes(bytecode), client.client, args.objects...) 140 if err != nil { 141 return nil, err 142 } 143 return &BoundContract{ 144 contract: bound, 145 address: addr, 146 deployer: tx, 147 }, nil 148 } 149 150 // BindContract creates a low level contract interface through which calls and 151 // transactions may be made through. 152 func BindContract(address *Address, abiJSON string, client *EthereumClient) (contract *BoundContract, _ error) { 153 parsed, err := abi.JSON(strings.NewReader(abiJSON)) 154 if err != nil { 155 return nil, err 156 } 157 return &BoundContract{ 158 contract: bind.NewBoundContract(address.address, parsed, client.client, client.client, client.client), 159 address: address.address, 160 }, nil 161 } 162 163 func (c *BoundContract) GetAddress() *Address { return &Address{c.address} } 164 func (c *BoundContract) GetDeployer() *Transaction { 165 if c.deployer == nil { 166 return nil 167 } 168 return &Transaction{c.deployer} 169 } 170 171 // Call invokes the (constant) contract method with params as input values and 172 // sets the output to result. 173 func (c *BoundContract) Call(opts *CallOpts, out *Interfaces, method string, args *Interfaces) error { 174 if len(out.objects) == 1 { 175 result := out.objects[0] 176 if err := c.contract.Call(&opts.opts, result, method, args.objects...); err != nil { 177 return err 178 } 179 out.objects[0] = result 180 } else { 181 results := make([]interface{}, len(out.objects)) 182 copy(results, out.objects) 183 if err := c.contract.Call(&opts.opts, &results, method, args.objects...); err != nil { 184 return err 185 } 186 copy(out.objects, results) 187 } 188 return nil 189 } 190 191 // Transact invokes the (paid) contract method with params as input values. 192 func (c *BoundContract) Transact(opts *TransactOpts, method string, args *Interfaces) (tx *Transaction, _ error) { 193 rawTx, err := c.contract.Transact(&opts.opts, method, args.objects...) 194 if err != nil { 195 return nil, err 196 } 197 return &Transaction{rawTx}, nil 198 } 199 200 // RawTransact invokes the (paid) contract method with raw calldata as input values. 201 func (c *BoundContract) RawTransact(opts *TransactOpts, calldata []byte) (tx *Transaction, _ error) { 202 rawTx, err := c.contract.RawTransact(&opts.opts, calldata) 203 if err != nil { 204 return nil, err 205 } 206 return &Transaction{rawTx}, nil 207 } 208 209 // Transfer initiates a plain transaction to move funds to the contract, calling 210 // its default method if one is available. 211 func (c *BoundContract) Transfer(opts *TransactOpts) (tx *Transaction, _ error) { 212 rawTx, err := c.contract.Transfer(&opts.opts) 213 if err != nil { 214 return nil, err 215 } 216 return &Transaction{rawTx}, nil 217 }