Skip to content

Commit

Permalink
Merge branch '4.x' into nikos/5931/coverage-web3-abi
Browse files Browse the repository at this point in the history
  • Loading branch information
nikoulai authored Mar 22, 2023
2 parents b3e886d + 5e469af commit b9bf13f
Show file tree
Hide file tree
Showing 10 changed files with 413 additions and 8 deletions.
14 changes: 14 additions & 0 deletions docs/docs/guides/sign_and_send_tx/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
sidebar_position: 2
sidebar_label: 'Sign and Send Transaction'
---

# Sign and Send Transaction

You can sign and send transactions in different ways.

- [Local wallet signing](/docs/guides/sign_and_send_tx/local_wallet)
- [Using raw transaction](/docs/guides/sign_and_send_tx/raw)
- [Using wallet of Eth Node](/docs/guides/sign_and_send_tx/wallet_of_eth_node)

For each of them you can use [Web3PromiEvent](/docs/guides/sign_and_send_tx/promi_event) to catch extra transaction's events
93 changes: 93 additions & 0 deletions docs/docs/guides/sign_and_send_tx/local_wallet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
---
sidebar_position: 0
sidebar_label: 'Local wallet'
---

# Using Local Wallet

The simplest way to sign and send transactions is using a local wallet:

## Eth Transaction

```ts
// First step: initialize `web3` instance
import { Web3 } from 'web3';
const web3 = new Web3(/* PROVIDER*/);

// Second step: add an account to wallet
const privateKeyString = '0x1f953dc9b6437fb94fcafa5dabe3faa0c34315b954dd66f41bf53273339c6d26';
const account = web3.eth.accounts.wallet.add(privateKeyString);

// Make sure the account has enough eth on balance to send the transaction

// Third step: sign and send the transaction
// Magic happens behind sendTransaction. If a transaction is sent from an account that exists in a wallet, it will be automatically signed.
try {
const receipt = await web3.eth.sendTransaction({
from: account.address,
to: '0xe4beef667408b99053dc147ed19592ada0d77f59',
value: '0x1',
gas: '300000',
// other transaction's params
});
} catch (error) {
// catch transaction error
console.error(error);
}
```

List of references:

