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