base.go 7.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// Copyright 2016 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package bind

import (
	"errors"
	"fmt"
	"math/big"
23
	"sync/atomic"
24 25 26 27

	"github.com/ethereum/go-ethereum/accounts/abi"
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/core/types"
28
	"github.com/ethereum/go-ethereum/crypto"
29 30 31 32 33 34
)

// SignerFn is a signer function callback when a contract requires a method to
// sign the transaction before submission.
type SignerFn func(common.Address, *types.Transaction) (*types.Transaction, error)

35 36 37 38 39 40 41 42
// CallOpts is the collection of options to fine tune a contract call request.
type CallOpts struct {
	Pending bool // Whether to operate on the pending state or the last known one
}

// TransactOpts is the collection of authorization data required to create a
// valid Ethereum transaction.
type TransactOpts struct {
43 44 45
	From   common.Address // Ethereum account to send the transaction from
	Nonce  *big.Int       // Nonce to use for the transaction execution (nil = use pending state)
	Signer SignerFn       // Method to use for signing the transaction (mandatory)
46 47 48 49 50 51 52 53 54 55 56 57 58 59

	Value    *big.Int // Funds to transfer along along the transaction (nil = 0 = no funds)
	GasPrice *big.Int // Gas price to use for the transaction execution (nil = gas price oracle)
	GasLimit *big.Int // Gas limit to set for the transaction execution (nil = estimate + 10%)
}

// BoundContract is the base wrapper object that reflects a contract on the
// Ethereum network. It contains a collection of methods that are used by the
// higher level contract bindings to operate.
type BoundContract struct {
	address    common.Address     // Deployment address of the contract on the Ethereum blockchain
	abi        abi.ABI            // Reflect based ABI to access the correct Ethereum methods
	caller     ContractCaller     // Read interface to interact with the blockchain
	transactor ContractTransactor // Write interface to interact with the blockchain
60 61 62

	latestHasCode  uint32 // Cached verification that the latest state contains code for this contract
	pendingHasCode uint32 // Cached verification that the pending state contains code for this contract
63 64 65 66 67 68 69 70 71 72 73 74 75
}

// NewBoundContract creates a low level contract interface through which calls
// and transactions may be made through.
func NewBoundContract(address common.Address, abi abi.ABI, caller ContractCaller, transactor ContractTransactor) *BoundContract {
	return &BoundContract{
		address:    address,
		abi:        abi,
		caller:     caller,
		transactor: transactor,
	}
}

76 77 78 79
// DeployContract deploys a contract onto the Ethereum blockchain and binds the
// deployment address with a Go wrapper.
func DeployContract(opts *TransactOpts, abi abi.ABI, bytecode []byte, backend ContractBackend, params ...interface{}) (common.Address, *types.Transaction, *BoundContract, error) {
	// Otherwise try to deploy the contract
80
	c := NewBoundContract(common.Address{}, abi, backend, backend)
81 82 83 84 85 86 87 88 89

	input, err := c.abi.Pack("", params...)
	if err != nil {
		return common.Address{}, nil, nil, err
	}
	tx, err := c.transact(opts, nil, append(bytecode, input...))
	if err != nil {
		return common.Address{}, nil, nil, err
	}
90
	c.address = crypto.CreateAddress(opts.From, tx.Nonce())
91 92 93
	return c.address, tx, c, nil
}

94 95 96 97
// 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.
98 99 100 101 102
func (c *BoundContract) Call(opts *CallOpts, result interface{}, method string, params ...interface{}) error {
	// Don't crash on a lazy user
	if opts == nil {
		opts = new(CallOpts)
	}
103 104 105 106 107 108 109 110 111 112 113 114 115
	// Make sure we have a contract to operate on, and bail out otherwise
	if (opts.Pending && atomic.LoadUint32(&c.pendingHasCode) == 0) || (!opts.Pending && atomic.LoadUint32(&c.latestHasCode) == 0) {
		if code, err := c.caller.HasCode(c.address, opts.Pending); err != nil {
			return err
		} else if !code {
			return ErrNoCode
		}
		if opts.Pending {
			atomic.StoreUint32(&c.pendingHasCode, 1)
		} else {
			atomic.StoreUint32(&c.latestHasCode, 1)
		}
	}
116
	// Pack the input, call and unpack the results
117 118 119 120
	input, err := c.abi.Pack(method, params...)
	if err != nil {
		return err
	}
121
	output, err := c.caller.ContractCall(c.address, input, opts.Pending)
122 123 124 125 126 127
	if err != nil {
		return err
	}
	return c.abi.Unpack(result, method, output)
}

