It turns out there is an API that we can query to estimate the transaction fee.
Query Transfer Fee
First a transfer in rinkeby so that we can double check how this API works:
curl -X POST -H 'Content-type: application/json' -d '{
"jsonrpc":"2.0",
"id":1, "method": "get_tx_fee",
"params": ["Transfer", "0x80a52B7F26426d2b16578FC5f376c349F54772A7", "STORJ"]
}' https://rinkeby-api.zksync.io/jsrpc | jq
{
"jsonrpc": "2.0",
"result": {
"feeType": "Transfer",
"gasTxAmount": "2334",
"gasPriceWei": "1000000000",
"gasFee": "1133892",
"zkpFee": "587642",
"totalFee": "1721000"
},
"id": 1
}
Looks like totalFee
/ 8 decimals. I have paid 0.01718 which is close enough. The STORJ fee will differ depending on the current STORJ price.
Now lets try the same on mainnet:
curl -X POST -H 'Content-type: application/json' -d '{
"jsonrpc":"2.0",
"id":1, "method": "get_tx_fee",
"params": ["Transfer", "0x80a52B7F26426d2b16578FC5f376c349F54772A7", "STORJ"]
}' https://api.zksync.io/jsrpc | jq
{
"jsonrpc": "2.0",
"error": {
"code": 105,
"message": "Chosen token is not suitable for paying fees."
},
"id": 1
}
Ok that means I have to raise a question to the team and ask if this is intensional or if the zkSync team can just enable it. For the moment we can’t use this directy.
Let’s try the same with ETH then. This should allow us to get close enough with our calculation:
curl -X POST -H 'Content-type: application/json' -d '{
"jsonrpc":"2.0",
"id":1, "method": "get_tx_fee",
"params": ["Transfer", "0x80a52B7F26426d2b16578FC5f376c349F54772A7", "STORJ"]
}' https://a^C.zksync.io/jsrpc | jq
root@datengrab:~# curl -X POST -H 'Content-type: application/json' -d '{
"jsonrpc":"2.0",
"id":1, "method": "get_tx_fee",
"params": ["Transfer", "0x80a52B7F26426d2b16578FC5f376c349F54772A7", "ETH"]
}' https://rinkeby-api.zksync.io/jsrpc | jq
{
"jsonrpc": "2.0",
"result": {
"feeType": "Transfer",
"gasTxAmount": "2334",
"gasPriceWei": "1000000000",
"gasFee": "3034200000000",
"zkpFee": "1572479852163",
"totalFee": "4600000000000"
},
"id": 1
}
curl -X POST -H 'Content-type: application/json' -d '{
"jsonrpc":"2.0",
"id":1, "method": "get_tx_fee",
"params": ["Transfer", "0x80a52B7F26426d2b16578FC5f376c349F54772A7", "ETH"]
}' https://api.zksync.io/jsrpc | jq
{
"jsonrpc": "2.0",
"result": {
"feeType": "TransferToNew",
"gasTxAmount": "6862",
"gasPriceWei": "38960000000",
"gasFee": "347546576000000",
"zkpFee": "4717439556487",
"totalFee": "352000000000000"
},
"id": 1
}
Transfer Fee
Factor Testnet to Mainnet fee: 76.5
Testnet Fee: 0.01718 STORJ
Estimated Mainnet Fee: 1.31427 STORJ
Query Withdraw Fee
This also allowes us to caculate the fee for a withdraw in mainnet:
curl -X POST -H 'Content-type: application/json' \
-d '{
"jsonrpc":"2.0",
"id":1, "method": "get_tx_fee",
"params": ["Withdraw", "0x80a52B7F26426d2b16578FC5f376c349F54772A7", "STORJ"]
}' \
https://rinkeby-api.zksync.io/jsrpc | jq
{
"jsonrpc": "2.0",
"result": {
"feeType": "Withdraw",
"gasTxAmount": "96991",
"gasPriceWei": "1000000000",
"gasFee": "47119641",
"zkpFee": "1762924",
"totalFee": "48800000"
},
"id": 1
}
curl -X POST -H 'Content-type: application/json' -d '{
"jsonrpc":"2.0",
"id":1, "method": "get_tx_fee",
"params": ["Withdraw", "0x80a52B7F26426d2b16578FC5f376c349F54772A7", "ETH"]
}' https://rinkeby-api.zksync.io/jsrpc | jq
{
"jsonrpc": "2.0",
"result": {
"feeType": "Withdraw",
"gasTxAmount": "96991",
"gasPriceWei": "1000000000",
"gasFee": "126088300000000",
"zkpFee": "4717439556487",
"totalFee": "130800000000000"
},
"id": 1
}
curl -X POST -H 'Content-type: application/json' -d '{
"jsonrpc":"2.0",
"id":1, "method": "get_tx_fee",
"params": ["Withdraw", "0x80a52B7F26426d2b16578FC5f376c349F54772A7", "ETH"]
}' https://api.zksync.io/jsrpc | jq
{
"jsonrpc": "2.0",
"result": {
"feeType": "Withdraw",
"gasTxAmount": "96991",
"gasPriceWei": "36000000000",
"gasFee": "4539178800000000",
"zkpFee": "4717439556487",
"totalFee": "4540000000000000"
},
"id": 1
}
Withdraw Fee
Factor Testnet to Mainnet fee: 34.7
Testnet Fee: 0.488 STORJ
Estimated Mainnet Fee: 16.9336 STORJ