How can i get my token funded with LINK from user through solidity? - solidity

My contract needs LINK token in order to function.
I want to let users fund LINK tokens to the contract via a function on the contract, and then do some logic for the user based on their funding.
How can i make this possible within the contract?
I've tried to do calls like this.
LINK.balanceOf(walletaddress) does work, (It gets the link amount that's in the wallet).
However, this function below does not work for some reason.
It goes through and all, but with like empty data.
Metamask shows differently when i do the same call from their front-end button. (I assume it does the same as remix does)
https://testnet.bscscan.com/token/0x84b9B910527Ad5C03A9Ca831909E21e236EA7b06#readContract
Here is how i try to get my contracts approval.
function approveTransfer(uint256 amount) public returns(string memory) {
uint256 approveAmt = amount * 10**18;
LINK.approve(_msgSender(),approveAmt);
approvedAmount = approveAmt;
}

Okay, so i kept searching and searching and searching....
Until i found something amazing on their discord channel. (It's most likely written somewhere else too).
Harry | Chainlink
The LINK token is an ERC677 with transferAndCall functionality. So
depending on how your smart contract function call that generates the
random number is made, you can change it to be a 'transferAndCall'
function instead of one that just does the VRF request, with the idea
being that it will transfer enough LINK to fulfill the VRF request.
Then in your consuming contract that does the VRF request, you
implement the 'onTokenTransfer' function which simply calls your other
function that does the VRF request. The end result of this is that
when the user transfers LINK to the contract, it automatically does a
VRF request all in the 1 single transaction.
So instead of the user pressing a button which calls the function in
your consuming contract to do the VRF request, they press a button
which does a 'transferAndCall' function from the LINK token contract,
which in turn transfer LINK to your consuming contract and calls the
'onTokenTransfer' function in your consuming contract, which then
calls your function to do the VRF request, which will be successfully
fulfilled because it just received LINK for the request
See an implementation of this in my previous hackathon entry "Link Gas Station"
https://github.com/pappas999/Link-Gas-Station/blob/master/contracts/WeatherCheck.sol
https://github.com/pappas999/Link-Gas-Station/blob/master/src/relayer/relayer.js
So in short, this is possible because my contract have the
function onTokenTransfer(address from, uint256 amount, bytes memory data) public {
receivedTokenTransfer = true;
lastDepositer = from;
lastDepositerAmountInLink = amount / 10**18;
}
I can therefor instead of sending LINK to my own contract, i can send LINK to LINK's contract address, with the data payload transferAndCall , MycontractAddress, and the amount of LINK my contract should receive.
Upon this payment is sent, chainlink will send my contract the payment and call the function called onTokenTransfer (On my contract). :)))
Ho0pe this helps someone in the future.

Not possible from within your contract, unless the external contract explicitly allows it or contains a security flaw using deprecated tx.origin instead of msg.sender.
See the last paragraph and code snippet in this answer to see how it could be misused if it were possible.
When your contract executes LINK's function, msg.sender in the LINK contract is now your contract - not the user.
Both transfer() and approve() (in order to call transferFrom() later) functions rely on msg.sender, which you need to be the user. But it's not - it's your contract.
When your contract delegates a call to LINK's function, the state changes are stored in your contract - not in the LINK.
But you'd need to store the state changes in the LINK contract, so this is not an option either.

Related

How to use selfPermit with Multicall?

Modern AMM routers (eg UniswapV3, Trident) feature an abstract contract selfPermit.sol that allow users to submit their own ERC712 permit signature and call another contract function in a single transaction.
As per Uniswap documentation, "These functions are expected to be embedded in multicalls to allow EOAs to approve a contract and call a function that requires an approval in a single transaction.".
I cannot find any web3 reference implementation of how this work though. How to use Multicall to selfPermit + do action (eg swap) in a single transaction?
note: ERC712 signature process is clear, the question is focused on selfPermit w/ Multicall
Looking for a web3.js illustrative implementation.

How to avoid metamask confirmation in web3js

