diff --git a/sdk/client/test/config.go b/sdk/client/test/config.go index af023ef6f..b897e1a97 100644 --- a/sdk/client/test/config.go +++ b/sdk/client/test/config.go @@ -1,7 +1,7 @@ package test const ( - TEST_PRIVATE_KEY = "3018f5ceec22c2a1389fe3a0933b52f3fc31450c5322002dd76477964797fb13" + TEST_PRIVATE_KEY = "ff0911d998e6f12cf4b0a88d90cc411aa3be06c9dc5a2b35834f0e25a44e2275" TEST_ADDR = "0x76d244CE05c3De4BbC6fDd7F56379B145709ade9" TEST_ADDR2 = "0x5dEfC28ce1Ed331D56aB6F607f78708880e11Bea" TEST_ADDR3 = "0x593107F1D5D10A68D3C3722C35ADa2eb779D44A4" @@ -10,5 +10,5 @@ const ( TEST_GRPC_ADDR = "localhost:9090" TEST_RPC_ADDR = "http://0.0.0.0:26750" TEST_CHAIN_ID = "greenfield_9000-121" - TEST_TOKEN_NAME = "bnb" + TEST_TOKEN_NAME = "BNB" ) diff --git a/sdk/client/tx.go b/sdk/client/tx.go index fd3a356be..5eb1d1cf6 100644 --- a/sdk/client/tx.go +++ b/sdk/client/tx.go @@ -16,8 +16,6 @@ import ( "github.com/bnb-chain/greenfield/sdk/types" ) -const GasMultiplier = float64(1.2) - type TransactionClient interface { BroadcastTx(msgs []sdk.Msg, txOpt *types.TxOption, opts ...grpc.CallOption) (*tx.BroadcastTxResponse, error) SimulateTx(msgs []sdk.Msg, txOpt *types.TxOption, opts ...grpc.CallOption) (*tx.SimulateResponse, error) @@ -31,7 +29,7 @@ func (c *GreenfieldClient) BroadcastTx(msgs []sdk.Msg, txOpt *types.TxOption, op txBuilder := txConfig.NewTxBuilder() // txBuilder holds tx info - if err := c.constructTxWithGasLimit(msgs, txOpt, txConfig, txBuilder); err != nil { + if err := c.constructTxWithGasInfo(msgs, txOpt, txConfig, txBuilder); err != nil { return nil, err } @@ -96,7 +94,7 @@ func (c *GreenfieldClient) simulateTx(txBytes []byte, opts ...grpc.CallOption) ( func (c *GreenfieldClient) SignTx(msgs []sdk.Msg, txOpt *types.TxOption) ([]byte, error) { txConfig := authtx.NewTxConfig(c.codec, []signing.SignMode{signing.SignMode_SIGN_MODE_EIP_712}) txBuilder := txConfig.NewTxBuilder() - if err := c.constructTxWithGasLimit(msgs, txOpt, txConfig, txBuilder); err != nil { + if err := c.constructTxWithGasInfo(msgs, txOpt, txConfig, txBuilder); err != nil { return nil, err } return c.signTx(txConfig, txBuilder) @@ -174,9 +172,6 @@ func (c *GreenfieldClient) constructTx(msgs []sdk.Msg, txOpt *types.TxOption, tx if txOpt.Memo != "" { txBuilder.SetMemo(txOpt.Memo) } - if !txOpt.FeeAmount.IsZero() { - txBuilder.SetFeeAmount(txOpt.FeeAmount) - } if !txOpt.FeePayer.Empty() { txBuilder.SetFeePayer(txOpt.FeePayer) } @@ -185,17 +180,11 @@ func (c *GreenfieldClient) constructTx(msgs []sdk.Msg, txOpt *types.TxOption, tx return c.setSingerInfo(txBuilder) } -func (c *GreenfieldClient) constructTxWithGasLimit(msgs []sdk.Msg, txOpt *types.TxOption, txConfig client.TxConfig, txBuilder client.TxBuilder) error { - // construct a tx with txOpt excluding GasLimit - err := c.constructTx(msgs, txOpt, txBuilder) - if err != nil { +func (c *GreenfieldClient) constructTxWithGasInfo(msgs []sdk.Msg, txOpt *types.TxOption, txConfig client.TxConfig, txBuilder client.TxBuilder) error { + // construct a tx with txOpt excluding GasLimit and + if err := c.constructTx(msgs, txOpt, txBuilder); err != nil { return err } - if txOpt != nil && txOpt.GasLimit != 0 { - txBuilder.SetGasLimit(txOpt.GasLimit) - return nil - } - txBytes, err := txConfig.TxEncoder()(txBuilder.GetTx()) if err != nil { return err @@ -204,7 +193,26 @@ func (c *GreenfieldClient) constructTxWithGasLimit(msgs []sdk.Msg, txOpt *types. if err != nil { return err } - txBuilder.SetGasLimit(uint64(GasMultiplier * float64(simulateRes.GasInfo.GetGasUsed()))) + + gasLimit := simulateRes.GasInfo.GetGasUsed() + if txOpt != nil && txOpt.GasLimit != 0 { + gasLimit = txOpt.GasLimit + } + gasPrice, err := sdk.ParseCoinsNormalized(simulateRes.GasInfo.GetMinGasPrices()) + if err != nil { + return err + } + if gasPrice.IsZero() { + return types.SimulatedGasPriceError + } + feeAmount := sdk.NewCoins(sdk.NewInt64Coin(types.Denom, + sdk.NewInt(int64(gasLimit)).Mul(gasPrice[0].Amount).Int64()), + ) + if txOpt != nil && !txOpt.FeeAmount.IsZero() { + feeAmount = txOpt.FeeAmount + } + txBuilder.SetGasLimit(gasLimit) + txBuilder.SetFeeAmount(feeAmount) return nil } diff --git a/sdk/client/tx_test.go b/sdk/client/tx_test.go index 23fdf0907..5cbe9adf7 100644 --- a/sdk/client/tx_test.go +++ b/sdk/client/tx_test.go @@ -43,11 +43,10 @@ func TestSendTokenWithTxOptionSucceed(t *testing.T) { assert.NoError(t, err) mode := tx.BroadcastMode_BROADCAST_MODE_BLOCK txOpt := &types.TxOption{ - Mode: &mode, - GasLimit: 123456, - Memo: "test", - FeeAmount: sdk.NewCoins(sdk.NewInt64Coin(test.TEST_TOKEN_NAME, 1)), - FeePayer: payerAddr, + Mode: &mode, + GasLimit: 123456, + Memo: "test", + FeePayer: payerAddr, } response, err := gnfdCli.BroadcastTx([]sdk.Msg{transfer}, txOpt) assert.NoError(t, err) diff --git a/sdk/types/errors.go b/sdk/types/errors.go index 11cf1822e..e50a14fd5 100644 --- a/sdk/types/errors.go +++ b/sdk/types/errors.go @@ -7,4 +7,5 @@ import ( var ( KeyManagerNotInitError = errors.New("Key manager is not initialized yet ") ChainIdNotSetError = errors.New("ChainID is not set yet ") + SimulatedGasPriceError = errors.New("Simulated gas price is 0 ") ) diff --git a/sdk/types/type.go b/sdk/types/type.go index e0b977722..38f216f38 100644 --- a/sdk/types/type.go +++ b/sdk/types/type.go @@ -5,6 +5,8 @@ import ( "github.com/cosmos/cosmos-sdk/types/tx" ) +const Denom = "BNB" + type TxOption struct { Mode *tx.BroadcastMode GasLimit uint64