How to solve this Gas estimation failed error on Remix? - blockchain

my code is
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.7.1;
import "https://github.com/Uniswap/uniswap-v2-periphery/blob/master/contracts/interfaces/IUniswapV2Router02.sol";
contract UniswapExample {
address internal constant UNISWAP_ROUTER_ADDRESS = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D ;
IUniswapV2Router02 public uniswapRouter;
address private multiDaiKovan = 0xaD6D458402F60fD3Bd25163575031ACDce07538D;
constructor() {
uniswapRouter = IUniswapV2Router02(UNISWAP_ROUTER_ADDRESS);
}
function convertEthToDai(uint daiAmount) public payable {
uint deadline = block.timestamp + 15; // using 'now' for convenience, for mainnet pass deadline from frontend!
uniswapRouter.swapETHForExactTokens{ value: msg.value }(daiAmount, getPathForETHtoDAI(), address(this), deadline);
// refund leftover ETH to user
(bool success,) = msg.sender.call{ value: address(this).balance }("");
require(success, "refund failed");
}
function getEstimatedETHforDAI(uint daiAmount) public view returns (uint[] memory) {
return uniswapRouter.getAmountsIn(daiAmount, getPathForETHtoDAI());
}
function getPathForETHtoDAI() private view returns (address[] memory) {
address[] memory path = new address[](2);
path[0] = uniswapRouter.WETH();
path[1] = multiDaiKovan;
return path;
}
//add Harry
function getaddresWETH() public view returns (address WETHaddress){
return uniswapRouter.WETH();
}
//add Harry
function getAmountsETHforDAI(uint daiAmount) public view returns (uint[] memory) {
return uniswapRouter.getAmountsOut(daiAmount, getPathForETHtoDAI());
}
}
i input input 100000000000000000 on convertEthToDai function then,
i saw this error enter image description here
how to solve the problem?
and this is remix setting
enter image description here

Related

I'm writing some Solidity and this error keeps poppping up

This is my code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "#openzeppelin/contracts#4.7.2/token/ERC721/ERC721.sol";
import "#openzeppelin/contracts#4.7.2/token/ERC721/extensions/ERC721Enumerable.sol";
import "#openzeppelin/contracts#4.7.2/token/ERC721/extensions/ERC721URIStorage.sol";
import "#openzeppelin/contracts#4.7.2/utils/Counters.sol";
contract Alchemy is ERC721, ERC721Enumerable, ERC721URIStorage {
using Counters for Counters.Counter;
Counters.Counter private _tokenIdCounter;
uint256 MAX_SUPPLY = 10000;
constructor() ERC721("Blaze", "BLZ") {}
function safeMint(address to, string memory uri) public {
uint256 tokenId = _tokenIdCounter.current();
require(tokenId <= MAX_SUPPLY, "All NFTs have been minted")
_tokenIdCounter.increment();
_safeMint(to, tokenId);
_setTokenURI(tokenId, uri);
}
// The following functions are overrides required by Solidity.
function _beforeTokenTransfer(address from, address to, uint256 tokenId)
internal
override(ERC721, ERC721Enumerable)
{
super._beforeTokenTransfer(from, to, tokenId);
}
function _burn(uint256 tokenId) internal override(ERC721, ERC721URIStorage) {
super._burn(tokenId);
}
function tokenURI(uint256 tokenId)
public
view
override(ERC721, ERC721URIStorage)
returns (string memory)
{
return super.tokenURI(tokenId);
}
function supportsInterface(bytes4 interfaceId)
public
view
override(ERC721, ERC721Enumerable)
returns (bool)
{
return super.supportsInterface(interfaceId);
}
}
On '_tokenIdCounter.increment();' it keeps making a ParserError but I don't understand how to fix it.
In safeMint() function, you missed to put semicolon after require statement at line 19.
Your original smart contract:
...
uint256 tokenId = _tokenIdCounter.current();
require(tokenId <= MAX_SUPPLY, "All NFTs have been minted") // <- Error is here!
_tokenIdCounter.increment();
_safeMint(to, tokenId);
...
Correct way to solve your issue:
...
uint256 tokenId = _tokenIdCounter.current();
require(tokenId <= MAX_SUPPLY, "All NFTs have been minted"); // <- Add semicolon
_tokenIdCounter.increment();
_safeMint(to, tokenId);
...

