2. Deploy the GetGift.sol

Where are we?

What is Functions consumer smart contract?

Requests to Chainlink Functions are constructed and sent from on-chain smart contracts, so we need to build a consumer smart contract for Chainlink Functions.

Compile and deploy the GetGift.sol as functions consumer

  1. Open remixarrow-up-right and create a new file with name GetGift.sol.

  2. Paste the code below into your new file (the file is also herearrow-up-right).

  3. ATTENTION: In the pasted code, look for the variable SOURCE. This contains the JavaScript program that Chainlink Functions will execute. Notice that there is a reference to the Supabase database connection URL. Update it, but make sure you leave the beginning and ending "\" symbols. The URL will be a REST API URL that also refers to your supabase project's ID : For example it would look like https://<<Your Project ID>>.supabase.co/rest/v1/Gifts?select=gift_name,gift_code

Note: The table name "Gifts" in the URL above is case-sensitive. Make sure it matches the casing in your table name in Supabase. The safest way to do this is copy the Supabase project ID only and paste it in. Replace only that bit and be careful not to touch any other part of the SOURCE javascript.

  1. Compile the smart contract.

  2. Switch the network in metamask to Avalanche Fuji.

  3. In the tab deploy, select "Injected Provider" under "Environment" to make sure you are using the Avalanche Fuji network in your metamask, the Custom (43113) network should show under Injected Provider.

  4. Deploy GetGift.sol

  5. Save the address of the deployed contract (we will use it on the next page).

Examine the GetGift code

Because the gifts are represented by NFTs, the contract needs to have NFT minting functionality. This contract also acts as the client for Chainlink Functions requests. Therefore GetGift inherits from the following two contracts:

  1. ERC721URIStorage: this makes GetGift an ERC-721(the NFT standard) compliant smart contract, so that users NFTs can be minted and transferred under the contract.

  2. FunctionsClient: This smart contract provides helper methods and structs to construct and send Chainlink Functions requests and receive the fulfillments from off-chain DON.

  1. giftToTokenUri: the mapping is to save the gift name as key and the corresponding NFT metadata as value.

  2. ITEM_1, ITEM_2, ITEM_3 are gifts' names in bytes, ITEM_1_METADATA, ITEM_2_METADATA, ITEM_3_METADATA are URLs that hold JSON metadata for the 3 items. The Metadata is formatted to comply with the Opensea metadata standardarrow-up-right.

For example, ITEM_1_METADATA saved at the URL ipfs://QmaGqBNqHazCjSMNMuDk6VrgjNLMQKNZqaab1vfMHAwkoj is as below:

In the metadata JSON file, imageis the URL of the saved image (https://ipfs.io/ipfs/QmU3aiUjBP66jSAHTJJP5kzAaiwvdsn43yoP1WTZCUsFrn), and it is the image for "100 discount":

Image for the gift 100 discount
  1. safeMint: Function to mint a new NFT under this collection. Token ID for all minted NFTs are incremented and input metadata is added to the minted token with function setTokenUri. Find more details about ERC-721 functions herearrow-up-right.

  2. addToAllowList: This is a user defined function to add access control for NFT related functions. The function creates an allowlist where users can manage giftToTokenUri and call function sendRequest.

  1. ROUTER_ADDR: Hardcoded Functions Router address on Avalanche Fuji testnet. The address is used to construct Chainlink Functions requests. Functions Router contracts addresses vary across different blockchain networks. Configurations for router addresses can be found on Supported Networksarrow-up-right page.

  2. DON_ID: Hardcoded ID for the DON (decentralized oracle network) employed by Chainlink Functions on Avalanche Fuji. Similar as Functions router addresses, DON ID varies in different blockchain network. Please check the Supported Networkarrow-up-right DON ID configurations.

  3. CALLBACK_GAS_LIMIT: Hardcoded maximum of gas limit that Chainlink Functions can spend to make callback call. Check the official docsarrow-up-right for more details.

  4. SOURCE: The JavaScript code you supply that that is executed by Chainlink Functions on the DON. In the Chainlink Functions requests, this source code is uploaded and executed in DON environment. This code fetches the data in the Supabase database, and returns the correct gift name corresponding to the gift code provided by the user.

  5. sendRequest: we use this to send Chainlink Functions requests. In this case, the function wraps SOURCE with all necessary data as a Request object and sends it to the DON.

  6. fulfillRequest: this is a method that must be implemented in any consumer contract for Chainlink Functions to work. It is used as the "callback" function that the DON sends responses to, after the computation in SOURCEis completed . In our case, the DON returns gift names to the GetGiftconsumer contract, and fulfillRequest then calls the internal function safeMint if the returned gift name is valid.You can find more details about the function in official documentarrow-up-right.

Last updated