I understand with caver it’s possible to use fee delegation (FD) while building a raw transaction. But for my workflow I’m looking to use the fee delegation directly from the contract method.
Unfortunately, for now FD type transactions cannot be used directly in the current contract.
If you want to use FD, you need to create a FEE_DELEGATED_SMART_CONTRACT_EXECUTION type transaction and send it to the network.
Please refer to the example code below. In the code below. When creating a transaction, change the value set in data field to match the function of the contract you want to call.
const caver = new Caver('https://api.baobab.klaytn.net:8651/')
const sender = caver.klay.accounts.wallet.add('0x{private key}')
const feePayer = caver.klay.accounts.wallet.add('0x{private key}')
const contactAddress = '0x{contract address}'
const fdTransaction = {
type: 'FEE_DELEGATED_SMART_CONTRACT_EXECUTION',
from: sender.address,
to: contactAddress,
data: caver.klay.abi.encodeFunctionCall(
{
constant: false,
inputs: [{ name: 'account', type: 'address' }, { name: 'amount', type: 'uint256' }],
name: 'mint',
outputs: [{ name: '', type: 'bool' }],
payable: false,
stateMutability: 'nonpayable',
type: 'function',
},
['0x7fbe0f15b48b45475fef618e8f82d61a3f04807d', 10]
),
gas: 100000,
}
// Sign transaction as a sender.
const signed = await caver.klay.accounts.signTransaction(fdTransaction)
console.log(signed)
// Sign transaction as a fee payer with RLP-encoded string that includes signature of sender(signed.rawTransaction)
const feePayerSigned = await caver.klay.accounts.feePayerSignTransaction(signed.rawTransaction, feePayer.address)
console.log(feePayerSigned)
const receipt = await caver.klay.sendSignedTransaction(feePayerSigned)
console.log(receipt)