Call chainlink from other smart contract? - ethereum

I need to get a value from an array which I read through a chainlink oracle. So far so good, but I need that value in another contract to make use of it, how could I make this happen?
Also, how can I make a combined payment, when someone buys one Service, the money would also be used to pay the gas for the Chainlink fee?
thanks!

To read a value from a Chainlink data feed, you need to pass a feed's address to the AggregatorV3Interface.sol interface.
AggregatorV3Interface dataFeed = AggregatorV3Interface(0x9326....);
You then can call the interface's latestRoundData() function, which returns a tuple of values that contain the information you requested. For an example price feed:
(
uint80 roundID,
int price,
uint startedAt,
uint timeStamp,
uint80 answeredInRound
) = priceFeed.latestRoundData();
See https://docs.chain.link/docs/get-the-latest-price/ for more details.
When you say combined payment, what do you mean? If the Chainlink data feed call (as above) is called during a transaction, that transaction's gas will be used to pay for the computation. So if a user buys a service and sends gas with that call, and then in that call the data feed is called, the original sender's gas will be used to call the data feed method.

Related

Allowance failed when attempting to transfer LINK tokens

I'm creating a smart contract where users can create NFT raffles. I will be using Chainlink VRF for getting provably fair results. For this, the user who creates the raffle needs to supply the contract with LINK tokens. I'm attempting to transfer these tokens using an allowance.
function initRaffle(address _tokenContract, uint256 _tokenId, uint256 _ticketPrice) external {
require(_ticketPrice > 0, "Ticket price must be bigger than 0");
require(LINKToken.balanceOf(msg.sender) >= ChainlinkFee, "Insufficient LINK supplied");
require(LINKToken.allowance(msg.sender, address(this)) >= ChainlinkFee, "Allowance failed");
Running initRaffle results in Allowance failed. I've checked and the LINKToken.balanceOf(msg.sender) is bigger than the fee, so that shouldn't be the problem. The LINKToken.balanceOf(address(this)) is 0.
What's going wrong? And how do I create a working function for having the user transfer (fee amount) link tokens to the contract.
The user needs to invoke approve(yourContractAddress, feeAmount) directly on the LINKToken contract (not through your contract). This sets the allowance.
You can then use transferFrom() to pull tokens (up to the allowed amount) from the user's wallet.
bool success = LINKToken.transferFrom(msg.sender, address(this), ChainlinkFee);

web3js Uniswap handle event log data

I am building a scraper to get all Swap data from Uniswap using web3js.
So far I subscribe to the log and filter topics by Swap() event.
then I decode the data and get amount0In, amount1In, amount0Out, amount1Out.
My problem is in the swapExactETHForTokensSupportingFeeOnTransferTokens() function.
Normally a swap has token0 in and token1 out, but this function gives me values for 3 of the 4 and I can not seem to understand how to handle that. eventually what I want is to know what they spend, what they got and what I need to update the new reserves of that pair.
If someone has the understanding of the Uniswap RouterV2 contract swap functions, I would like to get some pointers on how to handle the data to get my calculations right.
I found the solution here ethereum.stackexchange Anyone with the same question gets a very detailed answer there.
some contracts actually transfers to recipient specified amount of tokens minus 10% (5% tax fee and 5% liquidity fee) 208940457743532637 - 10% = 188046411969179375 and emits Transfer event. Then, PancakePair _swap function emits Swap event with base value of Amount0Out 208940457743532637
Amount0In is greater than zero because the token contract returns part of tokens as a liquidity pair on Pancake Swap.

Questions about mint, burn function in UniswapV2Pair.sol

I'm studying UniswapV2Pair.sol https://github.com/Uniswap/v2-core/blob/master/contracts/UniswapV2Pair.sol and I have some question about the mint and burn function.
What I understand:
When user deposit the token pair, mint function mints the new liquidity token and send to the user
When user withdraw the token pair, burn function burns the new liquidity token and sends the deposited token pair back to user .
What I'm confused about:
I'm confused about the bold part of burn function I mentioned above. I think that mint and burn function is like mirror(opposite) function, but mint function doesn't include the feature which token pair are send to the exchange contract. However, burn function uses _safeTransfer which sends the token pair back to user.
I'm confused why did they designed assymetrically.
The mint() function calculates the amount of minted LP tokens from the difference of
recorded reserves of the underlying tokens (_reserve0 and _reserve1)
and the actual underlying token balance owned by the pair contract (balance0 and balance1)
So theoretically, if Alice just sent the underlying tokens to the pair contract without invoking the mint() function, that would make the accounting difference described above. And Bob would be able to invoke the mint() function and mint the LP tokens for himself profiting off Alice.
But that's not the usual process flow. Usually, the liquidity provider (Alice), invokes the addLiquidity() function of the router contract that performs both actions at once:
transfers the (approved) tokens from the user to the pair contract
invokes the mint() function on the pair contract calculating the difference created in this transaction
Which removes the possibility for Bob to intercept the Alice's minting process.
And having the mint() function executable by itself also allows anyone to claim unclaimed tokens that were sent to the pair contract by mistake for example.
However, if you want to transfer the underlying tokens out of the pair contract (i.e. burn() the LP tokens), there needs to be check already in the burn() function so that you can't claim more of the underlying tokens than you're eligible to.
No matter if you're invoking the pair burn() function directly or from the router removeLiquidity() (that's normally invoked from the Uniswap UI).
but mint function doesn't include the feature which token pair are
send to the exchange contract.
You showed the UniswapV2Pair.sol. mint function is used when we add liquidity which is defined in UniswapV2Router02.sol
function addLiquidity(
address tokenA,
address tokenB,
uint amountADesired,
uint amountBDesired,
uint amountAMin,
uint amountBMin,
address to,
uint deadline
) external virtual override ensure(deadline) returns (uint amountA, uint amountB, uint liquidity) {
(amountA, amountB) = _addLiquidity(tokenA, tokenB, amountADesired, amountBDesired, amountAMin, amountBMin);
address pair = UniswapV2Library.pairFor(factory, tokenA, tokenB);
TransferHelper.safeTransferFrom(tokenA, msg.sender, pair, amountA);
TransferHelper.safeTransferFrom(tokenB, msg.sender, pair, amountB);
liquidity = IUniswapV2Pair(pair).mint(to);
}
we are passing the amount of tokens that we want to add liquidity, since we cannot add an arbitrary amount, adding liquidity should not affect on the price, so the function itself is calculating the proper amounts and then those amounts are transferred to the UniswapV2Router02 contract. After those amounts are transferred, we are minting a new LP token and sending it to the to address
IUniswapV2Pair(pair).mint(to)
Inside mint function how much token will be minted is calculated and then _mint function called
_mint(to, liquidity);