- [eth.accounts.wallet.add](/api/web3-eth-accounts/class/Wallet#add)
- [eth.sendTransaction](/api/web3-eth/class/Web3Eth#sendTransaction)

## Contract Transaction

```ts
// First step: initialize `web3` instance
import { Web3 } from 'web3';
const web3 = new Web3(/* PROVIDER*/);

// Second step: add an account to wallet
const privateKeyString = '0x1f953dc9b6437fb94fcafa5dabe3faa0c34315b954dd66f41bf53273339c6d26';
const account = web3.eth.accounts.wallet.add(privateKeyString);

// Make sure the account has enough eth on balance to send the transaction

// Third step: sign and send the transaction
// 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.

try {
// deploy
const contract = new web3.eth.Contract(ContractAbi);
const contractDeployed = await contract
.deploy({
input: ContractBytecode,
arguments: ['Constructor param1', 'Constructor param2'],
})
.send({
from: account.address,
gas: '1000000',
// other transaction's params
});

// call method
await contractDeployed.methods
.transfer('0xe2597eb05cf9a87eb1309e86750c903ec38e527e', '0x1')
.send({
from: account.address,
gas: '1000000',
// other transaction's params
});
} catch (error) {
// catch transaction error
console.error(error);
}
```

List of references:

- [eth.accounts.wallet.add](/api/web3-eth-accounts/class/Wallet#add)
- [eth.Contract](/api/web3-eth-contract/class/Contract)
- [contract.deploy](/api/web3-eth-contract/class/Contract#deploy)
- [contract.methods](/api/web3-eth-contract/class/Contract#methods)
92 changes: 92 additions & 0 deletions docs/docs/guides/sign_and_send_tx/promi_event.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
---
sidebar_position: 3
sidebar_label: 'Web3PromiEvent'
---

# PromiEvent

You can use Web3PromiEvent when you send transaction via `web3.eth.sendTransaction`, `web3.eth.sendSignedTransaction`, `contractDeployed.methods['methodName'](...methodParams).send` functions

```ts
web3.eth.sendTransaction({...})
.on('sending', (sending) => {
// Sending example
// 0x02f86d82053903849502f900849a9a0d16830186a0947ab80aeb6bb488b7f6c41c58e83ef248eb39c8828080c080a0b0fce643a6ca3077ee6b83590b1798d00edef99e2c65c1837daab88d46860887a07ca449a31b2430dbf21310b8c4491386762ade23e48c7cd0b70d315576374c7c
})
.on('sent', (sent) => {
// Sent example
// 0x02f86d82053903849502f900849a9a0d16830186a0947ab80aeb6bb488b7f6c41c58e83ef248eb39c8828080c080a0b0fce643a6ca3077ee6b83590b1798d00edef99e2c65c1837daab88d46860887a07ca449a31b2430dbf21310b8c4491386762ade23e48c7cd0b70d315576374c7c
})
.on('transactionHash', (transactionHash) => {
// Transaction hash example
// 0x6d85b2f07e7c8f2a7ce90a5bcfa3100c528f173f0707164434fb42d397d92d50
})
.on('confirmation', (confirmation) => {
// Confirmation example
// {
// confirmations: 1n,
// receipt: {
// blockHash: '0x947b8c95dea7f0c643f2be0e9d1c3bec76c7f5146fdf34f5f1efe6d2cab5f568',
// blockNumber: 22n,
// cumulativeGasUsed: 21000n,
// effectiveGasPrice: 2553565308n,
// from: '0xe2597eb05cf9a87eb1309e86750c903ec38e527e',
// gasUsed: 21000n,
// logs: [],
// logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
// status: 1n,
// to: '0x7ab80aeb6bb488b7f6c41c58e83ef248eb39c882',
// transactionHash: '0x3ec198ae10cf289b91210b4fd86a3b22cc9bcef16bca6beee21c35b76a2b7073',
// transactionIndex: 0n,
// type: 2n
// },
// latestBlockHash: '0x947b8c95dea7f0c643f2be0e9d1c3bec76c7f5146fdf34f5f1efe6d2cab5f568'
// }

})
.on('receipt', (receipt) => {
// Receipt example
// {
// blockHash: '0x135d14b724d90b97feec1e96df590ce9af762d424aea49d29e11feaa24fe02f1',
// blockNumber: 23n,
// cumulativeGasUsed: 21000n,
// effectiveGasPrice: 2546893579n,
// from: '0xe2597eb05cf9a87eb1309e86750c903ec38e527e',
// gasUsed: 21000n,
// logs: [],
// logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
// status: 1n,
// to: '0x7ab80aeb6bb488b7f6c41c58e83ef248eb39c882',
// transactionHash: '0x9a6497fe4028d716e66a24ab7dfd3d1bcf136ba2ec26f427719b4ddaaff76fb7',
// transactionIndex: 0n,
// type: 2n
// }

})
.on('error', (error) => {
// Error example
// InvalidResponseError: Returned error: exceeds block gas limit
// at Web3RequestManager._processJsonRpcResponse (.../web3_request_manager.js:193:23)
// at Web3RequestManager.<anonymous> (.../web3_request_manager.js:112:29)
// at Generator.next (<anonymous>)
// at fulfilled (.../web3_request_manager.js:5:58)
// at processTicksAndRejections (node:internal/process/task_queues:96:5) {
// innerError: { code: -32000, message: 'exceeds block gas limit' },
// code: 101,
// data: undefined,
// request: {
// jsonrpc: '2.0',
// id: 'ea1f8fb4-fe86-4492-9d89-c6e31bf1c036',
// method: 'eth_sendRawTransaction',
// params: [
// '0x02f86e82053903849502f900849a9a0d168405f7c1f0947ab80aeb6bb488b7f6c41c58e83ef248eb39c8828080c001a0ddd93f5ce9a6a0de130dc660e65d2cdf8784148b8c91b83635b8458e96a767a3a028c48b048bf041e530ded63a0d2198855043f782ef0aa47391a2afa9c50a5ff1'
// ]
// }
});


```

List of references:

- [Web3PromiEvent](/api/web3-core/class/Web3PromiEvent)
96 changes: 96 additions & 0 deletions docs/docs/guides/sign_and_send_tx/raw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
---
sidebar_position: 1
sidebar_label: 'Raw Transaction'
---

# Using Raw Transaction

## Eth Transaction

```ts
// First step: initialize web3 instance
import { Web3 } from 'web3';
const web3 = new Web3(/* PROVIDER*/);

// Second step: convert private key to account
const account = web3.eth.accounts.privateKeyToAccount(privateKey);
// Make sure the account has enough eth on balance to send the transaction

// Third step: sign and send the transaction
try {
const signedTx = await account.signTransaction({
from: account.address,
to: '0x7ab80aeb6bb488b7f6c41c58e83ef248eb39c882',
amount: '0x1',
gas: '100000',
// other transaction's params
});
await web3.eth.sendSignedTransaction(signedTx.rawTransaction);
} catch (error) {
// catch transaction error
console.error(error);
}
```

List of references:

- [account.signTransaction](/api/web3-eth-accounts/function/signTransaction)
- [eth.accounts.privateKeyToAccount](/api/web3-eth-accounts/function/privateKeyToAccount)
- [eth.sendSignedTransaction](/api/web3-eth/class/Web3Eth#sendSignedTransaction)
- [eth.sendTransaction](/api/web3-eth/class/Web3Eth#sendTransaction)

## Contract Transaction

```ts
// First step: initialize web3 instance
import { Web3 } from 'web3';
const web3 = new Web3(/* PROVIDER*/);

// Second step: convert private key to account
const account = web3.eth.accounts.privateKeyToAccount(privateKey);
// Make sure the account has enough eth on balance to send the transaction

// Third step: sign and send the transaction
try {
// deploy
const contract = new web3.eth.Contract(ERC20Token.abi);
const contractTx = contract.deploy({
input: ERC20TokenBytecode,
arguments: ['1000000000000000000'],
});
const signedTxData = await account.signTransaction({
input: contractTx.encodeABI(),
from: account.address,
gas: '10000000',
});
const deployResult = await web3.eth.sendSignedTransaction(signedTxData.rawTransaction);

// call method
const toAddress = '0x7ed0e85b8e1e925600b4373e6d108f34ab38a401';
const contractDeployed = new web3.eth.Contract(ERC20Token.abi, deployResult.logs[0].address);

const balance = await contractDeployed.methods.balanceOf(account.address).call();
const contractMethodTx = contractDeployed.methods.transfer(toAddress, '0x10');

const signedMethodData = await account.signTransaction({
input: contractMethodTx.encodeABI(),
to: contractDeployed.options.address,
from: account.address,
gas: '4700000',
// other transaction's params
});
await web3.eth.sendSignedTransaction(signedMethodData.rawTransaction);
} catch (error) {
// catch transaction error
console.error(error);
}
```

List of references:

- [account.signTransaction](/api/web3-eth-accounts/function/signTransaction)
- [contract.deploy](/api/web3-eth-contract/class/Contract#deploy)
- [contract.methods](/api/web3-eth-contract/class/Contract#methods)
- [eth.accounts.privateKeyToAccount](/api/web3-eth-accounts/function/privateKeyToAccount)
- [eth.Contract](/api/web3-eth-contract/class/Contract)
- [eth.sendSignedTransaction](/api/web3-eth/class/Web3Eth#sendSignedTransaction)
Loading

0 comments on commit b9bf13f

Please sign in to comment.