Web3.js is a set of libraries that allow developers to interact with Ethereum nodes using HTTP, IPC, or WebSocket protocols with JavaScript. Phron has an Ethereum-like API available that is fully compatible with Ethereum-style JSON-RPC invocations. Therefore, developers can leverage this compatibility and use the Web3.js library to interact with a Phron node as if they were doing so on Ethereum.
In this guide, you'll learn how to use the Web3.js library to send a transaction and deploy a contract on Phron.
Checking Prerequisites
For the examples in this guide, you will need to have the following:
An account with funds. You can get DEV tokens for testing on Phron once every 24 hours from the Phron Faucet
To test out the examples in this guide on Phron, you will need to have your own endpoint and API key, which you can get from one of the supported Endpoint Providers
Note
The examples in this guide assume you have a MacOS or Ubuntu 22.04-based environment and will need to be adapted accordingly for Windows.
Installing Web3.js
To get started, you'll need to start a basic JavaScript project. First, create a directory to store all of the files you'll be creating throughout this guide, and initialize the project with the following command:
mkdirweb3-examples&&cdweb3-examples&&npminit--y
For this guide, you'll need to install the Web3.js library and the Solidity compiler. To install both NPM packages, you can run the following command:
npminstallweb3solc@0.8.0
Setup Web3.js with Phron
You can configure Web3.js to work with any of the Phron networks. To configure your project for Phron, you will need to have your own endpoint and API key, which you can get from one of the supported Endpoint Providers.
The simplest way to get started with each of the networks is as follows:
const { Web3 } =require('web3');// Create Web3 instanceconstweb3=newWeb3('INSERT_RPC_API_ENDPOINT'); // Insert your RPC URL here
Save this code snippet, as you'll need it for the scripts that are used in the following sections.
Send a Transaction
During this section, you'll be creating a couple of scripts. The first one will be to check the balances of your accounts before trying to send a transaction. The second script will actually send the transaction.
You can also use the balance script to check the account balances after the transaction has been sent.
Check Balances Script
You'll only need one file to check the balances of both addresses before and after the transaction is sent. To get started, you can create a balances.js file by running:
touchbalances.js
Next, you will create the script for this file and complete the following steps:
Set up the Web3 provider
Define the addressFrom and addressTo variables
Create the asynchronous balances function, which wraps the web3.eth.getBalance method
Use the web3.eth.getBalance function to fetch the balances for the addressFrom and addressTo addresses. You can also leverage the web3.utils.fromWei function to transform the balance into a more readable number in DEV
Lastly, run the balances function
// 1. Add the Web3 provider logic// {...}// 2. Create address variablesconstaddressFrom='INSERT_FROM_ADDRESS';constaddressTo='INSERT_TO_ADDRESS';// 3. Create balances functionconstbalances=async () => {// 4. Fetch balance infoconstbalanceFrom=web3.utils.fromWei(awaitweb3.eth.getBalance(addressFrom),'ether' );constbalanceTo=web3.utils.fromWei(awaitweb3.eth.getBalance(addressTo),'ether' );console.log(`The balance of ${addressFrom} is: ${balanceFrom} DEV`);console.log(`The balance of ${addressTo} is: ${balanceTo} DEV`);};// 5. Call balances functionbalances();
View the complete script
const { Web3 } =require('web3');// 1. Add the Web3 provider logic here:constproviderRPC= { development:'http://localhost:9944', phron:'https://testnet.phron.ai',};constweb3=newWeb3(providerRPC.phron); // Change to correct network// 2. Create address variablesconstaddressFrom='INSERT_FROM_ADDRESS';constaddressTo='INSERT_TO_ADDRESS';// 3. Create balances functionconstbalances=async () => {// 4. Fetch balance infoconstbalanceFrom=web3.utils.fromWei(awaitweb3.eth.getBalance(addressFrom),'ether');constbalanceTo=web3.utils.fromWei(awaitweb3.eth.getBalance(addressTo),'ether');console.log(`The balance of ${addressFrom} is: ${balanceFrom} DEV`);console.log(`The balance of ${addressTo} is: ${balanceTo} DEV`);};// 5. Call balances functionbalances();
To run the script and fetch the account balances, you can run the following command:
nodebalances.js
If successful, the balances for the origin and receiving address will be displayed in your terminal in DEV.
Send Transaction Script
You'll only need one file to execute a transaction between accounts. For this example, you'll be transferring 1 DEV token from an origin address (from which you hold the private key) to another address. To get started, you can create a transaction.js file by running:
touchtransaction.js
Next, you will create the script for this file and complete the following steps:
Set up the Web3 provider
Define the accountFrom, including the privateKey, and the addressTo variables. The private key is required to create a wallet instance. Note: This is for example purposes only. Never store your private keys in a JavaScript file
Create the asynchronous send function, which wraps the transaction object, and the sign and send transaction functions
Create and sign the transaction using the web3.eth.accounts.signTransaction function. Pass in the gas, addressTo, value, gasPrice, and nonce for the transaction along with the sender's privateKey
Send the signed transaction using the web3.eth.sendSignedTransaction method and pass in the raw transaction. Then use await to wait until the transaction is processed and the transaction receipt is returned
Lastly, run the send function
// 1. Add the Web3 provider logic// {...}// 2. Create account variablesconstaccountFrom= { privateKey:'INSERT_YOUR_PRIVATE_KEY', address:'INSERT_PUBLIC_ADDRESS_OF_PK',};constaddressTo='INSERT_TO_ADDRESS'; // Change addressTo// 3. Create send functionconstsend=async () => {console.log(`Attempting to send transaction from ${accountFrom.address} to ${addressTo}` );// 4. Sign transaction with PKconstcreateTransaction=awaitweb3.eth.accounts.signTransaction( { gas:21000, to: addressTo, value:web3.utils.toWei('1','ether'), gasPrice:awaitweb3.eth.getGasPrice(), nonce:awaitweb3.eth.getTransactionCount(accountFrom.address), },accountFrom.privateKey );// 5. Send transaction and wait for receiptconstcreateReceipt=awaitweb3.eth.sendSignedTransaction(createTransaction.rawTransaction );console.log(`Transaction successful with hash: ${createReceipt.transactionHash}` );};// 6. Call send functionsend();
View the complete script
const { Web3 } =require('web3');// 1. Add the Web3 provider logicconstproviderRPC= { development:'http://localhost:9944', phron:'https://testnet.phron.ai',};constweb3=newWeb3(providerRPC.phron); // Change to correct network// 2. Create account variablesconstaccountFrom= { privateKey:'INSERT_YOUR_PRIVATE_KEY', address:'INSERT_PUBLIC_ADDRESS_OF_PK',};constaddressTo='INSERT_TO_ADDRESS';// 3. Create send functionconstsend=async () => {console.log(`Attempting to send transaction from ${accountFrom.address} to ${addressTo}` );// 4. Sign transaction with PKconstcreateTransaction=awaitweb3.eth.accounts.signTransaction( { gas:21000, to: addressTo, value:web3.utils.toWei('1','ether'), gasPrice:awaitweb3.eth.getGasPrice(), nonce:awaitweb3.eth.getTransactionCount(accountFrom.address), },accountFrom.privateKey );// 5. Send transaction and wait for receiptconstcreateReceipt=awaitweb3.eth.sendSignedTransaction(createTransaction.rawTransaction );console.log(`Transaction successful with hash: ${createReceipt.transactionHash}` );};// 6. Call send functionsend();
To run the script, you can run the following command in your terminal:
nodetransaction.js
If the transaction was successful, in your terminal, you'll see the transaction hash has been printed out.
You can also use the balances.js script to check that the balances for the origin and receiving accounts have changed. The entire workflow would look like this:
node balances.jsThe balance of 0x3B939FeaD1557C741Ff06492FD0127bd287A421e is: 3603.67979115380310679 DEVThe balance of 0xe29A0699e079FeBEe94A02f35C31B026f90F6040 is: 0. DEVnode transaction.jsAttempting to send transaction from 0x3B939FeaD1557C741Ff06492FD0127bd287A421e to 0xe29A0699e079FeBEe94A02f35C31B026f90F6040Transaction successful with hash: 0xf1d628ed12c5f40e03e29aa2c23c8c09680ee595c60607c7363a81c0be8ef3cbnode balances.jsThe balance of 0x3B939FeaD1557C741Ff06492FD0127bd287A421e is: 3602.67978852880310679 DEVThe balance of 0xe29A0699e079FeBEe94A02f35C31B026f90F6040 is: 1 DEV
Common Errors When Sending Transactions
When sending a transaction with Web3.js, it is important that you have all of the required data for the transaction. You'll need to provide the from address or the nonce of the sender, the gas or gasLimit, and the gasPrice.
If you do not specify the from address or the nonce of the sender, you may receive the following error:
UnableToPopulateNonceError: Invalid value given "UnableToPopulateNonceError". Error: unable to populate nonce, no from address available.
To fix this, simply add either the from or nonce field to the transaction object.
If you do not specify the gas correctly, you may receive the following error:
MissingGasError: Invalid value given "gas: 0x5208, gasPrice: undefined, maxPriorityFeePerGas: undefined, maxFeePerGas: undefined". Error: "gas" is missing.
To resolve this error, you'll need to make sure that you've provided a gasPrice for the transaction. You can use await web3.eth.getGasPrice() to programmatically get this value.
Deploy a Contract
The contract you'll be compiling and deploying in the next couple of sections is a simple incrementer contract, arbitrarily named Incrementer.sol. You can get started by creating a file for the contract:
touchIncrementer.sol
Next, you can add the Solidity code to the file:
// SPDX-License-Identifier: MITpragmasolidity ^0.8.0;contract Incrementer {uint256public number;constructor(uint256_initialNumber) { number = _initialNumber; }functionincrement(uint256_value) public { number = number + _value; }functionreset() public { number =0; }}
The constructor function, which runs when the contract is deployed, sets the initial value of the number variable stored on-chain (the default is 0). The increment function adds the _value provided to the current number, but a transaction needs to be sent, which modifies the stored data. Lastly, the reset function resets the stored value to zero.
Note
This contract is a simple example for illustration purposes only and does not handle values wrapping around.
Compile Contract Script
In this section, you'll create a script that uses the Solidity compiler to output the bytecode and interface (ABI) for the Incrementer.sol contract. To get started, you can create a compile.js file by running:
touchcompile.js
Next, you will create the script for this file and complete the following steps:
Import the fs and solc packages
Using the fs.readFileSync function, you'll read and save the file contents of Incrementer.sol to source
Build the input object for the Solidity compiler by specifying the language, sources, and settings to be used
Using the input object, you can compile the contract using solc.compile
Extract the compiled contract file and export it to be used in the deployment script
With the script for compiling the Incrementer.sol contract in place, you can then use the results to send a signed transaction that deploys it. To do so, you can create a file for the deployment script called deploy.js:
touchdeploy.js
Next, you will create the script for this file and complete the following steps:
Import the contract file from compile.js
Set up the Web3 provider
Define the accountFrom, including the privateKey, and the addressTo variables. The private key is required to create a wallet instance. Note: This is for example purposes only. Never store your private keys in a JavaScript file
Save the bytecode and abi for the compiled contract
Create the asynchronous deploy function that will be used to deploy the contract
Create the contract instance using the web3.eth.Contract function
Create the constructor and pass in the bytecode and the initial value for the incrementer. For this example, you can set the initial value to 5
Create and sign the transaction using the web3.eth.accounts.signTransaction function. Pass in the data, gas, gasPrice, and nonce for the transaction along with the sender's privateKey
Send the signed transaction using the web3.eth.sendSignedTransaction method and pass in the raw transaction. Then use await to wait until the transaction is processed and the transaction receipt is returned
Lastly, run the deploy function
// 1. Import the contract fileconstcontractFile=require('./compile');// 2. Add the Web3 provider logic// {...}// 3. Create address variablesconstaccountFrom= { privateKey:'INSERT_YOUR_PRIVATE_KEY', address:'INSERT_PUBLIC_ADDRESS_OF_PK',};// 4. Get the bytecode and APIconstbytecode=contractFile.evm.bytecode.object;constabi=contractFile.abi;// 5. Create deploy functionconstdeploy=async () => {console.log(`Attempting to deploy from account ${accountFrom.address}`);// 6. Create contract instanceconstincrementer=newweb3.eth.Contract(abi);// 7. Create constructor transactionconstincrementerTx=incrementer.deploy({ data: bytecode, arguments: [5], });// 8. Sign transaction with PKconstcreateTransaction=awaitweb3.eth.accounts.signTransaction( { data:incrementerTx.encodeABI(), gas:awaitincrementerTx.estimateGas(), gasPrice:awaitweb3.eth.getGasPrice(), nonce:awaitweb3.eth.getTransactionCount(accountFrom.address), },accountFrom.privateKey );// 9. Send transaction and wait for receiptconstcreateReceipt=awaitweb3.eth.sendSignedTransaction(createTransaction.rawTransaction );console.log(`Contract deployed at address: ${createReceipt.contractAddress}`);};// 10. Call deploy functiondeploy();
View the complete script
// 1. Import web3 and the contract fileconst { Web3 } =require('web3');constcontractFile=require('./compile');// 2. Add the Web3 provider logicconstproviderRPC= { development:'http://localhost:9944', phron:'https://testnet.phron.ai',};constweb3=newWeb3(providerRPC.phron); // Change to correct network// 3. Create address variablesconstaccountFrom= { privateKey:'INSERT_YOUR_PRIVATE_KEY', address:'INSERT_PUBLIC_ADDRESS_OF_PK',};// 4. Get the bytecode and APIconstbytecode=contractFile.evm.bytecode.object;constabi=contractFile.abi;// 5. Create deploy functionconstdeploy=async () => {console.log(`Attempting to deploy from account ${accountFrom.address}`);// 6. Create contract instanceconstincrementer=newweb3.eth.Contract(abi);// 7. Create constructor transactionconstincrementerTx=incrementer.deploy({ data: bytecode, arguments: [5], });// 8. Sign transaction with PKconstcreateTransaction=awaitweb3.eth.accounts.signTransaction( { data:incrementerTx.encodeABI(), gas:awaitincrementerTx.estimateGas(), gasPrice:awaitweb3.eth.getGasPrice(), nonce:awaitweb3.eth.getTransactionCount(accountFrom.address), },accountFrom.privateKey );// 9. Send transaction and wait for receiptconstcreateReceipt=awaitweb3.eth.sendSignedTransaction(createTransaction.rawTransaction );console.log(`Contract deployed at address: ${createReceipt.contractAddress}`);};// 10. Call deploy functiondeploy();
To run the script, you can enter the following command into your terminal:
nodedeploy.js
If successful, the contract's address will be displayed in the terminal.
node deploy.jsAttempting to deploy from account 0x3B939FeaD1557C741Ff06492FD0127bd287A421eContract deployed at address: 0x6dcb33a7f6235e74fd553b50c96f900707142892
Read Contract Data (Call Methods)
Call methods are the type of interaction that doesn't modify the contract's storage (change variables), meaning no transaction needs to be sent. They simply read various storage variables of the deployed contract.
To get started, you can create a file and name it get.js:
touchget.js
Then you can take the following steps to create the script:
Import the abi from the compile.js file
Set up the Web3 provider
Create the contractAddress variable using the address of the deployed contract
Create an instance of the contract using the web3.eth.Contract function and passing in the abi and contractAddress
Create the asynchronous get function
Use the contract instance to call one of the contract's methods and pass in any inputs if necessary. For this example, you will call the number method which doesn't require any inputs. You can use await, which will return the value requested once the request promise resolves
Lastly, call the get function
// 1. Import the contract ABIconst { abi } =require('./compile');// 2. Add the Web3 provider logic// {...}// 3. Create address variablesconstcontractAddress='INSERT_CONTRACT_ADDRESS';// 4. Create contract instanceconstincrementer=newweb3.eth.Contract(abi, contractAddress);// 5. Create get functionconstget=async () => {console.log(`Making a call to contract at address: ${contractAddress}`);// 6. Call contractconstdata=awaitincrementer.methods.number().call();console.log(`The current number stored is: ${data}`);};// 7. Call get functionget();
View the complete script
// 1. Import Web3js and the contract ABIconst { Web3 } =require('web3');const { abi } =require('./compile');// 2. Add the Web3 provider logicconstproviderRPC= { development:'http://localhost:9944', phron:'https://testnet.phron.ai',};constweb3=newWeb3(providerRPC.phron); // Change to correct network// 3. Create address variablesconstcontractAddress='INSERT_CONTRACT_ADDRESS';// 4. Create contract instanceconstincrementer=newweb3.eth.Contract(abi, contractAddress);// 5. Create get functionconstget=async () => {console.log(`Making a call to contract at address: ${contractAddress}`);// 6. Call contractconstdata=awaitincrementer.methods.number().call();console.log(`The current number stored is: ${data}`);};// 7. Call get functionget();
To run the script, you can enter the following command in your terminal:
nodeget.js
If successful, the value will be displayed in the terminal.
Interact with Contract (Send Methods)
Send methods are the type of interaction that modifies the contract's storage (change variables), meaning a transaction needs to be signed and sent. In this section, you'll create two scripts: one to increment and one to reset the incrementer. To get started, you can create a file for each script and name them increment.js and reset.js:
touchincrement.jsreset.js
Open the increment.js file and take the following steps to create the script:
Import the abi from the compile.js file
Set up the Web3 provider
Define the privateKey for the origin account, the contractAddress of the deployed contract, and the _value to increment by. The private key is required to create a wallet instance. Note: This is for example purposes only. Never store your private keys in a JavaScript file
Create an instance of the contract using the web3.eth.Contract function and passing in the abi and contractAddress
Use the contract instance to build the increment transaction using the methods.increment function and passing in the _value as an input
Create the asynchronous increment function
Use the contract instance and the increment transaction you previously created to sign the transaction with the sender's private key. You'll use the web3.eth.accounts.signTransaction function and specify the to address, data, gas, gasPrice, and nonce for the transaction
Send the signed transaction using the web3.eth.sendSignedTransaction method and pass in the raw transaction. Then use await to wait until the transaction is processed and the transaction receipt is returned
Lastly, call the increment function
// 1. Import the contract ABIconst { abi } =require('./compile');// 2. Add the Web3 provider logic// {...}// 3. Create variablesconstaccountFrom= { privateKey:'INSERT_YOUR_PRIVATE_KEY', address:'INSERT_PUBLIC_ADDRESS_OF_PK',};constcontractAddress='INSERT_CONTRACT_ADDRESS';const_value=3;// 4. Create contract instanceconstincrementer=newweb3.eth.Contract(abi, contractAddress);// 5. Build the increment transactionconstincrementTx=incrementer.methods.increment(_value);// 6. Create increment functionconstincrement=async () => {console.log(`Calling the increment by ${_value} function in contract at address: ${contractAddress}` );// 7. Sign transaction with PKconstcreateTransaction=awaitweb3.eth.accounts.signTransaction( { to: contractAddress, data:incrementTx.encodeABI(), gas:awaitincrementTx.estimateGas(), gasPrice:awaitweb3.eth.getGasPrice(), nonce:awaitweb3.eth.getTransactionCount(accountFrom.address), },accountFrom.privateKey );// 8. Send transaction and wait for receiptconstcreateReceipt=awaitweb3.eth.sendSignedTransaction(createTransaction.rawTransaction );console.log(`Tx successful with hash: ${createReceipt.transactionHash}`);};// 9. Call increment functionincrement();
View the complete script
// 1. Import Web3js and the contract ABIconst { Web3 } =require('web3');const { abi } =require('./compile');// 2. Add the Web3 provider logicconstproviderRPC= { development:'http://localhost:9944', phron:'https://testnet.phron.ai',};constweb3=newWeb3(providerRPC.phron); // Change to correct network// 3. Create variablesconstaccountFrom= { privateKey:'INSERT_YOUR_PRIVATE_KEY', address:'INSERT_PUBLIC_ADDRESS_OF_PK',};constcontractAddress='INSERT_CONTRACT_ADDRESS';const_value=3;// 4. Create contract instanceconstincrementer=newweb3.eth.Contract(abi, contractAddress);// 5. Build increment transactionconstincrementTx=incrementer.methods.increment(_value);// 6. Create increment functionconstincrement=async () => {console.log(`Calling the increment by ${_value} function in contract at address: ${contractAddress}` );// 7. Sign transaction with PKconstcreateTransaction=awaitweb3.eth.accounts.signTransaction( { to: contractAddress, data:incrementTx.encodeABI(), gas:awaitincrementTx.estimateGas(), gasPrice:awaitweb3.eth.getGasPrice(), nonce:awaitweb3.eth.getTransactionCount(accountFrom.address), },accountFrom.privateKey );// 8. Send transaction and wait for receiptconstcreateReceipt=awaitweb3.eth.sendSignedTransaction(createTransaction.rawTransaction );console.log(`Tx successful with hash: ${createReceipt.transactionHash}`);};// 9. Call increment functionincrement();
To run the script, you can enter the following command in your terminal:
nodeincrement.js
If successful, the transaction hash will be displayed in the terminal. You can use the get.js script alongside the increment.js script to make sure that value is changing as expected:
node get.jsMaking a call to contract at address: 0x6dcb33a7f6235e74fd553b50c96f900707142892The current number stored is: 5node increment.jsCalling the increment by 3 function in contract at address: 0x6dcb33a7f6235e74fd553b50c96f900707142892Tx successful with hash: 0xb03d1426376e7efc49d8b6c69aaf91e548578db7fd4a9ba575dbd8030821f6a3node get.jsMaking a call to contract at address: 0x6dcb33a7f6235e74fd553b50c96f900707142892The current number stored is: 8
Next, you can open the reset.js file and take the following steps to create the script:
Import the abi from the compile.js file
Set up the Web3 provider
Define the privateKey for the origin account and the contractAddress of the deployed contract. The private key is required to create a wallet instance. Note: This is for example purposes only. Never store your private keys in a JavaScript file
Create an instance of the contract using the web3.eth.Contract function and passing in the abi and contractAddress
Use the contract instance to build the reset transaction using the methods.reset function
Create the asynchronous reset function
Use the contract instance and the reset transaction you previously created to sign the transaction with the sender's private key. You'll use the web3.eth.accounts.signTransaction function and specify the to address, data, gas, gasPrice, and nonce for the transaction
Send the signed transaction using the web3.eth.sendSignedTransaction method and pass in the raw transaction. Then use await to wait until the transaction is processed and the transaction receipt is returned
Lastly, call the reset function
// 1. Import the contract ABIconst { abi } =require('./compile');// 2. Add the Web3 provider logic// {...}// 3. Create variablesconstaccountFrom= { privateKey:'INSERT_YOUR_PRIVATE_KEY', address:'INSERT_PUBLIC_ADDRESS_OF_PK',};constcontractAddress='INSERT_CONTRACT_ADDRESS';// 4. Create a contract instanceconstincrementer=newweb3.eth.Contract(abi, contractAddress);// 5. Build reset transactionconstresetTx=incrementer.methods.reset();// 6. Create reset functionconstreset=async () => {console.log(`Calling the reset function in contract at address: ${contractAddress}` );// 7. Sign transaction with PKconstcreateTransaction=awaitweb3.eth.accounts.signTransaction( { to: contractAddress, data:resetTx.encodeABI(), gas:awaitresetTx.estimateGas(), gasPrice:awaitweb3.eth.getGasPrice(), nonce:awaitweb3.eth.getTransactionCount(accountFrom.address), },accountFrom.privateKey );// 8. Send transaction and wait for receiptconstcreateReceipt=awaitweb3.eth.sendSignedTransaction(createTransaction.rawTransaction );console.log(`Tx successful with hash: ${createReceipt.transactionHash}`);};// 9. Call reset functionreset();
View the complete script
// 1. Import Web3js and the contract ABIconst { Web3 } =require('web3');const { abi } =require('./compile');// 2. Add the Web3 provider logicconstproviderRPC= { development:'http://localhost:9944', phron:'https://testnet.phron.ai',};constweb3=newWeb3(providerRPC.phron); // Change to correct network// 3. Create variablesconstaccountFrom= { privateKey:'INSERT_YOUR_PRIVATE_KEY', address:'INSERT_PUBLIC_ADDRESS_OF_PK',};constcontractAddress='INSERT_CONTRACT_ADDRESS';// 4. Create contract instanceconstincrementer=newweb3.eth.Contract(abi, contractAddress);// 5. Build reset transactionconstresetTx=incrementer.methods.reset();// 6. Create reset functionconstreset=async () => {console.log(`Calling the reset function in contract at address: ${contractAddress}`);// 7. Sign transaction with PKconstcreateTransaction=awaitweb3.eth.accounts.signTransaction( { to: contractAddress, data:resetTx.encodeABI(), gas:awaitresetTx.estimateGas(), gasPrice:awaitweb3.eth.getGasPrice(), nonce:awaitweb3.eth.getTransactionCount(accountFrom.address), },accountFrom.privateKey );// 8. Send transaction and wait for receiptconstcreateReceipt=awaitweb3.eth.sendSignedTransaction(createTransaction.rawTransaction);console.log(`Tx successful with hash: ${createReceipt.transactionHash}`);};// 9. Call reset functionreset();
To run the script, you can enter the following command in your terminal:
nodereset.js
If successful, the transaction hash will be displayed in the terminal. You can use the get.js script alongside the reset.js script to make sure that value is changing as expected:
node get.jsMaking a call to contract at address: 0x6dcb33a7f6235e74fd553b50c96f900707142892The current number stored is: 8node reset.jsCalling the reset function in contract at address: 0x6dcb33a7f6235e74fd553b50c96f900707142892Tx successful with hash: 0x557e908ca4da05d5af50983dbc116fbf8049bb2e86b9ec1e9f7d3f516b8a4c55node get.jsMaking a call to contract at address: 0x6dcb33a7f6235e74fd553b50c96f900707142892The current number stored is: 0
This tutorial is for educational purposes only. As such, any contracts or code created in this tutorial should not be used in production.The information presented herein has been provided by third parties and is made available solely for general information purposes. Phron does not endorse any project listed and described on the Phron Doc Website (https://docs.Phron.ai/). Phron does not warrant the accuracy, completeness or usefulness of this information. Any reliance you place on such information is strictly at your own risk. Phron disclaims all liability and responsibility arising from any reliance placed on this information by you or by anyone who may be informed of any of its contents. All statements and/or opinions expressed in these materials are solely the responsibility of the person or entity providing those materials and do not necessarily represent the opinion of Phron. The information should not be construed as professional or financial advice of any kind. Advice from a suitably qualified professional should always be sought in relation to any particular matter or circumstance. The information herein may link to or integrate with other websites operated or content provided by third parties, and such other websites may link to this website. Phron has no control over any such other websites or their content and will have no liability arising out of or related to such websites or their content. The existence of any such link does not constitute an endorsement of such websites, the content of the websites, or the operators of the websites. These links are being provided to you only as a convenience and you release and hold Phron harmless from any and all liability arising from your use of this information or the information provided by any third-party website or service.