solidity transaction error, The called function should be payable

I want to write a contract using router from uniswap. But when swapping 1 token to another, I get an error
The transaction has been reverted to the initial state. Note: The called function should be payable if you send value and the value you send should be less than your current balance. Debug the transaction to get more information.
I attach the code from below. How can I fix the error ?
// SPDX-License-Identifier: MIT
pragma solidity =0.8.7;
pragma abicoder v2;
import "https://github.com/Uniswap/uniswap-v3-periphery/blob/main/contracts/interfaces/ISwapRouter.sol";
import "https://github.com/Uniswap/uniswap-v3-periphery/blob/main/contracts/interfaces/IQuoter.sol";
interface IUniswapRouter is ISwapRouter {
function refundETH() external payable;
}
contract SwapV1 {
IUniswapRouter public constant uniswapRouter = IUniswapRouter(0xE592427A0AEce92De3Edee1F18E0157C05861564);
IQuoter public constant quoter = IQuoter(0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6);
address public token1;
uint24 public ownerFee;
function setToken1(address _token1) payable external {
token1 = _token1;
}
function setFee(uint24 _fee) payable external {
ownerFee = _fee;
}
function convertToken1ToToken2(address token2) external payable {
require(msg.value > 0, "Must pass non 0 ETH amount");
uint256 deadline = block.timestamp + 20; // using 'now' for convenience, for mainnet pass deadline from frontend!
address tokenIn = token1;
address tokenOut = token2;
uint24 fee = ownerFee;
address recipient = msg.sender;
uint256 amountIn = msg.value;
uint256 amountOutMinimum = 1;
uint160 sqrtPriceLimitX96 = 0;
ISwapRouter.ExactInputSingleParams memory params = ISwapRouter.ExactInputSingleParams(
tokenIn,
tokenOut,
fee,
recipient,
deadline,
amountIn,
amountOutMinimum,
sqrtPriceLimitX96
);
uniswapRouter.exactInputSingle{ value: msg.value }(params);
uniswapRouter.refundETH();
// refund leftover ETH to user
(bool success,) = msg.sender.call{ value: address(this).balance }("");
require(success, "refund failed");
}
function convertToken1ToExactToken2(uint256 token2Amount, address token2) external payable {
require(token2Amount > 0, "Must pass non 0 DAI amount");
require(msg.value > 0, "Must pass non 0 ETH amount");
uint256 deadline = block.timestamp + 20; // using 'now' for convenience, for mainnet pass deadline from frontend!
address tokenIn = token1;
address tokenOut = token2;
uint24 fee = ownerFee;
address recipient = msg.sender;
uint256 amountOut = token2Amount;
uint256 amountInMaximum = msg.value;
uint160 sqrtPriceLimitX96 = 0;
ISwapRouter.ExactOutputSingleParams memory params = ISwapRouter.ExactOutputSingleParams(
tokenIn,
tokenOut,
fee,
recipient,
deadline,
amountOut,
amountInMaximum,
sqrtPriceLimitX96
);
uniswapRouter.exactOutputSingle{ value: msg.value }(params);
uniswapRouter.refundETH();
// refund leftover ETH to user
(bool success,) = msg.sender.call{ value: address(this).balance }("");
require(success, "refund failed");
}
// do not used on-chain, gas inefficient!
function getEstimatedToken1forToken2(uint token2Amount, address token2) external payable returns (uint256) {
address tokenIn = token1;
address tokenOut = token2;
uint24 fee = ownerFee;
uint160 sqrtPriceLimitX96 = 0;
return quoter.quoteExactOutputSingle(
tokenIn,
tokenOut,
fee,
token2Amount,
sqrtPriceLimitX96
);
}
// important to receive ETH
receive() payable external {}
}

Internal JSON-RPC error with MetaMask on Polygon Blockchain. `ERC721: transfer caller is not owner nor approved.`

I am making an NFT marketplace. When I deployed my contract on the Mumbai-testnet. The createToken function might work cause it brings up the Metamask for the Gas Fee but after that, the Error occurs something regarding the ONWNERSHIP.
(Error image and text is present below.)
STEPS which I follow
npm hardhat node
npm run dev
Selecting the Creating Page.
Enter all the details.
Click on Create an Asset which calls the createToken function.
then the error occurs.
Here is my NFT contract
contract NFT is ERC721URIStorage {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
address contractAddress;
constructor(address marketplaceAddress) ERC721("Metaverse Tokens", "METT") {
contractAddress = marketplaceAddress;
}
function createToken(string memory tokenURI) public returns (uint256) {
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_mint(msg.sender, newItemId);
_setTokenURI(newItemId, tokenURI);
setApprovalForAll(contractAddress, true);
return newItemId;
}}
Here is my NFTMarket contract
contract NFTMarket is ReentrancyGuard {
using Counters for Counters.Counter;
Counters.Counter private _itemIds;
Counters.Counter private _itemSold;
address payable owner;
uint256 listingPrice = 0.025 ether; // Here ether is denoting the MATIC
constructor() {
owner = payable(msg.sender);
}
struct MarketItem {
uint256 itemId;
address nftContract;
uint256 tokenId;
address payable seller;
address payable owner;
uint256 price;
bool sold;
}
mapping(uint256 => MarketItem) private idToMarketItem;
event MarketItemCreated(
uint256 indexed itemId,
address indexed nftContract,
uint256 indexed tokenId,
address seller,
address owner,
uint256 price,
bool sold
);
function getListingPrice() public view returns (uint256) {
return listingPrice;
}
//Function to create an NFT
function createMarketItem(
address nftContract,
uint256 tokenId,
uint256 price
) public payable nonReentrant {
//Conditions for creating the Item.
require(price > 0, "Price must be at least 1 wei");
require(
msg.value == listingPrice,
"Price must be equal to listing price"
);
_itemIds.increment();
uint256 itemId = _itemIds.current();
idToMarketItem[itemId] = MarketItem(
itemId,
nftContract,
tokenId,
payable(msg.sender),
payable(address(0)), // When new NFT is created its ownership add is set to 0.
price,
false
);
IERC721(nftContract).transferFrom(msg.sender, address(this), tokenId);
//Trigger the Event
emit MarketItemCreated(
itemId,
nftContract,
tokenId,
msg.sender,
address(0),
price,
false
);
}
//Function to Transfer the Ownership
function createMarketSale(address nftContract, uint256 itemId)
public
payable
nonReentrant
{
uint256 price = idToMarketItem[itemId].price;
uint256 tokenId = idToMarketItem[itemId].tokenId;
require(
msg.value == price,
"Please submit the asking value in order to Purchase"
);
//Will transfer the MATIC to the seller address.
idToMarketItem[itemId].seller.transfer(msg.value);
//Will transfer the ownership from the owner of this contract to the Buyer.
IERC721(nftContract).transferFrom(address(this), msg.sender, tokenId);
//Set the local value of the owner to the Buyer(msg.sender).
idToMarketItem[itemId].owner = payable(msg.sender);
//Set this NFT as sold.
idToMarketItem[itemId].sold = true;
_itemSold.increment();
payable(owner).transfer(listingPrice);
}
//Returns number of items unsold
function fetchMarketItems() public view returns (MarketItem[] memory) {
uint256 itemCount = _itemIds.current();
uint256 unsoldItemCount = _itemIds.current() - _itemSold.current();
uint256 currentIndex = 0;
MarketItem[] memory items = new MarketItem[](unsoldItemCount);
for (uint256 i = 0; i < itemCount; i++) {
if (idToMarketItem[i + 1].owner == address(0)) {
uint256 currentId = idToMarketItem[i + 1].itemId;
MarketItem storage currentItem = idToMarketItem[currentId];
items[currentIndex] = currentItem;
currentIndex += 1;
}
}
return items;
}
//Returns number of Own(Created or Bought) NFTs
function fetchMyNFTs() public view returns (MarketItem[] memory) {
uint256 totalItemCount = _itemIds.current();
uint256 itemCount = 0;
uint256 currentIndex = 0;
for (uint256 i = 0; i < totalItemCount; i++) {
if (idToMarketItem[i + 1].owner == msg.sender) {
itemCount += 1;
}
}
MarketItem[] memory items = new MarketItem[](itemCount);
for (uint256 i = 0; i < totalItemCount; i++) {
if (idToMarketItem[i + 1].owner == msg.sender) {
uint256 currentId = idToMarketItem[i + 1].itemId;
MarketItem storage currentItem = idToMarketItem[currentId];
items[currentIndex] = currentItem;
currentIndex += 1;
}
}
return items;
}
//Returns the no of NFT created
function fetchItemsCreated() public view returns (MarketItem[] memory) {
uint256 totalItemCount = _itemIds.current();
uint256 itemCount = 0;
uint256 currentIndex = 0;
for (uint256 i = 0; i < totalItemCount; i++) {
if (idToMarketItem[i + 1].seller == msg.sender) {
itemCount += 1;
}
}
MarketItem[] memory items = new MarketItem[](itemCount);
for (uint256 i = 0; i < totalItemCount; i++) {
if (idToMarketItem[i + 1].seller == msg.sender) {
uint256 currentId = idToMarketItem[i + 1].itemId;
MarketItem storage currentItem = idToMarketItem[currentId];
items[currentIndex] = currentItem;
currentIndex += 1;
}
}
return items;
}}
I tried changing the RPC in the MetaMask and the configuration files and redeployed it many times with different accounts, but still, nothing changes.
The Error
MetaMask - RPC Error: Internal JSON-RPC error.
data:
code: 3
message: "execution reverted: ERC721: transfer caller is not owner nor approved"
Image of the console
If any other info is required please comment
Link of Blockchain Explorer
Thank You for your efforts.
I got the solution to it(After Searching for 3 days).
SOLUTION=>
There's currently a bug on Mumbai causing deployed addresses to be incorrect. This is causing the constructor of the NFT contract to approve the wrong address for NFT purchases (because it uses the address of the Market deployment for approval) — causing the annoying "execution reverted: ERC721: approve caller is not owner nor approved for all" error.
Try using Mainnet (yes, you'll have to use real money) but it works!
Reference
Here's a workaround deploy script that will make it work on Mumbai. Replace main() in deploy.js with:
const hre = require("hardhat");
async function main() {
const [deployer] = await hre.ethers.getSigners();
console.log(
"Deploying contracts with the account:",
deployer.address
);
let txHash, txReceipt
const NFTMarket = await hre.ethers.getContractFactory("NFTMarket");
const nftMarket = await NFTMarket.deploy();
await nftMarket.deployed();
txHash = nftMarket.deployTransaction.hash;
txReceipt = await ethers.provider.waitForTransaction(txHash);
let nftMarketAddress = txReceipt.contractAddress
console.log("nftMarket deployed to:", nftMarketAddress);
const NFT = await hre.ethers.getContractFactory("NFT");
const nft = await NFT.deploy(nftMarketAddress);
await nft.deployed();
txHash = nft.deployTransaction.hash;
// console.log(`NFT hash: ${txHash}\nWaiting for transaction to be mined...`);
txReceipt = await ethers.provider.waitForTransaction(txHash);
let nftAddress = txReceipt.contractAddress
console.log("nft deployed to:", nftAddress);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
Redeploy the Contracts with this Script, and change the config.js.
I checked your full code and it is working.
You are inheriting from ERC721URIStorage which inherits from ERC721 If you check the transferFrom inside ERC721:
function transferFrom(address from,address to,uint256 tokenId
) public virtual override {
// ***** THIS REQUIRE IS NOT SATISFIED *****
require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: transfer caller is not owner nor approved");
_transfer(from, to, tokenId);
}
you are getting that error, because require statement inside transferFrom is not satisfied.
this is the _isApprovedOrOwner
function _isApprovedOrOwner(address spender, uint256 tokenId) internal view virtual returns (bool) {
require(_exists(tokenId), "ERC721: operator query for nonexistent token");
address owner = ERC721.ownerOf(tokenId);
return (spender == owner || getApproved(tokenId) == spender || isApprovedForAll(owner, spender));
}
this function is not returning True. in order to get True, this
spender == owner || getApproved(tokenId) == spender || isApprovedForAll(owner, spender)
should return True. in or operations in order to get True, 3 of conditions must be True.
In my opinion, you are trying to transfer a token that is not yours.
During development, every time you compile your smart contract (using something like hardhat compile..) , you have to copy the new smart contract addresses generated in your command prompt and paste them in the designated location in your source code.

ERC721 Minted NFT not showing on Opensea.io testnet

I run my code on rinkeby etherscan network and it works perfectly. But the image and descriptions are not showing on opensea testnet. I run the /validate/ url it shows Valid: "false".
Here is what I found when I force update on opensea, https://testnets-api.opensea.io/api/v1/asset/0x668D179B933af761e4732B084290D32B3235C22b/0/?force_update=true
Here is my code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;
import "#openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "#openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "#openzeppelin/contracts/access/Ownable.sol";
import "#openzeppelin/contracts/utils/Counters.sol";
import "./ERC2981ContractWideRoyalties.sol";
import "hardhat/console.sol";
contract SmoothApe is ERC721URIStorage, Ownable, ERC2981ContractWideRoyalties {
using Counters for Counters.Counter;
using Strings for uint256;
Counters.Counter private _tokenIdCounter;
event EmitNFT(address sender, uint256 tokenId);
uint256 public preSaleTokenPrice = 0.01 ether;
uint256 public costPrice = 0.02 ether;
uint256 public maxSupply = 10000;
uint256 public maxMintNft = 10;
string public baseURI;
string public baseExtension = ".json";
string public notRevealedUri;
bool public paused = false;
bool public revealed = true;
bool public presale = false;
// set 0x7350243981aB92E2A3646e377EBbFC28e9DE96C1 as payable admn wallet
address payable public adminWallet = payable(0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2);
address payable public communityWallet = payable(0x4B20993Bc481177ec7E8f571ceCaE8A9e22C02db);
address payable public t1Wallet = payable(0x78731D3Ca6b7E34aC0F824c42a7cC18A495cabaB);
address payable public t2Wallet = payable(0x617F2E2fD72FD9D5503197092aC168c91465E7f2);
address payable public t3Wallet = payable(0x17F6AD8Ef982297579C203069C1DbfFE4348c372);
// Mapping from token ID to owner address
mapping(uint256 => address) private _owners;
constructor(string memory _name, string memory _symbol,string memory _initBaseURI, string memory _initNotRevealedUri) ERC721(_name, _symbol) {
setBaseURI(_initBaseURI);
setNotRevealedUri(_initNotRevealedUri);
}
// inherit and override erc165
function supportsInterface(bytes4 interfaceId) public view virtual override (ERC721, ERC2981Base) returns (bool) {
return super.supportsInterface(interfaceId);
}
// set Royalty value (between 0 and 10000)
function setRoyalties(address recipient, uint256 value) public onlyOwner {
_setRoyalties(recipient, value);
}
function _baseURI() internal view override returns (string memory) {
return baseURI;
}
function mintApe() public payable {
uint256 tokenId = _tokenIdCounter.current();
require(!paused, "Minting is paused");
require(msg.sender != owner(), "You can't mint ape to admin");
// require(_mintAmount > 0, "Mint amount must be greater than 0");
// require(_mintAmount <= maxSupply, "Mint amount must be less than or equal to max supply");
if(presale == true) {
preSaleTokenPrice = costPrice;
}
require(msg.value >= costPrice, string(abi.encodePacked("Must send at least ", costPrice.toString(), " ether to purchase")));
require(balanceOf(msg.sender) < maxMintNft, "You can only own 11 ape at a time");
// require(_mintAmount + balanceOf(msg.sender) <= maxMintNft, "You can only own 10 ape at a time");
// adminWallet.transfer(msg.value);
payable(owner()).transfer(msg.value);
emit EmitNFT(msg.sender, tokenId);
// for(uint256 i = 1; i <= _mintAmount; i++){
_safeMint(msg.sender, tokenId);
// _setTokenURI(tokenId, _baseURI());
_tokenIdCounter.increment();
// }
}
// admin mint ape to wallet
function mintApeTo(address _to, uint256 _mintAmount) public onlyOwner {
require(!paused, "Minting is paused");
require(_mintAmount > 0, "Mint amount must be greater than 0");
require(_mintAmount <= maxSupply, "Mint amount must be less than or equal to max supply");
for(uint256 i = 1; i <= _mintAmount; i++){
uint256 tokenId = _tokenIdCounter.current();
_safeMint(_to, tokenId);
// _setTokenURI(tokenId, _baseURI());
_tokenIdCounter.increment();
}
}
function getCostPrice() public view virtual returns (uint256) {
return costPrice;
}
// function to set cost of ape
function setCost(uint256 _preSaleTokenPrice, uint256 _publicTokenPrice) public onlyOwner {
preSaleTokenPrice = _preSaleTokenPrice;
costPrice = _publicTokenPrice;
}
// set presale to true
function setPresale(bool _presale) public onlyOwner {
presale = _presale;
}
function reveal(bool _reveal) public onlyOwner {
revealed = _reveal;
}
// pause function
function pause() public onlyOwner {
paused = true;
}
// set function for setBaseURI and setNotRevealed function
function setBaseURI(string memory _newBaseURI) public onlyOwner {
baseURI = _newBaseURI;
}
function setTokenURI(uint256 _tokenId, string memory _tokenURI) public onlyOwner {
_setTokenURI(_tokenId, _tokenURI);
}
// override tokenURI function
function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");
require(tokenId < _tokenIdCounter.current(), "Token ID must be less than the total supply");
if(!revealed) {
return notRevealedUri;
}
string memory currentBaseURI = _baseURI();
return bytes(currentBaseURI).length > 0
? string(
abi.encodePacked(
currentBaseURI,
tokenId.toString(),
baseExtension))
: "";
}
}
Here is a sample of NFT i mint:
https://testnets.opensea.io/assets/0x668D179B933af761e4732B084290D32B3235C22b/0
my ipfs CID:
ipfs://QmaoMZ19zhpC6T4id6jdBP1Qz5dQSmRZMkQZU7Zt8hyFNQ/
as you can see in the right up corner there is a "reload" button, you need to click it to reload the ipfs on opensea
I had the same problem. Turned out that I wasn't uploading the images to ipfs correctly because it was visible only to me on my machine. So try to see if the ipfs metadata are correctly formated and are accesible from different devices.
Also you shouldn't just paste your entire contract in. Tt is really hard to answer your question then. Just write out the important sections of the code.

"VM Exception while processing transaction: revert", when running a chainlink node and try to deploy TestnetConsumer contract?

The scenario is when deploying a ATestnetConsumer.sol on mathchain via Remix, following the official tutorial to run a node on mathchain, which is a EVM-compatitable public chain. And I finish deploying the Oracle contract and LINKToken contract, and running the node on mathchain successfully, but when trying to deploy a TestnetConsumer contract, it shows that:
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": -32603, "message": "VM Exception while processing transaction: revert", "data": "" }
Anybody know why?
The TestnetConsumer contract is list as below:
pragma solidity 0.4.24;
import "https://github.com/smartcontractkit/chainlink/evm-contracts/src/v0.4/ChainlinkClient.sol";
import "https://github.com/smartcontractkit/chainlink/evm-contracts/src/v0.4/vendor/Ownable.sol";
contract ATestnetConsumer is ChainlinkClient, Ownable {
uint256 constant private ORACLE_PAYMENT = 1 * LINK;
uint256 public currentPrice;
int256 public changeDay;
bytes32 public lastMarket;
event RequestEthereumPriceFulfilled(
bytes32 indexed requestId,
uint256 indexed price
);
event RequestEthereumChangeFulfilled(
bytes32 indexed requestId,
int256 indexed change
);
event RequestEthereumLastMarket(
bytes32 indexed requestId,
bytes32 indexed market
);
constructor() public Ownable() {
setPublicChainlinkToken();
}
function requestEthereumPrice(address _oracle, string _jobId)
public
onlyOwner
{
Chainlink.Request memory req = buildChainlinkRequest(stringToBytes32(_jobId), this, this.fulfillEthereumPrice.selector);
req.add("get", "https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD");
req.add("path", "USD");
req.addInt("times", 100);
sendChainlinkRequestTo(_oracle, req, ORACLE_PAYMENT);
}
function requestEthereumChange(address _oracle, string _jobId)
public
onlyOwner
{
Chainlink.Request memory req = buildChainlinkRequest(stringToBytes32(_jobId), this, this.fulfillEthereumChange.selector);
req.add("get", "https://min-api.cryptocompare.com/data/pricemultifull?fsyms=ETH&tsyms=USD");
req.add("path", "RAW.ETH.USD.CHANGEPCTDAY");
req.addInt("times", 1000000000);
sendChainlinkRequestTo(_oracle, req, ORACLE_PAYMENT);
}
function requestEthereumLastMarket(address _oracle, string _jobId)
public
onlyOwner
{
Chainlink.Request memory req = buildChainlinkRequest(stringToBytes32(_jobId), this, this.fulfillEthereumLastMarket.selector);
req.add("get", "https://min-api.cryptocompare.com/data/pricemultifull?fsyms=ETH&tsyms=USD");
string[] memory path = new string[](4);
path[0] = "RAW";
path[1] = "ETH";
path[2] = "USD";
path[3] = "LASTMARKET";
req.addStringArray("path", path);
sendChainlinkRequestTo(_oracle, req, ORACLE_PAYMENT);
}
function fulfillEthereumPrice(bytes32 _requestId, uint256 _price)
public
recordChainlinkFulfillment(_requestId)
{
emit RequestEthereumPriceFulfilled(_requestId, _price);
currentPrice = _price;
}
function fulfillEthereumChange(bytes32 _requestId, int256 _change)
public
recordChainlinkFulfillment(_requestId)
{
emit RequestEthereumChangeFulfilled(_requestId, _change);
changeDay = _change;
}
function fulfillEthereumLastMarket(bytes32 _requestId, bytes32 _market)
public
recordChainlinkFulfillment(_requestId)
{
emit RequestEthereumLastMarket(_requestId, _market);
lastMarket = _market;
}
function getChainlinkToken() public view returns (address) {
return chainlinkTokenAddress();
}
function withdrawLink() public onlyOwner {
LinkTokenInterface link = LinkTokenInterface(chainlinkTokenAddress());
require(link.transfer(msg.sender, link.balanceOf(address(this))), "Unable to transfer");
}
function cancelRequest(
bytes32 _requestId,
uint256 _payment,
bytes4 _callbackFunctionId,
uint256 _expiration
)
public
onlyOwner
{
cancelChainlinkRequest(_requestId, _payment, _callbackFunctionId, _expiration);
}
function stringToBytes32(string memory source) private pure returns (bytes32 result) {
bytes memory tempEmptyStringTest = bytes(source);
if (tempEmptyStringTest.length == 0) {
return 0x0;
}
assembly { // solhint-disable-line no-inline-assembly
result := mload(add(source, 32))
}
}
}
The problem is located at the constructor:
constructor() public Ownable() {
setPublicChainlinkToken();
}
When you run a node on a public-chain, and deploy a totally new LINKToken contract as the way to transferAndCall the service provided by your own node, do remember to replace the line: setPublicChainlinkToken() to:
// set the Oracle contract address
setChainlinkOracle(YOUR_ORACLE_CONTRACT_ADDRESS);
// set LINKToken contract address
setChainlinkToken(YOUR_LINKTOKEN_CONTRACT_ADDRESS);
Then you should be good to go. And remember that you should deposit some LINK token before invoking the function to get service from your Chainlink node!