Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

TxTypes for web3.js Ext #507

Merged
merged 32 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
460d0ea
Test Basic_08_TxTypeValueTransfer.js
nohkwak Sep 12, 2023
6c08148
Test Basic_10_TxTypeValueTransferMemol.js
nohkwak Sep 12, 2023
1646f60
Test Basic_20_TxTypeAccountUpdate.js
nohkwak Sep 12, 2023
eb945c8
Copy test files from ethers-ext
nohkwak Sep 12, 2023
96b9fda
Test Basic_28_TxTypeSmartContractDeploy.js
nohkwak Sep 13, 2023
319649f
Test Basic_30_TxTypeSmartContractExecution.js
nohkwak Sep 13, 2023
fce1d4c
Test Basic_38_TxTypeCancel.js
nohkwak Sep 13, 2023
5f15d0b
Resolve missing 'humanReadable' field
nohkwak Sep 14, 2023
0e75430
Change ABI
nohkwak Sep 14, 2023
533a23b
Try to import from caver.utils
nohkwak Sep 14, 2023
4b929f1
To be followed another branch
nohkwak Sep 14, 2023
7910886
Fix typo
nohkwak Sep 14, 2023
d321059
Add workaround for TxTypeCancel
nohkwak Sep 14, 2023
43afea2
Update test TxTypeAccountUpdate
nohkwak Sep 14, 2023
91dcaff
Debug nonce data type
nohkwak Sep 14, 2023
567a233
Change annotation
nohkwak Sep 14, 2023
c200de6
Add functions for feeDelegation
nohkwak Sep 21, 2023
8d524fc
Update FeeDel_09, 11
Sep 22, 2023
5cc8c7b
Update Basic 20, FeeDel 21 examples
Sep 22, 2023
854b25b
Rename initTxData to klaytnTxObject
nohkwak Sep 25, 2023
076027b
Check address when deploying smart contract
nohkwak Sep 25, 2023
8e8025d
Update FeeDel_29_TxTypeFeeDelegatedSmartContractDeploy.js
nohkwak Sep 25, 2023
3631533
Update FeeDel_31_TxTypeFeeDelegatedSmartContractExecution.js
nohkwak Sep 25, 2023
8246403
Update FeeDel_39_TxTypeFeeDelegatedCancel.js
nohkwak Sep 25, 2023
a450199
Add KlaytnTxData fields and remove ts-ignore
nohkwak Sep 25, 2023
bb21ecf
Add field for feeRatio
nohkwak Sep 25, 2023
4c21f95
Update PartialFeeDel_0a_TxTypeFeeDelegatedValueTransferWithRatio.js
nohkwak Sep 25, 2023
b68ec66
Update PartialFeeDel_12_TxTypeFeeDelegatedValueTransferMemoWithRatio.js
nohkwak Sep 25, 2023
2cf6037
Update PartialFeeDel_2a_TxTypeFeeDelegatedSmartContractDeployWithRati…
nohkwak Sep 25, 2023
0fd5dff
Update PartialFeeDel_32_TxTypeFeeDelegatedSmartContractExecutionWithR…
nohkwak Sep 25, 2023
f30cb73
Update PartialFeeDel_3a_TxTypeFeeDelegatedCancelWithRatio.js
nohkwak Sep 25, 2023
61756aa
Update PartialFeeDel_22_TxTypeFeeDelegatedAccountUpdateWithRatio.js
nohkwak Sep 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ethers-ext/src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { KlaytnTx, KlaytnTxFactory } from "./klaytn_tx";
export { KlaytnTx, KlaytnTxFactory, objectFromRLP } from "./klaytn_tx";
export { AccountKey, AccountKeyFactory } from "./accountKey";

/* eslint-disable import/order */
Expand Down
2 changes: 1 addition & 1 deletion ethers-ext/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { KlaytnTxFactory, AccountKeyFactory } from "./core";
export { KlaytnTxFactory, AccountKeyFactory, objectFromRLP } from "./core";
export { TxType, AccountKeyType, formatKlaytnUnits, formatKlay, parseKlaytnUnits, parseKlay } from "./core/util";
export { Wallet, JsonRpcProvider, Accounts, AccountStore } from "./ethers";
export { verifyMessageAsKlaytnAccountKey } from "./ethers/signer";
40 changes: 40 additions & 0 deletions web3js-ext/example/transactions/Basic_08_TxTypeValueTransfer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const { Web3 } = require("web3");
const { KlaytnWeb3 } = require( "../../dist/src");

