github.com/CommerciumBlockchain/go-commercium@v0.0.0-20220709212705-b46438a77516/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/CommerciumBlockchain/go-commercium/accounts/abi" 26 "github.com/CommerciumBlockchain/go-commercium/accounts/abi/bind" 27 "github.com/CommerciumBlockchain/go-commercium/accounts/keystore" 28 "github.com/CommerciumBlockchain/go-commercium/common" 29 "github.com/CommerciumBlockchain/go-commercium/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(addr *Address, unsignedTx *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(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 62 // GetContext cannot be reliably implemented without identity preservation (https://github.com/golang/go/issues/16876) 63 // Even then it's awkward to unpack the subtleties of a Go context out to Java. 64 // func (opts *CallOpts) GetContext() *Context { return &Context{opts.opts.Context} } 65 66 func (opts *CallOpts) SetPending(pending bool) { opts.opts.Pending = pending } 67 func (opts *CallOpts) SetContext(context *Context) { opts.opts.Context = context.context } 68 func (opts *CallOpts) SetFrom(addr *Address) { opts.opts.From = addr.address } 69 70 // TransactOpts is the collection of authorization data required to create a 71 // valid Ethereum transaction. 72 type TransactOpts struct { 73 opts bind.TransactOpts 74 } 75 76 // NewTransactOpts creates a new option set for contract transaction. 77 func NewTransactOpts() *TransactOpts { 78 return new(TransactOpts) 79 } 80 81 // NewKeyedTransactOpts is a utility method to easily create a transaction signer 82 // from a single private key. 83 func NewKeyedTransactOpts(keyJson []byte, passphrase string, chainID *big.Int) (*TransactOpts, error) { 84 key, err := keystore.DecryptKey(keyJson, passphrase) 85 if err != nil { 86 return nil, err 87 } 88 auth, err := bind.NewKeyedTransactorWithChainID(key.PrivateKey, chainID) 89 if err != nil { 90 return nil, err 91 } 92 return &TransactOpts{*auth}, nil 93 } 94 95 func (opts *TransactOpts) GetFrom() *Address { return &Address{opts.opts.From} } 96 func (opts *TransactOpts) GetNonce() int64 { return opts.opts.Nonce.Int64() } 97 func (opts *TransactOpts) GetValue() *BigInt { return &BigInt{opts.opts.Value} } 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(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) SetContext(context *Context) { opts.opts.Context = context.context } 119 120 // BoundContract is the base wrapper object that reflects a contract on the 121 // Ethereum network. It contains a collection of methods that are used by the 122 // higher level contract bindings to operate. 123 type BoundContract struct { 124 contract *bind.BoundContract 125 address common.Address 126 deployer *types.Transaction 127 } 128 129 // DeployContract deploys a contract onto the Ethereum blockchain and binds the 130 // deployment address with a wrapper. 131 func DeployContract(opts *TransactOpts, abiJSON string, bytecode []byte, client *EthereumClient, args *Interfaces) (contract *BoundContract, _ error) { 132 // Deploy the contract to the network 133 parsed, err := abi.JSON(strings.NewReader(abiJSON)) 134 if err != nil { 135 return nil, err 136 } 137 addr, tx, bound, err := bind.DeployContract(&opts.opts, parsed, common.CopyBytes(bytecode), client.client, args.objects...) 138 if err != nil { 139 return nil, err 140 } 141 return &BoundContract{ 142 contract: bound, 143 address: addr, 144 deployer: tx, 145 }, nil 146 } 147 148 // BindContract creates a low level contract interface through which calls and 149 // transactions may be made through. 150 func BindContract(address *Address, abiJSON string, client *EthereumClient) (contract *BoundContract, _ error) { 151 parsed, err := abi.JSON(strings.NewReader(abiJSON)) 152 if err != nil { 153 return nil, err 154 } 155 return &BoundContract{ 156 contract: bind.NewBoundContract(address.address, parsed, client.client, client.client, client.client), 157 address: address.address, 158 }, nil 159 } 160 161 func (c *BoundContract) GetAddress() *Address { return &Address{c.address} } 162 func (c *BoundContract) GetDeployer() *Transaction { 163 if c.deployer == nil { 164 return nil 165 } 166 return &Transaction{c.deployer} 167 } 168 169 // Call invokes the (constant) contract method with params as input values and 170 // sets the output to result. 171 func (c *BoundContract) Call(opts *CallOpts, out *Interfaces, method string, args *Interfaces) error { 172 results := make([]interface{}, len(out.objects)) 173 copy(results, out.objects) 174 if err := c.contract.Call(&opts.opts, &results, method, args.objects...); err != nil { 175 return err 176 } 177 copy(out.objects, results) 178 return nil 179 } 180 181 // Transact invokes the (paid) contract method with params as input values. 182 func (c *BoundContract) Transact(opts *TransactOpts, method string, args *Interfaces) (tx *Transaction, _ error) { 183 rawTx, err := c.contract.Transact(&opts.opts, method, args.objects...) 184 if err != nil { 185 return nil, err 186 } 187 return &Transaction{rawTx}, nil 188 } 189 190 // RawTransact invokes the (paid) contract method with raw calldata as input values. 191 func (c *BoundContract) RawTransact(opts *TransactOpts, calldata []byte) (tx *Transaction, _ error) { 192 rawTx, err := c.contract.RawTransact(&opts.opts, calldata) 193 if err != nil { 194 return nil, err 195 } 196 return &Transaction{rawTx}, nil 197 } 198 199 // Transfer initiates a plain transaction to move funds to the contract, calling 200 // its default method if one is available. 201 func (c *BoundContract) Transfer(opts *TransactOpts) (tx *Transaction, _ error) { 202 rawTx, err := c.contract.Transfer(&opts.opts) 203 if err != nil { 204 return nil, err 205 } 206 return &Transaction{rawTx}, nil 207 }