Skip to content

Commit

Permalink
Add min gas limit and its cost to estimated tx receipt (#1122)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbidenaio authored Mar 31, 2023
1 parent f671237 commit 0f6e117
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
33 changes: 23 additions & 10 deletions api/contract_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/idena-network/idena-go/deferredtx"
"github.com/idena-network/idena-go/subscriptions"
"github.com/idena-network/idena-go/vm"
"github.com/idena-network/idena-go/vm/costs"
"github.com/idena-network/idena-go/vm/env"
"github.com/idena-network/idena-go/vm/helpers"
models "github.com/idena-network/idena-wasm-binding/lib/protobuf"
Expand Down Expand Up @@ -174,16 +175,18 @@ func (d DynamicArgs) ToSlice() ([][]byte, error) {
}

type TxReceipt struct {
Contract common.Address `json:"contract"`
Method string `json:"method"`
Success bool `json:"success"`
GasUsed uint64 `json:"gasUsed"`
TxHash *common.Hash `json:"txHash"`
Error string `json:"error"`
GasCost decimal.Decimal `json:"gasCost"`
TxFee decimal.Decimal `json:"txFee"`
ActionResult *ActionResult `json:"actionResult"`
Events []Event `json:"events"`
Contract common.Address `json:"contract"`
Method string `json:"method"`
Success bool `json:"success"`
GasUsed uint64 `json:"gasUsed"`
TxHash *common.Hash `json:"txHash"`
Error string `json:"error"`
GasCost decimal.Decimal `json:"gasCost"`
TxFee decimal.Decimal `json:"txFee"`
ActionResult *ActionResult `json:"actionResult"`
Events []Event `json:"events"`
MinGasLimit *uint64 `json:"minGasLimit,omitempty"`
MinGasLimitCost *decimal.Decimal `json:"minGasLimitCost,omitempty"`
}

type ActionResult struct {
Expand Down Expand Up @@ -424,6 +427,16 @@ func convertEstimatedReceipt(tx *types.Transaction, receipt *types.TxReceipt, fe
if !tx.Signed() {
res.TxHash = nil
}
var remainingWasmGas uint64
if res.ActionResult != nil {
for _, actionResult := range res.ActionResult.SubActionResults {
remainingWasmGas += actionResult.RemainingGas
}
}
minGasLimit := res.GasUsed + costs.WasmGasToGas(remainingWasmGas)
minGasLimitCost := blockchain.ConvertToFloat(blockchain.GetGasCost(feePerGas, minGasLimit))
res.MinGasLimit = &minGasLimit
res.MinGasLimitCost = &minGasLimitCost
return res
}

Expand Down
5 changes: 4 additions & 1 deletion blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1608,7 +1608,10 @@ func (chain *Blockchain) getTxFee(feePerGas *big.Int, tx *types.Transaction) *bi
}

func (chain *Blockchain) GetGasCost(appState *appstate.AppState, gasUsed uint64) *big.Int {
feePerGas := appState.State.FeePerGas()
return GetGasCost(appState.State.FeePerGas(), gasUsed)
}

func GetGasCost(feePerGas *big.Int, gasUsed uint64) *big.Int {
if common.ZeroOrNil(feePerGas) {
return common.Big0
}
Expand Down

0 comments on commit 0f6e117

Please sign in to comment.