const { TxType, parseKlay } = require("@klaytn/ethers-ext");

//
// TxTypeValueTransfer
// https://docs.klaytn.foundation/content/klaytn/design/transactions/basic#txtypevaluetransfer
//
const recieverAddr = "0xc40b6909eb7085590e1c26cb3becc25368e249e9";
const senderAddr = "0xa2a8854b1802d8cd5de631e690817c253d6a9153";
const senderPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";

async function main() {
const provider = new Web3.providers.HttpProvider("https://public-en-baobab.klaytn.net");
const web3 = new KlaytnWeb3(provider);

const sender = web3.eth.accounts.privateKeyToAccount(senderPriv);

let tx = {
type: TxType.ValueTransfer,
to: recieverAddr,
value: 1e9,
// value: convertToPeb('1', 'KLAY'),
from: senderAddr,
gas: 21000,
gasPrice: 25e9,
};

let signResult = await web3.eth.accounts.signTransaction(tx, sender.privateKey);
console.log({ signResult });

let sendResult = await web3.eth.sendSignedTransaction(signResult.rawTransaction);
let txhash = sendResult.transactionHash;

let receipt = await web3.eth.getTransactionReceipt(txhash);
console.log({ receipt });
}

main();
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const { Web3 } = require("web3");
const { KlaytnWeb3 } = require( "../../dist/src");

const { TxType, parseKlay } = require("@klaytn/ethers-ext");

//
// TxTypeValueTransferMemo
// https://docs.klaytn.foundation/content/klaytn/design/transactions/basic#txtypevaluetransfermemo
//

const recieverAddr = "0xc40b6909eb7085590e1c26cb3becc25368e249e9";
const senderAddr = "0xa2a8854b1802d8cd5de631e690817c253d6a9153";
const senderPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";

async function main() {
const provider = new Web3.providers.HttpProvider("https://public-en-baobab.klaytn.net");
const web3 = new KlaytnWeb3(provider);

const sender = web3.eth.accounts.privateKeyToAccount(senderPriv);

let tx = {
type: TxType.ValueTransferMemo,
to: recieverAddr,
value: 1e9,
// value: parseKlay("1"), // TODO: add type conversion function
from: senderAddr,
input: "0x1234567890",
};

let signResult = await web3.eth.accounts.signTransaction(tx, sender.privateKey);
console.log({ signResult });

let sendResult = await web3.eth.sendSignedTransaction(signResult.rawTransaction);
let txhash = sendResult.transactionHash;

let receipt = await web3.eth.getTransactionReceipt(txhash);
console.log({ receipt });
}

main();
50 changes: 50 additions & 0 deletions web3js-ext/example/transactions/Basic_20_TxTypeAccountUpdate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const { Web3 } = require("web3");
const { KlaytnWeb3 } = require( "../../dist/src");

const { TxType, AccountKeyType } = require("@klaytn/ethers-ext");

const { secp256k1 } = require("ethereum-cryptography/secp256k1.js")

//
// TxTypeAccountUpdate
// https://docs.klaytn.foundation/content/klaytn/design/transactions/basic#txtypeaccountupdate
//
// from: address of sender to be updated
// key: Refer Klaytn account key
// https://docs.klaytn.foundation/content/klaytn/design/accounts#account-key
//

// create new account for testing
// https://baobab.wallet.klaytn.foundation/
const senderAddr = "0x7532967dda17c5e367c7a5c5dcb56ef6ed299e20";
const senderPriv = "0x4ef44daeb7941877bebc7b97c023e1e51b5593ee1fbc4a232387774603173b86";
const senderNewPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";

async function main() {
const provider = new Web3.providers.HttpProvider("https://public-en-baobab.klaytn.net");
const web3 = new KlaytnWeb3(provider);

const publicKey = "0x" + Buffer.from(secp256k1.getPublicKey( BigInt(senderNewPriv), true)).toString('hex')
console.log(publicKey);

let tx = {
type: TxType.AccountUpdate,
from: senderAddr,
key: {
type: AccountKeyType.Public,
key: publicKey,
}
};

const sender = web3.eth.accounts.privateKeyToAccount(senderPriv);
let signResult = await web3.eth.accounts.signTransaction(tx, sender.privateKey);
console.log({ signResult });

let sendResult = await web3.eth.sendSignedTransaction(signResult.rawTransaction);
let txhash = sendResult.transactionHash;

let receipt = await web3.eth.getTransactionReceipt(txhash);
console.log({ receipt });
}

