Vyper Equivalent for solidity's address(this) - smartcontracts

How to get the address of current contract in Vyper. In solidity address(this) is used. What is the equivalent in Vyper.
address myContractAddress;
myContractAddress = address(this);

It's just self, as in:
#public
def get_address() -> address:
return self

Related

How can uniswapV2Library pairFor addres be different from uniswapV2Factory getPair for same tokens?

I'm writing a Solidity smartcontract.
Working on Kovan testnet.
I understood from the Uniswap documentation, that the .pairFor method from the uniswapV2Library contract should return an address for this pair of tokens.
For free, since it's not making external calls, it's calculating inside.
And that .getPair method on the uniswapV2Factory should do the same, but via a request that costs gas.
However, both methods return a different address, and I can't understand how that is possible.
Can somebody explain what I'm missing?
This is my contract (i simplified it a bit), most important is the repl function:
pragma solidity =0.8.12;
import './UniswapV2Library.sol';
import './interfaces/IUniswapV2Router02.sol';
import './interfaces/IUniswapV2Pair.sol';
import './interfaces/IUniswapV2Factory.sol';
contract FlashLoaner {
address immutable factory;
IUniswapV2Router02 immutable sushiRouter;
IUniswapV2Factory immutable factoryV2;
address public var1;
address public var2;
address public resultPair;
address public resPair2;
address public fact;
constructor(address _factory, address _uniRouter, address _sushiRouter) public {
factory = _factory;
sushiRouter = IUniswapV2Router02(_sushiRouter);
factoryV2 = IUniswapV2Factory(0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f);
}
function repl(address _sender, uint _amount0, uint _amount1) external {
//0x052AE8b0F7E5c610937920e46ED265c2063Cb7B8 = uniswapV2pair WETH RAI
address msgsender = 0x052AE8b0F7E5c610937920e46ED265c2063Cb7B8;
IUniswapV2Pair v2Pair = IUniswapV2Pair(msgsender);
address token0 = v2Pair.token0();
address token1 = v2Pair.token1();
//uniswap v2 factory address
fact = 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f;
//results in 0x3c8B7Cf2bDCC9DEB44a72f40052ae8b0F7E5C610
//which i don't understand??
resultPair = UniswapV2Library.pairFor(fact, token1, token0);
//results in 0x052AE8b0F7E5c610937920e46ED265c2063Cb7B8 = uniswapV2pair WETH RAI
//which makes total sense
resPair2 = factoryV2.getPair(token0, token1);
}
}
Why is resPair2 not the same as resultPair??

gas estimation errored with message: "execution reverted: Below agreed payment"

