Skip to content

Commit

Permalink
gas-limit flag for function calls (#157)
Browse files Browse the repository at this point in the history
* Add StateMutability to ABIs

* remove conflicing args completely, force being explicit

* Flag cleaning

* adds gas-limit flag to function calls
  • Loading branch information
treeder committed Aug 6, 2020
1 parent 71b3bc9 commit 60bd37c
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 29 deletions.
7 changes: 4 additions & 3 deletions cmd/web3/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"math/big"
"time"

"github.com/gochain/gochain/v3/accounts/abi"
"github.com/gochain/web3"
)

Expand Down Expand Up @@ -52,7 +53,7 @@ func GetContractConst(ctx context.Context, rpcURL, contractAddress, contractFile
}

func callContract(ctx context.Context, rpcURL, privateKey, contractAddress, contractFile, functionName string,
amount *big.Int, waitForReceipt, toString bool, parameters ...interface{}) {
amount *big.Int, gasLimit uint64, waitForReceipt, toString bool, parameters ...interface{}) {
client, err := web3.Dial(rpcURL)
if err != nil {
fatalExit(fmt.Errorf("Failed to connect to %q: %v", rpcURL, err))
Expand All @@ -67,7 +68,7 @@ func callContract(ctx context.Context, rpcURL, privateKey, contractAddress, cont
fmt.Println("There is no such function:", functionName)
return
}
if m.Const {
if m.Const || m.StateMutability == abi.MutabilityView || m.StateMutability == abi.MutabilityPure { // view and pure are both read only: https://ethereum.stackexchange.com/a/57424/9815
res, err := web3.CallConstantFunction(ctx, client, *myabi, contractAddress, functionName, parameters...)
if err != nil {
fatalExit(fmt.Errorf("Error calling constant function: %v", err))
Expand Down Expand Up @@ -98,7 +99,7 @@ func callContract(ctx context.Context, rpcURL, privateKey, contractAddress, cont
}
return
}
tx, err := web3.CallTransactFunction(ctx, client, *myabi, contractAddress, privateKey, functionName, amount, parameters...)
tx, err := web3.CallTransactFunction(ctx, client, *myabi, contractAddress, privateKey, functionName, amount, gasLimit, parameters...)
if err != nil {
fatalExit(fmt.Errorf("Error calling contract: %v", err))
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/web3/did.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func CreateDID(ctx context.Context, rpcURL, privateKey, id, registryAddress stri
var idBytes32 [32]byte
copy(idBytes32[:], d.ID)

tx, err := web3.CallTransactFunction(ctx, client, myabi, registryAddress, privateKey, "register", &big.Int{}, idBytes32, hash)
tx, err := web3.CallTransactFunction(ctx, client, myabi, registryAddress, privateKey, "register", &big.Int{}, 70000, idBytes32, hash)
if err != nil {
log.Fatalf("Cannot register DID identifier: %v", err)
}
Expand Down
40 changes: 20 additions & 20 deletions cmd/web3/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ func main() {
args[i] = v
}
amount := toAmountBig(c.String("amount"))
callContract(ctx, network.URL, privateKey, contractAddress, contractFile, function, amount, waitForReceipt, c.Bool("to-string"), args...)
callContract(ctx, network.URL, privateKey, contractAddress, contractFile, function, amount, c.Uint64("gas-limit"), waitForReceipt, c.Bool("to-string"), args...)
},
Flags: []cli.Flag{
cli.StringFlag{
Expand Down Expand Up @@ -458,6 +458,10 @@ func main() {
Name: "to-string",
Usage: "Convert result to a string, useful if using byte arrays that are strings and you want to see the string value.",
},
cli.Uint64Flag{
Name: "gas-limit",
Value: 70000,
},
},
},
{
Expand Down Expand Up @@ -736,17 +740,16 @@ func main() {
Aliases: []string{"g"},
Subcommands: []cli.Command{
{
Name: "contract",
Usage: "Generate a contract",
Aliases: []string{"c"},
Name: "contract",
Usage: "Generate a contract",
Subcommands: []cli.Command{
{
Name: "erc20",
Usage: "Generate a erc20 contract",
Usage: "Generate an ERC20 contract",
Flags: []cli.Flag{
cli.BoolTFlag{
Name: "pausable, p",
Usage: "Pausable contract.",
Usage: "Pausable contract. Default: true",
},
cli.BoolTFlag{
Name: "mintable, m",
Expand Down Expand Up @@ -780,11 +783,11 @@ func main() {
},
{
Name: "erc721",
Usage: "Generate a erc721 contract",
Usage: "Generate an ERC721 contract",
Flags: []cli.Flag{
cli.BoolTFlag{
Name: "pausable, p",
Usage: "Pausable contract.",
Usage: "Pausable contract. Default: true",
},
cli.BoolTFlag{
Name: "mintable, m",
Expand All @@ -810,9 +813,8 @@ func main() {
},
},
{
Name: "code",
Usage: "Generate a code bindings",
Aliases: []string{"c"},
Name: "code",
Usage: "Generate code bindings",
Flags: []cli.Flag{
cli.StringFlag{
Name: "abi, a",
Expand Down Expand Up @@ -841,9 +843,8 @@ func main() {
},
},
{
Name: "did",
Aliases: []string{"c"},
Usage: "Distributed identity operations",
Name: "did",
Usage: "Distributed identity operations",
Subcommands: []cli.Command{
{
Name: "create",
Expand Down Expand Up @@ -929,9 +930,8 @@ func main() {
},

{
Name: "claim",
Aliases: []string{"c"},
Usage: "Verifiable claims operations",
Name: "claim",
Usage: "Verifiable claims operations",
Subcommands: []cli.Command{
{
Name: "sign",
Expand Down Expand Up @@ -1622,7 +1622,7 @@ func UpgradeContract(ctx context.Context, rpcURL, privateKey, contractAddress, n
if err != nil {
log.Fatalf("Cannot initialize ABI: %v", err)
}
tx, err := web3.CallTransactFunction(ctx, client, myabi, contractAddress, privateKey, "upgrade", amount, newTargetAddress)
tx, err := web3.CallTransactFunction(ctx, client, myabi, contractAddress, privateKey, "upgrade", amount, 100000, newTargetAddress)
if err != nil {
log.Fatalf("Cannot upgrade the contract: %v", err)
}
Expand Down Expand Up @@ -1669,7 +1669,7 @@ func PauseContract(ctx context.Context, rpcURL, privateKey, contractAddress stri
if err != nil {
log.Fatalf("Cannot initialize ABI: %v", err)
}
tx, err := web3.CallTransactFunction(ctx, client, myabi, contractAddress, privateKey, "pause", amount)
tx, err := web3.CallTransactFunction(ctx, client, myabi, contractAddress, privateKey, "pause", amount, 70000)
if err != nil {
log.Fatalf("Cannot pause the contract: %v", err)
}
Expand All @@ -1691,7 +1691,7 @@ func ResumeContract(ctx context.Context, rpcURL, privateKey, contractAddress str
if err != nil {
log.Fatalf("Cannot initialize ABI: %v", err)
}
tx, err := web3.CallTransactFunction(ctx, client, myabi, contractAddress, privateKey, "resume", amount)
tx, err := web3.CallTransactFunction(ctx, client, myabi, contractAddress, privateKey, "resume", amount, 70000)
if err != nil {
log.Fatalf("Cannot resume the contract: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/web3/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func IncreaseGas(ctx context.Context, privateKey string, network web3.Network, t
newPrice := new(big.Int).Add(txOrig.GasPrice, amount)
_ = ReplaceTx(ctx, privateKey, network, txOrig.Nonce, *txOrig.To, txOrig.Value, txOrig.GasLimit, newPrice, txOrig.Input)
fmt.Printf("Increased gas price to %v\n", newPrice)

}

func ReplaceTx(ctx context.Context, privateKey string, network web3.Network, nonce uint64, to common.Address, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte) *types.Transaction {
client, err := web3.Dial(network.URL)
if err != nil {
Expand Down Expand Up @@ -91,7 +91,7 @@ func Transfer(ctx context.Context, rpcURL, privateKey, contractAddress string, w
// fmt.Println("DECIMALS:", decimals, reflect.TypeOf(decimals))
// todo: could get symbol here to display
amount := web3.DecToInt(amountD, int32(decimals[0].(uint8)))
callContract(ctx, rpcURL, privateKey, contractAddress, "erc20", "transfer", &big.Int{}, wait, toString, toAddress, amount)
callContract(ctx, rpcURL, privateKey, contractAddress, "erc20", "transfer", &big.Int{}, 70000, wait, toString, toAddress, amount)
return
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5 // indirect
github.com/go-ini/ini v1.46.0 // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/gochain/gochain/v3 v3.3.4
github.com/gochain/gochain/v3 v3.3.7
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.3.2 // indirect
github.com/google/go-cmp v0.3.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/gochain/gochain/v3 v3.3.4 h1:7jvxLOjsUU3EuWTRw2sxppRKZfrfUsqMAGK963AQXiU=
github.com/gochain/gochain/v3 v3.3.4/go.mod h1:FmNsYhDZk7hM1n4XlJ/2F6WDmEWZpvstntaQXmoFlkY=
github.com/gochain/gochain/v3 v3.3.7 h1:9u+hWWUIFY6LuGeWNpeezz/9O25Qfm2LcU90dm2W2AI=
github.com/gochain/gochain/v3 v3.3.7/go.mod h1:FmNsYhDZk7hM1n4XlJ/2F6WDmEWZpvstntaQXmoFlkY=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6 h1:ZgQEtGgCBiWRM39fZuwSd1LwSqqSW0hOdXCYYDX0R3I=
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
Expand Down
4 changes: 2 additions & 2 deletions web3.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func CallConstantFunction(ctx context.Context, client Client, myabi abi.ABI, add

// CallTransactFunction submits a transaction to execute a smart contract function call.
func CallTransactFunction(ctx context.Context, client Client, myabi abi.ABI, address, privateKeyHex, functionName string,
amount *big.Int, params ...interface{}) (*Transaction, error) {
amount *big.Int, gasLimit uint64, params ...interface{}) (*Transaction, error) {
if address == "" {
return nil, errors.New("no contract address specified")
}
Expand Down Expand Up @@ -142,7 +142,7 @@ func CallTransactFunction(ctx context.Context, client Client, myabi abi.ABI, add
}
toAddress := common.HexToAddress(address)
// fmt.Println("Price: ", gasPrice)
tx := types.NewTransaction(nonce, toAddress, amount, 70000, gasPrice, input)
tx := types.NewTransaction(nonce, toAddress, amount, gasLimit, gasPrice, input)
signedTx, err := types.SignTx(tx, types.HomesteadSigner{}, privateKey)
if err != nil {
return nil, fmt.Errorf("cannot sign transaction: %v", err)
Expand Down

0 comments on commit 60bd37c

Please sign in to comment.