Last active
October 31, 2019 03:50
-
-
Save w3kim/b35cd6d30a1c3fdd40c41122dbdb73e4 to your computer and use it in GitHub Desktop.
Smart Contract Execution using Fee Delegation (sender has no KLAY)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Caver = require('caver-js'); | |
const caver = new Caver('https://api.baobab.klaytn.net:8651/'); | |
caver.klay.accounts.wallet.create(1); | |
const sender = caver.klay.accounts.wallet[0]; | |
// if there is only one key bound to an address where last160bits(keccak(key)) === address, then the key can function as payer key | |
const payer = caver.klay.accounts.wallet.add('0x42605fa83957d67ca8e5c843820c72037ee5dbc6b0b2d14f9d4bd5de41b446de'); | |
// an arbitrary contract is used | |
async function run() { | |
caver.klay.getAccount(sender.address).then(console.log); // should print `null` | |
// make sure `data` starts with 0x | |
const { rawTransaction: senderRawTransaction } = await caver.klay.accounts.signTransaction({ | |
type: 'FEE_DELEGATED_SMART_CONTRACT_EXECUTION', | |
from: sender.address, | |
data: "some_data_represented_in_hex", | |
gas: '3000000', | |
value: 0, | |
}, sender.privateKey); | |
// signed raw transaction | |
console.log("Raw TX:\n", senderRawTransaction); | |
// send fee delegated transaction with fee payer information | |
caver.klay.sendTransaction({ | |
senderRawTransaction: senderRawTransaction, | |
feePayer: payer.address | |
}) | |
.on('transactionHash', function (hash) { | |
console.log(">>> tx_hash for deploy =", hash); | |
}) | |
.on('receipt', function (receipt) { | |
console.log(">>> receipt arrived: ", receipt); | |
caver.klay.getAccount(sender.address).then(console.log); // should NOT print `null` | |
}) | |
.on('error', function (err) { | |
console.error(">>> error: ", err); | |
}); | |
} | |
run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment