Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge from dev #39

Merged
merged 2 commits into from
May 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: check balance before sending transaction
  • Loading branch information
renlulu committed May 11, 2020
commit 0fa57adda31832e3fdf6ea4194c188f5ae6b2efe
26 changes: 26 additions & 0 deletions account/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/Zilliqa/gozilliqa-sdk/transaction"
"github.com/Zilliqa/gozilliqa-sdk/util"
"github.com/Zilliqa/gozilliqa-sdk/validator"
"math/big"
"strconv"
"strings"
)
Expand Down Expand Up @@ -104,6 +105,31 @@ func (w *Wallet) SignWith(tx *transaction.Transaction, signer string, provider p
if response.Error == nil {
result := response.Result.(map[string]interface{})
n := result["nonce"].(json.Number)
bal := result["balance"].(string)
balNumber, ok := new(big.Int).SetString(bal, 10)
if !ok {
return errors.New("parse balance error")
}
amount, ok2 := new(big.Int).SetString(tx.Amount, 10)
if !ok2 {
return errors.New("parse amount error")
}
gasPrice, ok3 := new(big.Int).SetString(tx.GasPrice, 10)
if !ok3 {
return errors.New("parse gas price error")
}
gasLimit, ok4 := new(big.Int).SetString(tx.GasLimit, 10)
if !ok4 {
return errors.New("parse gas limit error")
}

gasFee := new(big.Int).Mul(gasPrice, gasLimit)
needed := new(big.Int).Add(gasFee, amount)

if needed.Cmp(balNumber) > 0 {
return errors.New("balance is not sufficient")
}

nonce, _ := n.Int64()
tx.Nonce = strconv.FormatInt(nonce+1, 10)
} else {
Expand Down
29 changes: 29 additions & 0 deletions account/wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,32 @@ func TestSendTransaction(t *testing.T) {
tx.Confirm(hash, 1000, 3, provider)
assert.True(t, tx.Status == transaction.Confirmed)
}

func TestSendTransactionInsufficientAmount(t *testing.T) {
if os.Getenv("CI") != "" {
t.Skip("Skipping testing in CI environment")
}
wallet := NewWallet()
wallet.AddByPrivateKey("e19d05c5452598e24caad4a0d85a49146f7be089515c905ae6a19e8a578a6930")
provider := provider2.NewProvider("https://dev-api.zilliqa.com/")

result, err := provider.GetMinimumGasPrice()
assert.Nil(t, err, err)
gasPrice := result.Result.(string)

tx := &transaction.Transaction{
Version: strconv.FormatInt(int64(util.Pack(333, 1)), 10),
SenderPubKey: "0246E7178DC8253201101E18FD6F6EB9972451D121FC57AA2A06DD5C111E58DC6A",
ToAddr: "4BAF5faDA8e5Db92C3d3242618c5B47133AE003C",
Amount: "10000000000000000",
GasPrice: gasPrice,
GasLimit: "1",
Code: "",
Data: "",
Priority: false,
}

err2 := wallet.Sign(tx, *provider)
assert.NotNil(t, err2)
assert.Equal(t, err2.Error(), "balance is not sufficient")
}