2. Deploy the GetGift.sol
Last updated
Last updated
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.
GetGift.sol
as functions consumerATTENTION: 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.
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).
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 makes GetGift
an 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.
giftToTokenUri
: the mapping is to save the gift name as key and the corresponding NFT metadata as value.
For example, ITEM_1_METADATA
saved at the URL ipfs://QmaGqBNqHazCjSMNMuDk6VrgjNLMQKNZqaab1vfMHAwkoj
is as below:
In the metadata JSON file, image
is the URL of the saved image (https://ipfs.io/ipfs/QmU3aiUjBP66jSAHTJJP5kzAaiwvdsn43yoP1WTZCUsFrn)
, and it is the image for "100 discount":
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
.
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 wraps SOURCE
with all necessary data as a Request object and sends it to the DON.
Open and create a new file with name GetGift.sol
.
Paste the code below into your new file (the file is also ).
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 .
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 .
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 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 DON ID configurations.
CALLBACK_GAS_LIMIT
: Hardcoded maximum of gas limit that Chainlink Functions can spend to make callback call. Check the for more details.
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 SOURCE
is completed . In our case, the DON returns gift names to the GetGift
consumer 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 .