-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add frequently searched usage examples - 1. transfer ether #3831
Conversation
I think the more documentation/usage examples we have the better, thank you for opening this PR! I noticed you're using the require('dotenv').config() // this ensures process.env. ... contains your .env file configuration values
const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider(process.env.PROVIDER_URL);
const getTxObject = async (from, to, value) => {
const txCount = await web3.eth.getTransactionCount(from, "pending")
const gasPrice = await web3.eth.getGasPrice()
return {
nonce: web3.utils.numberToHex(txCount),
gasLimit: web3.utils.numberToHex(33000),
gasPrice: web3.utils.toHex(gasPrice),
from,
to,
value,
chainId: '0x1',
}
}
export const signAndSend = async (to, value) => {
const signedTx = await web3.eth.accounts.signTransaction(
await getTxObject(process.env.SENDER_ADDRESS, to, value),
process.env.SENDER_PRIVATE_KEY)
web3.eth.sendSignedTransaction(signedTx.rawTransaction)
.on('transactionHash', (hash) => {
console.log(hash);
})
.on('receipt', (receipt) => {
console.log(receipt);
})
.on('confirmation', (confirmationNumber, receipt) => {
console.log(confirmationNumber);
console.log(receipt);
})
.on('error', console.error)
} P.s. In the future, given that this PR is in progress, you could open the PR as a draft until it's ready for the final review |
Thank You for the valuable feedback. I just updated things accordingly and created this one: #3844
|
I read the contribution guidelines and made this PR as focused as possible.
Type of change
In the following checklist I added some questionmarks, as it is my first PR on this repo and as I do not know if you want to include such usage examples in general. Only then I would update the changelog.md + documentation ...
Feedback = welcome.
Checklist:
npm run dtslint
with success and extended the tests and types if necessary.npm run test:unit
with success.npm run test:cov
and my test cases cover all the lines and branches of the added code.npm run build
and testeddist/web3.min.js
in a browser.CHANGELOG.md
file in the root folder.