Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit b9bf13f

Browse files
authored
Merge branch '4.x' into nikos/5931/coverage-web3-abi
2 parents b3e886d + 5e469af commit b9bf13f

File tree

10 files changed

+413
-8
lines changed

10 files changed

+413
-8
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
sidebar_position: 2
3+
sidebar_label: 'Sign and Send Transaction'
4+
---
5+
6+
# Sign and Send Transaction
7+
8+
You can sign and send transactions in different ways.
9+
10+
- [Local wallet signing](/docs/guides/sign_and_send_tx/local_wallet)
11+
- [Using raw transaction](/docs/guides/sign_and_send_tx/raw)
12+
- [Using wallet of Eth Node](/docs/guides/sign_and_send_tx/wallet_of_eth_node)
13+
14+
For each of them you can use [Web3PromiEvent](/docs/guides/sign_and_send_tx/promi_event) to catch extra transaction's events
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
---
2+
sidebar_position: 0
3+
sidebar_label: 'Local wallet'
4+
---
5+
6+
# Using Local Wallet
7+
8+
The simplest way to sign and send transactions is using a local wallet:
9+
10+
## Eth Transaction
11+
12+
```ts
13+
// First step: initialize `web3` instance
14+
import { Web3 } from 'web3';
15+
const web3 = new Web3(/* PROVIDER*/);
16+
17+
// Second step: add an account to wallet
18+
const privateKeyString = '0x1f953dc9b6437fb94fcafa5dabe3faa0c34315b954dd66f41bf53273339c6d26';
19+
const account = web3.eth.accounts.wallet.add(privateKeyString);
20+
21+
// Make sure the account has enough eth on balance to send the transaction
22+
23+
// Third step: sign and send the transaction
24+
// Magic happens behind sendTransaction. If a transaction is sent from an account that exists in a wallet, it will be automatically signed.
25+
try {
26+
const receipt = await web3.eth.sendTransaction({
27+
from: account.address,
28+
to: '0xe4beef667408b99053dc147ed19592ada0d77f59',
29+
value: '0x1',
30+
gas: '300000',
31+
// other transaction's params
32+
});
33+
} catch (error) {
34+
// catch transaction error
35+
console.error(error);
36+
}
37+
```
38+
39+
List of references:
40+
41+
- [eth.accounts.wallet.add](/api/web3-eth-accounts/class/Wallet#add)
42+
- [eth.sendTransaction](/api/web3-eth/class/Web3Eth#sendTransaction)
43+
44+
## Contract Transaction
45+
46+
```ts
47+
// First step: initialize `web3` instance
48+
import { Web3 } from 'web3';
49+
const web3 = new Web3(/* PROVIDER*/);
50+
51+
// Second step: add an account to wallet
52+
const privateKeyString = '0x1f953dc9b6437fb94fcafa5dabe3faa0c34315b954dd66f41bf53273339c6d26';
53+
const account = web3.eth.accounts.wallet.add(privateKeyString);
54+
55+
// Make sure the account has enough eth on balance to send the transaction
56+
57+
// Third step: sign and send the transaction
58+
// In any function where you can pass from the address set address of the account that exists in a wallet, it will be automatically signed.
59+
60+
try {
61+
// deploy
62+
const contract = new web3.eth.Contract(ContractAbi);
63+
const contractDeployed = await contract
64+
.deploy({
65+
input: ContractBytecode,
66+
arguments: ['Constructor param1', 'Constructor param2'],
67+
})
68+
.send({
69+
from: account.address,
70+
gas: '1000000',
71+
// other transaction's params
72+
});
73+
74+
// call method
75+
await contractDeployed.methods
76+
.transfer('0xe2597eb05cf9a87eb1309e86750c903ec38e527e', '0x1')
77+
.send({
78+
from: account.address,
79+
gas: '1000000',
80+
// other transaction's params
81+
});
82+
} catch (error) {
83+
// catch transaction error
84+
console.error(error);
85+
}
86+
```
87+
88+
List of references:
89+
90+
- [eth.accounts.wallet.add](/api/web3-eth-accounts/class/Wallet#add)
91+
- [eth.Contract](/api/web3-eth-contract/class/Contract)
92+
- [contract.deploy](/api/web3-eth-contract/class/Contract#deploy)
93+
- [contract.methods](/api/web3-eth-contract/class/Contract#methods)
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
---
2+
sidebar_position: 3
3+
sidebar_label: 'Web3PromiEvent'
4+
---
5+
6+
# PromiEvent
7+
8+
You can use Web3PromiEvent when you send transaction via `web3.eth.sendTransaction`, `web3.eth.sendSignedTransaction`, `contractDeployed.methods['methodName'](...methodParams).send` functions
9+
10+
```ts
11+
web3.eth.sendTransaction({...})
12+
.on('sending', (sending) => {
13+
// Sending example
14+
// 0x02f86d82053903849502f900849a9a0d16830186a0947ab80aeb6bb488b7f6c41c58e83ef248eb39c8828080c080a0b0fce643a6ca3077ee6b83590b1798d00edef99e2c65c1837daab88d46860887a07ca449a31b2430dbf21310b8c4491386762ade23e48c7cd0b70d315576374c7c
15+
})
16+
.on('sent', (sent) => {
17+
// Sent example
18+
// 0x02f86d82053903849502f900849a9a0d16830186a0947ab80aeb6bb488b7f6c41c58e83ef248eb39c8828080c080a0b0fce643a6ca3077ee6b83590b1798d00edef99e2c65c1837daab88d46860887a07ca449a31b2430dbf21310b8c4491386762ade23e48c7cd0b70d315576374c7c
19+
})
20+
.on('transactionHash', (transactionHash) => {
21+
// Transaction hash example
22+
// 0x6d85b2f07e7c8f2a7ce90a5bcfa3100c528f173f0707164434fb42d397d92d50
23+
})
24+
.on('confirmation', (confirmation) => {
25+
// Confirmation example
26+
// {
27+
// confirmations: 1n,
28+
// receipt: {
29+
// blockHash: '0x947b8c95dea7f0c643f2be0e9d1c3bec76c7f5146fdf34f5f1efe6d2cab5f568',
30+
// blockNumber: 22n,
31+
// cumulativeGasUsed: 21000n,
32+
// effectiveGasPrice: 2553565308n,
33+
// from: '0xe2597eb05cf9a87eb1309e86750c903ec38e527e',
34+
// gasUsed: 21000n,
35+
// logs: [],
36+
// logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
37+
// status: 1n,
38+
// to: '0x7ab80aeb6bb488b7f6c41c58e83ef248eb39c882',
39+
// transactionHash: '0x3ec198ae10cf289b91210b4fd86a3b22cc9bcef16bca6beee21c35b76a2b7073',
40+
// transactionIndex: 0n,
41+
// type: 2n
42+
// },
43+
// latestBlockHash: '0x947b8c95dea7f0c643f2be0e9d1c3bec76c7f5146fdf34f5f1efe6d2cab5f568'
44+
// }
45+
46+
})
47+
.on('receipt', (receipt) => {
48+
// Receipt example
49+
// {
50+
// blockHash: '0x135d14b724d90b97feec1e96df590ce9af762d424aea49d29e11feaa24fe02f1',
51+
// blockNumber: 23n,
52+
// cumulativeGasUsed: 21000n,
53+
// effectiveGasPrice: 2546893579n,
54+
// from: '0xe2597eb05cf9a87eb1309e86750c903ec38e527e',
55+
// gasUsed: 21000n,
56+
// logs: [],
57+
// logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
58+
// status: 1n,
59+
// to: '0x7ab80aeb6bb488b7f6c41c58e83ef248eb39c882',
60+
// transactionHash: '0x9a6497fe4028d716e66a24ab7dfd3d1bcf136ba2ec26f427719b4ddaaff76fb7',
61+
// transactionIndex: 0n,
62+
// type: 2n
63+
// }
64+
65+
})
66+
.on('error', (error) => {
67+
// Error example
68+
// InvalidResponseError: Returned error: exceeds block gas limit
69+
// at Web3RequestManager._processJsonRpcResponse (.../web3_request_manager.js:193:23)
70+
// at Web3RequestManager.<anonymous> (.../web3_request_manager.js:112:29)
71+
// at Generator.next (<anonymous>)
72+
// at fulfilled (.../web3_request_manager.js:5:58)
73+
// at processTicksAndRejections (node:internal/process/task_queues:96:5) {
74+
// innerError: { code: -32000, message: 'exceeds block gas limit' },
75+
// code: 101,
76+
// data: undefined,
77+
// request: {
78+
// jsonrpc: '2.0',
79+
// id: 'ea1f8fb4-fe86-4492-9d89-c6e31bf1c036',
80+
// method: 'eth_sendRawTransaction',
81+
// params: [
82+
// '0x02f86e82053903849502f900849a9a0d168405f7c1f0947ab80aeb6bb488b7f6c41c58e83ef248eb39c8828080c001a0ddd93f5ce9a6a0de130dc660e65d2cdf8784148b8c91b83635b8458e96a767a3a028c48b048bf041e530ded63a0d2198855043f782ef0aa47391a2afa9c50a5ff1'
83+
// ]
84+
// }
85+
});
86+
87+
88+
```
89+
90+
List of references:
91+
92+
- [Web3PromiEvent](/api/web3-core/class/Web3PromiEvent)
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
---
2+
sidebar_position: 1
3+
sidebar_label: 'Raw Transaction'
4+
---
5+
6+
# Using Raw Transaction
7+
8+
## Eth Transaction
9+
10+
```ts
11+
// First step: initialize web3 instance
12+
import { Web3 } from 'web3';
13+
const web3 = new Web3(/* PROVIDER*/);
14+
15+
// Second step: convert private key to account
16+
const account = web3.eth.accounts.privateKeyToAccount(privateKey);
17+
// Make sure the account has enough eth on balance to send the transaction
18+
19+
// Third step: sign and send the transaction
20+
try {
21+
const signedTx = await account.signTransaction({
22+
from: account.address,
23+
to: '0x7ab80aeb6bb488b7f6c41c58e83ef248eb39c882',
24+
amount: '0x1',
25+
gas: '100000',
26+
// other transaction's params
27+
});
28+
await web3.eth.sendSignedTransaction(signedTx.rawTransaction);
29+
} catch (error) {
30+
// catch transaction error
31+
console.error(error);
32+
}
33+
```
34+
35+
List of references:
36+
37+
- [account.signTransaction](/api/web3-eth-accounts/function/signTransaction)
38+
- [eth.accounts.privateKeyToAccount](/api/web3-eth-accounts/function/privateKeyToAccount)
39+
- [eth.sendSignedTransaction](/api/web3-eth/class/Web3Eth#sendSignedTransaction)
40+
- [eth.sendTransaction](/api/web3-eth/class/Web3Eth#sendTransaction)
41+
42+
## Contract Transaction
43+
44+
```ts
45+
// First step: initialize web3 instance
46+
import { Web3 } from 'web3';
47+
const web3 = new Web3(/* PROVIDER*/);
48+
49+
// Second step: convert private key to account
50+
const account = web3.eth.accounts.privateKeyToAccount(privateKey);
51+
// Make sure the account has enough eth on balance to send the transaction
52+
53+
// Third step: sign and send the transaction
54+
try {
55+
// deploy
56+
const contract = new web3.eth.Contract(ERC20Token.abi);
57+
const contractTx = contract.deploy({
58+
input: ERC20TokenBytecode,
59+
arguments: ['1000000000000000000'],
60+
});
61+
const signedTxData = await account.signTransaction({
62+
input: contractTx.encodeABI(),
63+
from: account.address,
64+
gas: '10000000',
65+
});
66+
const deployResult = await web3.eth.sendSignedTransaction(signedTxData.rawTransaction);
67+
68+
// call method
69+
const toAddress = '0x7ed0e85b8e1e925600b4373e6d108f34ab38a401';
70+
const contractDeployed = new web3.eth.Contract(ERC20Token.abi, deployResult.logs[0].address);
71+
72+
const balance = await contractDeployed.methods.balanceOf(account.address).call();
73+
const contractMethodTx = contractDeployed.methods.transfer(toAddress, '0x10');
74+
75+
const signedMethodData = await account.signTransaction({
76+
input: contractMethodTx.encodeABI(),
77+
to: contractDeployed.options.address,
78+
from: account.address,
79+
gas: '4700000',
80+
// other transaction's params
81+
});
82+
await web3.eth.sendSignedTransaction(signedMethodData.rawTransaction);
83+
} catch (error) {
84+
// catch transaction error
85+
console.error(error);
86+
}
87+
```
88+
89+
List of references:
90+
91+
- [account.signTransaction](/api/web3-eth-accounts/function/signTransaction)
92+
- [contract.deploy](/api/web3-eth-contract/class/Contract#deploy)
93+
- [contract.methods](/api/web3-eth-contract/class/Contract#methods)
94+
- [eth.accounts.privateKeyToAccount](/api/web3-eth-accounts/function/privateKeyToAccount)
95+
- [eth.Contract](/api/web3-eth-contract/class/Contract)
96+
- [eth.sendSignedTransaction](/api/web3-eth/class/Web3Eth#sendSignedTransaction)

0 commit comments

Comments
 (0)