icon-sdk-js icon indicating copy to clipboard operation
icon-sdk-js copied to clipboard

Fee handling question

Open roccomuso opened this issue 5 years ago • 16 comments

Looking at the code I didn't get how Fees are handled and computed in ICON when sending a tx. Can someone explain?

roccomuso avatar Feb 19 '20 02:02 roccomuso

I could be wrong but I think it is calculated using SCORE. The developer portal breaks it down based on the number of steps * the going rate of the coin.

Dev Portal: https://www.icondev.io/docs/step-estimation

trevatk avatar Feb 19 '20 02:02 trevatk

So for a simple transaction between 2 EOA, you just provide a stepLimit and the fee is deducted from your address balance?

What's the easiest way to know the conversion rate stepLimit to ICX?

roccomuso avatar Feb 19 '20 02:02 roccomuso

For a simple transaction, you wouldn't need to calculating the step limit. You could just an SDK that handles all of that for you.

The step limit is the number of steps on T-Bears to complete the transaction. The step limit is the conversion is referenced in a video on the docs page, think it was 1:1000 or something close to that.

trevatk avatar Feb 19 '20 02:02 trevatk

  • I don't wanna rely on the SDK, I need to understand if the stepLimit for normal transactions can be like a constant, without any server API call.
  • So the conversion is fixed. (Initial setup: 1 ICX = 100,000,000 Step)

roccomuso avatar Feb 19 '20 02:02 roccomuso

It looks like transactions are only completed once sent to the T-Bears network. Which is where the transaction fee comes from (activity on T-Bear). Requiring the use of an SDK to interact with the network.

trevatk avatar Feb 19 '20 02:02 trevatk

What do you mean with "completed"? validated? added to the blockchain? The T-Bears network is the Icon mainnet? The question is, can I use a fixed stepLimit for normal txs between EOA? Or do I need to get limit (or estimation) from the network?

roccomuso avatar Feb 19 '20 03:02 roccomuso

simple answer, no you can not use a fixed steplimit for a transaction. You need an estimate from the network. At least from my understanding of reading the docs.

trevatk avatar Feb 19 '20 03:02 trevatk

So, If I put a stepLimit, the tx fee is gonna be <= to that limit? but this could leave dust in the wallet

roccomuso avatar Feb 19 '20 18:02 roccomuso

That's how it looks, you can even use the API to return the transaction fee for the specified address.

trevatk avatar Feb 19 '20 19:02 trevatk

what do you mean? which API?

roccomuso avatar Feb 19 '20 22:02 roccomuso

the javascript API includes a function call under the

const IconService = require('icon-sdk-js');

/*  HttpProvider is used to communicate with http.
    In this example, we use Yeouido node as provider. */
const provider = new IconService.HttpProvider('https://bicon.net.solidwallet.io/api/v3');

/* Create IconService instance */
const iconService = new IconService(provider);

const apiList = iconService.getScoreApi('cx0000000000000000000000000000000000000001').execute().then(apiList => {
    console.log(apiList.getMethod('getStepCosts'))
});

at this point, can I ask what the project is?

trevatk avatar Feb 19 '20 22:02 trevatk

I don't wanna rely on the SDK, I need to understand if the stepLimit for normal transactions can be like a constant, without any server API call.

The "stepPrice" variable is a governance variable, that may be changed in the future. However, it hasn't changed since the launch of mainnet, and it isn't planned soon to change it.

So, the simple answer is you can assume the stepLimit is a constant. However, the complicated answer is you need to query the governance SCORE cx0000000000000000000000000000000000000001 to retrieve the step cost for an ICX transaction.

For a transaction with no data : stepCost = defaultCost * StepPrice = 100000 * 10000000000 = 1000000000000000 loops = 0.001 ICX

You can use the code mentionned by @BadAppsDevelopment above for retrieving this value using the JS SDK.

You can review all these governance variables here : https://tracker.icon.foundation/contract/cx0000000000000000000000000000000000000001#readcontract

Spl3en avatar Feb 19 '20 23:02 Spl3en

Ok thanks for the explanation. So right now the fee is 0.001 ICX but might change in the future.

roccomuso avatar Feb 19 '20 23:02 roccomuso

I recently wrote the JS function for calling the debug_estimateStep method using ICONex Connect, as it isn't supported by the current version of the ICON SDK JS. Here it is :


__estimateCallStep(from, to, method, value, params = {}) {
  const transaction = {
    "jsonrpc": "2.0",
    "method": "debug_estimateStep",
    "id": 1,
    "params": {
      "version": "0x3",
      "from": from,
      "to": to,
      "value": IconService.IconConverter.toHex(
        IconService.IconConverter.toBigNumber(value)),
      "timestamp": IconService.IconConverter.toHex(
        (new Date()).getTime() * 1000),
      "nid": IconService.IconConverter.toHex(
        IconService.IconConverter.toBigNumber(this._nid)),
      "nonce": "0x1",
      "dataType": "call",
      "data": {
        "method": method,
        "params": params
      }
    }
  }

  return new Promise((resolve, reject) => {
    try {
      const result = this._iconDebugService.provider.request(transaction).execute()
      resolve(result)
    } catch (err) {
      reject(err)
    }
  })
}

this._nid is the Network ID (see https://www.icondev.io/v0.8/docs/icon-networks) this._iconDebugService is an instance of the iconService pointing to the debug endpoint :

const httpDebugProvider = new IconService.HttpProvider(url + '/api/debug/v3')
this._iconDebugService = new IconService(httpDebugProvider)

Hope that helps anyone else looking for estimate steps with JS.

Spl3en avatar Feb 22 '20 22:02 Spl3en

Is this debug method reliable?

I tried to call it trying to put as amount 10 ICX and it returned 100000 as stepLimit. But networks fail to accept txs with such low fee.

roccomuso avatar Jul 27 '20 10:07 roccomuso

I have a look at how Hana wallet calculated Fee estimation, and here is what I figured out:

First, they get stepPrice by making a request to https://berlin.net.solidwallet.io/api/v3

{
  "jsonrpc": "2.0",
  "id": 1652537128704,
  "method": "icx_call",
  "params": {
    "to": "cx0000000000000000000000000000000000000001",
    "dataType": "call",
    "data": {
      "method": "getStepPrice"
    }
  }
}

Then, they get estimateStep by making a request to https://berlin.net.solidwallet.io/api/v3d, the difference is that they use the v3d version compared with the first request.

{
  "jsonrpc": "2.0",
  "method": "debug_estimateStep",
  "params": {
   ...
  },
  "id": 1652537128399
}

The fee estimation = stepPrice * estimateStep


About how to turn on Hana wallet debug page:

  • Go to extension page and click on Background page
image
  • Observe on Network tab
image

Hope this help!

duyphanz avatar May 14 '22 14:05 duyphanz