Skip to content

Instantly share code, notes, and snippets.

View w3kim's full-sized avatar

Eric Woojung Kim w3kim

  • Waterloo, ON, Canada
View GitHub Profile
@w3kim
w3kim / MyERC20.java
Created March 13, 2020 01:54
MyERC20 Java Class Generated by caver-java
package com.example;
import com.klaytn.caver.Caver;
import com.klaytn.caver.crypto.KlayCredentials;
import com.klaytn.caver.methods.response.KlayLogs;
import com.klaytn.caver.methods.response.KlayTransactionReceipt;
import com.klaytn.caver.tx.SmartContract;
import com.klaytn.caver.tx.manager.TransactionManager;
import java.math.BigInteger;
import java.util.ArrayList;
@w3kim
w3kim / erc20.sol
Created March 13, 2020 01:53
ERC20 from OpenZeppelin (Solidity 0.4.24)
pragma solidity ^0.4.24;
/**
* @dev Interface of the ERC20 standard as defined in the EIP. Does not include
* the optional functions; to access them see `ERC20Detailed`.
*/
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
@w3kim
w3kim / FeeDelegatedValueTransfer.java
Created January 10, 2020 08:23
Demonstrating fee delegated value transfer using caver-java 1.3.0
package com.klaytn.example.caver;
import com.klaytn.caver.Caver;
import com.klaytn.caver.crypto.KlayCredentials;
import com.klaytn.caver.fee.FeePayerManager;
import com.klaytn.caver.methods.response.KlayTransactionReceipt;
import com.klaytn.caver.tx.manager.PollingTransactionReceiptProcessor;
import com.klaytn.caver.tx.type.TxTypeFeeDelegatedValueTransfer;
import com.klaytn.caver.utils.ChainId;
import org.junit.Assert;
private KlayTransactionReceipt.TransactionReceipt transfer(KlayCredentials from, KlayCredentials to, BigInteger value) throws IOException {
log.info("transfer {} KLAY [{} -> {}]", value.toString(), from.getAddress(), to.getAddress());
BigInteger nonce = caver.klay().getTransactionCount(from.getAddress(), BLK_PARAM).send().getValue();
TxTypeValueTransfer tx = TxTypeValueTransfer.createTransaction(
nonce,
GAS_PRICE,
GAS_LIMIT,
to.getAddress(),
value,
@Test
public void VTTest() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException, IOException {
BigInteger privKey = Keys.createEcKeyPair().getPrivateKey();
KlayCredentials credential = KlayCredentials.create(privKey.toString(16)); // newly created credential
KlayTransactionReceipt.TransactionReceipt receipt;
receipt = transfer(senderCredential, credential, Convert.toPeb("0.5", Convert.Unit.KLAY).toBigInteger());
Assert.assertNotNull(receipt);
const Caver = require('caver-js')
const caver = new Caver('https://api.baobab.klaytn.net:8651')
const originalKey = 'original_key';
const originalAccount = caver.klay.accounts.wallet.add(originalKey);
const address = originalAccount.address;
const acct1 = caver.klay.accounts.privateKeyToAccount('key1');
const acct2 = caver.klay.accounts.privateKeyToAccount('key2');
const acct3 = caver.klay.accounts.privateKeyToAccount('key3');
@w3kim
w3kim / createAccoutForUpdate.js
Last active December 9, 2019 02:14
createAccountForUpdate example
const Caver = require('caver-js');
const caver = new Caver('https://api.baobab.klaytn.net:8651');
const address = '0x{address_of_account_you_wish_to_update}';
const oldPrivateKey = '0x{effective_private_key_bound_to_the_address_above}';
const newPrivateKey = caver.klay.accounts.create().privateKey; // new private key to update
// use createAccountForUpdate
const accountForUpdate = caver.klay.accounts.createAccountForUpdate(address, newPrivateKey);
console.log('>>> about to configure ACCOUNT(address=', address, ') with the following key:', newPrivateKey)
@w3kim
w3kim / contract.js
Created October 28, 2019 05:31
Send Multiple Transactions to a Contract with Modified Nonce
const Caver = require('caver-js');
const caver = new Caver('https://api.baobab.klaytn.net:8651/');
const acct = caver.klay.accounts.privateKeyToAccount('your_private_key')
caver.klay.accounts.wallet.add(acct)
// Refer to count.sol for Solidity code
const COUNT_BYTECODE = {
"linkReferences": {},
"object": "60806040526000805534801561001457600080fd5b5060e8806100236000396000f3fe6080604052348015600f57600080fd5b5060043610603c5760003560e01c806306661abd14604157806342cbb15c14605d578063d14e62b8146079575b600080fd5b604760a4565b6040518082815260200191505060405180910390f35b606360aa565b6040518082815260200191505060405180910390f35b60a260048036036020811015608d57600080fd5b810190808035906020019092919050505060b2565b005b60005481565b600043905090565b806000819055505056fea165627a7a7230582087453d981a85f80c5262508e1fe5abe94dc38b1167c49b6e3477b74293e9e7000029",
@w3kim
w3kim / send.js
Created October 28, 2019 05:24
Sending Multiple VT Transactions with Modified Nonce
const Caver = require('caver-js')
const caver = new Caver('https://api.baobab.klaytn.net:8651')
const sender = caver.klay.accounts.wallet.add('your_private_key');
const recipient = 'recipient_address';
const x = async () => {
const account = await caver.klay.getAccount(sender.address);
const startingNonce = account.account.nonce;
console.log(startingNonce);
@w3kim
w3kim / fee_delegated_execution.js
Last active October 31, 2019 03:50
Smart Contract Execution using Fee Delegation (sender has no KLAY)
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() {