Chainlink 101 Course

Hello, I have a question where is this _volume located or initialized in?

volume

1 Like

Its an argument for the fulfill function, which is used later in its logic.

Hope it helps, any other questions, let us know :nerd_face:

Carlos Z

1 Like

Thank you for the previous answer. :slight_smile:

I have another question on the lottery contract where does the player deposit the entrance fee into the contract?

Hi, I am having issues with the compiler, saying ^0.6.6 does not work, so I changed the solidity version to ^0.8.9 like recommended, but there is still an error. Can someone help?
Thank you, Brianna.

2021-09-29

2021-09-29updated

Hey @Bripeachy, hope you are well.

Could you please share your truffle.config file? which is the version specified in that one? should be at least the same than your contract.

Carlos Z

Hi Carlos (@thecil),

I’m have one bug and one question:

The bug is this: I’ve unboxed the chainlink examples via truffle, and I’ve run npm install and installed hardhat, but when I try to run the tests I get the following error:

An unexpected error occurred:

ReferenceError: contract is not defined
    at Object.<anonymous> (/Users/richardjamieson/Documents/GitHub/chainlink_examples/test/MyContract_test.js:5:1)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at /Users/richardjamieson/Documents/GitHub/chainlink_examples/node_modules/hardhat/node_modules/mocha/lib/mocha.js:349:36
    at Array.forEach (<anonymous>)
    at Mocha.loadFiles (/Users/richardjamieson/Documents/GitHub/chainlink_examples/node_modules/hardhat/node_modules/mocha/lib/mocha.js:346:14)
    at Mocha.run (/Users/richardjamieson/Documents/GitHub/chainlink_examples/node_modules/hardhat/node_modules/mocha/lib/mocha.js:1006:10)
    at testFailures (/Users/richardjamieson/Documents/GitHub/chainlink_examples/node_modules/hardhat/src/builtin-tasks/test.ts:57:13)
    at new Promise (<anonymous>)
    at SimpleTaskDefinition.action (/Users/richardjamieson/Documents/GitHub/chainlink_examples/node_modules/hardhat/src/builtin-tasks/test.ts:56:32)
    at Environment._runTaskDefinition (/Users/richardjamieson/Documents/GitHub/chainlink_examples/node_modules/hardhat/src/internal/core/runtime-environment.ts:217:14)
    at Environment.run (/Users/richardjamieson/Documents/GitHub/chainlink_examples/node_modules/hardhat/src/internal/core/runtime-environment.ts:129:14)
    at SimpleTaskDefinition.action (/Users/richardjamieson/Documents/GitHub/chainlink_examples/node_modules/hardhat/src/builtin-tasks/test.ts:102:28)
    at Environment._runTaskDefinition (/Users/richardjamieson/Documents/GitHub/chainlink_examples/node_modules/hardhat/src/internal/core/runtime-environment.ts:217:14)
    at Environment.run (/Users/richardjamieson/Documents/GitHub/chainlink_examples/node_modules/hardhat/src/internal/core/runtime-environment.ts:129:14)
    at main (/Users/richardjamieson/Documents/GitHub/chainlink_examples/node_modules/hardhat/src/internal/cli/cli.ts:203:5)
(node:61729) Warning: Accessing non-existent property 'INVALID_ALT_NUMBER' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)
(node:61729) Warning: Accessing non-existent property 'INVALID_ALT_NUMBER' of module exports inside circular dependency
(node:61729) Warning: Accessing non-existent property 'VERSION' of module exports inside circular dependency

The part of the test code that this is referring to can be seen in this excerpt:

/* eslint-disable @typescript-eslint/no-var-requires */
const { oracle } = require('@chainlink/test-helpers')
const { expectRevert, time } = require('@openzeppelin/test-helpers')