I am having issues when trying to use the Chainlink random number generator and deploying to Rinkeby. Relevant code pieces are the following:
Constructor from the importing contract (should be working fine).
// RandomNumberConsumer parameters for RINKEBY testnet
address _vrfCoordinator = 0xb3dCcb4Cf7a26f6cf6B120Cf5A73875B7BBc655B;
address _link = 0x01BE23585060835E02B77ef475b0Cc51aA1e0709;
bytes32 _keyHash = 0x2ed0feb3e7fd2022120aa84fab1945545a9f2ffc9076fd6156fa96eaff4c1311;
uint256 _fee = 0.1 * 10 ** 18; // 0.1 LINK
constructor() RandomNumberConsumer(_vrfCoordinator, _link, _keyHash, _fee) {}
RandomNumberConsumer.sol. As specified in the chainlink docs, with a few tweaks needed for my approach.
pragma solidity ^0.8.7;
import "#chainlink/contracts/src/v0.8/VRFConsumerBase.sol";
import "#openzeppelin/contracts/access/Ownable.sol";
import "hardhat/console.sol";
contract RandomNumberConsumer is VRFConsumerBase, Ownable{
// Variables
bytes32 internal s_keyHash;
uint256 internal s_fee;
uint256 private constant ROLL_IN_PROGRESS = 150;
mapping(bytes32 => address) private s_rollers;
mapping(address => uint256) private s_results;
//address vrfCoordinator = 0x3d2341ADb2D31f1c5530cDC622016af293177AE0;
//address link = 0xb0897686c545045aFc77CF20eC7A532E3120E0F1;
// Events
event DiceRolled(bytes32 indexed requestId, address indexed roller);
event DiceLanded(bytes32 indexed requestId, uint256 indexed result);
/**
* Constructor inherits VRFConsumerBase
*
* Network: Rinkeby
* Chainlink VRF Coordinator address: 0xb3dCcb4Cf7a26f6cf6B120Cf5A73875B7BBc655B
* LINK token address: 0x01BE23585060835E02B77ef475b0Cc51aA1e0709
* Key Hash: 0x2ed0feb3e7fd2022120aa84fab1945545a9f2ffc9076fd6156fa96eaff4c1311
*/
constructor(address vrfCoordinator, address link, bytes32 keyHash, uint256 fee)
VRFConsumerBase(vrfCoordinator, link){
s_keyHash = keyHash;
s_fee = fee;
}
// Functions
function rollDice(address roller) public onlyOwner returns (bytes32 requestId){
console.log("RNG Contract address",address(this));
// Checking LINK balance
require(LINK.balanceOf(address(this)) >= s_fee, "Not enough LINK in contract.");
// Checking if roller has already rolled dice since each roller can only ever be assigned to a single house. TODO: this can be changed
require(s_results[roller] == 0, "Already rolled");
// Requesting randomness
requestId = requestRandomness(s_keyHash, s_fee); // Error is happening here!
// Storing requestId and roller address
s_rollers[requestId] = roller;
// Emitting event to signal rolling of dice
s_results[roller] = ROLL_IN_PROGRESS;
emit DiceRolled(requestId, roller);
}
// fulfillRandomness is a special function defined within the VRFConsumerBase contract that our contract extends from.
// The coordinator sends the result of our generated randomness back to fulfillRandomness.
function fulfillRandomness(bytes32 requestId, uint256 randomness) override internal {
// Transform the result to a number between 0 and 100, both included. Using % as modulo
require(randomness!=0, "Modulo zero!");
uint256 d100Value = (randomness % 100) + 1; // +1 so s_results[player] can be 0 if no dice has been rolled
// Assign the transformed value to the address in the s_results mapping variable.
s_results[s_rollers[requestId]] = d100Value;
// Emit a DiceLanded event.
emit DiceLanded(requestId, d100Value);
}
// playerWins determines whether the player wins or lose the battle, based on a fixed chance (0-100)
function playerWins (address player, uint8 chance) internal view returns (bool wins){
require(s_results[player] != 0, "Player has not engaged in battle!");
require(s_results[player] != ROLL_IN_PROGRESS, "Battle in progress!");
return s_results[player] <= (chance + 1); //+1 because dice is 1-101
}
}
RNG call from the importing contract(simplified to relevant part only. _player address is working correctly).
address _player = ownerOf(_monId);
rollDice(_player);
I have the certainty that the error occurs inside the rollDice function, more specifically in the call to requestRandomness. Apart from that, I cannot seem to find why the error is hapenning, nor any references to the error message (Below agreed payment) inside any of the dependency contracts. Cannot find any references online either.
Any help is appreciated, thanks.
Below agreed payment error message comes from the sufficientLINK modifier of the VRFCoordinator.sol contract. You can see it here and here.
Double-check the constructor parameters, especially the fee value.
Also, make sure to fund your smart contract with Rinkeby LINK tokens which you can claim from the faucet.

how do i compute the result of a solidity contract without paying gas fee?

suppose I have this solidity contract, I would like to calculate the result of minAmountOut2, without actually paying the gas gas fee. Is it possible to achieve it? I think this should be theoretically possible, but I'm not sure how to achieve it practically.... Thanks!
pragma solidity >=0.7.0 <0.9.0;
contract Storage {
constructor() payable {
// uint256 number;
address wbnb_addres = 0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c;
address pancake_swap_v2 = 0x10ED43C718714eb63d5aA57B78B54704E256024E;
uint amount = msg.value ;
address target_token_address = 0x0E09FaBB73Bd3Ade0a17ECC321fD13a19e81cE82;
address sender_address = tx.origin;
address[] memory address_input = new address[](2);
address_input[0] = wbnb_addres;
address_input[1] = target_token_address;
uint[] memory result = IPancakeRouter02(pancake_swap_v2).getAmountsOut(amount,address_input);
uint minAmountOut = result[1];
uint deadline = 1e30;
address[] memory address_output2 = new address[](2);
address_output2[0] = target_token_address;
address_output2[1] = wbnb_addres;
uint[] memory result2 = IPancakeRouter02(pancake_swap_v2).getAmountsOut(minAmountOut,address_output2);
uint minAmountOut2 = result2[1];
}
}
One possible way is that I convert the result into string and trigger a revert function.... Then when I run estimatgas function in web3, then i got the result using error and exception handling.
Far as I know estimateGas is the only such function. estimateGas should be almost equal to web3.eth.sendTransaction, the only difference between them is estimateGas does not send the transaction out actually, and if your operation will fail when call a function, you can not get a valid result by estimateGas.
You have now all code in constructor. You probably want to split the code up in smaller functions and then call estimateGas.

