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
GetGift.sol as functions consumerOpen remix and create a new file with name
GetGift.sol.Paste the code below into your new file (the file is also here).
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 likehttps://<<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.
Compile the smart contract.
Switch the network in metamask to Avalanche Fuji.
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.
Deploy GetGift.sol
Save the address of the deployed contract (we will use it on the next page).
Examine the GetGift code
GetGift codeBecause 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:
ERC721URIStorage: this makesGetGiftan ERC-721(the NFT standard) compliant smart contract, so that users NFTs can be minted and transferred under the contract.FunctionsClient: This smart contract provides helper methods and structs to construct and send Chainlink Functions requests and receive the fulfillments from off-chain DON.
ERC-721 related attributes and functions
giftToTokenUri: the mapping is to save the gift name as key and the corresponding NFT metadata as value.ITEM_1,ITEM_2,ITEM_3are gifts' names in bytes,ITEM_1_METADATA,ITEM_2_METADATA,ITEM_3_METADATAare URLs that hold JSON metadata for the 3 items. The Metadata is formatted to comply with the Opensea metadata standard.
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":

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 functionsetTokenUri. Find more details about ERC-721 functions here.addToAllowList: This is a user defined function to add access control for NFT related functions. The function creates an allowlist where users can managegiftToTokenUriand call functionsendRequest.
Chainlink Function related attributes and functions
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 Networks page.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 Network DON ID configurations.CALLBACK_GAS_LIMIT: Hardcoded maximum of gas limit that Chainlink Functions can spend to make callback call. Check the official docs for more details.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.sendRequest: we use this to send Chainlink Functions requests. In this case, the function wrapsSOURCEwith all necessary data as a Request object and sends it to the DON.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 inSOURCEis completed . In our case, the DON returns gift names to theGetGiftconsumer contract, andfulfillRequestthen calls the internal functionsafeMintif the returned gift name is valid.You can find more details about the function in official document.
Last updated