Best way to register for a vote in solidity

Hi I'm creating a voting smart contract for a DAO and I have a security question. The voting system works like this:
You send your tokens to the smart contract then the smart contract registers how much tokens you have and assignes you "Power" which you use when you vote. Then the smart contract sends the funds back immidiately.
My question is if there is more secure way to do this. Without funds leaving usere's wallet.
Here is the code I have so far.
function getPower() payable public {
require(msg.value > 0, "The amount can't be 0");
require(election_state == ELECTION_STATE.OPEN);
require(votingPeriod > block.timestamp);
uint amountSent = msg.value;
// This function will take their money and assign power to the voter
// The power is equal to their deposit in eth * 10 so for each eth they get 10 power
voters[msg.sender].power = msg.value * 10;
payable(msg.sender).transfer(amountSent);
}
Thanks in advance.
Based on the provided code and question, I'm assuming you want to calculate the voting power based on the users' ETH balance - not based on their balance of the DAO token.
You can get the current ETH balance of an address, using the .balance member of an address type. So you could simplify your function as this:
function getPower() public {
require(election_state == ELECTION_STATE.OPEN);
require(votingPeriod > block.timestamp);
voters[msg.sender].power = msg.sender.balance * 10;
}
After performing the validations, it assigns the value based on the msg.sender ETH balance at the moment of the getPower() function being invoked. Without them needing to send ETH to the contract.
Note that this approach is not common and can be misused for example by users loaning a large amount of ETH just before executing the getPower() function. I'd recommend you to use a more common pattern of calculating the voting power based on their current holdings of the token representing their stake in the DAO.

Transfer money between accounts in solidity

I am writing a contract where I want to transfer money (present in the contract owners account and not the contract) to an account address passed to a function in the contract.
for some reason this code won't work
function payBill(uint value, address account) payable public {
account.transfer(value);
transactionCount += 1;
transactionAmount += value;
}
Your problem can be related to any frontend code, not only this.
You've got two options.
get that other user address from the contract and then run a direct transaction between two accounts (example here https://web3js.readthedocs.io/en/v1.2.6/web3-eth.html#id80)
send a value (see here https://web3js.readthedocs.io/en/v1.2.6/web3-eth-contract.html#id33) when you are calling the payBill method. If you don't do it, the default value is zero, and you don't see any transfer.
Also, please consider having a look at this https://diligence.consensys.net/blog/2019/09/stop-using-soliditys-transfer-now/
EDIT:
what you are trying to do here, is also not doable. This requires the contract to have the number of funds that you want to transfer. That is because, with the payable method, you are sending funds to the contract, but the funds will only be in the contract, once the transaction is accepted. On the other hand, you are trying to send that number of funds to another user, without having them on the contract. That is why you never see any balance.