main();
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const { Web3 } = require("web3");
const { KlaytnWeb3 } = require( "../../dist/src");

const { TxType } = require("@klaytn/ethers-ext");


//
// TxTypeSmartContractDeploy
// https://docs.klaytn.foundation/content/klaytn/design/transactions/basic#txtypesmartcontractdeploy
//
// to: Must be "0x0000000000000000000000000000000000000000",
// value: Must be 0, if not payable
// input: SmartContract binary,
// humanReadable: Must be false,
// codeFormat: Must be 0x00
//

const senderAddr = "0xa2a8854b1802d8cd5de631e690817c253d6a9153";
const senderPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";

async function main() {
const provider = new Web3.providers.HttpProvider("https://public-en-baobab.klaytn.net");
const web3 = new KlaytnWeb3(provider);

const sender = web3.eth.accounts.privateKeyToAccount(senderPriv);

let tx = {
type: TxType.SmartContractDeploy,
to: "0x0000000000000000000000000000000000000000",
value: 0,
from: senderAddr,
input: "0x608060405234801561001057600080fd5b5060f78061001f6000396000f3fe6080604052348015600f57600080fd5b5060043610603c5760003560e01c80633fb5c1cb1460415780638381f58a146053578063d09de08a14606d575b600080fd5b6051604c3660046083565b600055565b005b605b60005481565b60405190815260200160405180910390f35b6051600080549080607c83609b565b9190505550565b600060208284031215609457600080fd5b5035919050565b60006001820160ba57634e487b7160e01b600052601160045260246000fd5b506001019056fea2646970667358221220e0f4e7861cb6d7acf0f61d34896310975b57b5bc109681dbbfb2e548ef7546b364736f6c63430008120033",
humanReadable: false,
codeFormat: 0x00,
gas: 210000,
gasPrice: 25e9,
};

let signResult = await web3.eth.accounts.signTransaction(tx, sender.privateKey);
console.log({ signResult });

let sendResult = await web3.eth.sendSignedTransaction(signResult.rawTransaction);
let txhash = sendResult.transactionHash;

let receipt = await web3.eth.getTransactionReceipt(txhash);
console.log({ receipt });
}

main();
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const { Web3 } = require("web3");
const { KlaytnWeb3 } = require( "../../dist/src");

const { TxType } = require("@klaytn/ethers-ext");

const senderAddr = "0xa2a8854b1802d8cd5de631e690817c253d6a9153";
const senderPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";
const contractAddr = "0xD7fA6634bDDe0B2A9d491388e2fdeD0fa25D2067";

