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 / caver-js_bytes32.js
Created October 1, 2019 02:17
Using bytes32 with caver-js
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)
/*
We will use the following contract to demonstrate the use of bytes32:
pragma solidity 0.4.24;
@w3kim
w3kim / greeter.js
Created October 7, 2019 09:23
Klaytn: Check the Deployment Example
// The following example is a modified version of
// the one introduced on https://docs.klaytn.com/getting-started/quick-start/check-the-deployment
const Caver = require('caver-js');
const caver = new Caver('https://api.baobab.klaytn.net:8651'); // using Baobab testnet
const contractAddress = '0x799599e8170f210af2c85187bacd070751cb114a' // pre-deployed contract
caver.klay.getCode(contractAddress).then(console.log);
package com.klaytn.example.caver;
import com.klaytn.caver.Caver;
import com.klaytn.caver.crpyto.KlayCredentials;
import com.klaytn.caver.fee.FeePayerManager;
import com.klaytn.caver.methods.response.KlayTransactionReceipt;
import com.klaytn.caver.tx.gas.DefaultGasProvider;
import com.klaytn.caver.tx.manager.PollingTransactionReceiptProcessor;
import com.klaytn.caver.tx.model.ValueTransferTransaction;
@w3kim
w3kim / blockfilter.js
Created October 18, 2019 01:03
Filtering transactions that are included in a block
const Caver = require('caver-js');
const caver = new Caver('https://api.baobab.klaytn.net:8651/');
// getBlock's second parameter is boolean; if true, getBlock returns a block with transaction objects.
caver.klay.getBlock(9660831, true).then((resp) => {
const { transactions } = resp;
const filtered = transactions.filter((e) => { return e['to'] === 'address_you_are_looking_for' });
console.log(filtered);
});
@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 / 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 / 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() {
@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)
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');
@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);