how to use the ECDSA.sol module correctly?

I have this contract using the ECDSA library.
import "./ECDSA.sol";
struct Signature {
uint8 v;
bytes32 r;
bytes32 s;
}
function make(Signature memory sign) public returns(bool)
I try to understand the parameters I have to use in this case. What I can see it's a tuple type value, but I can't figure out what it looks like for v, r, s. Where can I get these values from my address?
The v, r, and s parameters are a result of signing a message with a private key. The signature has 65 bytes, which are split into 3 parts:
65 byte array (of type bytes in Solidity) arranged the following way: [[v (1)], [r (32)], [s (32)]].
Source: OpenZeppelin
Sign off-chain (because you're using a private key).
Note the address in the comment, we'll verify it on-chain later.
const signature = await web3.eth.accounts.sign(
'Hello world',
// below is private key to the address `0x0647EcF0D64F65AdA7991A44cF5E7361fd131643`
'02ed07b6d5f2e29907962d2bfde8f46f03c46e79d5f2ded0b1e0c27fa82f1384'
);
console.log(signature);
Output
{
message: 'Hello world',
messageHash: '0x8144a6fa26be252b86456491fbcd43c1de7e022241845ffea1c3df066f7cfede',
v: '0x1c',
r: '0x285e6fbb504b57dca3ceacc851a7bfa37743c79b5c53fb184f4cc0b10ebff6ad',
s: '0x245f558fa13540029f0ee2dc0bd73264cf04f28ba9c2520ad63ddb1f2e7e9b24',
signature: '0x285e6fbb504b57dca3ceacc851a7bfa37743c79b5c53fb184f4cc0b10ebff6ad245f558fa13540029f0ee2dc0bd73264cf04f28ba9c2520ad63ddb1f2e7e9b241c'
}
Note that v is the last byte of signature, r is the first half, and s is the second half (excluding the last byte).
Verify on-chain
pragma solidity ^0.8;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/cryptography/ECDSA.sol";
contract MyContract {
function foo() external pure returns (bool) {
address recovered = ECDSA.recover(
0x8144a6fa26be252b86456491fbcd43c1de7e022241845ffea1c3df066f7cfede, // messageHash
0x1c, // v
0x285e6fbb504b57dca3ceacc851a7bfa37743c79b5c53fb184f4cc0b10ebff6ad, // r
0x245f558fa13540029f0ee2dc0bd73264cf04f28ba9c2520ad63ddb1f2e7e9b24 // s
);
return recovered == address(0x0647EcF0D64F65AdA7991A44cF5E7361fd131643);
}
}

Using ecrecover function - Solidity

I'm trying to verify a message, I searched on StackOverflow and I found the ecrecover function. but when I use it, it returns a different address from what I expect.
function verify(bytes32 hash, uint8 v, bytes32 r, bytes32 s) constant returns(address) {
bytes memory prefix = "\x19Ethereum Signed Message:\n32";
bytes32 prefixedHash = keccak256(prefix, hash);
return ecrecover(prefixedHash, v, r, s);
}
signature object:
{
message: 'a',
messageHash: '0x34f291c0b5f0c13c8f43e9d37c04094c22234da43f4040adb36654c98235b4b3',
v: '0x1b',
r: '0x944f8187c19a711259e32dd9ab0f005c97c9e2013c735f823d3ad34c7cd5030f',
s: '0x254607e8d32e8a0436c8d678fe7d3478c8858fd903e164c51f8a8595e723b7a7',
signature: '0x944f8187c19a711259e32dd9ab0f005c97c9e2013c735f823d3ad34c7cd5030f254607e8d32e8a0436c8d678fe7d3478c8858fd903e164c51f8a8595e723b7a71b' }
input: (i pass it to remix-ide)
"0x34f291c0b5f0c13c8f43e9d37c04094c22234da43f4040adb36654c98235b4b3", 0x1b, "0x944f8187c19a711259e32dd9ab0f005c97c9e2013c735f823d3ad34c7cd5030f", "0x254607e8d32e8a0436c8d678fe7d3478c8858fd903e164c51f8a8595e723b7a7"
output: (wrong)
0x5dd277a46b3ab8ce30735d82df5e6e8312bce7ef
Please help me figure out the problems. many thanks.