//
// TxTypeSmartContractExecution
// https://docs.klaytn.foundation/content/klaytn/design/transactions/basic#txtypesmartcontractexecution
//
// to : deployed contract address
// value: Must be 0, if not payable
// input: Refer https://web3js.readthedocs.io/en/v1.2.11/web3-eth-contract.html#methods-mymethod-encodeabi
//
async function main() {
const provider = new Web3.providers.HttpProvider("https://public-en-baobab.klaytn.net");
const web3 = new KlaytnWeb3(provider);

const sender = web3.eth.accounts.privateKeyToAccount(senderPriv);

const CONTRACT_ADDRESS = contractAddr;
const CONTRACT_ABI = [
{
"inputs": [
{
"internalType": "uint256",
"name": "newNumber",
"type": "uint256"
}
],
"name": "setNumber",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "increment",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
];
const contract = new web3.eth.Contract(CONTRACT_ABI, CONTRACT_ADDRESS);
const param = contract.methods.setNumber(0x123).encodeABI();

let tx = {
type: TxType.SmartContractExecution,
to: contractAddr,
value: 0,
from: senderAddr,
input: param,
};

let signResult = await web3.eth.accounts.signTransaction(tx, sender.privateKey);
console.log({ signResult });

let sendResult = await web3.eth.sendSignedTransaction(signResult.rawTransaction);
let txhash = sendResult.transactionHash;

let receipt = await web3.eth.getTransactionReceipt(txhash);
console.log({ receipt });
}

main();
64 changes: 64 additions & 0 deletions web3js-ext/example/transactions/Basic_38_TxTypeCancel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const { Web3 } = require("web3");
const { KlaytnWeb3 } = require( "../../dist/src");

const { TxType } = require("@klaytn/ethers-ext");

const senderAddr = "0xa2a8854b1802d8cd5de631e690817c253d6a9153";
const senderPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";
const recieverAddr = "0xc40b6909eb7085590e1c26cb3becc25368e249e9";

//
// TxTypeCancel
// https://docs.klaytn.foundation/content/klaytn/design/transactions/basic#txtypecancel
//
// 1) send ValueTransfer tx with the next nonce + 1
// 2) send Cancel tx with the next nonce + 1
// 3) send ValueTransfer tx with the next nonce
// then you can see Cancel tx with the next nonce + 1
//
async function main() {
const provider = new Web3.providers.HttpProvider("https://public-en-baobab.klaytn.net");
const web3 = new KlaytnWeb3(provider);

const sender = web3.eth.accounts.privateKeyToAccount(senderPriv);

// 1) send ValueTransfer tx with the next nonce+1
let nextNonce = await web3.eth.getTransactionCount(senderAddr);
let tx = {
type: TxType.ValueTransfer,
nonce: nextNonce + 1n,
to: recieverAddr,
value: 1e12,
from: senderAddr,
};

let signResult = await web3.eth.accounts.signTransaction(tx, sender.privateKey);
web3.eth.sendSignedTransaction(signResult.rawTransaction);
// TODO: .on function not working
//.on("receipt", (receipt) => console.log("tx next + 1", receipt));

// 2) send Cancel tx with the next nonce+1
let txCancel = {
type: TxType.Cancel,
nonce: nextNonce + 1n,
from: senderAddr,
};

signResult = await web3.eth.accounts.signTransaction(txCancel, sender.privateKey);
web3.eth.sendSignedTransaction(signResult.rawTransaction)
// TODO: .on function not working
//.on("receipt", (receipt) => console.log("tx next + 1 cancel", receipt));

// 3) send ValueTransfer tx with the next nonce
tx.nonce = nextNonce;

signResult = await web3.eth.accounts.signTransaction(tx, sender.privateKey);
sendResult = await web3.eth.sendSignedTransaction(signResult.rawTransaction);

console.log("sendResult", sendResult);

let receipt = await web3.eth.getTransactionReceipt(sendResult.transactionHash);
console.log( receipt );
}

main();
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const { Web3 } = require("web3");
const { KlaytnWeb3 } = require( "../../dist/src");

const { TxType, objectFromRLP } = require("../../../ethers-ext/dist/src");

//
// TxTypeFeeDelegatedValueTransfer
// https://docs.klaytn.foundation/content/klaytn/design/transactions/fee-delegation#txtypefeedelegatedvaluetransfer
//

const senderAddr = "0xa2a8854b1802d8cd5de631e690817c253d6a9153";
const senderPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";
const feePayerAddr = "0xcb0eb737dfda52756495a5e08a9b37aab3b271da";
const feePayerPriv = "0x9435261ed483b6efa3886d6ad9f64c12078a0e28d8d80715c773e16fc000cff4";
const recieverAddr = "0xc40b6909eb7085590e1c26cb3becc25368e249e9";


async function main() {
const provider = new Web3.providers.HttpProvider("https://public-en-baobab.klaytn.net");
const web3 = new KlaytnWeb3(provider);

let tx = {
type: TxType.FeeDelegatedValueTransfer,
to: recieverAddr,
value: 1e9,
// value: convertToPeb('1', 'KLAY'),
from: senderAddr,
gas: 300000,
gasPrice: 100e9,
};

// sender
const senderWallet = web3.eth.accounts.privateKeyToAccount(senderPriv);
let senderTx = await web3.eth.accounts.signTransaction(tx, senderWallet.privateKey);
console.log(senderTx);

// tx = objectFromRLP(senderTx.rawTransaction);
// console.log(tx);

// fee payer
const feePayerWallet = web3.eth.accounts.privateKeyToAccount(feePayerPriv, provider);
let signResult = await web3.eth.accounts.signTransactionAsFeePayer(senderTx.rawTransaction, feePayerWallet.privateKey);
console.log(signResult);

// tx = objectFromRLP(signResult.rawTransaction);
// console.log(tx);

let sendResult = await web3.eth.sendSignedTransaction(signResult.rawTransaction);
let txhash = sendResult.transactionHash;

let receipt = await web3.eth.getTransactionReceipt(txhash);
console.log({ receipt });
}

main();
Loading