Skip to content
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

Closed
wants to merge 2 commits into from

Conversation

michael-spengler
Copy link

I read the contribution guidelines and made this PR as focused as possible.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

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:

  • [?] I have selected the correct base branch.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • [?] I have made corresponding changes to the documentation.
  • My changes generate no new warnings.
  • [?] Any dependent changes have been merged and published in downstream modules.
  • I ran npm run dtslint with success and extended the tests and types if necessary.
  • [?] I ran npm run test:unit with success.
  • [?] I ran npm run test:cov and my test cases cover all the lines and branches of the added code.
  • [?] I ran npm run build and tested dist/web3.min.js in a browser.
  • [?] I have tested my code on the live network.
  • [?] I have checked the Deploy Preview and it looks correct.
  • [?] I have updated the CHANGELOG.md file in the root folder.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 80.423% when pulling 170dfb2 on michael-spengler:1.x into 4469516 on ethereum:1.x.

@CLAassistant
Copy link

CLAassistant commented Jan 7, 2021

CLA assistant check
All committers have signed the CLA.

@spacesailor24
Copy link
Contributor

I think the more documentation/usage examples we have the better, thank you for opening this PR!

I noticed you're using the ethereumjs-tx package, but I wonder if the following code would be a better example, because it doesn't have the extra dependency?

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

@michael-spengler
Copy link
Author

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)
}

Thank You for the valuable feedback. I just updated things accordingly and created this one: #3844

  • closing the PR at hand. Hoping to keep things simple by this. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question General discussion questions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants