The Ethers.js library provides a set of tools to interact with Ethereum Nodes with JavaScript, similar to Web3.js. 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 Ethers.js library to interact with a Phron node as if they were doing so on Ethereum. For more information on Ethers.js, check their documentation site.
In this guide, you'll learn how to use the Ethers.js library to send a transaction and deploy a contract on Phron.
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
NoteThe examples in this guide assume you have a MacOS or Ubuntu 22.04-based environment and will need to be adapted accordingly for Windows.
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:
For this guide, you'll need to install the Ethers.js library and the Solidity compiler. To install both NPM packages, you can run the following command:
Throughout this guide, you'll be creating a bunch of scripts that provide different functionality such as sending a transaction, deploying a contract, and interacting with a deployed contract. In most of these scripts you'll need to create an Ethers provider to interact with the network.
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.
To create a provider, you can take the following steps:
Import the ethers
library
Define the providerRPC
object, which can include the network configurations for any of the networks you want to send a transaction on. You'll include the name
, rpc
, and chainId
for each network
Create the provider
using the ethers.JsonRpcProvider
method
Save this code snippet as you'll need it for the scripts that are used in the following sections.
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.
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:
Next, you will create the script for this file and complete the following steps:
Set up the Ethers provider
Define the addressFrom
and addressTo
variables
Create the asynchronous balances
function which wraps the provider.getBalance
method
Use the provider.getBalance
function to fetch the balances for the addressFrom
and addressTo
addresses. You can also leverage the ethers.formatEther
function to transform the balance into a more readable number in ETH
Lastly, run the balances
function
To run the script and fetch the account balances, you can run the following command:
If successful, the balances for the origin and receiving address will be displayed in your terminal in DEV.
You'll only need one file for executing 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:
Next, you will create the script for this file and complete the following steps:
Set up the Ethers provider
Define 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 a wallet using the privateKey
and provider
from the previous steps. The wallet instance is used to sign transactions
Create the asynchronous send
function which wraps the transaction object and the wallet.sendTransaction
method
Create the transaction object which only requires the recipient's address and the amount to send. Note that ethers.parseEther
can be used, which handles the necessary unit conversions from Ether to Wei - similar to using ethers.parseUnits(value, 'ether')
Send the transaction using the wallet.sendTransaction
method and then use await
to wait until the transaction is processed and the transaction receipt is returned
Lastly, run the send
function
To run the script, you can run the following command in your terminal:
If the transaction was succesful, 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:
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:
Next, you can add the Solidity code to the file:
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.
NoteThis contract is a simple example for illustration purposes only and does not handle values wrapping around.
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:
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
:
Next, you will create the script for this file and complete the following steps:
Import the contract file from compile.js
Set up the Ethers provider
Define the privateKey
for the origin account. 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 a wallet using the privateKey
and provider
from the previous steps. The wallet instance is used to sign transactions
Load the contract bytecode
and abi
for the compiled contract
Create a contract instance with signer using the ethers.ContractFactory
function, providing the abi
, bytecode
, and wallet
as parameters
Create the asynchronous deploy
function that will be used to deploy the contract
Within the deploy
function, use the incrementer
contract instance to call deploy
and pass in the initial value. For this example, you can set the initial value to 5
. This will send the transaction for contract deployment. To wait for a transaction receipt you can use the deployed
method of the contract deployment transaction
Lastly, run the deploy
function
To run the script, you can enter the following command into your terminal:
If successful, the contract's address will be displayed in the terminal.
Call methods are the type of interaction that don'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
:
Then you can take the following steps to create the script:
Import the abi
from the compile.js
file
Set up the Ethers provider
Create the contractAddress
variable using the address of the deployed contract
Create an instance of the contract using the ethers.Contract
function and passing in the contractAddress
, abi
, and provider
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
To run the script, you can enter the following command in your terminal:
If successful, the value will be displayed in the terminal.
Send methods are the type of interaction that modify 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
:
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 Ethers 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 a wallet using the privateKey
and provider
from the previous steps. The wallet instance is used to sign transactions
Create an instance of the contract using the ethers.Contract
function and passing in the contractAddress
, abi
, and provider
Create the asynchronous increment
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 increment
method which requires the value to increment by as an input. You can use await
which will return the value requested once the request promise resolves
Lastly, call the increment
function
To run the script, you can enter the following command in your terminal:
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:
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 Ethers 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 a wallet using the privateKey
and provider
from the previous steps. The wallet instance is used to sign transactions
Create an instance of the contract using the ethers.Contract
function and passing in the contractAddress
, abi
, and provider
Create the asynchronous reset
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 reset
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 reset
function
To run the script, you can enter the following command in your terminal:
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:
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.
The Ethers.rs library provides a set of tools to interact with Ethereum Nodes via the Rust programming language that works similar to Ethers.js. 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 Ethers.rs library to interact with a Phron node as if they were doing so on Ethereum. You can read more about how to use Ethers.rs on their official crate documentation.
In this guide, you'll learn how to use the Ethers.rs library to send a transaction and deploy a contract on Phron.
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
Have Rust installed on your device
Have solc installed on your device. Using solc-select is recommended by the Ethers.rs package
NoteThe examples in this guide assumes you have a MacOS or Ubuntu 20.04-based environment and will need to be adapted accordingly for Windows.
To get started, you can create a new Rust project with the Cargo tool:
For this guide, you'll need to install the Ethers.rs library among others. To tell the Rust project to install it, you must edit the Cargo.toml
file that's included with the document to include it under dependencies:
This example is using the ethers
and ethers-solc
crate versions 1.0.2
for RPC interactions and Solidity compiling. It also includes the tokio
crate to run asynchronous Rust environments, since interacting with RPCs requires asynchronous code. Finally, it includes the serde_json
and serde
crates to help serialize/deserialize this example's code.
If this is your first time using solc-select
, you'll need to install and configure the Solidity version using the following commands:
Throughout this guide, you'll be writing multiple functions that provide different functionality such as sending a transaction, deploying a contract, and interacting with a deployed contract. In most of these scripts you'll need to use an Ethers provider or an Ethers signer client to interact with the network.
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.
There are multiple ways to create a provider and signer, but the easiest way is through try_from
. In the src/main.rs
file, you can take the following steps:
Import Provider
and Http
from the ethers
crate
Add a Client
type for convenience, which will be used once you start to create the functions for sending a transaction and deploying a contract
Add a tokio
attribute above async fn main()
for asynchronous excution
Use try_from
to attempt to instantiate a JSON-RPC provider object from an RPC endpoint
Use a private key to create a wallet object (the private key will be used to sign transactions). Note: This is for example purposes only. Never store your private keys in a plain Rust file
Wrap the provider and wallet together into a client by providing them to a SignerMiddleware
object
During this section, you'll be creating a couple of functions, which will be contained in the same main.rs
file to avoid additional complexity from implementing modules. The first function will be to check the balances of your accounts before trying to send a transaction. The second function will actually send the transaction. To run each of these functions, you will edit the main
function and run the main.rs
script.
You should already have your provider and client set up in main.rs
in the way described in the previous section. In order to send a transaction, you'll need to add a few more lines of code:
Add use ethers::{utils, prelude::*};
to your imports, which will provide you access to utility functions and the prelude imports all of the necessary data types and traits
As you'll be sending a transaction from one address to another, you can specify the sending and receiving addresses in the main
function. Note: the address_from
value should correspond to the private key that is used in the main
function
Next, you will create the function for getting the sending and receiving accounts' balances by completing the following steps:
Create a new asynchronous function named print_balances
that takes a provider object's reference and the sending and receiving addresses as input
Use the provider
object's get_balance
function to get the balances of the sending and receiving addresses of the transaction
Print the resultant balances for the sending and receiving addresses
Call the print_balances
function in the main
function
For this example, you'll be transferring 1 DEV from an origin address (of which you hold the private key) to another address.
Create a new asynchronous function named send_transaction
that takes a client object's reference and the sending and receiving addresses as input
Create the transaction object, and include the to
, value
, and from
. When writing the value
input, use the ethers::utils::parse_ether
function
Use the client
object to send the transaction
Print the transaction after it is confirmed
Call the send_transaction
function in the main
function
To run the script, which will send the transaction and then check the balances once the transaction has been sent, you can run the following command:
If the transaction was succesful, in your terminal you'll see the transaction details printed out along with the balance of your address.
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:
Next, you can add the Solidity code to the file:
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.
NoteThis contract is a simple example for illustration purposes only and does not handle values wrapping around.
During the rest of this section, you'll be creating a couple of functions, which will be contained in the main.rs
file to avoid additional complexity from implementing modules. The first function will be to compile and deploy the contract. The remaining functions will interact with the deployed contract.
You should already have your provider and client set up in main.rs
in the way described in the Setting up the Ethers Provider and Client section.
Before getting started with the contract deployment, you'll need to add a few more imports to your main.rs
file:
The ethers_solc
import will be used to compile the smart contract. The prelude
from Ethers imports some necessary data types and traits. Lastly, the std
imports will enables you to store your smart contracts and wrap the client into an Arc
type for thread safety.
This example function will compile and deploy the Incrementer.sol
smart contract you created in the previous section. The Incrementer.sol
smart contract should be in the root directory. In the main.rs
file, you can take the following steps:
Create a new asynchronous function named compile_deploy_contract
that takes a client object's reference as input, and returns an address in the form of H160
Define a variable named source
as the path for the directory that hosts all of the smart contracts that should be compiled, which is the root directory
Use the Solc
crate to compile all of the smart contracts in the root directory
Get the ABI and bytecode from the compiled result, searching for the Incrementer.sol
contract
Create a contract factory for the smart contract using the ABI, bytecode, and client. The client must be wrapped into an Arc
type for thread safety
Use the factory to deploy. For this example, the value 5
is used as the initial value in the constructor
Print out the address after the deployment
Return the address
Call the compile_deploy_contract
function in main
Call methods are the type of interaction that don'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.
Rust is typesafe, which is why the ABI for the Incrementer.sol
contract is required to generate a typesafe Rust struct. For this example, you should create a new file in the root of the Cargo project called Incrementer_ABI.json
:
The ABI for Incrementer.sol
is below, which should be copied and pasted into the Incrementer_ABI.json
file:
Then you can take the following steps to create a function that reads and returns the number
method of the Incrementer.sol
contract:
Generate a type-safe interface for the Incrementer
smart contract with the abigen
macro
Create a new asynchronous function named read_number
that takes a client object's reference and a contract address reference as input, and returns a U256
Create a new instance of the Incrementer
object generated by the abigen macro with the client and contract address values
Call the number
function in the new Incrementer
object
Print out the resultant value
Return the resultant value
Call the read_number
function in main
To run the script, which will deploy the contract and return the current value stored in the Incrementer
contract, you can enter the following command into your terminal:
If successful, you'll see the deployed contract's address and initial value set, which should be 5
, displayed in the terminal.
Send methods are the type of interaction that modify the contract's storage (change variables), meaning a transaction needs to be signed and sent. In this section, you'll create two functions: one to increment and one to reset the incrementer. This section will also require the Incrementer_ABI.json
file initialized when reading from the smart contract.
Take the following steps to create the function to increment:
Ensure that the abigen macro is called for the Incrementer_ABI.json
somewhere in the main.rs
file (if it is already in the main.rs
file, you do not have to have a second one)
Create a new asynchronous function named increment_number
that takes a client object's reference and an address as input
Create a new instance of the Incrementer
object generated by the abigen macro with the client and contract address values
Call the increment
function in the new Incrementer
object by including a U256
object as input. In this instance, the value provided is 5
Call the read_number
function in main
To run the script, you can enter the following command into your terminal:
If successful, the transaction receipt will be displayed in the terminal. You can use the read_number
function in the main
function to make sure that value is changing as expected. If you're using the read_number
function after incrementing, you'll also see the incremented number, which should be 10
.
Next you can interact with the reset
function:
Ensure that the abigen macro is called for the Incrementer_ABI.json
somewhere in the main.rs
file (if it is already in the main.rs
file, you do not have to have a second one)
Create a new asynchronous function named reset
that takes a client object's reference and an address as input
Create a new instance of the Incrementer
object generated by the abigen macro with the client and contract address values
Call the reset
function in the new Incrementer
object
Call the reset
function in main
If successful, the transaction receipt will be displayed in the terminal. You can use the read_number
function in the main
function to make sure that value is changing as expected. If you're using the read_number
function after resetting the number, you should see 0
printed to the terminal.
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.
viem is a modular TypeScript library that allows developers to interact with abstractions over the JSON-RPC API, making it easy to interact with Ethereum nodes. Since Phron has an Ethereum-like API available that is fully compatible with Ethereum-style JSON RPC invocations, developers can leverage this compatibility to interact with Phron nodes. For more information on viem, check out their documentation site.
In this guide, you'll learn how to use viem to send a transaction and deploy a contract on the Phron TestNet.
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
NoteThe examples in this guide assume you have a MacOS or Ubuntu 22.04-based environment and will need to be adapted accordingly for Windows.
To get started, you'll need to create a basic TypeScript 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:
For this guide, you'll need to install the viem library and the Solidity compiler. To install both packages, you can run the following command:
You can create a TypeScript configuration file by running:
NoteThis tutorial was created using Node.js v18.18.0.
Throughout this guide, you'll be creating a bunch of scripts that provide different functionality, such as sending a transaction, deploying a contract, and interacting with a deployed contract. In most of these scripts, you'll need to create a viem client to interact with the network.
You can create a viem client for reading chain data, like balances or contract data, using the createPublicClient
function, or you can create a viem client for writing chain data, like sending transactions, using the createWalletClient
function.
To create a client for reading chain data, you can take the following steps:
Import the createPublicClient
and http
functions from viem
and the network you want to interact with from viem/chains
. The chain can be any of the following: phron
Create the client
using the createPublicClient
function and pass in the network and the HTTP RPC endpoint
To create a client for writing chain data, you can take the following steps:
Import the createWalletClient
and http
functions from viem
, the privateKeyToAccount
function for loading your accounts via their private keys, and the network you want to interact with from viem/chains
. The chain can be any of the following: phron
Create your account using the privateKeyToAccount
function
Create the client
using the createWalletClient
function and pass in the account, network, and the HTTP RPC endpoint
NoteTo interact with browser-based wallets, you can use the following code to create an account:
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.
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.ts
file by running:
Next, you will create the script for this file and complete the following steps:
Update your imports to include the createPublicClient
, http
, and formatEther
functions from viem
and the network you want to interact with from viem/chains
Set up a public viem client, which can be used for reading chain data, such as account balances
Define the addressFrom
and addressTo
variables
Create the asynchronous balances
function that wraps the publicClient.getBalance
method
Use the publicClient.getBalance
function to fetch the balances for the addressFrom
and addressTo
addresses. You can also leverage the formatEther
function to transform the balance into a more readable number (in GLMR, MOVR, or DEV)
Lastly, run the balances
function
To run the script and fetch the account balances, you can run the following command:
If successful, the balances for the origin and receiving address will be displayed in your terminal in DEV.
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.ts
file by running:
Next, you will create the script for this file and complete the following steps:
Update your imports to include the createWalletClient
, http
, and parseEther
functions from viem
, the privateKeyToAccount
function from viem/accounts
, and the network you want to interact with from viem/chains
Set up a viem wallet client for writing chain data, which can be used along with your private key to send transactions. Note: This is for example purposes only. Never store your private keys in a TypeScript file
Set up a public viem client for reading chain data, which will be used to wait for the transaction receipt
Define the addressTo
variable
Create the asynchronous send
function, which wraps the transaction object and the walletClient.sendTransaction
method
Use the walletClient.sendTransaction
function to sign and send the transaction. You'll need to pass in the transaction object, which only requires the recipient's address and the amount to send. Note that parseEther
can be used, which handles the necessary unit conversions from Ether to Wei, similar to using parseUnits(value, decimals)
. Use await
to wait until the transaction is processed and the transaction hash is returned
Use the publicClient.waitForTransactionReceipt
function to wait for the transaction receipt, signaling that the transaction has been completed. This is particularly helpful if you need the transaction receipt or if you're running the balances.ts
script directly after this one to check if the balances have been updated as expected
Lastly, run the send
function
To run the script, you can run the following command in your terminal:
If the transaction was successful, in your terminal you'll see the transaction hash has been printed out.
NoteViem requires that you prepend your private key with
0x
. Many wallets omit this0x
, so verify you've included it as you replaceINSERT_PRIVATE_KEY
.
You can also use the balances.ts
script to check that the balances for the origin and receiving accounts have changed. The entire workflow would look like this:
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:
Next, you can add the Solidity code to the file:
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.
NoteThis contract is a simple example for illustration purposes only and does not handle values wrapping around.
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.ts
file by running:
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.ts
:
Next, you will create the script for this file and complete the following steps:
Update your imports to include the createPublicClient
, createWalletClient
, and http
functions from viem
, the privateKeyToAccount
function from viem/accounts
, the network you want to interact with from viem/chains
, and the contractFile
from the compile.ts
file you created in the Compile Contract Script section
Set up a viem wallet client for writing chain data, which will be used along with your private key to deploy the Incrementer
contract. Note: This is for example purposes only. Never store your private keys in a TypeScript file
Set up a public viem client for reading chain data, which will be used to read the transaction receipt for the deployment
Load the contract bytecode
and abi
for the compiled contract
Create the asynchronous deploy
function that will be used to deploy the contract via the walletClient.deployContract
method
Use the walletClient.deployContract
function to sign and send the transaction. You'll need to pass in the contract's ABI and bytecode, the account to deploy the transaction from, and the initial value for the incrementer. Use await
to wait until the transaction is processed and the transaction hash is returned
Use the publicClient.readContract
function to get the transaction receipt for the deployment. Use await
to wait until the transaction is processed and the contract address is returned
Lastly, run the deploy
function
To run the script, you can enter the following command into your terminal:
If successful, the contract's address will be displayed in the terminal.
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.ts
:
Then you can take the following steps to create the script:
Update your imports to include the createPublicClient
and http
functions from viem
, the network you want to interact with from viem/chains
, and the contractFile
from the compile.ts
file you created in the Compile Contract Script section
Set up a public viem client for reading chain data, which will be used to read the current number of the Incrementer
contract
Create the contractAddress
variable using the address of the deployed contract and the abi
variable using the contractFile
from the compile.ts
file
Create the asynchronous get
function
Call the contract using the publicClient.readContract
function, passing in the abi
, the name of the function, the contractAddress
, and any arguments (if needed). You can use await
, which will return the value requested once the request promise resolves
Lastly, call the get
function
To run the script, you can enter the following command in your terminal:
If successful, the value will be displayed in the terminal.
Send methods are the type of interactions that modify 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.ts
and reset.ts
:
Open the increment.ts
file and take the following steps to create the script:
Update your imports to include the createWalletClient
and http
functions from viem
, the network you want to interact with from viem/chains
, and the contractFile
from the compile.ts
file you created in the Compile Contract Script section
Set up a viem wallet client for writing chain data, which will be used along with your private key to send a transaction. Note: This is for example purposes only. Never store your private keys in a TypeScript file
Set up a public viem client for reading chain data, which will be used to wait for the transaction receipt
Create the contractAddress
variable using the address of the deployed contract, the abi
variable using the contractFile
from the compile.ts
file, and the _value
to increment the contract by
Create the asynchronous increment
function
Call the contract using the walletClient.writeContract
function, passing in the abi
, the name of the function, the contractAddress
, and the _value
. You can use await
, which will return the transaction hash once the request promise resolves
Use the publicClient.waitForTransactionReceipt
function to wait for the transaction receipt, signaling that the transaction has been completed. This is particularly helpful if you need the transaction receipt or if you're running the get.ts
script directly after this one to check that the current number has been updated as expected
Lastly, call the increment
function
To run the script, you can enter the following command in your terminal:
If successful, the transaction hash will be displayed in the terminal. You can use the get.ts
script alongside the increment.ts
script to make sure that value is changing as expected.
Next, you can open the reset.ts
file and take the following steps to create the script:
Update your imports to include the createWalletClient
and http
functions from viem
, the network you want to interact with from viem/chains
, and the contractFile
from the compile.ts
file you created in the Compile Contract Script section
Set up a viem wallet client for writing chain data, which will be used along with your private key to send a transaction. Note: This is for example purposes only. Never store your private keys in a TypeScript file
Set up a public viem client for reading chain data, which will be used to wait for the transaction receipt
Create the contractAddress
variable using the address of the deployed contract and the abi
variable using the contractFile
from the compile.ts
file to increment the contract by
Create the asynchronous reset
function
Call the contract using the walletClient.writeContract
function, passing in the abi
, the name of the function, the contractAddress
, and an empty array for the arguments. You can use await
, which will return the transaction hash once the request promise resolves
Use the publicClient.waitForTransactionReceipt
function to wait for the transaction receipt, signaling that the transaction has been completed. This is particularly helpful if you need the transaction receipt or if you're running the get.ts
script directly after this one to check that the current number has been reset to 0
Lastly, call the reset
function
To run the script, you can enter the following command in your terminal:
If successful, the transaction hash will be displayed in the terminal. You can use the get.ts
script alongside the reset.ts
script to make sure that value is changing as expected.
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.
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.
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
NoteThe examples in this guide assume you have a MacOS or Ubuntu 22.04-based environment and will need to be adapted accordingly for Windows.
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:
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:
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:
Save this code snippet, as you'll need it for the scripts that are used in the following sections.
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.
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:
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
To run the script and fetch the account balances, you can run the following command:
If successful, the balances for the origin and receiving address will be displayed in your terminal in DEV.
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:
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
To run the script, you can run the following command in your terminal:
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:
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:
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:
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.
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:
Next, you can add the Solidity code to the file:
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.
NoteThis contract is a simple example for illustration purposes only and does not handle values wrapping around.
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:
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
:
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
To run the script, you can enter the following command into your terminal:
If successful, the contract's address will be displayed in the terminal.
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
:
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
To run the script, you can enter the following command in your terminal:
If successful, the value will be displayed in the terminal.
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
:
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
To run the script, you can enter the following command in your terminal:
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:
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
To run the script, you can enter the following command in your terminal:
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:
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.
Web3.py is a set of libraries that allow developers to interact with Ethereum nodes using HTTP, IPC, or WebSocket protocols with Python. 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.py library to interact with a Phron python3 as if they were doing so on Ethereum.
In this guide, you'll learn how to use the Web3.py library to send a transaction and deploy a contract on Phron.
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
NoteThe examples in this guide assume you have a MacOS or Ubuntu 22.04-based environment and will need to be adapted accordingly for Windows.
To get started, you can create a directory to store all of the files you'll be creating throughout this guide:
For this guide, you'll need to install the Web3.py library and the Solidity compiler. To install both packages, you can run the following command:
Throughout this guide, you'll be creating a bunch of scripts that provide different functionalities, such as sending a transaction, deploying a contract, and interacting with a deployed contract. In most of these scripts, you'll need to create a Web3.py provider to interact with the network.
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.
To create a provider, you can take the following steps:
Import the web3
library
Create the web3
provider using the Web3(Web3.HTTPProvider())
method and providing the endpoint URL
Save this code snippet, as you'll need it for the scripts that are used in the following sections.
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.
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.py
file by running:
Next, you will create the script for this file and complete the following steps:
Set up the Web3 provider
Define the address_from
and address_to
variables
Get the balance for the accounts using the web3.eth.get_balance
function and format the results using the web3.from_wei
To run the script and fetch the account balances, you can run the following command:
If successful, the balances for the origin and receiving address will be displayed in your terminal in ETH.
You'll only need one file for executing 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.py
file by running:
Next, you will create the script for this file and complete the following steps:
Add imports, including Web3.py and the rpc_gas_price_strategy
, which will be used in the following steps to get the gas price used for the transaction
Set up the Web3 provider
Define the account_from
, including the private_key
, and the address_to
variables. The private key is required to sign the transaction. Note: This is for example purposes only. Never store your private keys in a Python file
Use the Web3.py Gas Price API to set a gas price strategy. For this example, you'll use the imported rpc_gas_price_strategy
Create and sign the transaction using the web3.eth.account.sign_transaction
function. Pass in the nonce
gas
, gasPrice
, to
, and value
for the transaction along with the sender's private_key
. To get the nonce
you can use the web3.eth.get_transaction_count
function and pass in the sender's address. To predetermine the gasPrice
you'll use the web3.eth.generate_gas_price
function. For the value
, you can format the amount to send from an easily readable format to Wei using the web3.to_wei
function
Using the signed transaction, you can then send it using the web3.eth.send_raw_transaction
function and wait for the transaction receipt by using the web3.eth.wait_for_transaction_receipt
function
To run the script, you can run the following command in your terminal:
If the transaction was succesful, in your terminal you'll see the transaction hash has been printed out.
You can also use the balances.py
script to check that the balances for the origin and receiving accounts have changed. The entire workflow would look like this:
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:
Next, you can add the Solidity code to the file:
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.
NoteThis contract is a simple example for illustration purposes only and does not handle values wrapping around.
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.py
file by running:
Next, you will create the script for this file and complete the following steps:
Import the solcx
package
Optional - If you haven't already installed the Solidity compiler, you can do so with by using the solcx.install_solc
function
Compile the Incrementer.sol
function using the solcx.compile_files
function
Export the contract's ABI and bytecode
NoteIf you see an error stating that
Solc is not installed
, uncomment step 2 described in the code snippet.
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.py
:
Next, you will create the script for this file and complete the following steps:
Add imports, including Web3.py and the ABI and bytecode of the Incrementer.sol
contract
Set up the Web3 provider
Define the account_from
, including the private_key
. The private key is required to sign the transaction. Note: This is for example purposes only. Never store your private keys in a Python file
Create a contract instance using the web3.eth.contract
function and passing in the ABI and bytecode of the contract
Build a constructor transaction using the contract instance and passing in the value to increment by. For this example, you can use 5
. You'll then use the build_transaction
function to pass in the transaction information including the from
address and the nonce
for the sender. To get the nonce
you can use the web3.eth.get_transaction_count
function
Sign the transaction using the web3.eth.account.sign_transaction
function and pass in the constructor transaction and the private_key
of the sender
Using the signed transaction, you can then send it using the web3.eth.send_raw_transaction
function and wait for the transaction receipt by using the web3.eth.wait_for_transaction_receipt
function
To run the script, you can enter the following command into your terminal:
If successful, the contract's address will be displayed in the terminal.
Call methods are the type of interaction that don'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.py
:
Then you can take the following steps to create the script:
Add imports, including Web3.py and the ABI of the Incrementer.sol
contract
Set up the Web3 provider
Define the contract_address
of the deployed contract
Create a contract instance using the web3.eth.contract
function and passing in the ABI and address of the deployed contract
Using the contract instance, you can then call the number
function
To run the script, you can enter the following command in your terminal:
If successful, the value will be displayed in the terminal.
Send methods are the type of interaction that modify 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.py
and reset.py
:
Open the increment.py
file and take the following steps to create the script:
Add imports, including Web3.py and the ABI of the Incrementer.sol
contract
Set up the Web3 provider
Define the account_from
, including the private_key
, the contract_address
of the deployed contract, and the value
to increment by. The private key is required to sign the transaction. Note: This is for example purposes only. Never store your private keys in a Python file
Create a contract instance using the web3.eth.contract
function and passing in the ABI and address of the deployed contract
Build the increment transaction using the contract instance and passing in the value to increment by. You'll then use the build_transaction
function to pass in the transaction information including the from
address and the nonce
for the sender. To get the nonce
you can use the web3.eth.get_transaction_count
function
Sign the transaction using the web3.eth.account.sign_transaction
function and pass in the increment transaction and the private_key
of the sender
Using the signed transaction, you can then send it using the web3.eth.send_raw_transaction
function and wait for the transaction receipt by using the web3.eth.wait_for_transaction_receipt
function
To run the script, you can enter the following command in your terminal:
If successful, the transaction hash will be displayed in the terminal. You can use the get.py
script alongside the increment.py
script to make sure that value is changing as expected:
Next you can open the reset.py
file and take the following steps to create the script:
Add imports, including Web3.py and the ABI of the Incrementer.sol
contract
Set up the Web3 provider
Define the account_from
, including the private_key
, and the contract_address
of the deployed contract. The private key is required to sign the transaction. Note: This is for example purposes only. Never store your private keys in a Python file
Create a contract instance using the web3.eth.contract
function and passing in the ABI and address of the deployed contract
Build the reset transaction using the contract instance. You'll then use the build_transaction
function to pass in the transaction information including the from
address and the nonce
for the sender. To get the nonce
you can use the web3.eth.get_transaction_count
function
Sign the transaction using the web3.eth.account.sign_transaction
function and pass in the reset transaction and the private_key
of the sender
Using the signed transaction, you can then send it using the web3.eth.send_raw_transaction
function and wait for the transaction receipt by using the web3.eth.wait_for_transaction_receipt
function
To run the script, you can enter the following command in your terminal:
If successful, the transaction hash will be displayed in the terminal. You can use the get.py
script alongside the reset.py
script to make sure that value is changing as expected:
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.