128
// Transact invokes the (paid) contract method with params as input values.
129 130
func (c *BoundContract) Transact(opts *TransactOpts, method string, params ...interface{}) (*types.Transaction, error) {
	// Otherwise pack up the parameters and invoke the contract
131 132 133 134
	input, err := c.abi.Pack(method, params...)
	if err != nil {
		return nil, err
	}
135 136 137
	return c.transact(opts, &c.address, input)
}

138 139 140 141 142 143
// Transfer initiates a plain transaction to move funds to the contract, calling
// its default method if one is available.
func (c *BoundContract) Transfer(opts *TransactOpts) (*types.Transaction, error) {
	return c.transact(opts, &c.address, nil)
}

144 145 146 147 148
// transact executes an actual transaction invocation, first deriving any missing
// authorization fields, and then scheduling the transaction for execution.
func (c *BoundContract) transact(opts *TransactOpts, contract *common.Address, input []byte) (*types.Transaction, error) {
	var err error

149 150 151 152 153 154 155
	// Ensure a valid value field and resolve the account nonce
	value := opts.Value
	if value == nil {
		value = new(big.Int)
	}
	nonce := uint64(0)
	if opts.Nonce == nil {
156
		nonce, err = c.transactor.PendingAccountNonce(opts.From)
157 158 159 160 161 162 163 164 165
		if err != nil {
			return nil, fmt.Errorf("failed to retrieve account nonce: %v", err)
		}
	} else {
		nonce = opts.Nonce.Uint64()
	}
	// Figure out the gas allowance and gas price values
	gasPrice := opts.GasPrice
	if gasPrice == nil {
166
		gasPrice, err = c.transactor.SuggestGasPrice()
167 168 169 170 171 172
		if err != nil {
			return nil, fmt.Errorf("failed to suggest gas price: %v", err)
		}
	}
	gasLimit := opts.GasLimit
	if gasLimit == nil {
173 174 175 176 177 178 179 180 181 182
		// Gas estimation cannot succeed without code for method invocations
		if contract != nil && atomic.LoadUint32(&c.pendingHasCode) == 0 {
			if code, err := c.transactor.HasCode(c.address, true); err != nil {
				return nil, err
			} else if !code {
				return nil, ErrNoCode
			}
			atomic.StoreUint32(&c.pendingHasCode, 1)
		}
		// If the contract surely has code (or code is not needed), estimate the transaction
183
		gasLimit, err = c.transactor.EstimateGasLimit(opts.From, contract, value, input)
184 185 186 187 188
		if err != nil {
			return nil, fmt.Errorf("failed to exstimate gas needed: %v", err)
		}
	}
	// Create the transaction, sign it and schedule it for execution
189 190 191 192 193 194
	var rawTx *types.Transaction
	if contract == nil {
		rawTx = types.NewContractCreation(nonce, value, gasLimit, gasPrice, input)
	} else {
		rawTx = types.NewTransaction(nonce, c.address, value, gasLimit, gasPrice, input)
	}
195 196 197
	if opts.Signer == nil {
		return nil, errors.New("no signer to authorize the transaction with")
	}
198
	signedTx, err := opts.Signer(opts.From, rawTx)
199 200 201 202 203 204 205 206
	if err != nil {
		return nil, err
	}
	if err := c.transactor.SendTransaction(signedTx); err != nil {
		return nil, err
	}
	return signedTx, nil
}