Calculating transaction fee

Who is this tutorial for:

  • API users that want to calculate the transaction fee of a transaction before sending

Brief Outline

  • sendDeviceForSigning: boolean is used to indicate if the transaction request should be saved in TrustVault platform so that the transaction request can be fetched by the iOS app or SDK for signing
  • If sendDeviceForSigning: false the transaction values (including the transaction fee) will be calculated but will NOT be saved in TrustVault platform (you can think of sendDeviceForSigning: false as a dry run flag). This gives us the chance to see what the transaction values would look like before the transaction request it actually created

Steps

  1. Send a transaction with sendDeviceForSigning: false to allow TrustVault to calculate the fees for you
  2. Send the transaction using the values from 1 using either the graphQL query with sendDeviceForSigning: true or the SDK

Calculating Ethereum Transaction Fee

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
mutation ethereumTransactionFee(
$from: String!
$to: String!
$value: String!
$assetSymbol: String!
$speed: TransactionSpeed
$currency: String
$sendToDevicesForSigning: Boolean
) {
createEthereumTransaction(
createTransactionInput: {
ethereumTransaction: {
to: $to
assetSymbol: $assetSymbol
value: $value
fromAddress: $from
speed: $speed
}
currency: $currency
source: "API"
sendToNetworkWhenSigned: true
sendToDevicesForSigning: $sendToDevicesForSigning
}
) {
assetRate
chainRate
signData {
transaction {
to
fromAddress
value
gasPrice
gasLimit
}
}
}
}

Variables:

1
2
3
4
5
6
7
8
9
{
"from": "<FROM_TRUSTOLOGY_ADDRESS>",
"to":"0xaa8f71f62c93f705d70e47748987548b31c0d67e",
"value": "<TRANSACTION_VALUE_IN_WEI>",
"assetSymbol": "ETH",
"speed": "SLOW" | "MEDIUM" | "FAST",
"currency": "GBP",
"sendToDevicesForSigning": false
}

Response:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"data": {
"createEthereumTransaction": {
"chainRate": "3487.9665",
"assetRate": "3487.9665",
"signData": {
"transaction": {
"to": "0xaa8f71f62c93f705d70e47748987548b31c0d67e",
"fromAddress": "0x2F46147dE40e96Ae55a6cC68560645e69eB703e6",
"value": "1",
"gasPrice": "130000000000",
"gasLimit": "21000",
}
}
}
}
}
  • gasPrice - price per unit of gas in satoshi
  • gasLimit - total unit of gas the transaction can use
  • chainRate - the current rate for 1 ETH of the given currency (this can be used to convert the transaction fee from satoshi to the given currency)
  • assetRate - the current rate for 1 unit of asset to be sent of the given currency (this can be used to convert the asset value to be sent to the given currency)
  • transaction fee calculation - gasPrice x gasLimit (the transaction fee is in satoshi)

Creating Ethereum Transaction

Ensure that the gasPrice/gasLimit result from the graphQL call above is passed when creating a transaction as gasPrice/gasLimit can change

GraphQL query

NOTE:

  • $speed is now replaced by $gasPrice and $gasLimit values
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
mutation createEthereumTransaction (
$from: String!
$to: String!
$value: String!
$assetSymbol: String!
$currency: String
$gasPrice: String
$gasLimit: String
$sendToDevicesForSigning: Boolean
) {
createEthereumTransaction(
createTransactionInput: {
ethereumTransaction: {
to: $to
assetSymbol: $assetSymbol
value: $value
fromAddress: $from
gasPrice: $gasPrice
gasLimit: $gasLimit
}
currency: $currency
source: "API"
sendToNetworkWhenSigned: true
sendToDevicesForSigning: $sendToDevicesForSigning
}
) {
assetRate
chainRate
signData {
transaction {
to
fromAddress
value
gasPrice
gasLimit
nonce
chainId
data
}
}
}
}

Variables:

NOTE:

  • set "sendToDevicesForSigning": true to create the transaction
  • speed is now replaced by gasPrice and gasLimit
1
2
3
4
5
6
7
8
9
10
{
"from": "<FROM_TRUSTOLOGY_ADDRESS>",
"to":"0xaa8f71f62c93f705d70e47748987548b31c0d67e",
"value": "<TRANSACTION_VALUE_IN_WEI>",
"assetSymbol": "ETH",
"gasPrice": "<GAS_PRICE_RESULT_FROM_PREVIOUS_CALL>",
"gasLimit": "<GAS_LIMIT_RESULT_FROM_PREVIOUS_CALL>",
"currency": "GBP",
"sendToDevicesForSigning": true
}

SDK:

Use the gasPrice and gasLimit result from the ethereumTransactionFee graphQL mutation call (see Calculating Ethereum Transaction Fee) when creating an ethereum transaction request as gasPrice/gasLimit can change

1
2
3
4
const gasPrice = result.data?.createEthereumTransaction.signData.transaction.gasPrice;
const gasLimit = result.data?.createEthereumTransaction.signData.transaction.gasLimit;
const speed = undefined; // speed can be skipped if both gasPrice and gasLimit are manually set
const requestId = await trustVault.sendEthereum(fromAddress, toAddress, amount, asset, speed, currency, signCallback, gasPrice, gasLimit)

see more: TrustVault Node.js SDK