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

feat: sdk gas price simulation #64

Merged
merged 9 commits into from
Feb 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions sdk/client/test/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package test

const (
TEST_PRIVATE_KEY = "3018f5ceec22c2a1389fe3a0933b52f3fc31450c5322002dd76477964797fb13"
TEST_PRIVATE_KEY = "ff0911d998e6f12cf4b0a88d90cc411aa3be06c9dc5a2b35834f0e25a44e2275"
TEST_ADDR = "0x76d244CE05c3De4BbC6fDd7F56379B145709ade9"
TEST_ADDR2 = "0x5dEfC28ce1Ed331D56aB6F607f78708880e11Bea"
TEST_ADDR3 = "0x593107F1D5D10A68D3C3722C35ADa2eb779D44A4"
Expand All @@ -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"
)
42 changes: 25 additions & 17 deletions sdk/client/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
Expand All @@ -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
}

Expand Down
9 changes: 4 additions & 5 deletions sdk/client/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions sdk/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ")
)
2 changes: 2 additions & 0 deletions sdk/types/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"github.com/cosmos/cosmos-sdk/types/tx"
)

const Denom = "BNB"

type TxOption struct {
Mode *tx.BroadcastMode
GasLimit uint64
Expand Down