From 29530f57b8aeca2495b2d1006ea204593c63184c Mon Sep 17 00:00:00 2001 From: Albertov19 Date: Thu, 31 Aug 2023 20:09:34 +0200 Subject: [PATCH 01/11] final draft --- .snippets/code/ethers-contract-local/reset.js | 38 ------ .../deploy.js | 22 +++- .../get.js | 18 ++- .../increment.js | 27 +++-- .snippets/code/ethers-js-contract/reset.js | 51 ++++++++ .snippets/code/ethers-js-tx/balances.js | 41 +++++++ .snippets/code/ethers-js-tx/transaction.js | 49 ++++++++ .snippets/code/ethers-tx-local/balances.js | 32 ----- .snippets/code/ethers-tx-local/transaction.js | 40 ------- .snippets/code/web3-contract-local/deploy.js | 10 +- builders/build/eth-api/libraries/ethersjs.md | 111 ++++++++++++------ builders/build/eth-api/libraries/web3js.md | 6 +- 12 files changed, 271 insertions(+), 174 deletions(-) delete mode 100644 .snippets/code/ethers-contract-local/reset.js rename .snippets/code/{ethers-contract-local => ethers-js-contract}/deploy.js (67%) rename .snippets/code/{ethers-contract-local => ethers-js-contract}/get.js (62%) rename .snippets/code/{ethers-contract-local => ethers-js-contract}/increment.js (55%) create mode 100644 .snippets/code/ethers-js-contract/reset.js create mode 100644 .snippets/code/ethers-js-tx/balances.js create mode 100644 .snippets/code/ethers-js-tx/transaction.js delete mode 100644 .snippets/code/ethers-tx-local/balances.js delete mode 100644 .snippets/code/ethers-tx-local/transaction.js diff --git a/.snippets/code/ethers-contract-local/reset.js b/.snippets/code/ethers-contract-local/reset.js deleted file mode 100644 index 749de016e..000000000 --- a/.snippets/code/ethers-contract-local/reset.js +++ /dev/null @@ -1,38 +0,0 @@ -const ethers = require('ethers'); -const { abi } = require('./compile'); - -const providerRPC = { - development: { - name: 'moonbeam-development', - rpc: 'http://localhost:9944', - chainId: 1281, - }, - moonbase: { - name: 'moonbase-alpha', - rpc: 'https://rpc.api.moonbase.moonbeam.network', - chainId: 1287, - }, -}; -const provider = new ethers.JsonRpcProvider(providerRPC.development.rpc, { - chainId: providerRPC.development.chainId, - name: providerRPC.development.name, -}); // Change to correct network - -const account_from = { - privateKey: 'YOUR-PRIVATE-KEY-HERE', -}; -const contractAddress = 'CONTRACT-ADDRESS-HERE'; - -let wallet = new ethers.Wallet(account_from.privateKey, provider); - -const incrementer = new ethers.Contract(contractAddress, abi, wallet); -const reset = async () => { - console.log(`Calling the reset function in contract at address: ${contractAddress}`); - - const createReceipt = await incrementer.reset(); - await createReceipt.wait(); - - console.log(`Tx successful with hash: ${createReceipt.hash}`); -}; - -reset(); \ No newline at end of file diff --git a/.snippets/code/ethers-contract-local/deploy.js b/.snippets/code/ethers-js-contract/deploy.js similarity index 67% rename from .snippets/code/ethers-contract-local/deploy.js rename to .snippets/code/ethers-js-contract/deploy.js index 8ad6a8e62..124850fae 100644 --- a/.snippets/code/ethers-contract-local/deploy.js +++ b/.snippets/code/ethers-js-contract/deploy.js @@ -1,6 +1,8 @@ +// Import ethers and compile const ethers = require('ethers'); const contractFile = require('./compile'); +// Define network configurations const providerRPC = { development: { name: 'moonbeam-development', @@ -13,28 +15,36 @@ const providerRPC = { chainId: 1287, }, }; -const provider = new ethers.JsonRpcProvider(providerRPC.development.rpc, { - chainId: providerRPC.development.chainId, - name: providerRPC.development.name, + +// Create ethers provider +const provider = new ethers.JsonRpcProvider(providerRPC.moonbase.rpc, { + chainId: providerRPC.moonbase.chainId, + name: providerRPC.moonbase.name, }); // Change to correct network +// Define accounts and wallet const account_from = { privateKey: 'YOUR-PRIVATE-KEY-HERE', }; +let wallet = new ethers.Wallet(account_from.privateKey, provider); + +// Load contract info const bytecode = contractFile.evm.bytecode.object; const abi = contractFile.abi; -let wallet = new ethers.Wallet(account_from.privateKey, provider); - +// Create contract instance with signer const incrementer = new ethers.ContractFactory(abi, bytecode, wallet); +// Create deploy function const deploy = async () => { console.log(`Attempting to deploy from account: ${wallet.address}`); + // Send tx (initial value set to 5) and wait for receipt const contract = await incrementer.deploy(5); const txReceipt = await contract.deploymentTransaction().wait(); console.log(`Contract deployed at address: ${txReceipt.contractAddress}`); }; -deploy(); \ No newline at end of file +// Call the deploy function +deploy(); diff --git a/.snippets/code/ethers-contract-local/get.js b/.snippets/code/ethers-js-contract/get.js similarity index 62% rename from .snippets/code/ethers-contract-local/get.js rename to .snippets/code/ethers-js-contract/get.js index 776eecb6a..8973006f8 100644 --- a/.snippets/code/ethers-contract-local/get.js +++ b/.snippets/code/ethers-js-contract/get.js @@ -1,6 +1,8 @@ +// Import ethers and compile const ethers = require('ethers'); const { abi } = require('./compile'); +// Define network configurations const providerRPC = { development: { name: 'moonbeam-development', @@ -13,21 +15,27 @@ const providerRPC = { chainId: 1287, }, }; -const provider = new ethers.JsonRpcProvider(providerRPC.development.rpc, { - chainId: providerRPC.development.chainId, - name: providerRPC.development.name, + +const provider = new ethers.JsonRpcProvider(providerRPC.moonbase.rpc, { + chainId: providerRPC.moonbase.chainId, + name: providerRPC.moonbase.name, }); // Change to correct network -const contractAddress = 'CONTRACT-ADDRESS-HERE'; +// Contract address variable +const contractAddress = 'CONTRACT_ADDRESS_HERE'; +// Create contract instance const incrementer = new ethers.Contract(contractAddress, abi, provider); +// Create get function const get = async () => { console.log(`Making a call to contract at address: ${contractAddress}`); + // Call contract const data = await incrementer.number(); console.log(`The current number stored is: ${data}`); }; -get(); \ No newline at end of file +// Call get function +get(); diff --git a/.snippets/code/ethers-contract-local/increment.js b/.snippets/code/ethers-js-contract/increment.js similarity index 55% rename from .snippets/code/ethers-contract-local/increment.js rename to .snippets/code/ethers-js-contract/increment.js index 8baeee3c0..6a6870dac 100644 --- a/.snippets/code/ethers-contract-local/increment.js +++ b/.snippets/code/ethers-js-contract/increment.js @@ -1,6 +1,8 @@ +// Import ethers and compile const ethers = require('ethers'); const { abi } = require('./compile'); +// Define network configurations const providerRPC = { development: { name: 'moonbeam-development', @@ -13,29 +15,38 @@ const providerRPC = { chainId: 1287, }, }; -const provider = new ethers.JsonRpcProvider(providerRPC.development.rpc, { - chainId: providerRPC.development.chainId, - name: providerRPC.development.name, + +// Create ethers provider +const provider = new ethers.JsonRpcProvider(providerRPC.moonbase.rpc, { + chainId: providerRPC.moonbase.chainId, + name: providerRPC.moonbase.name, }); // Change to correct network -const account_from = { - privateKey: 'YOUR-PRIVATE-KEY-HERE', +// Create variables +const accountFrom = { + privateKey: 'YOUR_PRIVATE_KEY_HERE', }; -const contractAddress = 'CONTRACT-ADDRESS-HERE'; +const contractAddress = 'CONTRACT_ADDRESS_HERE'; const _value = 3; -let wallet = new ethers.Wallet(account_from.privateKey, provider); +// Create wallet +let wallet = new ethers.Wallet(accountFrom.privateKey, provider); +// Create contract instance with signer const incrementer = new ethers.Contract(contractAddress, abi, wallet); + +// Create reset function const increment = async () => { console.log( `Calling the increment by ${_value} function in contract at address: ${contractAddress}` ); + // Sign and send tx and wait for receipt const createReceipt = await incrementer.increment(_value); await createReceipt.wait(); console.log(`Tx successful with hash: ${createReceipt.hash}`); }; -increment(); \ No newline at end of file +// Call the reset function +increment(); diff --git a/.snippets/code/ethers-js-contract/reset.js b/.snippets/code/ethers-js-contract/reset.js new file mode 100644 index 000000000..8a043b14b --- /dev/null +++ b/.snippets/code/ethers-js-contract/reset.js @@ -0,0 +1,51 @@ +// Import ethers and compile +const ethers = require('ethers'); +const { abi } = require('./compile'); + +// Define network configurations +const providerRPC = { + development: { + name: 'moonbeam-development', + rpc: 'http://localhost:9944', + chainId: 1281, + }, + moonbase: { + name: 'moonbase-alpha', + rpc: 'https://rpc.api.moonbase.moonbeam.network', + chainId: 1287, + }, +}; + +// Create ethers provider +const provider = new ethers.JsonRpcProvider(providerRPC.moonbase.rpc, { + chainId: providerRPC.moonbase.chainId, + name: providerRPC.moonbase.name, +}); // Change to correct network + +// Create variables +const accountFrom = { + privateKey: 'YOUR_PRIVATE_KEY_HERE', +}; +const contractAddress = 'CONTRACT_ADDRESS_HERE'; + +// Create wallet +let wallet = new ethers.Wallet(accountFrom.privateKey, provider); + +// Create contract instance with signer +const incrementer = new ethers.Contract(contractAddress, abi, wallet); + +// Create reset function +const reset = async () => { + console.log( + `Calling the reset function in contract at address: ${contractAddress}` + ); + + // Sign and send tx and wait for receipt + const createReceipt = await incrementer.reset(); + await createReceipt.wait(); + + console.log(`Tx successful with hash: ${createReceipt.hash}`); +}; + +// Call the reset function +reset(); diff --git a/.snippets/code/ethers-js-tx/balances.js b/.snippets/code/ethers-js-tx/balances.js new file mode 100644 index 000000000..3c62845d9 --- /dev/null +++ b/.snippets/code/ethers-js-tx/balances.js @@ -0,0 +1,41 @@ +// Import ethers +const ethers = require('ethers'); + +// Define network configurations +const providerRPC = { + development: { + name: 'moonbeam-development', + rpc: 'http://localhost:9944', + chainId: 1281, + }, + moonbase: { + name: 'moonbase-alpha', + rpc: 'https://rpc.api.moonbase.moonbeam.network', + chainId: 1287, + }, +}; + +// Create ethers provider +const provider = new ethers.JsonRpcProvider(providerRPC.moonbase.rpc, { + chainId: providerRPC.moonbase.chainId, + name: providerRPC.moonbase.name, +}); // Change to correct network + +// Define addresses +const addressFrom = 'ADDRESS_FROM_HERE'; +const addressTo = 'ADDRESS_TO_HERE'; + +// Create balances function +const balances = async () => { + // 6. Fetch balances + const balanceFrom = ethers.formatEther( + await provider.getBalance(addressFrom) + ); + const balanceTo = ethers.formatEther(await provider.getBalance(addressTo)); + + console.log(`The balance of ${addressFrom} is: ${balanceFrom} DEV`); + console.log(`The balance of ${addressTo} is: ${balanceTo} DEV`); +}; + +// Call the balances function +balances(); diff --git a/.snippets/code/ethers-js-tx/transaction.js b/.snippets/code/ethers-js-tx/transaction.js new file mode 100644 index 000000000..b45499b96 --- /dev/null +++ b/.snippets/code/ethers-js-tx/transaction.js @@ -0,0 +1,49 @@ +// Import ethers +const ethers = require('ethers'); + +// Define network configurations +const providerRPC = { + development: { + name: 'moonbeam-development', + rpc: 'http://localhost:9944', + chainId: 1281, + }, + moonbase: { + name: 'moonbase-alpha', + rpc: 'https://rpc.api.moonbase.moonbeam.network', + chainId: 1287, + }, +}; +// Create ethers provider +const provider = new ethers.JsonRpcProvider(providerRPC.moonbase.rpc, { + chainId: providerRPC.moonbase.chainId, + name: providerRPC.moonbase.name, +}); // Change to correct network + +// Define accounts and wallet +const accountFrom = { + privateKey: 'YOUR_PRIVATE_KEY_HERE', +}; +const addressTo = 'ADDRESS_TO_HERE'; +const wallet = new ethers.Wallet(accountFrom.privateKey, provider); + +// Create send function +const send = async () => { + console.log( + `Attempting to send transaction from ${wallet.address} to ${addressTo}` + ); + + // Create transaction + const tx = { + to: addressTo, + value: ethers.parseEther('1'), + }; + + // Send transaction and get hash + const createReceipt = await wallet.sendTransaction(tx); + await createReceipt.wait(); + console.log(`Transaction successful with hash: ${createReceipt.hash}`); +}; + +// Call the send function +send(); diff --git a/.snippets/code/ethers-tx-local/balances.js b/.snippets/code/ethers-tx-local/balances.js deleted file mode 100644 index 7195e510e..000000000 --- a/.snippets/code/ethers-tx-local/balances.js +++ /dev/null @@ -1,32 +0,0 @@ -const ethers = require('ethers'); - -const providerRPC = { - development: { - name: 'moonbeam-development', - rpc: 'http://localhost:9944', - chainId: 1281, - }, - moonbase: { - name: 'moonbase-alpha', - rpc: 'https://rpc.api.moonbase.moonbeam.network', - chainId: 1287, - }, -}; -const provider = new ethers.JsonRpcProvider(providerRPC.development.rpc, { - chainId: providerRPC.development.chainId, - name: providerRPC.development.name, -}); // Change to correct network - -const addressFrom = 'ADDRESS-FROM-HERE'; -const addressTo = 'ADDRESS-TO-HERE'; - -const balances = async () => { - const balanceFrom = ethers.formatEther(await provider.getBalance(addressFrom)); - - const balanceTo = ethers.formatEther(await provider.getBalance(addressTo)); - - console.log(`The balance of ${addressFrom} is: ${balanceFrom} ETH`); - console.log(`The balance of ${addressTo} is: ${balanceTo} ETH`); -}; - -balances(); \ No newline at end of file diff --git a/.snippets/code/ethers-tx-local/transaction.js b/.snippets/code/ethers-tx-local/transaction.js deleted file mode 100644 index 5b41eb01d..000000000 --- a/.snippets/code/ethers-tx-local/transaction.js +++ /dev/null @@ -1,40 +0,0 @@ -const ethers = require('ethers'); - -const providerRPC = { - development: { - name: 'moonbeam-development', - rpc: 'http://localhost:9944', - chainId: 1281, - }, - moonbase: { - name: 'moonbase-alpha', - rpc: 'https://rpc.api.moonbase.moonbeam.network', - chainId: 1287, - }, -}; -const provider = new ethers.JsonRpcProvider(providerRPC.development.rpc, { - chainId: providerRPC.development.chainId, - name: providerRPC.development.name, -}); // Change to correct network - -const account_from = { - privateKey: 'YOUR-PRIVATE-KEY-HERE', -}; -const addressTo = 'ADDRESS-TO-HERE'; - -let wallet = new ethers.Wallet(account_from.privateKey, provider); - -const send = async () => { - console.log(`Attempting to send transaction from ${wallet.address} to ${addressTo}`); - - const tx = { - to: addressTo, - value: ethers.parseEther('1'), - }; - - const createReceipt = await wallet.sendTransaction(tx); - await createReceipt.wait(); - console.log(`Transaction successful with hash: ${createReceipt.hash}`); -}; - -send(); \ No newline at end of file diff --git a/.snippets/code/web3-contract-local/deploy.js b/.snippets/code/web3-contract-local/deploy.js index b04780b7d..226a4e862 100644 --- a/.snippets/code/web3-contract-local/deploy.js +++ b/.snippets/code/web3-contract-local/deploy.js @@ -7,7 +7,7 @@ const providerRPC = { }; const web3 = new Web3(providerRPC.development); // Change to correct network -const account_from = { +const accountFrom = { privateKey: 'YOUR-PRIVATE-KEY-HERE', address: 'PUBLIC-ADDRESS-OF-PK-HERE', }; @@ -15,7 +15,7 @@ const bytecode = contractFile.evm.bytecode.object; const abi = contractFile.abi; const deploy = async () => { - console.log(`Attempting to deploy from account ${account_from.address}`); + console.log(`Attempting to deploy from account ${accountFrom.address}`); const incrementer = new web3.eth.Contract(abi); @@ -32,8 +32,10 @@ const deploy = async () => { account_from.privateKey ); - const createReceipt = await web3.eth.sendSignedTransaction(createTransaction.rawTransaction); + const createReceipt = await web3.eth.sendSignedTransaction( + createTransaction.rawTransaction + ); console.log(`Contract deployed at address: ${createReceipt.contractAddress}`); }; -deploy(); \ No newline at end of file +deploy(); diff --git a/builders/build/eth-api/libraries/ethersjs.md b/builders/build/eth-api/libraries/ethersjs.md index 776e391e6..eb744340c 100644 --- a/builders/build/eth-api/libraries/ethersjs.md +++ b/builders/build/eth-api/libraries/ethersjs.md @@ -9,7 +9,7 @@ description: Follow this tutorial to learn how to use the Ethereum EtherJS Libra ## Introduction {: #introduction } -The [Ethers.js](https://docs.ethers.io/){target=_blank} library provides a set of tools to interact with Ethereum Nodes with JavaScript, similar to Web3.js. Moonbeam 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 Moonbeam node as if they were doing so on Ethereum. You can read more about Ethers.js on this [blog post](https://medium.com/l4-media/announcing-ethers-js-a-web3-alternative-6f134fdd06f3){target=_blank}. +The [Ethers.js](https://docs.ethers.io/){target=_blank} library provides a set of tools to interact with Ethereum Nodes with JavaScript, similar to Web3.js. Moonbeam 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 Moonbeam node as if they were doing so on Ethereum. For more information on Ethers.js, check their [documentation site](https://docs.ethers.org/v5/){target=_blank}. In this guide, you'll learn how to use the Ethers.js library to send a transaction and deploy a contract on Moonbase Alpha. This guide can be adapted for [Moonbeam](/builders/get-started/networks/moonbeam/){target=_blank}, [Moonriver](/builders/get-started/networks/moonriver/){target=_blank}, or a [Moonbeam development node](/builders/get-started/networks/moonbeam-dev/){target=_blank}. @@ -25,19 +25,27 @@ For the examples in this guide, you will need to have the following: !!! note --8<-- 'text/common/assumes-mac-or-ubuntu-env.md' -## Create a JavaScript Project {: #create-a-javascript-project } +## Installing Ethers.js {: #install-ethersjs } -To get started, you can create a directory to store all of the files you'll be creating throughout this guide: +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: ``` -mkdir ethers-examples && cd ethers-examples +mkdir ethers-examples && cd ethers-examples && npm init --y ``` 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: -``` -npm install ethers solc@0.8.0 -``` +=== "npm" + + ```bash + npm install ethers solc@0.8.0 + ``` + +=== "yarn" + + ```bash + yarn add ethers solc@0.8.0 + ``` ## Setting up the Ethers Provider {: #setting-up-the-ethers-provider } @@ -147,6 +155,8 @@ To create a provider, you can take the following steps: ); ``` +Save this code snippet as you'll need it for the scripts that are used in the following sections. + ## Send a Transaction {: #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. @@ -183,15 +193,19 @@ const balances = async () => { const balanceFrom = ethers.formatEther(await provider.getBalance(addressFrom)); const balanceTo = ethers.formatEther(await provider.getBalance(addressTo)); - console.log(`The balance of ${addressFrom} is: ${balanceFrom} ETH`); - console.log(`The balance of ${addressTo} is: ${balanceTo} ETH`); + console.log(`The balance of ${addressFrom} is: ${balanceFrom} DEV`); + console.log(`The balance of ${addressTo} is: ${balanceTo} DEV`); }; // 5. Call the balances function balances(); ``` -You can view the [complete script on GitHub](https://raw.githubusercontent.com/moonbeam-foundation/moonbeam-docs/master/.snippets/code/ethers-tx-local/balances.js){target=_blank}. +??? code "View the complete script" + + ```js + --8<-- 'code/ethers-js-tx/balances.js' + ``` To run the script and fetch the account balances, you can run the following command: @@ -199,7 +213,7 @@ To run the script and fetch the account balances, you can run the following comm node balances.js ``` -If successful, the balances for the origin and receiving address will be displayed in your terminal in ETH. +If successful, the balances for the origin and receiving address will be displayed in your terminal in DEV. ### Send Transaction Script {: #send-transaction-script } @@ -224,13 +238,13 @@ Next, you will create the script for this file and complete the following steps: // {...} // 2. Create account variables -const account_from = { - privateKey: 'YOUR-PRIVATE-KEY-HERE', +const accountFrom = { + privateKey: 'YOUR_PRIVATE_KEY_HERE', }; -const addressTo = 'ADDRESS-TO-HERE'; +const addressTo = 'ADDRESS_TO_HERE'; // 3. Create wallet -let wallet = new ethers.Wallet(account_from.privateKey, provider); +let wallet = new ethers.Wallet(accountFrom.privateKey, provider); // 4. Create send function const send = async () => { @@ -252,7 +266,11 @@ const send = async () => { send(); ``` -You can view the [complete script on GitHub](https://raw.githubusercontent.com/moonbeam-foundation/moonbeam-docs/master/.snippets/code/ethers-tx-local/transaction.js){target=_blank}. +??? code "View the complete script" + + ```js + --8<-- 'code/ethers-js-tx/transaction.js' + ``` To run the script, you can run the following command in your terminal: @@ -287,8 +305,8 @@ Next, you will create the script for this file and complete the following steps: 1. Import the contract file from `compile.js` 2. [Set up the Ethers provider](#setting-up-the-ethers-provider) 3. 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** -4. Save the `bytecode` and `abi` for the compiled contract -5. Create a wallet using the `privateKey` and `provider` from the previous steps. The wallet instance is used to sign transactions +3. Create a wallet using the `privateKey` and `provider` from the previous steps. The wallet instance is used to sign transactions +5. Load the contract `bytecode` and `abi` for the compiled contract 6. Create a contract instance with signer using the `ethers.ContractFactory` function, providing the `abi`, `bytecode`, and `wallet` as parameters 7. Create the asynchronous `deploy` function that will be used to deploy the contract 8. 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 @@ -302,17 +320,17 @@ const contractFile = require('./compile'); // {...} // 3. Create account variables -const account_from = { - privateKey: 'YOUR-PRIVATE-KEY-HERE', +const accountFrom = { + privateKey: 'YOUR_PRIVATE_KEY_HERE', }; -// 4. Save the bytecode and ABI +// 4. Create wallet +let wallet = new ethers.Wallet(accountFrom.privateKey, provider); + +// 5. Load contract information const bytecode = contractFile.evm.bytecode.object; const abi = contractFile.abi; -// 5. Create wallet -let wallet = new ethers.Wallet(account_from.privateKey, provider); - // 6. Create contract instance with signer const incrementer = new ethers.ContractFactory(abi, bytecode, wallet); @@ -331,7 +349,11 @@ const deploy = async () => { deploy(); ``` -You can view the [complete script on GitHub](https://raw.githubusercontent.com/moonbeam-foundation/moonbeam-docs/master/.snippets/code/ethers-contract-local/deploy.js){target=_blank}. +??? code "View the complete script" + + ```js + --8<-- 'code/ethers-js-contract/deploy.js' + ``` To run the script, you can enter the following command into your terminal: @@ -343,7 +365,6 @@ If successful, the contract's address will be displayed in the terminal. ![Deploy Contract Etherjs](/images/builders/build/eth-api/libraries/ethers/ethers-2.png) - ### Read Contract Data (Call Methods) {: #read-contract-data } 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. @@ -372,7 +393,7 @@ const { abi } = require('./compile'); // {...} // 3. Contract address variable -const contractAddress = 'CONTRACT-ADDRESS-HERE'; +const contractAddress = 'CONTRACT_ADDRESS_HERE'; // 4. Create contract instance const incrementer = new ethers.Contract(contractAddress, abi, provider); @@ -391,7 +412,11 @@ const get = async () => { get(); ``` -You can view the [complete script on GitHub](https://raw.githubusercontent.com/moonbeam-foundation/moonbeam-docs/master/.snippets/code/ethers-contract-local/get.js){target=_blank}. +??? code "View the complete script" + + ```js + --8<-- 'code/ethers-js-contract/get.js' + ``` To run the script, you can enter the following command in your terminal: @@ -428,14 +453,14 @@ const { abi } = require('./compile'); // {...} // 3. Create variables -const account_from = { - privateKey: 'YOUR-PRIVATE-KEY-HERE', +const accountFrom = { + privateKey: 'YOUR_PRIVATE_KEY_HERE', }; -const contractAddress = 'CONTRACT-ADDRESS-HERE'; +const contractAddress = 'CONTRACT_ADDRESS_HERE'; const _value = 3; // 4. Create wallet -let wallet = new ethers.Wallet(account_from.privateKey, provider); +let wallet = new ethers.Wallet(accountFrom.privateKey, provider); // 5. Create contract instance with signer const incrementer = new ethers.Contract(contractAddress, abi, wallet); @@ -457,7 +482,12 @@ const increment = async () => { increment(); ``` -You can view the [complete script on GitHub](https://raw.githubusercontent.com/moonbeam-foundation/moonbeam-docs/master/.snippets/code/ethers-contract-local/increment.js){target=_blank}. +??? code "View the complete script" + + ```js + --8<-- 'code/ethers-js-contract/increment.js' + ``` + To run the script, you can enter the following command in your terminal: @@ -488,13 +518,13 @@ const { abi } = require('./compile'); // {...} // 3. Create variables -const account_from = { - privateKey: 'YOUR-PRIVATE-KEY-HERE', +const accountFrom = { + privateKey: 'YOUR_PRIVATE_KEY_HERE', }; -const contractAddress = 'CONTRACT-ADDRESS-HERE'; +const contractAddress = 'CONTRACT_ADDRESS_HERE'; // 4. Create wallet -let wallet = new ethers.Wallet(account_from.privateKey, provider); +let wallet = new ethers.Wallet(accountFrom.privateKey, provider); // 5. Create contract instance with signer const incrementer = new ethers.Contract(contractAddress, abi, wallet); @@ -514,7 +544,12 @@ const reset = async () => { reset(); ``` -You can view the [complete script on GitHub](https://raw.githubusercontent.com/moonbeam-foundation/moonbeam-docs/master/.snippets/code/ethers-contract-local/reset.js){target=_blank}. +??? code "View the complete script" + + ```js + --8<-- 'code/ethers-js-contract/reset.js' + ``` + To run the script, you can enter the following command in your terminal: diff --git a/builders/build/eth-api/libraries/web3js.md b/builders/build/eth-api/libraries/web3js.md index d06e1c3ae..33624e0c4 100644 --- a/builders/build/eth-api/libraries/web3js.md +++ b/builders/build/eth-api/libraries/web3js.md @@ -25,12 +25,12 @@ For the examples in this guide, you will need to have the following: !!! note --8<-- 'text/common/assumes-mac-or-ubuntu-env.md' -## Create a JavaScript Project {: #create-a-javascript-project } +## Installing Web3.js {: #install-web3js } -To get started, you can create a directory to store all of the files you'll be creating throughout this guide: +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: ``` -mkdir web3-examples && cd web3-examples +mkdir web3-examples && cd web3-examples && npm init --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: From 1351b74dc8648f91a75027648c0a4b65395e5b2a Mon Sep 17 00:00:00 2001 From: Kevin Neilson Date: Thu, 31 Aug 2023 17:07:36 -0400 Subject: [PATCH 02/11] update web3js page for new format view the complete script --- builders/build/eth-api/libraries/web3js.md | 36 ++++++++++++++++++---- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/builders/build/eth-api/libraries/web3js.md b/builders/build/eth-api/libraries/web3js.md index 8a738f6d0..0d52fd925 100644 --- a/builders/build/eth-api/libraries/web3js.md +++ b/builders/build/eth-api/libraries/web3js.md @@ -124,7 +124,11 @@ const balances = async () => { balances(); ``` -You can view the [complete script on GitHub](https://raw.githubusercontent.com/moonbeam-foundation/moonbeam-docs/master/.snippets/code/web3-tx-local/balances.js){target=_blank}. +??? code "View the complete script" + + ```js + --8<-- 'code/web3-tx-local/balances.js' + ``` To run the script and fetch the account balances, you can run the following command: @@ -185,7 +189,11 @@ const send = async () => { send(); ``` -You can view the [complete script on GitHub](https://raw.githubusercontent.com/moonbeam-foundation/moonbeam-docs/master/.snippets/code/web3-tx-local/transaction.js){target=_blank}. +??? code "View the complete script" + + ```js + --8<-- 'code/web3-tx-local/transaction.js' + ``` To run the script, you can run the following command in your terminal: @@ -276,7 +284,11 @@ const deploy = async () => { deploy(); ``` -You can view the [complete script on GitHub](https://raw.githubusercontent.com/moonbeam-foundation/moonbeam-docs/master/.snippets/code/web3-contract-local/deploy.js){target=_blank}. +??? code "View the complete script" + + ```js + --8<-- 'code/web3-contract-local/deploy.js' + ``` To run the script, you can enter the following command into your terminal: @@ -335,7 +347,11 @@ const get = async () => { get(); ``` -You can view the [complete script on GitHub](https://raw.githubusercontent.com/moonbeam-foundation/moonbeam-docs/master/.snippets/code/web3-contract-local/get.js){target=_blank}. +??? code "View the complete script" + + ```js + --8<-- 'code/web3-contract-local/get.js' + ``` To run the script, you can enter the following command in your terminal: @@ -410,7 +426,11 @@ const increment = async () => { increment(); ``` -You can view the [complete script on GitHub](https://raw.githubusercontent.com/moonbeam-foundation/moonbeam-docs/master/.snippets/code/web3-contract-local/increment.js){target=_blank}. +??? code "View the complete script" + + ```js + --8<-- 'code/web3-contract-local/increment.js' + ``` To run the script, you can enter the following command in your terminal: @@ -476,7 +496,11 @@ const reset = async () => { reset(); ``` -You can view the [complete script on GitHub](https://raw.githubusercontent.com/moonbeam-foundation/moonbeam-docs/master/.snippets/code/web3-contract-local/reset.js){target=_blank}. +??? code "View the complete script" + + ```js + --8<-- 'code/web3-contract-local/reset.js' + ``` To run the script, you can enter the following command in your terminal: From 3e1b48c9f56ad3302b9c794f93f1905ed5e5262b Mon Sep 17 00:00:00 2001 From: Kevin Neilson Date: Fri, 1 Sep 2023 10:27:29 -0400 Subject: [PATCH 03/11] formatting docs style --- builders/build/eth-api/libraries/web3js.md | 24 +++++++++++----------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/builders/build/eth-api/libraries/web3js.md b/builders/build/eth-api/libraries/web3js.md index 0d52fd925..8334aafac 100644 --- a/builders/build/eth-api/libraries/web3js.md +++ b/builders/build/eth-api/libraries/web3js.md @@ -107,8 +107,8 @@ Next, you will create the script for this file and complete the following steps: // {...} // 2. Create address variables -const addressFrom = 'ADDRESS-FROM-HERE'; -const addressTo = 'ADDRESS-TO-HERE'; +const addressFrom = 'ADDRESS_FROM_HERE'; +const addressTo = 'ADDRESS_TO_HERE'; // 3. Create balances function const balances = async () => { @@ -161,10 +161,10 @@ Next, you will create the script for this file and complete the following steps: // 2. Create account variables const accountFrom = { - privateKey: 'YOUR-PRIVATE-KEY-HERE', - address: 'PUBLIC-ADDRESS-OF-PK-HERE', + privateKey: 'YOUR_PRIVATE_KEY_HERE', + address: 'PUBLIC_ADDRESS_OF_PK_HERE', }; -const addressTo = 'ADDRESS-TO-HERE'; // Change addressTo +const addressTo = 'ADDRESS_TO_HERE'; // Change addressTo // 3. Create send function const send = async () => { @@ -245,8 +245,8 @@ const contractFile = require('./compile'); // 3. Create address variables const accountFrom = { - privateKey: 'YOUR-PRIVATE-KEY-HERE', - address: 'PUBLIC-ADDRESS-OF-PK-HERE', + privateKey: 'YOUR_PRIVATE_KEY_HERE', + address: 'PUBLIC_ADDRESS_OF_PK_HERE', }; // 4. Get the bytecode and API @@ -328,7 +328,7 @@ const { abi } = require('./compile'); // {...} // 3. Create address variables -const contractAddress = 'CONTRACT-ADDRESS-HERE'; +const contractAddress = 'CONTRACT_ADDRESS_HERE'; // 4. Create contract instance const incrementer = new web3.eth.Contract(abi, contractAddress); @@ -390,9 +390,9 @@ const { abi } = require('./compile'); // 3. Create variables const accountFrom = { - privateKey: 'YOUR-PRIVATE-KEY-HERE', + privateKey: 'YOUR_PRIVATE_KEY_HERE', }; -const contractAddress = 'CONTRACT-ADDRESS-HERE'; +const contractAddress = 'CONTRACT_ADDRESS_HERE'; const _value = 3; // 4. Create contract instance @@ -463,9 +463,9 @@ const { abi } = require('./compile'); // 3. Create variables const accountFrom = { - privateKey: 'YOUR-PRIVATE-KEY-HERE', + privateKey: 'YOUR_PRIVATE_KEY_HERE', }; -const contractAddress = 'CONTRACT-ADDRESS-HERE'; +const contractAddress = 'CONTRACT_ADDRESS_HERE'; // 4. Create Contract Instance const incrementer = new web3.eth.Contract(abi, contractAddress); From 567c1c86e1016a61b8f4330f025c4c6f35ee0f76 Mon Sep 17 00:00:00 2001 From: Kevin Neilson Date: Fri, 1 Sep 2023 10:57:51 -0400 Subject: [PATCH 04/11] add comments etc --- .snippets/code/web3-contract-local/deploy.js | 16 ++++++++++++++-- .snippets/code/web3-contract-local/get.js | 9 ++++++++- .snippets/code/web3-contract-local/increment.js | 16 ++++++++++++++-- .snippets/code/web3-contract-local/reset.js | 14 ++++++++++++-- .snippets/code/web3-tx-local/balances.js | 9 +++++++-- .snippets/code/web3-tx-local/transaction.js | 12 +++++++++--- builders/build/eth-api/libraries/web3js.md | 4 ++-- 7 files changed, 66 insertions(+), 14 deletions(-) diff --git a/.snippets/code/web3-contract-local/deploy.js b/.snippets/code/web3-contract-local/deploy.js index b04780b7d..39f6c4a20 100644 --- a/.snippets/code/web3-contract-local/deploy.js +++ b/.snippets/code/web3-contract-local/deploy.js @@ -1,29 +1,39 @@ +// 1. Import web3 and the contract file + const Web3 = require('web3'); const contractFile = require('./compile'); +// 2. Add the Web3 provider logic here: const providerRPC = { development: 'http://localhost:9944', moonbase: 'https://rpc.api.moonbase.moonbeam.network', }; const web3 = new Web3(providerRPC.development); // Change to correct network +// 3. Create address variables const account_from = { - privateKey: 'YOUR-PRIVATE-KEY-HERE', - address: 'PUBLIC-ADDRESS-OF-PK-HERE', + privateKey: 'YOUR_PRIVATE_KEY_HERE', + address: 'PUBLIC_ADDRESS_OF_PK_HERE', }; + +// 4. Get the bytecode and API const bytecode = contractFile.evm.bytecode.object; const abi = contractFile.abi; +// 5. Create deploy function const deploy = async () => { console.log(`Attempting to deploy from account ${account_from.address}`); + // 6. Create contract instance const incrementer = new web3.eth.Contract(abi); + // 7. Create constructor tx const incrementerTx = incrementer.deploy({ data: bytecode, arguments: [5], }); + // 8. Sign transacation and send const createTransaction = await web3.eth.accounts.signTransaction( { data: incrementerTx.encodeABI(), @@ -32,8 +42,10 @@ const deploy = async () => { account_from.privateKey ); + // 9. Send tx and wait for receipt const createReceipt = await web3.eth.sendSignedTransaction(createTransaction.rawTransaction); console.log(`Contract deployed at address: ${createReceipt.contractAddress}`); }; +// 10. Call deploy function deploy(); \ No newline at end of file diff --git a/.snippets/code/web3-contract-local/get.js b/.snippets/code/web3-contract-local/get.js index a2488c406..1621b0b10 100644 --- a/.snippets/code/web3-contract-local/get.js +++ b/.snippets/code/web3-contract-local/get.js @@ -1,22 +1,29 @@ +// 1. Import Web3js and the contract abi const Web3 = require('web3'); const { abi } = require('./compile'); +// 2. Add the Web3 provider logic here: const providerRPC = { development: 'http://localhost:9944', moonbase: 'https://rpc.api.moonbase.moonbeam.network', }; const web3 = new Web3(providerRPC.development); // Change to correct network -const contractAddress = 'CONTRACT-ADDRESS-HERE'; +// 3. Create address variables +const contractAddress = 'CONTRACT_ADDRESS_HERE'; +// 4. Create contract instance const incrementer = new web3.eth.Contract(abi, contractAddress); +// 5. Create get function const get = async () => { console.log(`Making a call to contract at address: ${contractAddress}`); + // 6. Call contract const data = await incrementer.methods.number().call(); console.log(`The current number stored is: ${data}`); }; +// 7. Call get function get(); \ No newline at end of file diff --git a/.snippets/code/web3-contract-local/increment.js b/.snippets/code/web3-contract-local/increment.js index 637021934..bc5659a36 100644 --- a/.snippets/code/web3-contract-local/increment.js +++ b/.snippets/code/web3-contract-local/increment.js @@ -1,25 +1,35 @@ +// 1. Import Web3js and the contract abi + const Web3 = require('web3'); const { abi } = require('./compile'); +// 2. Add the Web3 provider logic here: const providerRPC = { development: 'http://localhost:9944', moonbase: 'https://rpc.api.moonbase.moonbeam.network', }; const web3 = new Web3(providerRPC.development); //Change to correct network +// 3. Create variables const account_from = { - privateKey: 'YOUR-PRIVATE-KEY-HERE', + privateKey: 'YOUR_PRIVATE_KEY_HERE', }; -const contractAddress = 'CONTRACT-ADDRESS-HERE'; +const contractAddress = 'CONTRACT_ADDRESS_HERE'; const _value = 3; +// 4. Create contract instance const incrementer = new web3.eth.Contract(abi, contractAddress); + +// 5. Build increment tx const incrementTx = incrementer.methods.increment(_value); + +// 6. Create increment function const increment = async () => { console.log( `Calling the increment by ${_value} function in contract at address: ${contractAddress}` ); + // 7. Prepare and Sign Tx with PK const createTransaction = await web3.eth.accounts.signTransaction( { to: contractAddress, @@ -29,8 +39,10 @@ const increment = async () => { account_from.privateKey ); + // 8. Send Tx and Wait for Receipt const createReceipt = await web3.eth.sendSignedTransaction(createTransaction.rawTransaction); console.log(`Tx successful with hash: ${createReceipt.transactionHash}`); }; +// 9. Call increment function increment(); \ No newline at end of file diff --git a/.snippets/code/web3-contract-local/reset.js b/.snippets/code/web3-contract-local/reset.js index 807c2eae4..2ed1947c2 100644 --- a/.snippets/code/web3-contract-local/reset.js +++ b/.snippets/code/web3-contract-local/reset.js @@ -1,23 +1,31 @@ +// 1. Import Web3js and the contract abi const Web3 = require('web3'); const { abi } = require('./compile'); +// 2. Add the Web3 provider logic here: const providerRPC = { development: 'http://localhost:9944', moonbase: 'https://rpc.api.moonbase.moonbeam.network', }; const web3 = new Web3(providerRPC.development); // Change to correct network +// 3. Create variables const account_from = { - privateKey: 'YOUR-PRIVATE-KEY-HERE', + privateKey: 'YOUR_PRIVATE_KEY_HERE', }; -const contractAddress = 'CONTRACT-ADDRESS-HERE'; +const contractAddress = 'CONTRACT_ADDRESS_HERE'; +// 4. Create Contract Instance const incrementer = new web3.eth.Contract(abi, contractAddress); + +// 5. Build reset tx const resetTx = incrementer.methods.reset(); +// 6. Create reset function const reset = async () => { console.log(`Calling the reset function in contract at address: ${contractAddress}`); + // 7. Prepare and Sign Tx with PK const createTransaction = await web3.eth.accounts.signTransaction( { to: contractAddress, @@ -27,8 +35,10 @@ const reset = async () => { account_from.privateKey ); + // 8. Send tx and wait for receipt const createReceipt = await web3.eth.sendSignedTransaction(createTransaction.rawTransaction); console.log(`Tx successful with hash: ${createReceipt.transactionHash}`); }; +// 9. Call reset function reset(); \ No newline at end of file diff --git a/.snippets/code/web3-tx-local/balances.js b/.snippets/code/web3-tx-local/balances.js index f7d26e6a8..15020cc49 100644 --- a/.snippets/code/web3-tx-local/balances.js +++ b/.snippets/code/web3-tx-local/balances.js @@ -1,15 +1,19 @@ const Web3 = require('web3'); +// 1. Add the Web3 provider logic here: const providerRPC = { development: 'http://localhost:9944', moonbase: 'https://rpc.api.moonbase.moonbeam.network', }; const web3 = new Web3(providerRPC.development); // Change to correct network -const addressFrom = 'ADDRESS-FROM-HERE'; -const addressTo = 'ADDRESS-TO-HERE'; +// 2. Create address variables +const addressFrom = 'ADDRESS_FROM_HERE'; +const addressTo = 'ADDRESS_TO_HERE'; +// 3. Create balances function const balances = async () => { + // 4. Fetch balance info const balanceFrom = web3.utils.fromWei(await web3.eth.getBalance(addressFrom), 'ether'); const balanceTo = web3.utils.fromWei(await web3.eth.getBalance(addressTo), 'ether'); @@ -17,4 +21,5 @@ const balances = async () => { console.log(`The balance of ${addressTo} is: ${balanceTo} ETH`); }; +// 5. Call balances function balances(); \ No newline at end of file diff --git a/.snippets/code/web3-tx-local/transaction.js b/.snippets/code/web3-tx-local/transaction.js index 906893a25..fde6f96a1 100644 --- a/.snippets/code/web3-tx-local/transaction.js +++ b/.snippets/code/web3-tx-local/transaction.js @@ -1,20 +1,24 @@ const Web3 = require('web3'); +// 1. Add the Web3 provider logic here: const providerRPC = { development: 'http://localhost:9944', moonbase: 'https://rpc.api.moonbase.moonbeam.network', }; const web3 = new Web3(providerRPC.development); // Change to correct network +// 2. Create account variables const account_from = { - privateKey: 'YOUR-PRIVATE-KEY-HERE', - address: 'PUBLIC-ADDRESS-OF-PK-HERE', + privateKey: 'YOUR_PRIVATE_KEY_HERE', + address: 'PUBLIC_ADDRESS_OF_PK_HERE', }; -const addressTo = 'ADDRESS-TO-HERE'; +const addressTo = 'ADDRESS_TO_HERE'; +// 3. Create send function const send = async () => { console.log(`Attempting to send transaction from ${account_from.address} to ${addressTo}`); + // 4. Prepare and sign tx with PK const createTransaction = await web3.eth.accounts.signTransaction( { gas: 21000, @@ -24,8 +28,10 @@ const send = async () => { account_from.privateKey ); + // 5. Send tx and wait for receipt const createReceipt = await web3.eth.sendSignedTransaction(createTransaction.rawTransaction); console.log(`Transaction successful with hash: ${createReceipt.transactionHash}`); }; +// 6. Call send function send(); \ No newline at end of file diff --git a/builders/build/eth-api/libraries/web3js.md b/builders/build/eth-api/libraries/web3js.md index 8334aafac..66c84dfe9 100644 --- a/builders/build/eth-api/libraries/web3js.md +++ b/builders/build/eth-api/libraries/web3js.md @@ -407,7 +407,7 @@ const increment = async () => { `Calling the increment by ${_value} function in contract at address: ${contractAddress}` ); - // Sign Tx with PK + // 7. Prepare and Sign Tx with PK const createTransaction = await web3.eth.accounts.signTransaction( { to: contractAddress, @@ -417,7 +417,7 @@ const increment = async () => { accountFrom.privateKey ); - // Send Tx and Wait for Receipt + // 8. Send Tx and Wait for Receipt const createReceipt = await web3.eth.sendSignedTransaction(createTransaction.rawTransaction); console.log(`Tx successful with hash: ${createReceipt.transactionHash}`); }; From 3206d9f9a2bb9325f60d68c4d379299a772a2b28 Mon Sep 17 00:00:00 2001 From: Kevin Neilson Date: Fri, 1 Sep 2023 11:08:43 -0400 Subject: [PATCH 05/11] additions --- .snippets/code/web3-contract-local/deploy.js | 4 ++-- .snippets/code/web3-tx-local/balances.js | 6 +++--- builders/build/eth-api/libraries/web3js.md | 22 +++++++++++++------- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/.snippets/code/web3-contract-local/deploy.js b/.snippets/code/web3-contract-local/deploy.js index 39f6c4a20..860850994 100644 --- a/.snippets/code/web3-contract-local/deploy.js +++ b/.snippets/code/web3-contract-local/deploy.js @@ -11,7 +11,7 @@ const providerRPC = { const web3 = new Web3(providerRPC.development); // Change to correct network // 3. Create address variables -const account_from = { +const accountFrom = { privateKey: 'YOUR_PRIVATE_KEY_HERE', address: 'PUBLIC_ADDRESS_OF_PK_HERE', }; @@ -22,7 +22,7 @@ const abi = contractFile.abi; // 5. Create deploy function const deploy = async () => { - console.log(`Attempting to deploy from account ${account_from.address}`); + console.log(`Attempting to deploy from account ${accountFrom.address}`); // 6. Create contract instance const incrementer = new web3.eth.Contract(abi); diff --git a/.snippets/code/web3-tx-local/balances.js b/.snippets/code/web3-tx-local/balances.js index 15020cc49..a51540cf4 100644 --- a/.snippets/code/web3-tx-local/balances.js +++ b/.snippets/code/web3-tx-local/balances.js @@ -5,7 +5,7 @@ const providerRPC = { development: 'http://localhost:9944', moonbase: 'https://rpc.api.moonbase.moonbeam.network', }; -const web3 = new Web3(providerRPC.development); // Change to correct network +const web3 = new Web3(providerRPC.moonbase); // Change to correct network // 2. Create address variables const addressFrom = 'ADDRESS_FROM_HERE'; @@ -17,8 +17,8 @@ const balances = async () => { const balanceFrom = web3.utils.fromWei(await web3.eth.getBalance(addressFrom), 'ether'); const balanceTo = web3.utils.fromWei(await web3.eth.getBalance(addressTo), 'ether'); - console.log(`The balance of ${addressFrom} is: ${balanceFrom} ETH`); - console.log(`The balance of ${addressTo} is: ${balanceTo} ETH`); + console.log(`The balance of ${addressFrom} is: ${balanceFrom} DEV`); + console.log(`The balance of ${addressTo} is: ${balanceTo} DEV`); }; // 5. Call balances function diff --git a/builders/build/eth-api/libraries/web3js.md b/builders/build/eth-api/libraries/web3js.md index 66c84dfe9..813741403 100644 --- a/builders/build/eth-api/libraries/web3js.md +++ b/builders/build/eth-api/libraries/web3js.md @@ -33,9 +33,17 @@ mkdir web3-examples && cd web3-examples 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: -```bash -npm install web3 solc@0.8.0 -``` +=== "npm" + + ```bash + npm install web3 solc@0.8.0 + ``` + +=== "yarn" + + ```bash + yarn add web3 solc@0.8.0 + ``` ## Setup Web3.js with Moonbeam {: #setup-web3-with-moonbeam } @@ -99,7 +107,7 @@ Next, you will create the script for this file and complete the following steps: 1. [Set up the Web3 provider](#setup-web3-with-moonbeam) 2. Define the `addressFrom` and `addressTo` variables 3. Create the asynchronous `balances` function which wraps the `web3.eth.getBalance` method -4. 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 ETH +4. 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 5. Lastly, run the `balances` function ```js @@ -116,8 +124,8 @@ const balances = async () => { const balanceFrom = web3.utils.fromWei(await web3.eth.getBalance(addressFrom), 'ether'); const balanceTo = web3.utils.fromWei(await web3.eth.getBalance(addressTo), 'ether'); - console.log(`The balance of ${addressFrom} is: ${balanceFrom} ETH`); - console.log(`The balance of ${addressTo} is: ${balanceTo} ETH`); + console.log(`The balance of ${addressFrom} is: ${balanceFrom} DEV`); + console.log(`The balance of ${addressTo} is: ${balanceTo} DEV`); }; // 5. Call balances function @@ -136,7 +144,7 @@ To run the script and fetch the account balances, you can run the following comm node balances.js ``` -If successful, the balances for the origin and receiving address will be displayed in your terminal in ETH. +If successful, the balances for the origin and receiving address will be displayed in your terminal in DEV. ### Send Transaction Script {: #send-transaction-script } From 80f1f872dd4166c715549020f22e12ea4435b0c1 Mon Sep 17 00:00:00 2001 From: Kevin Neilson Date: Fri, 1 Sep 2023 11:12:48 -0400 Subject: [PATCH 06/11] revisions --- .snippets/code/web3-contract-local/deploy.js | 2 +- .snippets/code/web3-contract-local/increment.js | 4 ++-- .snippets/code/web3-contract-local/reset.js | 4 ++-- .snippets/code/web3-tx-local/transaction.js | 6 +++--- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/.snippets/code/web3-contract-local/deploy.js b/.snippets/code/web3-contract-local/deploy.js index 860850994..a80b210fa 100644 --- a/.snippets/code/web3-contract-local/deploy.js +++ b/.snippets/code/web3-contract-local/deploy.js @@ -39,7 +39,7 @@ const deploy = async () => { data: incrementerTx.encodeABI(), gas: await incrementerTx.estimateGas(), }, - account_from.privateKey + accountFrom.privateKey ); // 9. Send tx and wait for receipt diff --git a/.snippets/code/web3-contract-local/increment.js b/.snippets/code/web3-contract-local/increment.js index bc5659a36..cfbaba0ea 100644 --- a/.snippets/code/web3-contract-local/increment.js +++ b/.snippets/code/web3-contract-local/increment.js @@ -11,7 +11,7 @@ const providerRPC = { const web3 = new Web3(providerRPC.development); //Change to correct network // 3. Create variables -const account_from = { +const accountFrom = { privateKey: 'YOUR_PRIVATE_KEY_HERE', }; const contractAddress = 'CONTRACT_ADDRESS_HERE'; @@ -36,7 +36,7 @@ const increment = async () => { data: incrementTx.encodeABI(), gas: await incrementTx.estimateGas(), }, - account_from.privateKey + accountFrom.privateKey ); // 8. Send Tx and Wait for Receipt diff --git a/.snippets/code/web3-contract-local/reset.js b/.snippets/code/web3-contract-local/reset.js index 2ed1947c2..d031aa725 100644 --- a/.snippets/code/web3-contract-local/reset.js +++ b/.snippets/code/web3-contract-local/reset.js @@ -10,7 +10,7 @@ const providerRPC = { const web3 = new Web3(providerRPC.development); // Change to correct network // 3. Create variables -const account_from = { +const accountFrom = { privateKey: 'YOUR_PRIVATE_KEY_HERE', }; const contractAddress = 'CONTRACT_ADDRESS_HERE'; @@ -32,7 +32,7 @@ const reset = async () => { data: resetTx.encodeABI(), gas: await resetTx.estimateGas(), }, - account_from.privateKey + accountFrom.privateKey ); // 8. Send tx and wait for receipt diff --git a/.snippets/code/web3-tx-local/transaction.js b/.snippets/code/web3-tx-local/transaction.js index fde6f96a1..47d1a693b 100644 --- a/.snippets/code/web3-tx-local/transaction.js +++ b/.snippets/code/web3-tx-local/transaction.js @@ -8,7 +8,7 @@ const providerRPC = { const web3 = new Web3(providerRPC.development); // Change to correct network // 2. Create account variables -const account_from = { +const accountFrom = { privateKey: 'YOUR_PRIVATE_KEY_HERE', address: 'PUBLIC_ADDRESS_OF_PK_HERE', }; @@ -16,7 +16,7 @@ const addressTo = 'ADDRESS_TO_HERE'; // 3. Create send function const send = async () => { - console.log(`Attempting to send transaction from ${account_from.address} to ${addressTo}`); + console.log(`Attempting to send transaction from ${accountFrom.address} to ${addressTo}`); // 4. Prepare and sign tx with PK const createTransaction = await web3.eth.accounts.signTransaction( @@ -25,7 +25,7 @@ const send = async () => { to: addressTo, value: web3.utils.toWei('1', 'ether'), }, - account_from.privateKey + accountFrom.privateKey ); // 5. Send tx and wait for receipt From 634bb9265efab8944e518fbe69be1d847dc468ff Mon Sep 17 00:00:00 2001 From: Kevin Neilson Date: Fri, 1 Sep 2023 12:02:22 -0400 Subject: [PATCH 07/11] update all files to use moonbase by default --- .snippets/code/web3-contract-local/deploy.js | 2 +- .snippets/code/web3-contract-local/get.js | 2 +- .snippets/code/web3-contract-local/increment.js | 2 +- .snippets/code/web3-contract-local/reset.js | 2 +- .snippets/code/web3-tx-local/transaction.js | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.snippets/code/web3-contract-local/deploy.js b/.snippets/code/web3-contract-local/deploy.js index a80b210fa..31174025e 100644 --- a/.snippets/code/web3-contract-local/deploy.js +++ b/.snippets/code/web3-contract-local/deploy.js @@ -8,7 +8,7 @@ const providerRPC = { development: 'http://localhost:9944', moonbase: 'https://rpc.api.moonbase.moonbeam.network', }; -const web3 = new Web3(providerRPC.development); // Change to correct network +const web3 = new Web3(providerRPC.moonbase); // Change to correct network // 3. Create address variables const accountFrom = { diff --git a/.snippets/code/web3-contract-local/get.js b/.snippets/code/web3-contract-local/get.js index 1621b0b10..5d954d60d 100644 --- a/.snippets/code/web3-contract-local/get.js +++ b/.snippets/code/web3-contract-local/get.js @@ -7,7 +7,7 @@ const providerRPC = { development: 'http://localhost:9944', moonbase: 'https://rpc.api.moonbase.moonbeam.network', }; -const web3 = new Web3(providerRPC.development); // Change to correct network +const web3 = new Web3(providerRPC.moonbase); // Change to correct network // 3. Create address variables const contractAddress = 'CONTRACT_ADDRESS_HERE'; diff --git a/.snippets/code/web3-contract-local/increment.js b/.snippets/code/web3-contract-local/increment.js index cfbaba0ea..7f2266a2e 100644 --- a/.snippets/code/web3-contract-local/increment.js +++ b/.snippets/code/web3-contract-local/increment.js @@ -8,7 +8,7 @@ const providerRPC = { development: 'http://localhost:9944', moonbase: 'https://rpc.api.moonbase.moonbeam.network', }; -const web3 = new Web3(providerRPC.development); //Change to correct network +const web3 = new Web3(providerRPC.moonbase); //Change to correct network // 3. Create variables const accountFrom = { diff --git a/.snippets/code/web3-contract-local/reset.js b/.snippets/code/web3-contract-local/reset.js index d031aa725..30854abef 100644 --- a/.snippets/code/web3-contract-local/reset.js +++ b/.snippets/code/web3-contract-local/reset.js @@ -7,7 +7,7 @@ const providerRPC = { development: 'http://localhost:9944', moonbase: 'https://rpc.api.moonbase.moonbeam.network', }; -const web3 = new Web3(providerRPC.development); // Change to correct network +const web3 = new Web3(providerRPC.moonbase); // Change to correct network // 3. Create variables const accountFrom = { diff --git a/.snippets/code/web3-tx-local/transaction.js b/.snippets/code/web3-tx-local/transaction.js index 47d1a693b..9892d410f 100644 --- a/.snippets/code/web3-tx-local/transaction.js +++ b/.snippets/code/web3-tx-local/transaction.js @@ -5,7 +5,7 @@ const providerRPC = { development: 'http://localhost:9944', moonbase: 'https://rpc.api.moonbase.moonbeam.network', }; -const web3 = new Web3(providerRPC.development); // Change to correct network +const web3 = new Web3(providerRPC.moonbase); // Change to correct network // 2. Create account variables const accountFrom = { From 54c659824e3b0ccaca566fe2b089a847b7a15594 Mon Sep 17 00:00:00 2001 From: Erin Shaben Date: Fri, 1 Sep 2023 14:39:13 -0400 Subject: [PATCH 08/11] minor updates - update variable placeholders - use docs.ethers.org instead of docs.ethers.io - update account_from to accountFrom in js snippets --- .snippets/code/ethers-js-contract/deploy.js | 6 ++--- .snippets/code/ethers-js-contract/get.js | 2 +- .../code/ethers-js-contract/increment.js | 4 +-- .snippets/code/ethers-js-contract/reset.js | 4 +-- .snippets/code/ethers-js-tx/balances.js | 4 +-- .snippets/code/ethers-js-tx/transaction.js | 4 +-- .snippets/code/web3-contract-local/deploy.js | 6 ++--- .snippets/code/web3-contract-local/get.js | 2 +- .../code/web3-contract-local/increment.js | 8 +++--- .snippets/code/web3-contract-local/reset.js | 8 +++--- .snippets/code/web3-tx-local/balances.js | 4 +-- .snippets/code/web3-tx-local/transaction.js | 12 ++++----- .snippets/code/web3py-contract/deploy.py | 4 +-- .snippets/code/web3py-contract/get.py | 2 +- .snippets/code/web3py-contract/increment.py | 6 ++--- .snippets/code/web3py-contract/reset.py | 6 ++--- .snippets/code/web3py-tx/balances.py | 4 +-- .snippets/code/web3py-tx/transaction.py | 6 ++--- builders/build/eth-api/libraries/ethersjs.md | 26 +++++++++---------- builders/build/eth-api/libraries/web3js.md | 24 ++++++++--------- .../eth-compare/consensus-finality.md | 2 +- .../integrations/wallets/walletconnect.md | 2 +- .../xcm/xcm-sdk/v0/xcm-sdk.md | 4 +-- .../precompiles/call-permit.md | 4 +-- tutorials/integrations/nft-subsquid.md | 2 +- 25 files changed, 77 insertions(+), 79 deletions(-) diff --git a/.snippets/code/ethers-js-contract/deploy.js b/.snippets/code/ethers-js-contract/deploy.js index 124850fae..ef32ab10d 100644 --- a/.snippets/code/ethers-js-contract/deploy.js +++ b/.snippets/code/ethers-js-contract/deploy.js @@ -23,10 +23,10 @@ const provider = new ethers.JsonRpcProvider(providerRPC.moonbase.rpc, { }); // Change to correct network // Define accounts and wallet -const account_from = { - privateKey: 'YOUR-PRIVATE-KEY-HERE', +const accountFrom = { + privateKey: 'INSERT_YOUR_PRIVATE_KEY', }; -let wallet = new ethers.Wallet(account_from.privateKey, provider); +let wallet = new ethers.Wallet(accountFrom.privateKey, provider); // Load contract info const bytecode = contractFile.evm.bytecode.object; diff --git a/.snippets/code/ethers-js-contract/get.js b/.snippets/code/ethers-js-contract/get.js index 8973006f8..834551ee8 100644 --- a/.snippets/code/ethers-js-contract/get.js +++ b/.snippets/code/ethers-js-contract/get.js @@ -22,7 +22,7 @@ const provider = new ethers.JsonRpcProvider(providerRPC.moonbase.rpc, { }); // Change to correct network // Contract address variable -const contractAddress = 'CONTRACT_ADDRESS_HERE'; +const contractAddress = 'INSERT_CONTRACT_ADDRESS'; // Create contract instance const incrementer = new ethers.Contract(contractAddress, abi, provider); diff --git a/.snippets/code/ethers-js-contract/increment.js b/.snippets/code/ethers-js-contract/increment.js index 6a6870dac..21c146b1a 100644 --- a/.snippets/code/ethers-js-contract/increment.js +++ b/.snippets/code/ethers-js-contract/increment.js @@ -24,9 +24,9 @@ const provider = new ethers.JsonRpcProvider(providerRPC.moonbase.rpc, { // Create variables const accountFrom = { - privateKey: 'YOUR_PRIVATE_KEY_HERE', + privateKey: 'INSERT_YOUR_PRIVATE_KEY', }; -const contractAddress = 'CONTRACT_ADDRESS_HERE'; +const contractAddress = 'INSERT_CONTRACT_ADDRESS'; const _value = 3; // Create wallet diff --git a/.snippets/code/ethers-js-contract/reset.js b/.snippets/code/ethers-js-contract/reset.js index 8a043b14b..4127e94f0 100644 --- a/.snippets/code/ethers-js-contract/reset.js +++ b/.snippets/code/ethers-js-contract/reset.js @@ -24,9 +24,9 @@ const provider = new ethers.JsonRpcProvider(providerRPC.moonbase.rpc, { // Create variables const accountFrom = { - privateKey: 'YOUR_PRIVATE_KEY_HERE', + privateKey: 'INSERT_YOUR_PRIVATE_KEY', }; -const contractAddress = 'CONTRACT_ADDRESS_HERE'; +const contractAddress = 'INSERT_CONTRACT_ADDRESS'; // Create wallet let wallet = new ethers.Wallet(accountFrom.privateKey, provider); diff --git a/.snippets/code/ethers-js-tx/balances.js b/.snippets/code/ethers-js-tx/balances.js index 3c62845d9..69895bc34 100644 --- a/.snippets/code/ethers-js-tx/balances.js +++ b/.snippets/code/ethers-js-tx/balances.js @@ -22,8 +22,8 @@ const provider = new ethers.JsonRpcProvider(providerRPC.moonbase.rpc, { }); // Change to correct network // Define addresses -const addressFrom = 'ADDRESS_FROM_HERE'; -const addressTo = 'ADDRESS_TO_HERE'; +const addressFrom = 'INSERT_FROM_ADDRESS'; +const addressTo = 'INSERT_TO_ADDRESS'; // Create balances function const balances = async () => { diff --git a/.snippets/code/ethers-js-tx/transaction.js b/.snippets/code/ethers-js-tx/transaction.js index b45499b96..007538f97 100644 --- a/.snippets/code/ethers-js-tx/transaction.js +++ b/.snippets/code/ethers-js-tx/transaction.js @@ -22,9 +22,9 @@ const provider = new ethers.JsonRpcProvider(providerRPC.moonbase.rpc, { // Define accounts and wallet const accountFrom = { - privateKey: 'YOUR_PRIVATE_KEY_HERE', + privateKey: 'INSERT_YOUR_PRIVATE_KEY', }; -const addressTo = 'ADDRESS_TO_HERE'; +const addressTo = 'INSERT_TO_ADDRESS'; const wallet = new ethers.Wallet(accountFrom.privateKey, provider); // Create send function diff --git a/.snippets/code/web3-contract-local/deploy.js b/.snippets/code/web3-contract-local/deploy.js index 226a4e862..4ef4fa7cb 100644 --- a/.snippets/code/web3-contract-local/deploy.js +++ b/.snippets/code/web3-contract-local/deploy.js @@ -8,8 +8,8 @@ const providerRPC = { const web3 = new Web3(providerRPC.development); // Change to correct network const accountFrom = { - privateKey: 'YOUR-PRIVATE-KEY-HERE', - address: 'PUBLIC-ADDRESS-OF-PK-HERE', + privateKey: 'INSERT_YOUR_PRIVATE_KEY', + address: 'INSERT_PUBLIC_ADDRESS_OF_PK', }; const bytecode = contractFile.evm.bytecode.object; const abi = contractFile.abi; @@ -29,7 +29,7 @@ const deploy = async () => { data: incrementerTx.encodeABI(), gas: await incrementerTx.estimateGas(), }, - account_from.privateKey + accountFrom.privateKey ); const createReceipt = await web3.eth.sendSignedTransaction( diff --git a/.snippets/code/web3-contract-local/get.js b/.snippets/code/web3-contract-local/get.js index a2488c406..709a2fdd8 100644 --- a/.snippets/code/web3-contract-local/get.js +++ b/.snippets/code/web3-contract-local/get.js @@ -7,7 +7,7 @@ const providerRPC = { }; const web3 = new Web3(providerRPC.development); // Change to correct network -const contractAddress = 'CONTRACT-ADDRESS-HERE'; +const contractAddress = 'INSERT_CONTRACT_ADDRESS'; const incrementer = new web3.eth.Contract(abi, contractAddress); diff --git a/.snippets/code/web3-contract-local/increment.js b/.snippets/code/web3-contract-local/increment.js index 637021934..e8797d088 100644 --- a/.snippets/code/web3-contract-local/increment.js +++ b/.snippets/code/web3-contract-local/increment.js @@ -7,10 +7,10 @@ const providerRPC = { }; const web3 = new Web3(providerRPC.development); //Change to correct network -const account_from = { - privateKey: 'YOUR-PRIVATE-KEY-HERE', +const accountFrom = { + privateKey: 'INSERT_YOUR_PRIVATE_KEY', }; -const contractAddress = 'CONTRACT-ADDRESS-HERE'; +const contractAddress = 'INSERT_CONTRACT_ADDRESS'; const _value = 3; const incrementer = new web3.eth.Contract(abi, contractAddress); @@ -26,7 +26,7 @@ const increment = async () => { data: incrementTx.encodeABI(), gas: await incrementTx.estimateGas(), }, - account_from.privateKey + accountFrom.privateKey ); const createReceipt = await web3.eth.sendSignedTransaction(createTransaction.rawTransaction); diff --git a/.snippets/code/web3-contract-local/reset.js b/.snippets/code/web3-contract-local/reset.js index 807c2eae4..0567b50b9 100644 --- a/.snippets/code/web3-contract-local/reset.js +++ b/.snippets/code/web3-contract-local/reset.js @@ -7,10 +7,10 @@ const providerRPC = { }; const web3 = new Web3(providerRPC.development); // Change to correct network -const account_from = { - privateKey: 'YOUR-PRIVATE-KEY-HERE', +const accountFrom = { + privateKey: 'INSERT_YOUR_PRIVATE_KEY', }; -const contractAddress = 'CONTRACT-ADDRESS-HERE'; +const contractAddress = 'INSERT_CONTRACT_ADDRESS'; const incrementer = new web3.eth.Contract(abi, contractAddress); const resetTx = incrementer.methods.reset(); @@ -24,7 +24,7 @@ const reset = async () => { data: resetTx.encodeABI(), gas: await resetTx.estimateGas(), }, - account_from.privateKey + accountFrom.privateKey ); const createReceipt = await web3.eth.sendSignedTransaction(createTransaction.rawTransaction); diff --git a/.snippets/code/web3-tx-local/balances.js b/.snippets/code/web3-tx-local/balances.js index f7d26e6a8..381b84e35 100644 --- a/.snippets/code/web3-tx-local/balances.js +++ b/.snippets/code/web3-tx-local/balances.js @@ -6,8 +6,8 @@ const providerRPC = { }; const web3 = new Web3(providerRPC.development); // Change to correct network -const addressFrom = 'ADDRESS-FROM-HERE'; -const addressTo = 'ADDRESS-TO-HERE'; +const addressFrom = 'INSERT_FROM_ADDRESS'; +const addressTo = 'INSERT_TO_ADDRESS'; const balances = async () => { const balanceFrom = web3.utils.fromWei(await web3.eth.getBalance(addressFrom), 'ether'); diff --git a/.snippets/code/web3-tx-local/transaction.js b/.snippets/code/web3-tx-local/transaction.js index 906893a25..9a2456825 100644 --- a/.snippets/code/web3-tx-local/transaction.js +++ b/.snippets/code/web3-tx-local/transaction.js @@ -6,14 +6,14 @@ const providerRPC = { }; const web3 = new Web3(providerRPC.development); // Change to correct network -const account_from = { - privateKey: 'YOUR-PRIVATE-KEY-HERE', - address: 'PUBLIC-ADDRESS-OF-PK-HERE', +const accountFrom = { + privateKey: 'INSERT_YOUR_PRIVATE_KEY', + address: 'INSERT_PUBLIC_ADDRESS_OF_PK', }; -const addressTo = 'ADDRESS-TO-HERE'; +const addressTo = 'INSERT_TO_ADDRESS'; const send = async () => { - console.log(`Attempting to send transaction from ${account_from.address} to ${addressTo}`); + console.log(`Attempting to send transaction from ${accountFrom.address} to ${addressTo}`); const createTransaction = await web3.eth.accounts.signTransaction( { @@ -21,7 +21,7 @@ const send = async () => { to: addressTo, value: web3.utils.toWei('1', 'ether'), }, - account_from.privateKey + accountFrom.privateKey ); const createReceipt = await web3.eth.sendSignedTransaction(createTransaction.rawTransaction); diff --git a/.snippets/code/web3py-contract/deploy.py b/.snippets/code/web3py-contract/deploy.py index fb6e144bb..924c74904 100644 --- a/.snippets/code/web3py-contract/deploy.py +++ b/.snippets/code/web3py-contract/deploy.py @@ -11,8 +11,8 @@ # 3. Create address variable account_from = { - 'private_key': 'YOUR_PRIVATE_KEY_HERE', - 'address': 'PUBLIC_ADDRESS_OF_PK_HERE', + 'private_key': 'INSERT_YOUR_PRIVATE_KEY', + 'address': 'INSERT_PUBLIC_ADDRESS_OF_PK', } print(f'Attempting to deploy from account: { account_from["address"] }') diff --git a/.snippets/code/web3py-contract/get.py b/.snippets/code/web3py-contract/get.py index 4c7079304..dfadee0e3 100644 --- a/.snippets/code/web3py-contract/get.py +++ b/.snippets/code/web3py-contract/get.py @@ -10,7 +10,7 @@ web3 = Web3(Web3.HTTPProvider(provider_rpc['development'])) # Change to correct network # 3. Create address variable -contract_address = 'CONTRACT_ADDRESS_HERE' +contract_address = 'INSERT_CONTRACT_ADDRESS' print(f'Making a call to contract at address: { contract_address }') diff --git a/.snippets/code/web3py-contract/increment.py b/.snippets/code/web3py-contract/increment.py index 4205bf02b..c062c1d2c 100644 --- a/.snippets/code/web3py-contract/increment.py +++ b/.snippets/code/web3py-contract/increment.py @@ -11,10 +11,10 @@ # 3. Create variables account_from = { - 'private_key': 'YOUR_PRIVATE_KEY_HERE', - 'address': 'PUBLIC_ADDRESS_OF_PK_HERE', + 'private_key': 'INSERT_YOUR_PRIVATE_KEY', + 'address': 'INSERT_PUBLIC_ADDRESS_OF_PK', } -contract_address = 'CONTRACT_ADDRESS_HERE' +contract_address = 'INSERT_CONTRACT_ADDRESS' value = 3 print( diff --git a/.snippets/code/web3py-contract/reset.py b/.snippets/code/web3py-contract/reset.py index d87712a16..9e62d97ab 100644 --- a/.snippets/code/web3py-contract/reset.py +++ b/.snippets/code/web3py-contract/reset.py @@ -11,10 +11,10 @@ # 3. Create variables account_from = { - 'private_key': 'YOUR_PRIVATE_KEY_HERE', - 'address': 'PUBLIC_ADDRESS_OF_PK_HERE', + 'private_key': 'INSERT_YOUR_PRIVATE_KEY', + 'address': 'INSERT_PUBLIC_ADDRESS_OF_PK', } -contract_address = 'CONTRACT_ADDRESS_HERE' +contract_address = 'INSERT_CONTRACT_ADDRESS' print(f'Calling the reset function in contract at address: { contract_address }') diff --git a/.snippets/code/web3py-tx/balances.py b/.snippets/code/web3py-tx/balances.py index e92ac6de3..cbb74dcdd 100644 --- a/.snippets/code/web3py-tx/balances.py +++ b/.snippets/code/web3py-tx/balances.py @@ -9,8 +9,8 @@ web3 = Web3(Web3.HTTPProvider(provider_rpc['development'])) # Change to correct network # 2. Create address variables -address_from = 'ADDRESS_FROM_HERE' -address_to = 'ADDRESS_TO_HERE' +address_from = 'INSERT_FROM_ADDRESS' +address_to = 'INSERT_TO_ADDRESS' # 3. Fetch balance data balance_from = web3.from_wei(web3.eth.getBalance(address_from), 'ether') diff --git a/.snippets/code/web3py-tx/transaction.py b/.snippets/code/web3py-tx/transaction.py index fcd7e1474..c40284a93 100644 --- a/.snippets/code/web3py-tx/transaction.py +++ b/.snippets/code/web3py-tx/transaction.py @@ -11,10 +11,10 @@ # 3. Create address variables account_from = { - 'private_key': 'YOUR_PRIVATE_KEY_HERE', - 'address': 'PUBLIC_ADDRESS_OF_PK_HERE', + 'private_key': 'INSERT_YOUR_PRIVATE_KEY', + 'address': 'INSERT_PUBLIC_ADDRESS_OF_PK', } -address_to = 'ADDRESS_TO_HERE' +address_to = 'INSERT_TO_ADDRESS' print( f'Attempting to send transaction from { account_from["address"] } to { address_to }' diff --git a/builders/build/eth-api/libraries/ethersjs.md b/builders/build/eth-api/libraries/ethersjs.md index 353c7d7d9..706f5fc46 100644 --- a/builders/build/eth-api/libraries/ethersjs.md +++ b/builders/build/eth-api/libraries/ethersjs.md @@ -7,7 +7,7 @@ description: Follow this tutorial to learn how to use the Ethereum Ethers.js Lib ## Introduction {: #introduction } -The [Ethers.js](https://docs.ethers.io/){target=_blank} library provides a set of tools to interact with Ethereum Nodes with JavaScript, similar to Web3.js. Moonbeam 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 Moonbeam node as if they were doing so on Ethereum. For more information on Ethers.js, check their [documentation site](https://docs.ethers.org/v5/){target=_blank}. +The [Ethers.js](https://docs.ethers.org/){target=_blank} library provides a set of tools to interact with Ethereum Nodes with JavaScript, similar to Web3.js. Moonbeam 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 Moonbeam node as if they were doing so on Ethereum. For more information on Ethers.js, check their [documentation site](https://docs.ethers.org/v6/){target=_blank}. In this guide, you'll learn how to use the Ethers.js library to send a transaction and deploy a contract on Moonbase Alpha. This guide can be adapted for [Moonbeam](/builders/get-started/networks/moonbeam/){target=_blank}, [Moonriver](/builders/get-started/networks/moonriver/){target=_blank}, or a [Moonbeam development node](/builders/get-started/networks/moonbeam-dev/){target=_blank}. @@ -47,7 +47,7 @@ For this guide, you'll need to install the Ethers.js library and the Solidity co ## Setting up the Ethers Provider {: #setting-up-the-ethers-provider } -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](https://docs.ethers.io/v6/api/providers/){target=_blank} to interact with the network. +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](https://docs.ethers.org/v6/api/providers/){target=_blank} to interact with the network. --8<-- 'text/common/endpoint-setup.md' @@ -182,8 +182,8 @@ Next, you will create the script for this file and complete the following steps: // {...} // 2. Create address variables -const addressFrom = 'ADDRESS-FROM-HERE'; -const addressTo = 'ADDRESS-TO-HERE'; +const addressFrom = 'INSERT_FROM_ADDRESS'; +const addressTo = 'INSERT_TO_ADDRESS'; // 3. Create balances function const balances = async () => { @@ -237,9 +237,9 @@ Next, you will create the script for this file and complete the following steps: // 2. Create account variables const accountFrom = { - privateKey: 'YOUR_PRIVATE_KEY_HERE', + privateKey: 'INSERT_YOUR_PRIVATE_KEY', }; -const addressTo = 'ADDRESS_TO_HERE'; +const addressTo = 'INSERT_TO_ADDRESS'; // 3. Create wallet let wallet = new ethers.Wallet(accountFrom.privateKey, provider); @@ -319,7 +319,7 @@ const contractFile = require('./compile'); // 3. Create account variables const accountFrom = { - privateKey: 'YOUR_PRIVATE_KEY_HERE', + privateKey: 'INSERT_YOUR_PRIVATE_KEY', }; // 4. Create wallet @@ -391,7 +391,7 @@ const { abi } = require('./compile'); // {...} // 3. Contract address variable -const contractAddress = 'CONTRACT_ADDRESS_HERE'; +const contractAddress = 'INSERT_CONTRACT_ADDRESS'; // 4. Create contract instance const incrementer = new ethers.Contract(contractAddress, abi, provider); @@ -452,9 +452,9 @@ const { abi } = require('./compile'); // 3. Create variables const accountFrom = { - privateKey: 'YOUR_PRIVATE_KEY_HERE', + privateKey: 'INSERT_YOUR_PRIVATE_KEY', }; -const contractAddress = 'CONTRACT_ADDRESS_HERE'; +const contractAddress = 'INSERT_CONTRACT_ADDRESS'; const _value = 3; // 4. Create wallet @@ -486,7 +486,6 @@ increment(); --8<-- 'code/ethers-js-contract/increment.js' ``` - To run the script, you can enter the following command in your terminal: ```bash @@ -517,9 +516,9 @@ const { abi } = require('./compile'); // 3. Create variables const accountFrom = { - privateKey: 'YOUR_PRIVATE_KEY_HERE', + privateKey: 'INSERT_YOUR_PRIVATE_KEY', }; -const contractAddress = 'CONTRACT_ADDRESS_HERE'; +const contractAddress = 'INSERT_CONTRACT_ADDRESS'; // 4. Create wallet let wallet = new ethers.Wallet(accountFrom.privateKey, provider); @@ -548,7 +547,6 @@ reset(); --8<-- 'code/ethers-js-contract/reset.js' ``` - To run the script, you can enter the following command in your terminal: ```bash diff --git a/builders/build/eth-api/libraries/web3js.md b/builders/build/eth-api/libraries/web3js.md index 136377efe..b1e8234ee 100644 --- a/builders/build/eth-api/libraries/web3js.md +++ b/builders/build/eth-api/libraries/web3js.md @@ -107,8 +107,8 @@ Next, you will create the script for this file and complete the following steps: // {...} // 2. Create address variables -const addressFrom = 'ADDRESS-FROM-HERE'; -const addressTo = 'ADDRESS-TO-HERE'; +const addressFrom = 'INSERT_FROM_ADDRESS'; +const addressTo = 'INSERT_TO_ADDRESS'; // 3. Create balances function const balances = async () => { @@ -157,10 +157,10 @@ Next, you will create the script for this file and complete the following steps: // 2. Create account variables const accountFrom = { - privateKey: 'YOUR-PRIVATE-KEY-HERE', - address: 'PUBLIC-ADDRESS-OF-PK-HERE', + privateKey: 'INSERT_YOUR_PRIVATE_KEY', + address: 'INSERT_PUBLIC_ADDRESS_OF_PK', }; -const addressTo = 'ADDRESS-TO-HERE'; // Change addressTo +const addressTo = 'INSERT_TO_ADDRESS'; // Change addressTo // 3. Create send function const send = async () => { @@ -237,8 +237,8 @@ const contractFile = require('./compile'); // 3. Create address variables const accountFrom = { - privateKey: 'YOUR-PRIVATE-KEY-HERE', - address: 'PUBLIC-ADDRESS-OF-PK-HERE', + privateKey: 'INSERT_YOUR_PRIVATE_KEY', + address: 'INSERT_PUBLIC_ADDRESS_OF_PK', }; // 4. Get the bytecode and API @@ -316,7 +316,7 @@ const { abi } = require('./compile'); // {...} // 3. Create address variables -const contractAddress = 'CONTRACT-ADDRESS-HERE'; +const contractAddress = 'INSERT_CONTRACT_ADDRESS'; // 4. Create contract instance const incrementer = new web3.eth.Contract(abi, contractAddress); @@ -374,9 +374,9 @@ const { abi } = require('./compile'); // 3. Create variables const accountFrom = { - privateKey: 'YOUR-PRIVATE-KEY-HERE', + privateKey: 'INSERT_YOUR_PRIVATE_KEY', }; -const contractAddress = 'CONTRACT-ADDRESS-HERE'; +const contractAddress = 'INSERT_CONTRACT_ADDRESS'; const _value = 3; // 4. Create contract instance @@ -443,9 +443,9 @@ const { abi } = require('./compile'); // 3. Create variables const accountFrom = { - privateKey: 'YOUR-PRIVATE-KEY-HERE', + privateKey: 'INSERT_YOUR_PRIVATE_KEY', }; -const contractAddress = 'CONTRACT-ADDRESS-HERE'; +const contractAddress = 'INSERT_CONTRACT_ADDRESS'; // 4. Create Contract Instance const incrementer = new web3.eth.Contract(abi, contractAddress); diff --git a/builders/get-started/eth-compare/consensus-finality.md b/builders/get-started/eth-compare/consensus-finality.md index 28eac0821..f1ecf59b8 100644 --- a/builders/get-started/eth-compare/consensus-finality.md +++ b/builders/get-started/eth-compare/consensus-finality.md @@ -83,7 +83,7 @@ The snippets below follow the strategy outlined in the [previous section](#strat --8<-- 'code/vs-ethereum/consensus-finality/web3.py' ``` -You can modify these scripts to use `moon_isBlockFinalized` and `moon_isTxFinalized`. To do this, you can make custom calls to the Substrate JSON-RPC using the `send` method of both [Web3.js](https://web3js.readthedocs.io/){target=_blank} and [Ethers.js](https://docs.ethers.io/){target=_blank}. Custom RPC requests are also possible using [Web3.py](https://web3py.readthedocs.io/){target=_blank} with the `make_request` method. You'll need to pass in the method name and the parameters to the custom request, which you can find on the [Moonbeam Custom API](/builders/build/moonbeam-custom-api/){target=_blank} page. +You can modify these scripts to use `moon_isBlockFinalized` and `moon_isTxFinalized`. To do this, you can make custom calls to the Substrate JSON-RPC using the `send` method of both [Web3.js](https://web3js.readthedocs.io/){target=_blank} and [Ethers.js](https://docs.ethers.org/){target=_blank}. Custom RPC requests are also possible using [Web3.py](https://web3py.readthedocs.io/){target=_blank} with the `make_request` method. You'll need to pass in the method name and the parameters to the custom request, which you can find on the [Moonbeam Custom API](/builders/build/moonbeam-custom-api/){target=_blank} page. === "Ethers.js" diff --git a/builders/integrations/wallets/walletconnect.md b/builders/integrations/wallets/walletconnect.md index cb36471f8..e1e8b4889 100644 --- a/builders/integrations/wallets/walletconnect.md +++ b/builders/integrations/wallets/walletconnect.md @@ -339,7 +339,7 @@ You can test this logic out by refreshing the page after establishing a connecti ## Add Account Balance {: #add-account-balance } -Depending on your needs, you might want to show the connected account's balance for the connected network. To do so, you can use [Ethers](https://docs.ethers.io/){target=_blank} to create a provider which can then be used to fetch the balance of the connected account. +Depending on your needs, you might want to show the connected account's balance for the connected network. To do so, you can use [Ethers](https://docs.ethers.org/){target=_blank} to create a provider which can then be used to fetch the balance of the connected account. You can start by adding another state variable for `balance`. diff --git a/builders/interoperability/xcm/xcm-sdk/v0/xcm-sdk.md b/builders/interoperability/xcm/xcm-sdk/v0/xcm-sdk.md index 7e5f79e2f..5e0e2a85e 100644 --- a/builders/interoperability/xcm/xcm-sdk/v0/xcm-sdk.md +++ b/builders/interoperability/xcm/xcm-sdk/v0/xcm-sdk.md @@ -35,7 +35,7 @@ To install the XCM SDK and XCM config packages, you can run the following comman npm install @moonbeam-network/xcm-sdk @moonbeam-network/xcm-config ``` -You need to have peer dependencies, like [Ethers.js](https://docs.ethers.io/){target=_blank} and the [Polkadot.js API](https://polkadot.js.org/docs/api/){target=_blank} installed. +You need to have peer dependencies, like [Ethers.js](https://docs.ethers.org/){target=_blank} and the [Polkadot.js API](https://polkadot.js.org/docs/api/){target=_blank} installed. You can install them by running the following command: @@ -48,7 +48,7 @@ npm i @polkadot/api-augment @polkadot/types @polkadot/util @polkadot/util-crypto ### Creating Signers {: creating-signers } -When interacting with the `deposit` and `withdraw` functions of the XCM SDK, you'll need to provide an [Ethers.js](https://docs.ethers.io/){target=_blank} and [Polkadot.js](https://polkadot.js.org/docs/api/){target=_blank} signer, which will be used to sign and send the transactions. The Ethers signer is used to sign transactions on Moonbeam, and the Polkadot signer will be used to sign transactions on the origin chain you're depositing assets from. +When interacting with the `deposit` and `withdraw` functions of the XCM SDK, you'll need to provide an [Ethers.js](https://docs.ethers.org/){target=_blank} and [Polkadot.js](https://polkadot.js.org/docs/api/){target=_blank} signer, which will be used to sign and send the transactions. The Ethers signer is used to sign transactions on Moonbeam, and the Polkadot signer will be used to sign transactions on the origin chain you're depositing assets from. You can pass, for example, a [MetaMask signer into Ethers](https://docs.ethers.org/v6/getting-started/#starting-connecting){target=_blank} or another compatible wallet. Similarly with Polkadot, you can [pass a compatible wallet to the signer using the `@polkadot/extension-dapp` library](https://polkadot.js.org/docs/extension/){target=_blank}. diff --git a/builders/pallets-precompiles/precompiles/call-permit.md b/builders/pallets-precompiles/precompiles/call-permit.md index 8b13ec0c3..132a30995 100644 --- a/builders/pallets-precompiles/precompiles/call-permit.md +++ b/builders/pallets-precompiles/precompiles/call-permit.md @@ -162,7 +162,7 @@ Regardless of which method you choose to generate the signature, the following s 2. A JSON structure of the data the user needs to sign will be assembled for the call permit and include all of the types for the `dispatch` arguments and the nonce. This will result in the `CallPermit` type and will be saved as the `primaryType` 3. The domain separator will be created using `"Call Permit Precompile"` exactly for the name, the version of your DApp or platform, the chain ID of the network the signature is to be used on, and the address of the contract that will verify the signature 4. All of the assembled data, the `types`, `domain`, `primaryType` and `message`, will be signed using MetaMask (either in the browser or through the MetaMask's JavaScript signing library) -5. The signature will be returned and you can use [Ethers.js](https://docs.ethers.io/){target=_blank} [`Signature.from` method](https://docs.ethers.org/v6/api/crypto/#Signature_from){target=_blank} to return the `v`, `r`, and `s` values of the signature +5. The signature will be returned and you can use [Ethers.js](https://docs.ethers.org/){target=_blank} [`Signature.from` method](https://docs.ethers.org/v6/api/crypto/#Signature_from){target=_blank} to return the `v`, `r`, and `s` values of the signature ### The Call Permit Arguments {: #call-permit-arguments } @@ -229,7 +229,7 @@ You should now have a file where you can create the script to get the signature "type": "module" ``` -Next, you can install the MetaMask signing library and [Ethers.js](https://docs.ethers.io/){target=_blank}: +Next, you can install the MetaMask signing library and [Ethers.js](https://docs.ethers.org/){target=_blank}: ```bash npm i @metamask/eth-sig-util ethers diff --git a/tutorials/integrations/nft-subsquid.md b/tutorials/integrations/nft-subsquid.md index 9a0d111ed..b7eb3195c 100644 --- a/tutorials/integrations/nft-subsquid.md +++ b/tutorials/integrations/nft-subsquid.md @@ -103,7 +103,7 @@ The (re)generated entity classes can then be browsed at `src/model/generated`. Subsquid maintains [tools](https://docs.subsquid.io/substrate-indexing/squid-substrate-typegen/){target=_blank} for automated generation of TypeScript classes for handling Substrate data sources (events, extrinsics, storage items). Possible runtime upgrades are automatically detected and accounted for. -Similar functionality is available for EVM indexing through the [`squid-evm-typegen`](https://docs.subsquid.io/evm-indexing/squid-evm-typegen/){target=_blank} tool. It generates TypeScript modules for handling EVM logs and transactions based on a [JSON ABI](https://docs.ethers.io/v5/api/utils/abi/){target=_blank} of the contract. +Similar functionality is available for EVM indexing through the [`squid-evm-typegen`](https://docs.subsquid.io/evm-indexing/squid-evm-typegen/){target=_blank} tool. It generates TypeScript modules for handling EVM logs and transactions based on a [JSON ABI](https://docs.ethers.org/v5/api/utils/abi/){target=_blank} of the contract. For our squid we will need such a module for the [ERC-721](https://eips.ethereum.org/EIPS/eip-721){target=_blank}-compliant part of the contracts' interfaces. Once again, the template repository already includes it, but it is still important to explain what needs to be done in case one wants to index a different type of contract. From 1cb73dab916fc855b4c29adba3b1b0cc116a1bae Mon Sep 17 00:00:00 2001 From: Erin Shaben Date: Fri, 1 Sep 2023 14:46:07 -0400 Subject: [PATCH 09/11] update number order --- builders/build/eth-api/libraries/ethersjs.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builders/build/eth-api/libraries/ethersjs.md b/builders/build/eth-api/libraries/ethersjs.md index 706f5fc46..15f117175 100644 --- a/builders/build/eth-api/libraries/ethersjs.md +++ b/builders/build/eth-api/libraries/ethersjs.md @@ -303,7 +303,7 @@ Next, you will create the script for this file and complete the following steps: 1. Import the contract file from `compile.js` 2. [Set up the Ethers provider](#setting-up-the-ethers-provider) 3. 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** -3. Create a wallet using the `privateKey` and `provider` from the previous steps. The wallet instance is used to sign transactions +4. Create a wallet using the `privateKey` and `provider` from the previous steps. The wallet instance is used to sign transactions 5. Load the contract `bytecode` and `abi` for the compiled contract 6. Create a contract instance with signer using the `ethers.ContractFactory` function, providing the `abi`, `bytecode`, and `wallet` as parameters 7. Create the asynchronous `deploy` function that will be used to deploy the contract From 80006bb2e78bfde0e66965575f74d7bfafbf479a Mon Sep 17 00:00:00 2001 From: Erin Shaben Date: Fri, 1 Sep 2023 15:47:06 -0400 Subject: [PATCH 10/11] fix capitalization --- .snippets/code/web3-contract-local/reset.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.snippets/code/web3-contract-local/reset.js b/.snippets/code/web3-contract-local/reset.js index 8a0fd93fa..f898d593f 100644 --- a/.snippets/code/web3-contract-local/reset.js +++ b/.snippets/code/web3-contract-local/reset.js @@ -15,7 +15,7 @@ const accountFrom = { }; const contractAddress = 'INSERT_CONTRACT_ADDRESS'; -// 4. Create Contract Instance +// 4. Create contract instance const incrementer = new web3.eth.Contract(abi, contractAddress); // 5. Build reset tx @@ -25,7 +25,7 @@ const resetTx = incrementer.methods.reset(); const reset = async () => { console.log(`Calling the reset function in contract at address: ${contractAddress}`); - // 7. Prepare and Sign Tx with PK + // 7. Prepare and sign tx with PK const createTransaction = await web3.eth.accounts.signTransaction( { to: contractAddress, From a5e7cd9b92ee30aaade3d9dd86528ca853728c6b Mon Sep 17 00:00:00 2001 From: Erin Shaben Date: Fri, 1 Sep 2023 15:54:52 -0400 Subject: [PATCH 11/11] add save snippet line --- builders/build/eth-api/libraries/web3js.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/builders/build/eth-api/libraries/web3js.md b/builders/build/eth-api/libraries/web3js.md index cec6c0a93..cd697b136 100644 --- a/builders/build/eth-api/libraries/web3js.md +++ b/builders/build/eth-api/libraries/web3js.md @@ -88,6 +88,8 @@ The simplest way to get started with each of the networks is as follows: const web3 = new Web3('{{ networks.development.rpc_url }}'); ``` +Save this code snippet as you'll need it for the scripts that are used in the following sections. + ## Send a Transaction {: #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.