-1

I am trying to interact with the Chainlink VRF function.

I have a Coordinator contract that needs random numbers to draw cards out of a deck. The Coordinator calls a Random Number Generation Oracle that implements Chainlink's VRFConsumerBase contract.

I want my Coordinator/Oracle to purchase LINK tokens automatically using the Ether that the user sent for the draw price so that it can always provide the required LINK fee to the VRF function and it does not need a human to manually top up the contract with LINK.

Is there any working example/tutorial on how one could achieve this? I really wish to avoid forcing the users of my contract to purchase LINK themselves.

Thanks!

1 Answers1

3

A contract itself can't 'automatically have LINK transferred to it', because to transfer LINK from one account to another, the owner of the LINK must be the one that signs the transaction, however, there are a few other ways to achieve the end result you're looking for.

1 - Rather than a straight transfer, you can do a swap on a DEX like Uniswap programatically, ie https://docs.uniswap.org/protocol/V2/guides/smart-contract-integration/trading-from-a-smart-contract

2 - If you know how much LINK will be required for the contract, you can simply pre-fund it beforehand, or when it's created. Am guessing this isn't the case for you though.

3 - Do it external to the blockchain. ie you can have a process running that funds the contract with link every so often, checking if its empty or near empty

4 - You can make use of meta-transactions so that the people playing the game don't need any LINK. See this example. Take note this still uses an external relayer like I mentioned in the last point

5 - I've seen examples where others just pay an upfront cost to ensure the coordinator contracts are well funded with LINK, and then they manually just top them up as needed, using users deposited eth/funds as a kind of payment to offset them having to send LINK to the contract.

  • 1
    Thank you for your detailed reply! I was thinking the "automatic" purchase of LINK could be done through a decentralized exchange, something like Uniswap maybe. So my "Coordinator" contract would receive payment in ETH as a requirement of the drawCard() function, then place a market order on a DEX to buy 2 LINK for the cost of the RNG. Once the LINK is transfered to the contract, continue to call the Chainlink VRF function. Is there something super wrong in my logic or something that would make this sort of implementation unnecessary? LINK is around $50 atm and pre-funding would cost loads – Alexandru Chiriac May 09 '21 at 15:51
  • 1
    If you can somehow perform the swap via uniswap or some other measure then yes you're logic is ok. All Chainlink cares about is the contract having enough LINK once the VRF request is initiated. Suggest maybe checking out the uniswap docs to see if you can perform the swap in solidity https://docs.uniswap.org/concepts/introduction/swaps – Harry Papacharissiou May 10 '21 at 01:43
  • This is wrong. Uniswap can be used via contracts to do swaps. How do you think dex aggregators work? – ParkerD Jan 30 '22 at 22:41
  • 1
    good point @ParkerD, I updated the answer – Harry Papacharissiou Feb 01 '22 at 02:14