0

I am new to Solidity and lost how i can acomplish to integrate the Chainlink Pricefeed into the OpenZepellin 2.5.0 AllowanceCrowdsale contract.

What i have got so far:

The Crowdsale contract:

pragma solidity ^0.5.5;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v2.5.0/contracts/crowdsale/Crowdsale.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v2.5.0/contracts/token/ERC20/ERC20.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v2.5.0/contracts/crowdsale/emission/AllowanceCrowdsale.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v2.5.0/contracts/crowdsale/validation/PausableCrowdsale.sol";

contract Seedround is Crowdsale, AllowanceCrowdsale, PausableCrowdsale {

  constructor(
    uint256 _rate,
    address payable _wallet,
    ERC20 _token,
    address _tokenWallet
  )
    Crowdsale(_rate, _wallet, _token)
    AllowanceCrowdsale(_tokenWallet)
    public
  {}

}

I want to set the swap "rate" based on the BNB price, so i wrote a small contract to fetch the BNB Chainlink pricefeed and calculate the actuall rate based on my desired token sell price.

The Pricefeed contract:

// SPDX-License-Identifier: MIT
pragma solidity ^0.5.5;

import "@chainlink/contracts/src/v0.5/interfaces/AggregatorV3Interface.sol";

contract MyRate {
    
    AggregatorV3Interface internal priceFeed;

    /**
     * Network: Binance Smart Chain Testnet
     * Aggregator: BNB/USD
     * Address: 0x2514895c72f50D8bd4B4F9b1110F0D6bD2c97526
     */
    constructor() public {
        priceFeed = AggregatorV3Interface(0x2514895c72f50D8bd4B4F9b1110F0D6bD2c97526);
    }

    function getRate() public view returns (int) {
        (
            uint80 roundID,
            int price,
            uint startedAt,
            uint timeStamp,
            uint80 answeredInRound
        ) = priceFeed.latestRoundData();
        price = price / 10 ** 6; // get BNB price in cents
        price = 1000000000000000000 / price; //calculate amount of BNB wei for one centUSD
        uint256 a = 10000000000000000000/uint256(85); //calculate amount of MyToken wei for one centUSD
        uint256 exchangeRate = a / uint256(price); //calculate _rate for crowdsale
        return int256(exchangeRate);
    }
}

Up to here everything is fine and working. Now i tried to merge both contracts and feed the _rate from the pricefeed contract into the Crowdsale(_rate, _wallet, _token) function and this is where i am stuck. Beside that the contract probably wont work because i could not test it yet, i can't even get it deployed, it exits with following error message:

Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending?
Internal JSON-RPC error. { "code": -32000, "message": "execution reverted" }

The merged contract:

// SPDX-License-Identifier: MIT
pragma solidity ^0.5.5;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v2.5.0/contracts/crowdsale/Crowdsale.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v2.5.0/contracts/token/ERC20/ERC20.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v2.5.0/contracts/crowdsale/emission/AllowanceCrowdsale.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v2.5.0/contracts/crowdsale/validation/PausableCrowdsale.sol";
import "@chainlink/contracts/src/v0.5/interfaces/AggregatorV3Interface.sol";

contract Swaplast is Crowdsale, AllowanceCrowdsale, PausableCrowdsale {

    AggregatorV3Interface internal priceFeed;
    
    function getCrowdsaleRate() public view returns (int) {
        (
            uint80 roundID,
            int price,
            uint startedAt,
            uint timeStamp,
            uint80 answeredInRound
        ) = priceFeed.latestRoundData();
        price = price / 10 ** 6; // get BNB price in cents
        price = 1000000000000000000 / price; //calculate amount of BNB wei for one centUSD
        uint256 a = 10000000000000000000/uint256(85); //calculate amount of MyToken wei for one centUSD
        uint256 exchangeRate = a / uint256(price); //calculate _rate for crowdsale
        return int256(exchangeRate);
    }

    uint256 myrate = uint256(getCrowdsaleRate());
    
    /**
     * Network: Binance Smart Chain Testnet
     * Aggregator: BNB/USD
     * Address: 0x2514895c72f50D8bd4B4F9b1110F0D6bD2c97526
     */
    constructor(
    uint256 _rate,
    address payable _wallet,
    ERC20 _token,
    address _tokenWallet
  ) 
    
    Crowdsale(myrate, _wallet, _token)
    AllowanceCrowdsale(_tokenWallet)
    public {
        priceFeed = AggregatorV3Interface(0x2514895c72f50D8bd4B4F9b1110F0D6bD2c97526); 
    }

}

So my questions are:

  1. why do i get this gas error at deploying?
  2. will this work at all or i am totally on the wrong path? and
  3. what would be the best way to use the getRate() function to get the "myRate" value into Crowdsale(myrate, _wallet, _token) ? Should i deploy the contracts separately and do a remote contract function call to get myRate?

You see i am somewhat lost here;) every input is welcome!

1 Answer 1

0

You can't call the contract's function before the contract itself is deployed. Delete next line

uint256 myrate = uint256(getCrowdsaleRate());

To get the rate you should probably separate your logic into several smart contracts

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.