Skip to content

Commit

Permalink
fees for ethereum / erc20 tx ? #153 (#173)
Browse files Browse the repository at this point in the history
* fees for ethereum / erc20 tx
  • Loading branch information
r-gochain authored Dec 16, 2020
1 parent 33f3f9f commit 80f3f5f
Showing 5 changed files with 31 additions and 25 deletions.
4 changes: 2 additions & 2 deletions cmd/web3/contracts.go
Original file line number Diff line number Diff line change
@@ -53,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, gasLimit uint64, waitForReceipt, toString bool, parameters ...interface{}) {
amount *big.Int, gasPrice *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))
@@ -99,7 +99,7 @@ func callContract(ctx context.Context, rpcURL, privateKey, contractAddress, cont
}
return
}
tx, err := web3.CallTransactFunction(ctx, client, *myabi, contractAddress, privateKey, functionName, amount, gasLimit, parameters...)
tx, err := web3.CallTransactFunction(ctx, client, *myabi, contractAddress, privateKey, functionName, amount, gasPrice, gasLimit, parameters...)
if err != nil {
fatalExit(fmt.Errorf("Error calling contract: %v", err))
}
2 changes: 1 addition & 1 deletion cmd/web3/did.go
Original file line number Diff line number Diff line change
@@ -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{}, 70000, idBytes32, hash)
tx, err := web3.CallTransactFunction(ctx, client, myabi, registryAddress, privateKey, "register", &big.Int{}, nil, 70000, idBytes32, hash)
if err != nil {
log.Fatalf("Cannot register DID identifier: %v", err)
}
20 changes: 11 additions & 9 deletions cmd/web3/main.go
Original file line number Diff line number Diff line change
@@ -294,7 +294,7 @@ func main() {
if err != nil {
fatalExit(err)
}
ReplaceTx(ctx, privateKey, network, c.Uint64("nonce"), to, amount, limit, price, dataB)
ReplaceTx(ctx, privateKey, network, c.Uint64("nonce"), to, amount, price, limit, dataB)
},
},
{
@@ -352,9 +352,10 @@ func main() {
for i, v := range c.Args().Tail() {
args[i] = v
}
price, limit := parseGasPriceAndLimit(c)
DeploySol(ctx, network, privateKey, binFile, c.String("verify"),
c.String("solc-version"), c.String("evm-version"), c.BoolT("optimize"),
c.String("explorer-api"), c.Uint64("gas-limit"), upgradeable, args...)
c.String("explorer-api"), price, limit, upgradeable, args...)
},
Flags: []cli.Flag{
cli.StringFlag{
@@ -450,7 +451,8 @@ func main() {
args[i] = v
}
amount := toAmountBig(c.String("amount"))
callContract(ctx, network.URL, privateKey, contractAddress, contractFile, function, amount, c.Uint64("gas-limit"), waitForReceipt, c.Bool("to-string"), args...)
price, limit := parseGasPriceAndLimit(c)
callContract(ctx, network.URL, privateKey, contractAddress, contractFile, function, amount, price, limit, waitForReceipt, c.Bool("to-string"), args...)
},
Flags: []cli.Flag{
cli.StringFlag{
@@ -1544,7 +1546,7 @@ func FlattenSol(ctx context.Context, iFile, oFile string) {

func DeploySol(ctx context.Context, network web3.Network,
privateKey, binFile, contractSource, solcVersion, evmVersion string, optimize bool, explorerURL string,
gasLimit uint64, upgradeable bool, params ...interface{}) {
gasPrice *big.Int, gasLimit uint64, upgradeable bool, params ...interface{}) {

if binFile == "" {
fatalExit(errors.New("Missing contract name arg."))
@@ -1578,7 +1580,7 @@ func DeploySol(ctx context.Context, network web3.Network,
}
abi = string(b)
}
tx, err := web3.DeployContract(ctx, client, privateKey, string(bin), abi, gasLimit, params...)
tx, err := web3.DeployContract(ctx, client, privateKey, string(bin), abi, gasPrice, gasLimit, params...)
if err != nil {
fatalExit(fmt.Errorf("Error deploying contract: %v", err))
}
@@ -1610,7 +1612,7 @@ func DeploySol(ctx context.Context, network web3.Network,
}

// Deploy proxy contract.
proxyTx, err := web3.DeployContract(ctx, client, privateKey, assets.OwnerUpgradeableProxyCode(receipt.ContractAddress), "", gasLimit)
proxyTx, err := web3.DeployContract(ctx, client, privateKey, assets.OwnerUpgradeableProxyCode(receipt.ContractAddress), "", gasPrice, gasLimit)
if err != nil {
log.Fatalf("Cannot deploy the upgradeable proxy contract: %v", err)
}
@@ -1720,7 +1722,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, 100000, newTargetAddress)
tx, err := web3.CallTransactFunction(ctx, client, myabi, contractAddress, privateKey, "upgrade", amount, nil, 100000, newTargetAddress)
if err != nil {
log.Fatalf("Cannot upgrade the contract: %v", err)
}
@@ -1767,7 +1769,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, 70000)
tx, err := web3.CallTransactFunction(ctx, client, myabi, contractAddress, privateKey, "pause", amount, nil, 70000)
if err != nil {
log.Fatalf("Cannot pause the contract: %v", err)
}
@@ -1789,7 +1791,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, 70000)
tx, err := web3.CallTransactFunction(ctx, client, myabi, contractAddress, privateKey, "resume", amount, nil, 70000)
if err != nil {
log.Fatalf("Cannot resume the contract: %v", err)
}
6 changes: 3 additions & 3 deletions cmd/web3/transactions.go
Original file line number Diff line number Diff line change
@@ -34,12 +34,12 @@ func IncreaseGas(ctx context.Context, privateKey string, network web3.Network, t
return
}
newPrice := new(big.Int).Add(txOrig.GasPrice, amount)
_ = ReplaceTx(ctx, privateKey, network, txOrig.Nonce, *txOrig.To, txOrig.Value, txOrig.GasLimit, newPrice, txOrig.Input)
_ = ReplaceTx(ctx, privateKey, network, txOrig.Nonce, *txOrig.To, txOrig.Value, newPrice, txOrig.GasLimit, 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 {
gasPrice *big.Int, gasLimit uint64, data []byte) *types.Transaction {
client, err := web3.Dial(network.URL)
if err != nil {
fatalExit(fmt.Errorf("Failed to connect to %q: %v", network.URL, err))
@@ -95,7 +95,7 @@ func Transfer(ctx context.Context, rpcURL, privateKey, contractAddress string, g
// 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{}, 70000, wait, toString, toAddress, amount)
callContract(ctx, rpcURL, privateKey, contractAddress, "erc20", "transfer", &big.Int{}, nil, 70000, wait, toString, toAddress, amount)
return
}

24 changes: 14 additions & 10 deletions web3.go
Original file line number Diff line number Diff line change
@@ -107,7 +107,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, gasLimit uint64, params ...interface{}) (*Transaction, error) {
amount *big.Int, gasPrice *big.Int, gasLimit uint64, params ...interface{}) (*Transaction, error) {
if address == "" {
return nil, errors.New("no contract address specified")
}
@@ -127,9 +127,11 @@ func CallTransactFunction(ctx context.Context, client Client, myabi abi.ABI, add
if err != nil {
return nil, fmt.Errorf("invalid private key: %v", err)
}
gasPrice, err := client.GetGasPrice(ctx)
if err != nil {
return nil, fmt.Errorf("cannot get gas price: %v", err)
if gasPrice == nil || gasPrice.Int64() == 0 {
gasPrice, err = client.GetGasPrice(ctx)
if err != nil {
return nil, fmt.Errorf("cannot get gas price: %v", err)
}
}
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
@@ -161,7 +163,7 @@ func CallTransactFunction(ctx context.Context, client Client, myabi abi.ABI, add

// DeployBin will deploy a bin file to the network
func DeployBin(ctx context.Context, client Client,
privateKeyHex, binFilename, abiFilename string, gasLimit uint64, constructorArgs ...interface{}) (*Transaction, error) {
privateKeyHex, binFilename, abiFilename string, gasPrice *big.Int, gasLimit uint64, constructorArgs ...interface{}) (*Transaction, error) {
bin, err := ioutil.ReadFile(binFilename)
if err != nil {
return nil, fmt.Errorf("Cannot read the bin file %q: %v", binFilename, err)
@@ -174,13 +176,13 @@ func DeployBin(ctx context.Context, client Client,
}
abi = string(b)
}
return DeployContract(ctx, client, privateKeyHex, string(bin), abi, gasLimit, constructorArgs...)
return DeployContract(ctx, client, privateKeyHex, string(bin), abi, gasPrice, gasLimit, constructorArgs...)

}

// DeployContract submits a contract creation transaction.
// abiJSON is only required when including params for the constructor.
func DeployContract(ctx context.Context, client Client, privateKeyHex string, binHex, abiJSON string, gasLimit uint64, constructorArgs ...interface{}) (*Transaction, error) {
func DeployContract(ctx context.Context, client Client, privateKeyHex string, binHex, abiJSON string, gasPrice *big.Int, gasLimit uint64, constructorArgs ...interface{}) (*Transaction, error) {
if len(privateKeyHex) > 2 && privateKeyHex[:2] == "0x" {
privateKeyHex = privateKeyHex[2:]
}
@@ -189,9 +191,11 @@ func DeployContract(ctx context.Context, client Client, privateKeyHex string, bi
return nil, fmt.Errorf("invalid private key: %v", err)
}

gasPrice, err := client.GetGasPrice(ctx)
if err != nil {
return nil, fmt.Errorf("cannot get gas price: %v", err)
if gasPrice == nil || gasPrice.Int64() == 0 {
gasPrice, err = client.GetGasPrice(ctx)
if err != nil {
return nil, fmt.Errorf("cannot get gas price: %v", err)
}
}

publicKey := privateKey.Public()

0 comments on commit 80f3f5f

Please sign in to comment.