Skip to content

Commit

Permalink
updated transfer to return tx hash value
Browse files Browse the repository at this point in the history
  • Loading branch information
Susruth Nadimpalli committed Dec 20, 2018
1 parent d28940a commit a23260d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
14 changes: 10 additions & 4 deletions account.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ type Account interface {
ReadAddress(key string) (common.Address, error)

// Transfer sends the specified value of Eth to the given address.
Transfer(ctx context.Context, to common.Address, value *big.Int, confirmBlocks int64) error
Transfer(ctx context.Context, to common.Address, value *big.Int, confirmBlocks int64) (string, error)

// Transact performs a write operation on the Ethereum blockchain. It will
// first conduct a preConditionCheck and if the check passes, it will
Expand Down Expand Up @@ -318,14 +318,15 @@ func (account *account) Transact(ctx context.Context, preConditionCheck func() b
}

// Transfer transfers eth from the account to an ethereum address.
func (account *account) Transfer(ctx context.Context, to common.Address, value *big.Int, confirmBlocks int64) error {
func (account *account) Transfer(ctx context.Context, to common.Address, value *big.Int, confirmBlocks int64) (string, error) {

// Pre-condition check: Check if the account has enough balance
preConditionCheck := func() bool {
accountBalance, err := account.client.BalanceOf(ctx, account.Address())
return err == nil && accountBalance.Cmp(value) >= 0
}

var txHash string
// Transaction: Transfer eth to address
f := func(transactOpts *bind.TransactOpts) (*types.Transaction, error) {
bound := bind.NewBoundContract(to, abi.ABI{}, nil, account.client.EthClient(), nil)
Expand All @@ -344,10 +345,15 @@ func (account *account) Transfer(ctx context.Context, to common.Address, value *
transactor.GasPrice = big.NewInt(0).Set(transactOpts.GasPrice)
}

return bound.Transfer(transactor)
tx, err := bound.Transfer(transactor)
if err != nil {
return tx, err
}
txHash = tx.Hash().String()
return tx, nil
}

return account.Transact(ctx, preConditionCheck, f, nil, confirmBlocks)
return txHash, account.Transact(ctx, preConditionCheck, f, nil, confirmBlocks)
}

// Sign the given message with the account's private key.
Expand Down
2 changes: 1 addition & 1 deletion beth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ var _ = Describe("contracts", func() {
Expect(err).ShouldNot(HaveOccurred())
// Transfer 1 Eth to the other account's address
value, _ := big.NewFloat(1 * math.Pow10(18)).Int(nil)
if err := account.Transfer(ctx, toAddrs[i], value, waitBlocks); err != nil {
if _, err := account.Transfer(ctx, toAddrs[i], value, waitBlocks); err != nil {
Expect(err).ShouldNot(HaveOccurred())
}
})
Expand Down

0 comments on commit a23260d

Please sign in to comment.