Giter Site home page Giter Site logo

truffle-starter-kit's Introduction


Chainlink Truffle logo


Open in Gitpod

Chainlink Truffle Starter Kit

Implementation of the following 4 Chainlink features using the Truffle development environment:

Getting Started

It's recommended that you've gone through the Truffle getting started documentation before proceeding here.

Open in Gitpod

Requirements

  • git
    • You'll know you did it right if you can run git --version and you see a response like git version x.x.x
  • Nodejs
    • You'll know you've installed nodejs right if you can run:
      • node --versionand get an ouput like: vx.x.x
  • Yarn instead of npm
    • You'll know you've installed yarn right if you can run:
      • yarn --version And get an output like: x.x.x
      • You might need to install it with npm

If you're familiar with npx and npm instead of yarn, you can use npx for execution and npm for installing dependencies.

Quickstart

  1. Clone and install dependencies

After installing all the requirements, run the following:

git clone https://github.com/smartcontractkit/truffle-starter-kit/
cd truffle-starter-kit

Then:

yarn

or

npm i
  1. You can now do stuff!

Run truffle test

This will run your JavaScript, TypeScript, and Solidity tests underneath the ./test folder. If the development network is not specified in your truffle-config, truffle test will automatically bring up and tear down a local network on localhost:8545.

Usage

If you run truffle --help you'll get an output of all the tasks you can run.

Deploying Contracts

