Skip to content
Draft
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: 3 additions & 1 deletion internal/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ func (p *Pipeline) Execute(ctx context.Context) error {
return err
}

simulatedGas := estimatedGas.Amount / int64(p.cfg.Transactions)

// Extract the addresses
addresses := make([]crypto.Address, 0, len(accounts[1:]))
for _, account := range accounts[1:] {
Expand Down Expand Up @@ -131,7 +133,7 @@ func (p *Pipeline) Execute(ctx context.Context) error {
runKeys,
runAccounts,
p.cfg.Transactions,
maxGas,
simulatedGas,
gasPrice,
p.cfg.ChainID,
p.cli.EstimateGas,
Expand Down
35 changes: 27 additions & 8 deletions internal/runtime/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,29 @@ const gasBuffer = 10_000 // 10k gas
// msgFn defines the transaction message constructor
type msgFn func(creator std.Account, index int) std.Msg

// cappedSimulationGas returns the gas value to use for simulation fees.
// It caps the gas to what the account can actually afford, so the
// simulate endpoint's balance check doesn't reject the transaction.
func cappedSimulationGas(account std.Account, maxGas int64, gasPrice std.GasPrice) int64 {
balance := account.GetCoins().AmountOf(common.Denomination)
// maxAffordableGas = balance * gasPrice.Gas / gasPrice.Price.Amount
affordableGas := balance * gasPrice.Gas / gasPrice.Price.Amount

if affordableGas < maxGas {
return affordableGas
}

return maxGas
}

// constructTransactions constructs and signs the transactions
// using the passed in message generator and signer
func constructTransactions(
ctx context.Context,
keys []crypto.PrivKey,
accounts []std.Account,
transactions uint64,
maxGas int64,
simulatedGas int64,
gasPrice std.GasPrice,
chainID string,
getMsg msgFn,
Expand All @@ -40,10 +55,11 @@ func constructTransactions(

fmt.Printf("\n⏳ Estimating Gas ⏳\n")

// Estimate the fee for the transaction batch
// passing in the maximum block gas, this is just a simulation
// Estimate the fee for the transaction batch using the
// simulated gas from the initial estimation,
// so sub-accounts can afford the simulation fee
txFee := common.CalculateFeeInRatio(
maxGas,
simulatedGas,
gasPrice,
)

Expand Down Expand Up @@ -85,7 +101,7 @@ func constructTransactions(
return nil, fmt.Errorf("unable to sign transaction, %w", err)
}

fmt.Printf("\nEstimated Gas for 1 run tx: %d \n", tx.Fee.GasWanted)
fmt.Printf("\nEstimated gas (with buffer) for 1 run tx: %d \n", tx.Fee.GasWanted)
fmt.Printf("\n🔨 Constructing Transactions 🔨\n\n")

bar := progressbar.Default(int64(transactions), "constructing txs")
Expand Down Expand Up @@ -146,10 +162,13 @@ func calculateRuntimeCosts(
) (std.Coin, error) {
fmt.Printf("\n⏳ Estimating Gas ⏳\n")

// Cap the simulation gas to what the account can afford,
// so the simulate endpoint's balance check doesn't reject the tx
simGas := cappedSimulationGas(account, maxBlockMaxGas, gasPrice)

// Estimate the fee for the transaction batch
// passing in the maximum block gas, this is just a simulation
txFee := common.CalculateFeeInRatio(
maxBlockMaxGas,
simGas,
gasPrice,
)

Expand All @@ -170,7 +189,7 @@ func calculateRuntimeCosts(

return std.Coin{
Denom: common.Denomination,
Amount: int64(transactions) * estimatedGas,
Amount: int64(transactions) * (estimatedGas + gasBuffer),
}, nil
}

Expand Down
4 changes: 2 additions & 2 deletions internal/runtime/package_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (c *packageDeployment) ConstructTransactions(
keys []crypto.PrivKey,
accounts []std.Account,
transactions uint64,
maxGas int64,
simulatedGas int64,
gasPrice std.GasPrice,
chainID string,
estimateFn EstimateGasFn,
Expand All @@ -65,7 +65,7 @@ func (c *packageDeployment) ConstructTransactions(
keys,
accounts,
transactions,
maxGas,
simulatedGas,
gasPrice,
chainID,
c.getMsgFn,
Expand Down
8 changes: 4 additions & 4 deletions internal/runtime/realm_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ func (r *realmCall) Initialize(

tx := &std.Tx{
Msgs: []std.Msg{msg},
// passing in the maximum block gas, this is just a simulation
Fee: common.CalculateFeeInRatio(currentMaxGas, gasPrice),
// Cap the simulation gas to what the account can afford
Fee: common.CalculateFeeInRatio(cappedSimulationGas(account, currentMaxGas, gasPrice), gasPrice),
}

err := signFn(tx)
Expand Down Expand Up @@ -113,7 +113,7 @@ func (r *realmCall) ConstructTransactions(
keys []crypto.PrivKey,
accounts []std.Account,
transactions uint64,
maxGas int64,
simulatedGas int64,
gasPrice std.GasPrice,
chainID string,
estimateFn EstimateGasFn,
Expand All @@ -123,7 +123,7 @@ func (r *realmCall) ConstructTransactions(
keys,
accounts,
transactions,
maxGas,
simulatedGas,
gasPrice,
chainID,
r.getMsgFn,
Expand Down
4 changes: 2 additions & 2 deletions internal/runtime/realm_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (c *realmDeployment) ConstructTransactions(
keys []crypto.PrivKey,
accounts []std.Account,
transactions uint64,
maxGas int64,
simulatedGas int64,
gasPrice std.GasPrice,
chainID string,
estimateFn EstimateGasFn,
Expand All @@ -94,7 +94,7 @@ func (c *realmDeployment) ConstructTransactions(
keys,
accounts,
transactions,
maxGas,
simulatedGas,
gasPrice,
chainID,
c.getMsgFn,
Expand Down
2 changes: 1 addition & 1 deletion internal/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type Runtime interface {
keys []crypto.PrivKey,
accounts []std.Account,
transactions uint64,
maxGas int64,
simulatedGas int64,
gasPrice std.GasPrice,
chainID string,
estimateFn EstimateGasFn,
Expand Down
Loading