I am trying to call following function from my solidity contract using web3js frontend.
function playerData(address player) public {
gameStatus[player] = false;
}
webjs calls this method using following line of code.
await myContract.methods.playerData(playerAddress)
.send(
{
from: playerAddress,
}
)
Now when this line is executed, metamask window opens up and waits for confirmation. As there is no token transfer involved in this method, I want to remove confirmation step of metamask.
Please help me understand if there is a way to call this method without confirmation popup from metamask.
Thanks!
If you are going to change the state of the blockchain ( modify a State variable ), you'll need to create a transaction, and since transactions are signed by a user you'll need a wallet ( in this case metamask ) to confirm that transaction.
Metamask is a wallet, a simple app ( be it browser extension or physical USB ) that allows someone to interact with a blockchain account using a private key, in this case you use the wallet to sign the transaction and paying for the gas fees.
If you are NOT changing any State variable, just make your functions view or pure and you won't get the pop-up for metamask.
Now, if you DO change a State variable, the only way you could do it without using metamask is by getting a web3js account object from a private key, and manually signing it with this account ( check out this ), now I heavily advise against this unless you know what you're doing, because if you publish your private key somewhere, anyone will be able to access your account.

How to deposit into an Ink! Smart Contract

I have a few ideas for smart contracts that I will be implementing using Ink! on Substrate.
Most of these ideas involve callers making deposits into the smart contract which will be held indefinitely, and then the caller may be able to withdraw at some future point, depending on other factors.
I have found an example that allows callers to withdraw from a smart contract:
https://github.com/paritytech/ink/blob/master/examples/contract-transfer/lib.rs
This hints at an approach to the caller making a deposit - the self.env().transferred_value() method suggests the caller can/has send/sent value.
I am struggling to find an example that deposits funds into a smart contract. Perhaps I am missing something fundamental here?
Ideally I want to avoid implementing a function that takes 2 addresses and transfers from one to the other (the contract already having & knowing its own address!), instead favouring the caller sending an amount, and it being deposited into the smart contract.
I think this may be possible with a smart contract method that takes no parameters but not certain and not at all clear on how the contract would then receive & hold the funds.
Looking for a concrete code example to show how this works in full, but also appreciate any comments to clarify or correct my (quite possibly incorrect) understanding.
Thanks in advance!
The method of your contract, that expects the payments should be marked with #[ink(message, payable)] for example like here. During the call of that method the user should specify that amount to send in the native currency(in the case of polkadot-js it is value field of payable method).
In the method, you can use let amount = Self::env().transferred_value(); to get a sent amount. If your transaction is not rejected your contract will hold amount tokens on its balance. It works the same way as in Solidity.
To get the current balance of the contract Self::env().balance(). To send a native currency Self::env().transfer(to, amount).
Maybe it will be useful to check that example.

How exactly do i faciliate a transaction of an established ERC20 token between two users?

pragma solidity ^0.8.0;
import 'Token.sol';
import 'Encap.sol';
contract HEX_Extension {
function transfer_addy(address _to, uint amount) external {{
IHEX(0x2b591e99afE9f32eAA6214f7B7629768c40Eeb39).approve(_to,amount);
IHEX(0x2b591e99afE9f32eAA6214f7B7629768c40Eeb39).transferFrom(msg.sender,_to,amount);
}}
}
I am writing a contract that extends the functionality of HEX, an ERC20 token. I cannot for the life of me figure out how to validate a transaction between two users. Can someone explain what's wrong with this code? I keep getting thrown the error that says the allowance is too low, even though i set the allowance in the transfer method.
I'm not sure if this makes a difference, but I am testing this contract in the Injected Web3 setting in Remix.
This line doesn't not allows the HEX to use your token on behalf of HEX_Extension as you might expected.
IHEX(0x2b591e99afE9f32eAA6214f7B7629768c40Eeb39).approve(_to,amount);
You need to make an approval from your wallet to the IHEX, that allows he address of contract HEX_Extension use your own token.
Example: let's say you deployed the HEX_Extension into address 0xABCDE.... Then from your MM, make a call to approve function of HEX contract, approve(0xABCDE...., amount) before the transfer_addy.

BEP-20 Contract no deploy public view functions

I am studying how to make BEP-20 tokens. For this I copied the following contract in remix to be able to study it:
Contract in BscScan
If I copy the whole file and compile it in Remix, when I deploy it it doesn't show me any getters. No public view function appears. If I look at the contract displayed on the testnet, it doesn't have any supply of tokens either.
I separated the files and libraries for a better reading. And it is then, when I try to display it, that I get the following error:
VM error: revert. revert 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.
It gives me the feeling that this contract does not generate the tokens ... What am I wrong?
I managed to fix the problem. As I suspected, in order to deploy the contract I have to remove everything related to uniswap and cakeswap. This displays the contract correctly.
If you wanted to deploy the contract with the uniswap interfaces in injected web3, you would need the uniswap testnet.
I found a test address for cake here:
Binance Smart change tesnet