Fee Delegation Example

Table of Contents

1. Introduction

This tutorial helps you to write a simple server-client example using caver-js SDK to illustrate how fee delegated value transfer transaction works in Klaytn. This tutorial and the example code is using the Baobab testnet.

2. How fee delegation works

Let's skim through how fee delegation works.

2.1 Transaction signing by the sender

Sender always should sign the transaction before sending a transaction.

To sign a transaction, use signTransaction which signs a transaction with given private key.

If there are no errors, then senderRawTransaction will have a signed transaction which is signed by the senderPrivateKey.

Now, you need to send the senderRawTransaction to the fee payer. There will be various ways to implement this. In this tutorial, we will provide you a simple server-client code as an example of sending a senderRawTransaction to the fee payer.

2.2 Transaction signing by the fee payer

When fee payer receives the senderRawTransaction, fee payer signs the senderRawTransaction again with their private key and sends the transaction to Klaytn. The below code snippet illustrates the process. klay.sendTransaction method signs the transaction with the given account's private key before sending the transaction. Before running the code, please replace "FEEPAYER_ADDRESS" and "PRIVATE_KEY" with the actual values.

Note that when the fee payer submits the transaction to Klaytn on behalf of the sender, the senderRawTransaction type must be a FEE_DELEATED type of transaction. In the below example, sendTransaction(FEE_DELEGATED_VALUE_TRANSFER) method is invoked, because the original senderRawTransaction generated by the sender was TxTypeFeeDelegatedValueTransfer.

3. Simple server and client for fee delegation

Let's write a simple server and client using above fee delegation code.

3.1 Environment setup

We will use npm and caver-js to setup environment for this example as below.

3.1 Sender's client

First, we are going to write a sender_client.js as below.

In the example, please replace "SENDER_ADDRESS", "SENDER_PRIVATEKEY" and "TO_ADDRESS" with the actual values.

The above code signs a fee delegated value transfer transaction with senderPrivateKey and sends the signed senderRawTransaction to the fee payer's server which is running on port 1337 on 127.0.0.1, i.e. localhost.

3.2 Fee payer's server

Now let's write the fee payer's server, feepayer_server.js, which signs received senderRawTransaction with feePayerPrivateKey and sends it to Baobab testnet.

In the below example, please replace "FEEPAYER_ADDRESS" and "FEEPAYER_PRIVATEKEY" with actual values.

The server listens on port 1337.

When there is incoming data, it signs the data with feePayerPrivateKey and sends it to the Klaytn blockchain. It assumes that the data is senderRawTransaction from the sender_client.js.

4. Run example

Prepare two terminals, one for sender_client.js and another for feepayer_server.js.

4.1 Run feepayer_server.js

Below the command will start the fee payer's server.

The server starts and is now listening on port 1337.

4.2 Run sender_client.js

Let's run sender_client.js to send a fee delegated transaction.

It will sign a transaction with the sender private key and send the signed transaction to the fee delegated service (i.e., fee payer's server). Then it will receive the response from the fee delegate service including the Fee payer address, Tx hash, and Sender Tx hash. Tx hash is hash of a transaction submitted to the Klaytn network, while Sender Tx hash is hash of a transaction without the fee payer's address and signature. For more details, please take a look at SenderTxHash.

4.3 Check feepayer_server.js

On the server's console, you will see below outputs. It prints the transaction receipt from the Klaytn.

4.4 Klaytn scope

You can also find the above transaction on the Klaytn scope.

It shows that the transaction is TxTypeFeeDelegatedValueTransfer and Fee payer is 0x2645ba5be42ffee907ca8e9d88f6ee6dad8c1410 or feepayerAddress that you entered, while From is a different address which should be the senderAddress in above example.

Fee delegated Tx

Last updated