PatrickAlphaC / smartcontract-lottery

MIT License
79 stars 113 forks source link

Funding Lottery Contract in development mode: Where does LINK come from? #60

Closed JongCleo closed 2 years ago

JongCleo commented 2 years ago

https://github.com/PatrickAlphaC/smartcontract-lottery/blob/8876d504b707883f148f9a8cac7a287b90d0f5df/scripts/helpful_scripts.py#L81

When fund_with_link() is called on a live testnet like Rinkeby, get_account resolves to our real metamask wallet, which is probably loaded with LINK from a chainlink faucet.

When fund_with_link() is called on a local blockchain network, is ganache smart enough to generate LINK on demand from its test account?

PatrickAlphaC commented 2 years ago

Hi!

Great question. It's not smart enough, however, before you call fund with link, you'll often call deploy_mocks, which will deploy a mock LINK token.

In our deploy scripts, we call get_contract which calls the deploy_mocks if on a local network:

https://github.com/PatrickAlphaC/smartcontract-lottery/blob/8876d504b707883f148f9a8cac7a287b90d0f5df/scripts/helpful_scripts.py#L39

JongCleo commented 2 years ago

Ah silly oversight on my part, thanks @PatrickAlphaC for pointing that out.

Just to be super clear for anyone else that got confused:

In a development, local blockchain context:

  1. Deploy Mocks is called
  2. LinkToken.sol is deployed

In the LinkToken constructor, https://github.com/PatrickAlphaC/smartcontract-lottery/blob/8876d504b707883f148f9a8cac7a287b90d0f5df/contracts/test/LinkToken.sol#L14 Contract allocates the entire token supply of 1 billion LINK tokens to Msg.sender (our wallet)

  1. other stuff happens (creating lottery, entering lottery, ending lottery etc.)
  2. fund_with_link() is called and transfer 0.1 LINK from our wallet to the Lottery contract, leaving our balance at 999.99 million LINK