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

Return hbar fee / gas used on failed smart contract execution #1612

Closed
iron4548 opened this issue May 24, 2023 · 5 comments · Fixed by #1643
Closed

Return hbar fee / gas used on failed smart contract execution #1612

iron4548 opened this issue May 24, 2023 · 5 comments · Fixed by #1643
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@iron4548
Copy link

iron4548 commented May 24, 2023

Description

I use ContractExecuteTransaction and on a successful call, I can get the hbar fee / gas used value via contractExecuteRec.transactionFee. However, if the smart contract call failed due to a revert or due to a require, it will throw an error (unsure if this is by design or not). Within the error there are a bunch of records but none of them contains the hbar fee / gas used value. The failed transaction can be queried via mirror node to get the fee. The question is - is it possible to get the hbar fee / gas used value on a failed/reverted smart contract call so that I don't need to query the mirror node for it?

const contractExecuteTx = new ContractExecuteTransaction()
.setContractId(contractId)
.setGas(gasLim)
.setTransactionMemo(memo)
.setFunction(functionName, params)
.freezeWith(client);

try {      

    const signedTx = await contractExecuteTx.sign(privateKey);
    const contractExecuteSubmit = await signedTx.execute(client);
    const contractExecuteRec = await contractExecuteSubmit.getRecord(client);

    console.log(`txFee: ${contractExecuteRec.transactionFee}`);

} catch (err:any) {
    console.log(`txFee: ?`);
}

Steps to reproduce

Use @hashgraph/sdk 2.24.2

  1. Create a ContractExecuteTransaction
  2. Call a smart contract and cause a require or a revert to occur
  3. An 'Status error' exception occurs
  4. Now try and figure out how to get the hbar fee / gas used for the failed smart contract call.

Additional context

No response

Hedera network

mainnet

Version

2.24.2

Operating system

None

@iron4548 iron4548 added the bug Something isn't working label May 24, 2023
@iron4548
Copy link
Author

Here's the dump of the error object on smart contract call revert/require trigger

status: Status { _code: 33 },
  transactionId: TransactionId {
    accountId: AccountId {
      shard: [Long],
      realm: [Long],
      num: [Long],
      aliasKey: null,
      evmAddress: null,
      _checksum: null
    },
    validStart: Timestamp { seconds: [Long], nanos: [Long] },
    scheduled: false,
    nonce: null
  },
  transactionReceipt: TransactionReceipt {
    status: Status { _code: 33 },
    accountId: null,
    fileId: null,
    contractId: ContractId {
      shard: [Long],
      realm: [Long],
      num: [Long],
      evmAddress: null,
      _checksum: null
    },
    topicId: null,
    tokenId: null,
    scheduleId: null,
    exchangeRate: ExchangeRate {
      hbars: 30000,
      cents: 150926,
      expirationTime: 2023-05-25T04:00:00.000Z,
      exchangeRateInCents: 5.030866666666666
    },
    topicSequenceNumber: Long { low: 0, high: 0, unsigned: false },
    topicRunningHash: Uint8Array(0) [],
    totalSupply: Long { low: 0, high: 0, unsigned: false },
    scheduledTransactionId: null,
    serials: [],
    duplicates: [],
    children: []
  }
}

@SimiHunjan SimiHunjan modified the milestones: 2.26.0, 0.27.0 May 25, 2023
@ochikov ochikov self-assigned this May 26, 2023
@ochikov ochikov added enhancement New feature or request and removed bug Something isn't working labels May 29, 2023
@ochikov
Copy link
Contributor

ochikov commented May 29, 2023

Hello @iron4548, and thank you for the issue.

  • On the first question about the require: There are three main solidity error functions: assert, require, and revert. Using require unused gas is returned to the caller and the state is reversed to the original state, so it's by design to throw.
  • On the second question about the transactionFee: Currently, there is no way of returning it. You need to get it through the mirror node. I've requested the core team if it's possible to be added and they will consider it, but for now, it can be retrieved by the mirror node.
  • We are detecting the status code and if the status code is not OK (status 22) we are returning the exception error with the transaction id and status code.

@ochikov
Copy link
Contributor

ochikov commented May 30, 2023

@iron4548 I will close the issue?

@iron4548
Copy link
Author

@ochikov yes you may.

"I've requested the core team if it's possible to be added and they will consider it" - can this be tracked elsewhere?

@ochikov
Copy link
Contributor

ochikov commented Jun 7, 2023

Hello @iron4548, I've researched more deeply the issue and there is a way to get it through the SDK because the consensus node is returning that information. We are going to provide such kind of functionality with the new version. I will tag you here when we are ready.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants