I want to get all reserve in UniswapV2 pool in one request to the EVM. Here is the code getReserve :
// SPDX-License-Identifier: MIT
// compiled with 0.8.17
pragma solidity >=0.4.22 <0.9.0;
contract IUniswapV2Pair {
function getReserves() public view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast) {}
}
contract getReserve {
function get(address[] memory _add) external view returns (uint[] memory) {
uint n = _add.length;
uint[] memory liste = new uint[](n*2);
// Define variables to store the returned values
uint112 reserve0;
uint112 reserve1;
uint32 blockTimestampLast;
for (uint i=0; i<n; i++) {
// Call the getReserves function in the other contract and store the returned values
(reserve0, reserve1, blockTimestampLast) = IUniswapV2Pair(_add[i]).getReserves();
liste[i*2] = reserve0;
liste[i*2+1] = reserve1;
}
return liste;
}
function getOnlyOne(address _add) external view returns (uint112, uint112, uint32) {
return IUniswapV2Pair(_add).getReserves();
}
}
To test that this program work well on my Ganache EVM I have created this program IUniswapV2Pair:
// SPDX-License-Identifier: MIT
// compiled with 0.8.17
pragma solidity >=0.4.22 <0.9.0;
contract IUniswapV2Pair {
function getReserves() external pure returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast) {
reserve0 = 11;
reserve1 = 12;
blockTimestampLast = 13;
return (reserve0, reserve1, blockTimestampLast);
}
}
Like I said, it is working well on my Ganache EVM. But when I deploy it on the GOERLI testnet it is not working, here is the addresss :
getReserve : 0x993305D7d90675656857c7Cd69f1CF57242D79D5
IUniswapV2Pair : 0x33e57A530F90aB2A5572E2a877161Ca644e8FC95
My Problem is to make getOnlyOne("0x33e57A530F90aB2A5572E2a877161Ca644e8FC95") working.
1 Testing code on Goerli
I have tried to connect to GOERLI via Python, here is the code :
from web3 import Web3
import json, web3
server = "https://eth-goerli.public.blastapi.io"
w3 = Web3(Web3.HTTPProvider(server))
w3.isConnected()
# Contrat intermédiaire
address = "0x993305D7d90675656857c7Cd69f1CF57242D79D5"
abi = [{"inputs":[{"internalType":"address[]","name":"_add","type":"address[]"}],"name":"get","outputs":[{"internalType":"uint256[]","name":"","type":"uint256[]"}],"stateMutability":"view","type":"function","constant":true},{"inputs":[{"internalType":"address","name":"_add","type":"address"}],"name":"getOnlyOne","outputs":[{"internalType":"uint112","name":"","type":"uint112"},{"internalType":"uint112","name":"","type":"uint112"},{"internalType":"uint32","name":"","type":"uint32"}],"stateMutability":"view","type":"function","constant":true}]
contract = w3.eth.contract(address=address, abi=abi)
contract.all_functions()
contract.functions.get(["0x33e57A530F90aB2A5572E2a877161Ca644e8FC95"]).call()
contract.functions.getOnlyOne("0x33e57A530F90aB2A5572E2a877161Ca644e8FC95").call()
I have this error web3.exceptions.ContractLogicError: execution reverted, which I don't have when I do it on Ganache.
And to make sure that it is not a problem from IUniswapV2Pair I have this code:
address = "0x33e57A530F90aB2A5572E2a877161Ca644e8FC95"
abi = [{"inputs":[],"name":"getReserves","outputs":[{"internalType":"uint112","name":"reserve0","type":"uint112"},{"internalType":"uint112","name":"reserve1","type":"uint112"},{"internalType":"uint32","name":"blockTimestampLast","type":"uint32"}],"stateMutability":"pure","type":"function","constant":true}]
contract = w3.eth.contract(address=address, abi=abi)
contract.all_functions()
contract.functions.getReserves().call()
Which return me : [11, 12, 13]
2 Testing connection between contracts
I have created 2 others easier smart contract which have been working well here it is :
// SPDX-License-Identifier: MIT
// compiled with 0.8.17
pragma solidity >=0.4.22 <0.9.0;
contract FavoriteNumber {
uint favoriteNumber;
function getFavoriteNumber() external view returns(uint) {
return favoriteNumber;
}
function setFavoriteNumber(uint _favoriteNumber) external {
favoriteNumber = _favoriteNumber;
}
}
// SPDX-License-Identifier: MIT
// compiled with 0.8.17
pragma solidity >=0.4.22 <0.9.0;
contract FavoriteNumber {
function getFavoriteNumber() public view returns(uint) {}
function setFavoriteNumber(uint _favoriteNumber) public {}
}
contract ExistingNoSet {
function getA(address _t) public view returns (uint result) {
return FavoriteNumber(_t).getFavoriteNumber();
}
function setA(address _t, uint _val) public {
FavoriteNumber(_t).setFavoriteNumber(_val);
}
}
Here is the address :
FavoriteNumber : 0x14c89b4F462C11961Bb48aD6B2008f64617CF62a
ExistingNoSet : 0x97BdDaff1a971580f99C1DB850dE5EcF4982251a
And to test the code, here is the Python program, be careful I give a private key here (don't use it outside of a testnet) :
from web3 import Web3
import json, web3
server = "https://eth-goerli.public.blastapi.io"
private_key = "df49b58fbc863c5e60fe4e64829a853c46a8a12c3310404bc2a03bfefb89f68a"
public_add = "0xb4311ad11530F735ecE2d652Cbd56D1FB8D6Efeb"
w3 = Web3(Web3.HTTPProvider(server))
w3.isConnected()
# Contrat intermédiaire
address = "0x97BdDaff1a971580f99C1DB850dE5EcF4982251a"
abi = [{"inputs":[{"internalType":"address","name":"_t","type":"address"}],"name":"getA","outputs":[{"internalType":"uint256","name":"result","type":"uint256"}],"stateMutability":"view","type":"function","constant":true},{"inputs":[{"internalType":"address","name":"_t","type":"address"},{"internalType":"uint256","name":"_val","type":"uint256"}],"name":"setA","outputs":[],"stateMutability":"nonpayable","type":"function"}]
contract = w3.eth.contract(address=address, abi=abi)
contract.all_functions()
contract.functions.getA("0x14c89b4F462C11961Bb48aD6B2008f64617CF62a").call() # It should return 15
txn = contract.functions.setA(
"0x14c89b4F462C11961Bb48aD6B2008f64617CF62a", 3 # You set this new number
).build_transaction({
'nonce': w3.eth.get_transaction_count(public_add),
'gas': 200000,
'gasPrice': w3.toWei('20', 'gwei')
})
signed_txn = w3.eth.account.sign_transaction(txn, private_key=private_key)
w3.eth.send_raw_transaction(signed_txn.rawTransaction)
# You have to time sleep a little bit like 1min
contract.functions.getA("0x14c89b4F462C11961Bb48aD6B2008f64617CF62a").call() # It should return 3
So this program works well.
Thank you very to have read my post until here, I hope you will be able to help me.
I am trying to receive the funds into a specific address during mint process. This is my code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "https://github.com/OpenZeppelin/openzeppelin-
contracts/blob/master/contracts/token/ERC1155/ERC1155.sol";
contract NFTContract is ERC1155 {
uint256 public constant Jack = 0;
constructor() ERC1155("") {
_mint(msg.sender, Jack, 0, "");
}
function mint(address account, uint256 id, uint256 amount) public payable {
payable(address(0xdD870fA1b7C4700F2BD7f44238821C26f7392148)).transfer(100000000000000000);
_mint(account, id, amount, "");
}
}
But I get an error saying:
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.
How do I receive a payment during the mint?
Just needed to do regular transfer:
addr.transfer(amount)
I'm trying to call a method from a contract that was instantiated from another contract. I use truffle console.
Here are the details: (I think you can gloss over the code, since this question is aimed at using truffle console, not fixing any bugs)
I have a contract called MyCoinSupply.sol:
pragma solidity ^0.8.0;
import "./MyCoin.sol";
contract MyCoinSupply is MyCoin("MyCoin", "MYC") // MyCoin is ERC20
{
constructor() public // gives 1000 tokens to the owner
{
_mint(msg.sender, 1000);
}
function getCoinbase() public returns (address) // for debugging purposes
{
return block.coinbase;
}
function _mintMinerReward() internal // gives 20 tokens to the miner
{
// _mint(block.coinbase, 20); // for some reason, block.coinbase is address 0
}
function _transfer(address from, address to, uint256 value) override internal
{
_mintMinerReward();
super._transfer(from, to, value);
}
}
I instantiate MyCoinSupply.sol within MyCoinDEX.sol:
pragma solidity ^0.8.0;
import "./IERC20.sol";
import "./MyCoinSupply.sol";
contract MyCoinDEX
{
IERC20 public token;
event Bought(uint256 amount);
event Sold(uint256 amount);
constructor() public
{
token = new MyCoinSupply();
}
function showSender() public view returns (address) // for debugging purposes
{
return (msg.sender);
}
function buy(uint256 amountTobuy) payable public // send ether and get tokens in exchange; 1 token == 1 ether
{
uint256 dexBalance = token.balanceOf(address(this));
require(amountTobuy > 0, "You need to send some ether");
require(amountTobuy <= dexBalance, "Not enough tokens in the reserve");
token.transfer(msg.sender, amountTobuy);
emit Bought(amountTobuy);
}
function sell(uint256 amount) public // send tokens to get ether back
{
require(amount > 0, "You need to sell at least some tokens");
uint256 allowance = token.allowance(msg.sender, address(this));
require(allowance >= amount, "Check the token allowance");
token.transferFrom(msg.sender, address(this), amount);
payable(msg.sender).transfer(amount);
emit Sold(amount);
}
}
Here's my migration for deploying contracts:
const MyCoinSupply = artifacts.require("MyCoinSupply");
const MyCoinDEX = artifacts.require("MyCoinDEX");
module.exports = function(deployer) {
deployer.deploy(MyCoinSupply);
deployer.deploy(MyCoinDEX);
};
Here's my problem:
When I try to call functions that belong to MyCoinDEX, all works well:
truffle(development)> let instance = await MyCoinDEX.deployed()
undefined
truffle(development)> instance.buy(1)
All is good here.
However, when I try the following I get an error:
truffle(development)> instance.token.balanceOf("0xE1994C1054f9c4171B8ae9a7E7a68F404c2bF829")
evalmachine.<anonymous>:0
instance.token.balanceOf("0xE1994C1054f9c4171B8ae9a7E7a68F404c2bF829")
^
Uncaught TypeError: instance.token.balanceOf is not a function
I Googled this error (I read this solution and this solution), but I didn't really stumble upon a solution. Can someone tell me how can I call token's methods from MyCoinDEX?
Thank you in advance!
I have the following contract in Solidity that was working up until I added the line
require(msg.value == mintPrice, "Not Enough Ether");
// contracts/NFT.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "#openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "#openzeppelin/contracts/utils/Counters.sol";
contract NFT is ERC721URIStorage {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
uint256 mintPrice = 0.025 ether;
constructor() ERC721("NFT", "ITM"){}
function mint(address user, string memory tokenURI)
public
payable
returns (uint256)
{
require(msg.value == mintPrice, "Not Enough Ether");
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_mint(user, newItemId);
_setTokenURI(newItemId, tokenURI);
return newItemId;
}
}
I have the following chai test that fails saying not enough ether, but the address I am using is a hardhat address with tons of ether in it
describe("NFT", function () {
it("Should return a transaction hash, async function", async function () {
const NFT = await ethers.getContractFactory("NFT");
const nft = await NFT.deploy();
await nft.deployed();
expect(await nft.mint('0xf3...fFb92266', "/Users/.../web3/nft-next-minter/public/test.json")).to.have.any.keys('hash');
expect(await nft.tokenURI(1)).to.equal('/Users/.../web3/nft-next-minter/public/test.json');
});
})
I am running npx hardhat test --network localhost
Not quite sure why I am getting this error, any help would be greatly appreciated.
Thanks ahead of time.
await nft.mint(
'0xf3...fFb92266',
"/Users/.../web3/nft-next-minter/public/test.json"
)
This JS snippet doesn't specify any value of the transaction, so it's sent with 0 value by default.
And since the contract is expecting the value to equal mintPrice (0.025 ether), it fails the require() condition, effectively reverting the transaction.
You can specify the value in the overrides param (docs).
await nft.mint(
'0xf3...fFb92266',
"/Users/.../web3/nft-next-minter/public/test.json",
{value: ethers.utils.parseEther('0.025')}
)
let’s consider in our application the users have ideas as NFT on which they will get funding. Each idea should be unique So people can send funds to NFT as ERC 20 tokens. Now i want to make the contract in which can can use both standards how is it possible kindly please tell?
Description: I want to create a contract in which user upload his idea as NFT and these NFT's can get fund from different users and users must send erc20 token as fund. and we know that there are some common functions in ERC20 and 721 so that we cannot use both in single cotract like i.e.
contract idea is ERC20,ERC721{
.
.
.
}
So what i did for that purpose
I have 3 contracts '1' is main.sol where i'm importing my '2' other contracts Erc.sol and NFT.sol
------- main.sol -------
SPDX-License-Identifier: Unlicense
pragma solidity 'some version';
import "./NFT.sol";
import "./ERc.sol";
contract main {
ERc erc =new ERc();
constructor(uint256 _initialSupply) {
erc.addInitialSupply(_initialSupply);
}
NFT nft = new NFT();
//by using nft instance i can call the NFT contract functions
}
------- NFT.sol -------
pragma solidity 'some version';
import "#openzeppelin/contracts/utils/Counters.sol";
import "#openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "#openzeppelin/contracts/token/ERC721/ERC721.sol";
// SPDX-License-Identifier: Unlicense
contract NFT is ERC721URIStorage{
//_minting will be here and constructor will overide ERC721
//get fund on nftIdea
function fund () public view returns(bool) {
bool res=erc.fundidea('address',value)
return res;
}
}
------- ERC20.sol -------
// SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.2;
import "#openzeppelin/contracts/token/ERC20/ERC20.sol";
import "./NFT.sol";
contract ERc is ERC20 {
constructor() ERC20('','') {
}
function addInitialSupply(uint256 _initialSupply) public {
_mint(msg.sender, _initialSupply * 10 );
}
function fundIdea(address _author, uint value) public returns(bool){
transfer(address(_author), value);
return true;
}
}