contract('MyContract', accounts => {
  const { LinkToken } = require('@chainlink/contracts/truffle/v0.4/LinkToken')
  const { Oracle } = require('@chainlink/contracts/truffle/v0.6/Oracle')
  const MyContract = artifacts.require('MyContract')

  const defaultAccount = accounts[0]
  const oracleNode = accounts[1]
  const stranger = accounts[2]
  const consumer = accounts[3]

  // These parameters are used to validate the data was received
  // on the deployed oracle contract. The Job ID only represents
  // the type of data, but will not work on a public testnet.
  // For the latest JobIDs, visit a node listing service like:
  // https://market.link/
  const jobId = web3.utils.toHex('4c7b7ffb66b344fbaa64995af81e355a')
  const url =
    'https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD,EUR,JPY'
  const path = 'USD'
  const times = 100

It seems that it doesn’t recognise the type ‘contract’ in line 5 - perhaps there is an extra import that I need in this file?

And then the second thing is a question: I am wanting to do an API call to the coinmarketcap API, and it requires a key. Do you know where in the chainlink contract I can put the key? Here is what the coinmarketcap docs say about using their API:

### Using Your API Key

You may use any  *server side*  programming language that can make HTTP requests to target the CoinMarketCap API. All requests should target domain  `https://pro-api.coinmarketcap.com` .

You can supply your API Key in REST API calls in one of two ways:
0)
1. Preferred method: Via a custom header named  `X-CMC_PRO_API_KEY`
2. Convenience method: Via a query string parameter named  `CMC_PRO_API_KEY`

So this is what I’ve tried:

But doesn’t seem to be working yet. I’ve also considered request.add(“headers”, "…)

Any help much appreciated.

Thanks!
Richard

1 Like

Sorry, I have one more follow up question - I have got the chainlink API_consumer example contract working in remix on Kovan, but when I try to deploy it to either Binance Smart Chain or Binance Smart Chain Testnet, I’m getting a deploy error - any idea why this might be?

Here is the error in remix:
Screenshot 2021-11-03 at 13.44.41

The only things I’ve changed in the contract are the oracle and the job id. And my network settings for BSC and BSC testnet are good - I’ve used them to deploy other contracts.

Any help much appreciated!

R

1 Like

Hey @RichJamo, hope you are well.

Now about the first post, are you trying it with hardhat? because it might require extra configuration that is not explained in the course (because the course was made with truffle).

Now the second issue with BSC, might be because the chainlink contract does not exist on that network, have you checked that? because remember that you are interacting with chainlink VRF through the address of that contract, which might not be the same in the BSC network.

Carlos Z

@thecil, Hi Carlos, Im in the Lottery part of course nad I am having an issue compiling, think you can take a look for me?

project:/contracts/Lottery.sol:31:15: TypeError: Type int_const 100000000000000000 is not implicitly convertible to expected type address.
        fee = 100000000000000000; // 0.1 LINK
              ^----------------^
,project:/contracts/Lottery.sol:79:29: TypeError: Wrong argument count for function call: 3 arguments given but expected 2.
        bytes32 requestId = requestRandomness(keyHash, fee, userProvidedSeed);
                            ^-----------------------------------------------^

hers my contract

pragma solidity ^0.6.6;

import "@chainlink/contracts/src/v0.6/interfaces/AggregatorV3Interface.sol";
import "@chainlink/contracts/src/v0.6/vendor/SafeMathChainlink.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@chainlink/contracts/src/v0.6/VRFConsumerBase.sol";


contract Lottery is VRFConsumerBase, Ownable {
    using SafeMathChainlink for uint256;

    enum LOTTERY_STATE {OPEN,CLOSED,CALCULATING_WINNER}
    LOTTERY_STATE public lotteryState;
    AggregatorV3Interface internal ethUsdPriceFeed;
    uint256 public usdEntryFee;
    address payable[] public players;
    uint256 public randomness;
    address public fee;
    bytes32 public keyHash;
    event RequestedRandomness(bytes32 requestId);


    constructor(address _ethUsdPriceFeed, address _vrfCoordinator, address _link, bytes32 _keyHash) 
        VRFConsumerBase(
            _vrfCoordinator,
            _link
        )public {
        ethUsdPriceFeed = AggregatorV3Interface(_ethUsdPriceFeed);
        usdEntryFee = 50;
        lotteryState = LOTTERY_STATE.CLOSED;
        fee = 100000000000000000; // 0.1 LINK
        keyHash = _keyHash;

    }
    function enter() public payable {
        require(msg.value >= getEntranceFee(), "Not enough ETH to enter!");
        require(lotteryState == LOTTERY_STATE.OPEN);
        players.push(msg.sender);

    }
    function getEntranceFee() public view returns(uint256){
        uint256 precision = 1 * 10 ** 18;
        uint256 price = getLatestEthUsdPrice(); //8 decimals
        uint256 costToEnter = (precision / price) * (usdEntryFee * 10000000);
        return costToEnter;


    }
    function getLatestEthUsdPrice() public view returns (uint256){
         (
            uint80 roundID,
            int price,
            uint startedAt,
            uint timeStamp,
            uint80 answeredInRound 
        ) = ethUsdPriceFeed.latestRoundData();
        return uint256(price);

    }

    //Access control functions

    function startLottery() public onlyOwner {
        require(lotteryState == LOTTERY_STATE.CLOSED, "Can't start a new lottery");
        lotteryState == LOTTERY_STATE.OPEN;
        randomness = 0;

    }

    function endLottery(uint256 userProvidedSeed) public onlyOwner{
        require(lotteryState == LOTTERY_STATE.OPEN, "Can't end lottery yet");
        lotteryState == LOTTERY_STATE.CALCULATING_WINNER;
        pickWinner(userProvidedSeed);


    }
    function pickWinner(uint256 userProvidedSeed) private returns(bytes32) {
        require(lotteryState == LOTTERY_STATE.CALCULATING_WINNER, "Needs to be calculating the winner!");
        bytes32 requestId = requestRandomness(keyHash, fee, userProvidedSeed);
        emit RequestedRandomness(requestId);

    }
    function fulfillRandomness(bytes32 requestId, uint256 randomness)internal override{
        require(randomness > 0, "random number not found");
        uint256 index = randomness % players.length;
        players[index].transfer(address(this).balance);
        players = new address payable[](0);
        lotteryState = LOTTERY_STATE.CLOSED;
        randomness = randomness;

    }
}

When I truffle compile, get this error, it was compiling just fine before requestId section of course.
Thank you.

1 Like

It means that the function requestRandomness() is expecting 2 arguments, instead of 3.

Check the contract VRFConsumerBase, code line 158.
located at: node_modules@chainlink\contracts\src\v0.6\VRFConsumerBase.sol

function requestRandomness(bytes32 _keyHash, uint256 _fee)

So you only have to send 2 arguments, the keyHash and fee, remove userProvidedSeed which is not need it anymore (its now located at code line 131 in the same VRF contract above).

Hope this helps. :nerd_face:

Carlos Z

This one, you just have a wrong variable type declaration.
At code line 31 you have: address public fee; instead of integer.

1 Like

@thecil, Awesome that did it , thanks buddy. :vulcan_salute:

1 Like

@thecil, Man, just when I think I’m in the clear, lol. I’m having the HDWalletProvider issue again,

I’m trying to deploy lottery contract, I’ve setup Mnemonic, RPC_Url, but error keeps popping up like last time in truffle section of pervious course.

Error: Unknown arguments format passed to new HDWalletProvider. Please check your configuration and try again
    at Object.getOptions (C:\Users\jflor\Documents\Solidity-Lottery\node_modules\@truffle\hdwallet-provider\src\constructor\getOptions.ts:143:11)      
    at new HDWalletProvider (C:\Users\jflor\Documents\Solidity-Lottery\node_modules\@truffle\hdwallet-provider\src\index.ts:68:9)
    at Object.provider (C:\Users\jflor\Documents\Solidity-Lottery\truffle-config.js:28:16)
    at Object.getProvider (C:\Users\jflor\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\provider\index.js:20:1)
    at Object.create (C:\Users\jflor\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\provider\index.js:13:1)
    at TruffleConfig.get [as provider] (C:\Users\jflor\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\config\dist\configDefaults.js:235:1)
    at Object.detect (C:\Users\jflor\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\environment\environment.js:19:1)
    at Object.run (C:\Users\jflor\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\commands\migrate.js:201:1)
    at Command.run (C:\Users\jflor\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\command.js:183:1)

.env file

MNEMONIC='1c49d2330c06ba699edb3ecc24a4f674db2f1b38d7ab699a6d4936..........'
RPC_URL='https://kovan.infura.io/v3/7d82a16ccf554b9a9062...............'

2_deploy.js

const Lottery = artifacts.require('Lottery')
const {LinkToken} = require('@chainlink/contracts/truffle/v0.4/LinkToken')

module.exports = async (deployer, network, [defaultAccount]) => {
    if (!network.startsWith('kovan')){
        console.log('Currently only works with Kovan!')
        LinkToken.setProvider(deployer.provider)
    } else {
        const KOVAN_KEYHASH = '0x6c3699283bda56ad74f6b855546325b68d482e983852a7a82979cc4807b641f4'
        const KOVAN_VRF_COORDINATOR = '0xdD3782915140c8f3b190B5D67eAc6dc5760C46E9'
        const ETH_USD_PRICE_FEED = '0x9326BFA02ADD2366b30bacB125260Af641031331'
        const KOVAN_LINK_TOKEN = '0xa36085F69e2889c224210F603D836748e7dC0088'
        deployer.deploy(Lottery, ETH_USD_PRICE_FEED, KOVAN_VRF_COORDINATOR, KOVAN_LINK_TOKEN, KOVAN_KEYHASH)
        

    }

}

If you can take look for me, Im still trying to put up repository on github, but files to big.
thanks .

I think it might be the hdwalletProvider version, but not that sure, check my package.json which mention the versions of my dependencies, if yours is the same?

would be awesome if you can provide the github repo, you can use github desktop which will be easier to manage.

https://desktop.github.com/

you can check some videos on youtube on how to create a repo using github desktop, keep in mind that there is no need to upload the node_modules folder, so you can ignore it on the .gitignore file.

WARNING!!! :warning: BE SURE TO NOT UPLOAD YOUR .env FILE TO THE PUBLIC REPO, YOUR PRIV KEY COULD GET COMPROMISE! (just ensure you are also ignoring that file).

Carlos Z

@thecil, Hi Carlos, thanks for looking into this, Here is my package json,

I know in the video he following migrations, he talks about package json config.
Only difference is the truffle assertions.

I’ll started on that desktop github,make this easier, thanks again.

Javier

@thecil, Hi Carlos, here is github link.

https://github.com/TotalingArc/Solidity-Lottery

@thecil Hi Carlos, I am having a hard time compiling the contract because of tis error

Error: Truffle is currently using solc 0.6.6, but one or more of your contracts specify "pragma solidity ^0.8.0".

I believe the reason is because openzeppelin’s Ownable version has to be ^0.8.0 while the video is using ^0.6.6. I tried changing the pragma solidity and truffle-config solidity version to ^0.8.0 but it still complains as the chainlink import is using v0.6. Not too sure what my next step should be.

Appreciate your time, thanks!

1 Like

Hey @jeffersonchoi, hope your well.

If you are using truffle, you might have just to change your truffle compiler version, go into your truffle.config.js, search for compiler version and change it to ^0.8.0.

Carlos Z

Hi @thecil,

Thanks for the reply. I tried changing the truffle.config.js version to ^0.8.0, updated all .sol files which contains ^0.6.6 to ^0.8.0 already, but my compiler is still showing errors.

ParserError: Source file requires different compiler version (current compiler is 0.8.12+commit.f00d7308.Emscripten.clang) - note that nightly builds are considered to be strictly less than the released version
 --> @chainlink/contracts/src/v0.6/vendor/SafeMathChainlink.sol:2:1:
  |
2 | pragma solidity ^0.6.0;

I believe this is because SafeMathChainLink library is still referencing ^0.6.0, as seen from https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.6/vendor/SafeMathChainlink.sol#L2.

I tried searching in 0.8 directory but was unable to see the SafeMathChainLink file.

Please advice. Thanks!

1 Like

There is a very easy fix, remove the Ownable.sol library and implement your own onlyOwner modifier,

address private _owner;

constructor(...) {
      _owner = msg.sender;
}

modifier onlyOwner {
   ...
  _;
}

This will get rid of the 0.8.0 problem upon compiling