我怎样才能通过 solidity 从用户那里获得 LINK 资助的代币?

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

我的合约需要 LINK 令牌才能运行。

我想让用户通过合约上的函数向合约提供 LINK 代币,然后根据他们的资金为用户做一些逻辑。

我怎样才能在合同范围内做到这一点? 我试过这样打电话。

LINK.balanceOf(walletaddress) 确实有效,(它获取钱包中的 link 金额)。 但是,由于某些原因,下面的这个功能不起作用。 它经历了所有,但像空数据。

当我从他们的前端按钮执行相同的调用时,Metamask 显示不同。 (我假设它和混音一样)

https://testnet.bscscan.com/token/0x84b9B910527Ad5C03A9Ca831909E21e236EA7b06#readContract

以下是我如何尝试让我的合同获得批准。

function approveTransfer(uint256 amount) public returns(string memory) {
        uint256 approveAmt = amount * 10**18;
        LINK.approve(_msgSender(),approveAmt);
        approvedAmount = approveAmt;
    }

在您的合同中不可能,除非外部合同明确允许它或包含使用已弃用的 tx.origin 而不是 msg.sender 的安全漏洞。

请参阅 中的最后一段和代码片段,了解它可能如何被滥用。


your contract 执行 LINK 的功能时,LINK contract 中的 msg.sender 现在是 your contract - 而不是用户。

transfer()approve()(以便稍后调用transferFrom())功能都依赖于msg.sender,您需要成为用户。但它不是 - 这是你的合同。


your contract 委托调用 LINK 的函数时,状态更改存储在 your contract 中 - 而不是 LINK.

但是您需要将状态更改存储在 LINK 合约中,因此这也不是一个选项。

好的,所以我一直在搜索、搜索和搜索....

直到我在他们的不和谐频道上发现了一些惊人的东西。 (它很可能也写在其他地方)。

哈里 |链环

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

所以简而言之,这是可能的,因为我的合同有

function onTokenTransfer(address from, uint256 amount, bytes memory data)  public {
        receivedTokenTransfer = true;
        lastDepositer = from;
        lastDepositerAmountInLink = amount / 10**18;
    }

因此,我可以发送 LINK 到 LINK 的合约地址,而不是将 LINK 发送到我自己的合约我的合同应该收到 LINK 的金额。

发送此付款后,chainlink 将向我的合约发送付款并调用名为 onTokenTransfer 的函数(在我的合约上)。 :)))

希望这对以后的人有帮助。