Skip to content
This repository has been archived by the owner on Dec 11, 2023. It is now read-only.

Latest commit

 

History

History
 
 

standard-transactions

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Ethereum transactions demo

You'll find here a collection of scripts that show you how to interact with Ethereum using the two most popular Javascript libraries: web3.js and ethers.js.

Prerequisites

Make sure to have Node.js 12+ installed and an Infura Project ID available. Clone this repository and run:

cd demo-eth-tx/
npm install

We have already supplied a default configuration in .env that includes an Infura ID, a private signing key (Buidler), a demo contract and a default network (rinkeby). Be kind and don't drain the wallets so other people can use them as well to test their apps. 😊

Send a simple transaction

Let's first test how we can send a small amount of Ether between two accounts.

You can find a script for doing this using the ethers.js library in the ethers/send.js file. Run it and wait for the transaction to be mined:

node ethers/send.js

The same script, using the web3.js library is located at web3/send.js. Run it with:

node web3/send.js

Working with smart contracts

In this section we'll look into the steps required to write, deploy and interact with a smart contract.

Let's start with a simple contract (Demo.sol):

contract Demo {
    event Echo(string message);

    function echo(string calldata message) external {
        emit Echo(message);
    }
}

This contract has a single method (called echo) that can be called by anyone with a message and emits an event that echoes the input message.

Contract compilation

Before deploying the contract on the network, we need to compile it. There's a simple compile.js script included here to serve this purpose for now:

node compile.js

Note: When you start building your own smart contracts, you should probably use a development suite such as Truffle or Buidler - these tools will make your life easier 👍

As soon as the contract is compiled, a Demo.json file will show up in the main directory. This file includes the contract bytecode (required for deployment) and the Application Binary Interface (ABI) required for contract interactions.

Contract deployment

You can find the deployment scripts in ethers/deploy.js and web3/deploy.js. Run any of these to deploy your contract:

node ethers/deploy.js
# or
node web3/deploy.js

As soon as the deployment transaction is mined, the script will output the address of the new contract.

Contract interaction

Now that the contract is deployed, you can interact with it. The scripts are configured to interact with an older, existing contract, but feel free to edit this line of ethers/call.js or this line of web3/call.js and replace it with the address of your newly deployed contract.

Now you can run:

node ethers/call.js
# or
node web3/call.js

Congratulations! You deployed and interacted with an Ethereum smart contract. It's time for you to go build something awesome! 🚀