From 7db941531b43fe4427c8a47d18503bc27a6a60bd Mon Sep 17 00:00:00 2001 From: Gabriel Cardona Date: Tue, 8 Mar 2022 19:09:54 -0800 Subject: [PATCH] Updates to truffle tutorial (#625) * Update truffle tutorial and scripts/fund-cchain-addresses.js to work w/ the Avalanche network runner. * Fix command. --- ...sing-truffle-with-the-avalanche-c-chain.md | 29 ++++++++----------- scripts/fund-cchain-addresses.js | 6 ++-- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/docs/build/tutorials/smart-contracts/using-truffle-with-the-avalanche-c-chain.md b/docs/build/tutorials/smart-contracts/using-truffle-with-the-avalanche-c-chain.md index 966c956169e..61a8e1bf2b9 100644 --- a/docs/build/tutorials/smart-contracts/using-truffle-with-the-avalanche-c-chain.md +++ b/docs/build/tutorials/smart-contracts/using-truffle-with-the-avalanche-c-chain.md @@ -10,27 +10,22 @@ You've completed [Run an Avalanche Node](../nodes-and-staking/run-avalanche-node ## Dependencies -* [Avash](https://github.com/ava-labs/avash) is a tool for running a local Avalanche network. It's similar to Truffle's [Ganache](https://www.trufflesuite.com/ganache). +* [Avalanche Network Runner](https://github.com/ava-labs/avalanche-network-runner) is a tool for running a local Avalanche network. It's similar to Truffle's [Ganache](https://www.trufflesuite.com/ganache). * [NodeJS](https://nodejs.org/en) v8.9.4 or later. * Truffle, which you can install with `npm install -g truffle` ## Start up a local Avalanche network -[Avash](https://github.com/ava-labs/avash) allows you to spin up private test network deployments with up to 15 AvalancheGo nodes out-of-the-box. Avash supports automation of regular tasks via lua scripts. This enables rapid testing against a wide variety of configurations. The first time you use avash you'll need to [install and build it](https://github.com/ava-labs/avash#quick-setup). - -Start a local five node Avalanche network: +[Avalanche Network Runner](https://github.com/ava-labs/avalanche-network-runner) allows you to spin up private test network deployments. Start a local five node Avalanche network: ```text -cd /path/to/avash +cd /path/to/avalanche-network-runner # build Avash if you haven't done so -go build -# start Avash -./avash # start a five node staking network -runscript scripts/five_node_staking.lua +./go run examples/local/fivenodenetwork/main.go ``` -A five node Avalanche network is running on your machine. When you want to exit Avash, run `exit`, but don't do that now, and don't close this terminal tab. +A five node Avalanche network is running on your machine. Network will run until you CTRL + C to exit. ## Create truffle directory and install dependencies @@ -48,10 +43,10 @@ Create and enter a new directory named `truffle`: mkdir truffle; cd truffle ``` -Use `npm` to install [web3](https://web3js.readthedocs.io), which is a library through which we can talk to the EVM: +Use `npm` to install [web3](https://web3js.readthedocs.io), which is a library through which we can talk to the EVM and [AvalancheJS](../../tools/avalanchejs/README.md) which is used for cross chain swaps. ```text -npm install web3 -s +npm install web3 avalanche -s ``` We'll use web3 to set an HTTP Provider which is how web3 will speak to the EVM. Lastly, create a boilerplace truffle project: @@ -77,6 +72,7 @@ const HDWalletProvider = require("@truffle/hdwallet-provider"); const protocol = "http"; const ip = "localhost"; const port = 9650; +Web3.providers.HttpProvider.prototype.sendAsync = Web3.providers.HttpProvider.prototype.send; const provider = new Web3.providers.HttpProvider( `${protocol}://${ip}:${port}/ext/bc/C/rpc` ); @@ -119,7 +115,7 @@ In the `contracts` directory add a new file called `Storage.sol` and add the fol ```text // SPDX-License-Identifier: MIT -pragma solidity >=0.4.22 <0.8.0; +pragma solidity >=0.4.22 <0.9.0; /** * @title Storage @@ -192,7 +188,7 @@ You can view imported accounts with truffle console. To open the truffle console: ```bash -$ truffle console --network development +truffle console --network development ``` Note: If you see `Error: Invalid JSON RPC response: "API call rejected because chain is not done bootstrapping"`, you need to wait until network is bootstrapped and ready to use. It should not take too long. @@ -232,7 +228,7 @@ Notice that `accounts[0]` (default account) has some balance, while `accounts[1] There is a convenient script that funds the `accounts` list . You can find it [here](https://raw.githubusercontent.com/ava-labs/avalanche-docs/master/scripts/fund-cchain-addresses.js). You can also download it using this command: ```text -wget -nd -m https://raw.githubusercontent.com/ava-labs/avalanche-docs/master/scripts/fund-cchain-addresses.js; +wget -nd -m https://raw.githubusercontent.com/ava-labs/avalanche-docs/master/scripts/fund-cchain-addresses.js ``` You can run the script with: @@ -365,7 +361,7 @@ truffle(development)> instance.store(1234) You should see something like: -```json +```js { tx: '0x10afbc5e0b9fa0c1ef1d9ec3cdd673e7947bd8760b22b8cdfe08f27f3a93ef1e', receipt: { @@ -416,4 +412,3 @@ You should see the number you stored. ## Summary Now you have the tools you need to launch a local Avalanche network, create a truffle project, as well as create, compile, deploy and interact with Solidity contracts. - diff --git a/scripts/fund-cchain-addresses.js b/scripts/fund-cchain-addresses.js index 5f2914884fa..bd0212d9900 100644 --- a/scripts/fund-cchain-addresses.js +++ b/scripts/fund-cchain-addresses.js @@ -8,7 +8,7 @@ const sleep = (ms) => { const ip = "localhost"; const port = 9650; const protocol = "http"; -const networkID = 12345; +const networkID = 1337 const avalanche = new avalanche_1.Avalanche(ip, port, protocol, networkID); const mstimeout = 3000; const xchain = avalanche.XChain(); @@ -48,9 +48,9 @@ const cHexAddresses = [ "0x61e0b3cd93f36847abbd5d40d6f00a8ec6f3cffb", "0x0fa8ea536be85f32724d57a37758761b86416123", ]; -const cChainBlockchainID = utils_1.Defaults.network["12345"].C.blockchainID; +const cChainBlockchainID = utils_1.Defaults.network[networkID].C.blockchainID const cChainBlockchainIdBuf = bintools.cb58Decode(cChainBlockchainID); -const xChainBlockchainID = utils_1.Defaults.network["12345"].X.blockchainID; +const xChainBlockchainID = utils_1.Defaults.network[networkID].X.blockchainID const xChainBlockchainIdBuf = bintools.cb58Decode(xChainBlockchainID); const exportedOuts = []; const outputs = [];