Hi, i try to merge the OpenZeppelin AllowanceCrowdsale contract with a Chainlink Pricefeed but i cant get it to work. On deploy in Remix i get this error code:
BlockquoteGas 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" }
I know that the error is in line 14: uint256 myrate = uint256(getRate());
becuase if i comment it out and set myrate static like in line 15 i can deploy the contract.
I am new to Solitity so i dont even know if my contract will work at all like it is designed. Would it be better launch 2 contracts? one for calculating the "myrate" based on the pricefeed, and calling the function from my Crowdsale contract?
Or how can i get this to work? Any help is very much appreciated!
My 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;
uint256 myrate = uint256(getRate());
//uint256 myrate = 10;
/**
* 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);
}
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);
}
}
I deployed both contracts before the merge and tested them and they work.
Crowdsale 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";
contract Seedround is Crowdsale, AllowanceCrowdsale, PausableCrowdsale {
constructor(
uint256 _rate,
address payable _wallet,
ERC20 _token,
address _tokenWallet
)
Crowdsale(_rate, _wallet, _token)
AllowanceCrowdsale(_tokenWallet)
public
{}
}
and my pricefeed contract which fetches the price from Chainlink and calculates my "rate" for the Crowdsale 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);
}
}
Both of them are tested and work, i just cant get them into one contract merged.