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

Commit

Permalink
web3js: Override web3.eth.sendSignedTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
blukat29 committed Aug 29, 2023
1 parent 1414f6b commit 8340d48
Show file tree
Hide file tree
Showing 5 changed files with 458 additions and 39 deletions.
33 changes: 18 additions & 15 deletions web3js-ext/example/valueTransfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const priv = "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
const addr = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266";
const to = "0x70997970c51812dc3a010c7d01b50e0d17dc79c8";
const url = "http://localhost:8545";
const contractAddr = "0x5FbDB2315678afecb367f032d93F642f64180aa3";
const data_increment = "0xd09de08a"; // Counter.sol:increment()

async function main() {
let provider = new Web3.providers.HttpProvider(url);
Expand All @@ -15,34 +17,35 @@ async function main() {
let sender = web3.eth.accounts.privateKeyToAccount(priv);
console.log({ sender });

/*
let tx = {
from: sender.address,
to: to,
value: 1e9,
// nonce: await web3.eth.getTransactionCount(addr),
// gas: 21000,
gasPrice: 25e9,
type: 8,
// gasPrice: 25e9,
type: 0x08,
};
/*/
let tx = {
from: sender.address,
to: contractAddr,
value: 0,
nonce: await web3.eth.getTransactionCount(addr),
gas: 100_000,
gasPrice: 25e9,
data: data_increment,
data: "0xdeadbeef", // trigger error
type: 0x30,
}
//*/

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

//*
// TODO: auto-assign jsonrpc and id fields
let sendResult = await web3.eth.provider.request({
jsonrpc: "2.0", id: "1",
method: "klay_sendRawTransaction", params: [signResult.rawTransaction] });
let txhash = sendResult.result;
console.log({ sendResult });

// web3.eth.sendSignedTransaction would wait for tx mining, but provider.requestt do not.
// TODO: modify sendSignedTransaction to use klay_sendRawTransaction
await new Promise((r) => setTimeout(r, 2000));
/*/
let sendResult = await web3.eth.sendSignedTransaction(signResult.rawTransaction);
let txhash = sendResult.transactionHash;
//*/

let receipt = await web3.eth.getTransactionReceipt(txhash);
console.log({ receipt });
Expand Down
5 changes: 3 additions & 2 deletions web3js-ext/src/web3/klaytn_tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const web3jsAllowedTransactionKeys = [

// web3.js may strip or reject some Klaytn-specific transaction fields.
// To prserve transaction fields around web3js function calls, use saveCustomFields.
function saveCustomFields(tx: any): any {
export function saveCustomFields(tx: any): any {
// Save fields that are not allowed in web3.js
const savedFields: any = {};
for (const key in tx) {
Expand Down Expand Up @@ -58,7 +58,7 @@ export async function prepareTransaction(

let txData = { ...tx, ...savedFields };

// Below fields might
// Below fields might be
// (1) not specified at the first place,
// (2) or lost during prepareTransactionForSigning,
// (3) or not populated by prepareTransactionForSigning.
Expand Down Expand Up @@ -129,6 +129,7 @@ export class KlaytnTx extends LegacyTransaction {
value: toHex(this.value),
from: this.from ? this.from : undefined,
data: bytesToHex(this.data),
input: bytesToHex(this.data),
chainId: this.chainId ? toHex(this.chainId) : undefined,
});
if (this.v && this.r && this. s) {
Expand Down
18 changes: 0 additions & 18 deletions web3js-ext/src/web3/rpc.ts

This file was deleted.

Loading

0 comments on commit 8340d48

Please sign in to comment.