In your truffle-config.js you'll see section like:

  networks: {

This section of the file is where you define which networks you want to interact with. You can read more about that whole file in the Truffle documentation.. To deploy a contract, you would call:

truffle migrate --network <NETWORK>

This will deploy your contracts to the network you specify. If you do not specify network, by default truffle migrate will deploy to the development network if it is defined in your truffle-config.

If you'd like to interact with your deployed contracts, skip down to Interacting with Deployed Contracts.

Run a Local Network

One of the best ways to test and interact with smart contracts is with a local network. To start a local network with all your contracts in it, run the following:

ganache -d

This will start ganache, a local blockchain with private keys, pre-funded wallets, and an endpoint to potentially add to an EVM wallet. -d specifies a deterministic wallet. You can see all other ways you can configure Ganache here.

In our truffle-config, we've defined the local ganache network. To migrate, simply call:

truffle migrate --network ganache

Using a Testnet or Live Network (like Mainnet or Polygon)

To interact with a live or test network, you will need ETH and LINK token by following these steps:

  1. Get some Sepolia Testnet ETH and LINK

Head over to the Chainlink faucets and get some ETH and LINK. Please follow the chainlink documentation if unfamiliar.

  1. Create VRF V2 subscription

Head over to VRF Subscription Page and create the new subscription. Save your subscription ID and place it in your helper-truffle-config.js under subId.

Truffle Dashboard

This is our recommended way of deploying your contracts. With Truffle dashboard, you can deploy contracts and sign transactions through MetaMask - so you never exposing your private key! To do so, run:

truffle dashboard

This should bring up the dashboard on localhost:24012, where you can connect your wallet to the network of your choice. Note that if you want to deploy to a testnet or mainnet, you'll need ETH to pay the gas fees. Then, if you want to migrate your contracts, simply call:

truffle migrate --network dashboard

You should then see prompts to sign the transactions on the dashboard view. Check out this video on how to use it with Truffle and HardHat here.

Environment configuration

In some cases, you might want your transactions to be automatically signed. In which case, you will need to provide your wallet's private key

  1. An RPC URL
  2. A private key
  3. ETH & LINK token (either testnet or real)

Let's look at an example of setting these up using the Sepolia testnet.

Sepolia Ethereum Testnet Setup

First, we will need to set environment variables. We can do so by setting them in our .env file (create it if it's not there). You can also read more about environment variables from the linked twilio blog. You'll find a sample of what this file will look like in .env.example

IMPORTANT: MAKE SURE YOU'D DONT EXPOSE THE KEYS YOU PUT IN THIS .env FILE. By that, I mean don't push them to a public repo, and please try to keep them keys you use in development not associated with any real funds.

  1. Set your SEPOLIA_RPC_URL environment variable.

You can get one for free from Infura, Alchemy, or Moralis. This is your connection to the blockchain.

  1. Set your PRIVATE_KEY environment variable.

This is your private key from your wallet, ie MetaMask. This is needed for deploying contracts to public networks.

WARNING WARNING WARNING

When developing, it's best practice to use a Metamask that isn't associated with any real money. A good way to do this is to use Truffle Dashboard. Alternatively, make a new browser profile (on Chrome, Brave, Firefox, etc) and install Metamask on that brower, and never send this wallet money.

Don't commit and push any changes to .env files that may contain sensitive information, such as a private key! If this information reaches a public GitHub repository, someone can use it to check if you have any Mainnet funds in that wallet address, and steal them!

.env example:

SEPOLIA_RPC_URL='https://sepolia.infura.io/v3/asdfasdfasdfasdf'
PRIVATE_KEY='abcdef'

bash example

export SEPOLIA_RPC_URL='https://sepolia.infura.io/v3/asdfasdfasdfasdf'
export PRIVATE_KEY='abcdef'

For other networks like mainnet and polygon, you can use different environment variables for your RPC URL and your private key. See the truffle-config.js to learn more.

Uncomment the sepolia network in your truffle-config. You should now be all setup! You can run any command and just pass the --network sepolia now!

To deploy all contracts:

truffle migrate --network sepolia

Test

Tests are located in the test directory, and are split between unit tests and staging/testnet tests. Unit tests should only be run on local environments, and staging tests should only run on live environments.

To run unit tests:

truffle test

Interacting with Deployed Contracts

After deploying your contracts, the deployment output will give you the contract addresses as they are deployed. You can then use these contract addresses in conjunction with Truffle scripts to perform operations on each contract. Alternatively, if you want to interact with contracts on the fly, you can read about how to use truffle console or truffle develop here.

Chainlink Price Feeds

The Price Feeds consumer contract has one script, to read the latest price of a specified price feed contract.

You can deploy just the price feed consumer with:

truffle migrate --f 3 --to 3 --network <NETWORK>

After deployment, run the following:

truffle exec scripts/readPriceConsumer.js --network <NETWORK>

Request & Receive Data

The API Consumer contract has one script, to request data from the API and wait for a response.

You can deploy just the API Consumer with:

truffle migrate --f 4 --to 4 --network <NETWORK>

After deployment, run the following:

yarn truffle exec scripts/requestAndReadAPI.js --network <NETWORK>

VRF Get a random number

The VRF Consumer contract has one script, to request a random number and wait for a response.

You can deploy just the VRF Consumer with:

truffle deploy --f 5 --to 5 --network <NETWORK>

After deployment, you'll need to add your contract address to your subscription. Head over to vrf.chain.link and add your consumer.

Then, run the following:

truffle exec scripts/requestAndReadRandomNumber.js --network <NETWORK>

Keepers

The Keepers Consumer contract has one script, to check the upkeep. After deployment, run the following:

truffle exec scripts/checkUpkeep.js --network <NETWORK>

To see everything in action, you'll want to set up a consumer at keepers.chain.link.

Verify on Etherscan

You'll need an ETHERSCAN_API_KEY environment variable. You can get one from the Etherscan API site.. If you have it set, your deploy script will try to verify them by default, but if you want to verify any manually, you can run:

truffle run verify <CONTRACT> --network <NETWORK>

example:

truffle run verify PriceConsumerV3 --network sepolia

Contributing

Contributions are always welcome! Open a PR or an issue!

Thank You!

Resources

truffle-starter-kit's People

Contributors

aadorian avatar alexroan avatar andrejrakic avatar dependabot-preview[bot] avatar dependabot[bot] avatar emilyjlin95 avatar gmsteuart avatar henrynguyen5 avatar maikotrindade avatar patrickalphac avatar rgottleber avatar se3000 avatar shih-yu avatar solangegueiros avatar thodges-gh avatar toine08 avatar zeuslawyer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

truffle-starter-kit's Issues

update kovan and rinkeby oracle and jobids

To the Chainlink devrel node

kovan:
    oracle: '0xc57b33452b4f7bb189bb5afae9cc4aba1f7a4fd8'
    jobId: 'd5270d1c311941d0b08bead21fea7747'
  rinkeby:
    oracle: '0xc57b33452b4f7bb189bb5afae9cc4aba1f7a4fd8'
    jobId: '6b88e0402e5d415eb946e528b8e0c7ba'

Upgrade box to v0.6.6

  1. VRF doesnt work with the current truffle box (needs solc v 0.6.6)
  2. Upgrade contracts to v0.6 of solidity
  3. Fix the fund-contracts script - right now it just hangs due to some recent changes of metamask

smartcontractkit/box-box doesn't exist. If you believe this is an error, please contact Truffle support.

When trying to run truffle unbox smartcontractkit/box

I have the following error:

Starting unbox...
=================

✔ Preparing to download box
✖ Downloading
Unbox failed!
✖ Downloading
Unbox failed!
Error: Truffle Box at URL https://github.com:smartcontractkit/box-box doesn't exist. If you believe this is an error, please contact Truffle support.
    at /usr/lib/node_modules/truffle/build/webpack:/packages/box/dist/lib/utils/unbox.js:42:1
    at Generator.throw (<anonymous>)
    at rejected (/usr/lib/node_modules/truffle/build/webpack:/packages/box/dist/lib/utils/unbox.js:6:41)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
Truffle v5.3.12 (core: 5.3.12)
Node v16.4.0

Node v16.4.0
Truffle v5.3.12

I was able to unbox other boxes such as react.

Unbox failed

Steps:

  1. npm install truffle -g
  2. mkdir myChainlinkProject
  3. cd myChainlinkProject
  4. truffle unbox smartcontractkit/box

Error:

Error: Command failed: bash unbox.sh && rm unbox.sh
    at checkExecSyncError (child_process.js:640:11)
    at Object.execSync (child_process.js:676:15)
    at Object.installBoxDependencies (C:\Users\alexa\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\box\dist\lib\utils\unbox.js:136:1)
    at Object.setUpBox (C:\Users\alexa\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\box\dist\lib\utils\index.js:66:1)
    at C:\Users\alexa\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\box\dist\box.js:183:1
    at Generator.next (<anonymous>)
    at fulfilled (C:\Users\alexa\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\box\dist\box.js:5:42)
Truffle v5.3.14 (core: 5.3.14)
Node v14.17.0```

Getting error while running fund-contract.js script

I'm facing an error when I'm trying to execute fund-contract.js. I'm not sure if I'm using some incompatible version of Truffle or so. The deployment worked like a charm.

npx truffle exec scripts/fund-contract.js --network kovan

Error:

Funding contract: 0x1123c6967701ab4a86f7781Fa4E3091383314121
Transaction: 0x0e5447b9f367d76a0bc4c6b9683d4e8d8f7e49c28dc4aeac07ed2e11c0e80d91 exited with an error (status 0) after consuming all gas.
     Please check that the transaction:
     - satisfies all conditions set by Solidity `assert` statements.
     - has enough gas to execute the full transaction.
     - does not trigger an invalid opcode by other means (ex: accessing an array out of bounds).
     
#Versions
Truffle v5.3.6 (core: 5.3.6)
Node v14.16.1
Npm v6.14.12
Yarn 1.22.10
MacOS 11.2.1

This is what I see at Etherscan.

Do you have any idea what I'm doing wrong? 🐻 with me 🙂

Error when running npm install

Hi team,

I've been struggling for days to be able to install the dependencies for this starter kit.
Whenever I run the "npm install" command right after successful unboxing I get the following error:
`npm ERR! code 128
npm ERR! command failed
npm ERR! command git ls-remote ssh://[email protected]/ethereumjs/ethereumjs-abi.git
npm ERR! fatal: Could not read from remote repository.
npm ERR!
npm ERR! Please make sure you have the correct access rights
npm ERR! and the repository exists.

npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\Mariano Collarte\AppData\Local\npm-cache_logs\2021-05-12T01_02_24_897Z-debug.log
`
I've setup the ssh account in my github profile and checked connection. This repository seems to be deprecated so I'm not sure why it is pulling it however the npm install fails and the node_modules folder is not being created.

Any help you can provide would be awesome!

Thanks

fund-contract.js hangs on attempting to fund contracts

Issue

In fund-contract.js when running npx truffle exec scripts/fund-contract.js --network live after successfully migrating(deploying) the default contract MyContract.sol, the contract will hang at const token = await LinkToken.at(tokenAddress); due to what is believed to be metamask changes. See the truffle issue for more information.

Steps to Reproduce

Set up a new Chainlinked project, you can follow the documentation to do this here

mkdir MyChainlinkedProject
cd MyChainlinkedProject/
truffle unbox smartcontractkit/box

Due to the issue with truffle & metamask (mentioned above) you will have to changed one package in package.json:

"@truffle/hdwallet-provider": "^1.0.30", -> "@truffle/hdwallet-provider": "next",
then run:
npm install

Then proceed to set your RPC_URL to interact with the ropsten chain and your MNEMONIC (seed phrase) from your metamask account.

export RPC_URL=asdfasfas
export MNEMONIC='cat dog frog......'

Run the migration script to deploy 'MyContract.sol'

npm run migrate:live

After it is deployed, just run

npx truffle exec scripts/fund-contract.js --network live

After some testing, the output will hang on the fund-contracts file on this line:

const token = await LinkToken.at(tokenAddress);

I was able to get it past this line by adding some pieces of code and a try catch to see the error:

module.exports = async (callback) => {
  try {
    const mc = await MyContract.deployed();
    const tokenAddress = await mc.getChainlinkToken();
    LinkToken.setProvider(process.env.RPC_URL);
    const token = await LinkToken.at(tokenAddress);
    console.log("Funding contract:", mc.address);
    const tx = await token.transfer(mc.address, payment, {
      from: web3.currentProvider.addresses[0],
    });
    callback(tx.tx);
  } catch (error) {
    callback(error);
  }
};

And the error we get is:

Funding contract: 0x64738dc4359739c402fE1523Ff96308DDa47ea5C
Error: Returned error: The method eth_sendTransaction does not exist/is not available
    at module.exports (/Users/patrick/code/demos/truffle/MyChainlinkedProject/scripts/fund-contract.js:20:28)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
Truffle v5.1.32 (core: 5.1.32)
Node v14.5.0

Expected Behavior

The contract that was deployed during npm run migrate:live will be funded

Actual Results

The script hangs and never completes.

Environment

  • Operating System: macOS Catalina 10.15.5
  • Ethereum client: Infura
  • Truffle version (truffle version): ("@truffle/hdwallet-provider": "^1.0.36") / ("@truffle/hdwallet-provider": "next")
  • node version (node --version): v14.5.0
  • npm version (npm --version): 6.14.5

fund-contract script returns non-zero exit code under truffle exec even when it succeeds

When running ./scripts/fund-contract.js using truffle exec, the transaction is returned to the callback function where an error is supposed to be returned. This means that the script is reporting an error even under successful execution, example:

/chainlink # truffle exec scripts/fund-contract.js --network cldev
Using network 'cldev'.

Funding contract: 0xa8B323695d987DdE23F63e6B4500FBc84Cd3cd4e
0x3d210eb43710fe4d777a08d871bde9f7bf63b95014c346275a1d203693f2afd9
Truffle v5.2.3 (core: 5.2.3)
Node v10.24.0
/chainlink # echo $?
1

Line 20 here: https://github.com/smartcontractkit/box/blob/master/scripts/fund-contract.js#L20 uses the callback function to return a transaction ID, although according to https://www.trufflesuite.com/docs/truffle/getting-started/writing-external-scripts this should be used for error reporting

add support for testnet kovan 'out of the box'

The example provided in the README does not work when deploying to dev / cldev (localhost), as there is no Chainlink node on localhost.

Testing on mainnet is prohibitive expensive.

Updating truffle-config.js and README.md to use kovan testnet , as well as adding a note that it will not work on localhost without deploying a Chainlink node to localhost, would help to avoid time consuming efforts by new users.

Migration of MyContract.sol fails

After unboxing the Truffle Starter kit and installing dependencies with npm, migration of MyContract.sol fails.
.....\ChainlinkTest\node_modules\request\request.js:816 var e = new Error('ESOCKETTIMEDOUT') ^ Error: PollingBlockTracker - encountered an error while attempting to update latest block: Error: ESOCKETTIMEDOUT at ClientRequest.<anonymous> .....\ChainlinkTest\node_modules\request\request.js:816:19) at Object.onceWrapper (node:events:513:28) at ClientRequest.emit (node:events:394:28) at TLSSocket.emitRequestTimeout (node:_http_client:761:9) at Object.onceWrapper (node:events:513:28) at TLSSocket.emit (node:events:406:35) at TLSSocket.Socket._onTimeout (node:net:475:8) at listOnTimeout (node:internal/timers:557:17) at processTimers (node:internal/timers:500:7) at PollingBlockTracker._performSync (C:\Users\rapha\Documents\NFT\ETH201\ChainlinkTest\node_modules\eth-block-tracker\src\polling.js:51:24) at runMicrotasks (<anonymous>) at processTicksAndRejections (node:internal/process/task_queues:96:5)

Revamp

This repo needs an overhaul

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.