1. Create table in Supabase
Last updated
Last updated
It is very common for Web3 projects to have a hybrid design. While assets are created, stored and transferred on blockchains, most business logic and application data run on Web2 infrastructure.
In this case, while NFTs gifts are minted and transferred on-chain, secret gift codes that are supposed to be shared with giftees are saved in a traditional database. After the request from users, the AI agent will check for user input gift codes with database records.
Now let's move on to managing data off-chain. In this step, we will use Supabase to store some crucial data, specifically the gift code consumed by users.
The Web2 infrastructure we are going to use is Supabase which provides a suite of tools to simplify the backend development process. We are going to use Supabase to create rapid backend services with its built-in PostgreSQL database.
Create a new organization
Complete configurations of the organization with free plan. You can name the organization with whatever name you like.
Once the Organization and its project is created, go the table editor to create a new table called "Gifts" to save gift names and codes.
Note:
The Table name "Gifts" is case sensitive - it will be important when you compose the URL to fetch from the database via the Supabase API.
Keep the "Enable Row Level Security (RLS)" checked as default since the setting allow you to manage the row-level read and write access.
Table Columns
We need 3 columns in the Gifts
table as follows:
ID: This will serve as the primary key for our table. It will be an integer type to uniquely identify each gift entry.
gift_name: A varchar type column to store the name of the gift. This can be used to display or describe the gift to the users.
gift_code: Also a varchar type, where we'll store the unique code associated with each gift. This code will be what users input on Twitter, which our smart contract will then verify against this stored data.
In the table, there is a auto-generated column "id" that is the primary key of the record. You need to create 2 other columns: second column "gift_name" that holds names of gifts in the table; third column "gift_code" for secret code thats will be shared to giftees.
Insert records to the table
After the table is created, click on the green "SAVE" button.
The "Gifts" table will look like this:
These values in gift_code
must be identical to the above to match the variable we defined in GetGift.sol
later.
Then in Supabase, in the Table Editor view, click the green INSERT button and choose "Import data from CSV" and then choose the CSV you just downloaded.
After records are uploaded and inserted, your table should look as below:
Create RLS policy for the table
On the left navigator bar: go to "Authentication" >> "Policies". Under the table, click "Create policy".
On the right-panel "Template", choose the first "SELECT" policy template ("Enable Read Access For All Users") and click "save policy". By doing this, you have successfully created a RLS (Row Level Security) policy to allow users who have API key to read the table.
Go the tab "project settings" on the left navigation bar (it has the ⚙️ icon) >> "Data API" to get project access URL and API key.
Note the URL and API key somewhere safe or just keep this tab open. You will need these in the next step.
Create a new organization at Supabase . Within an organization, you can have multiple projects.
Now we need to insert 3 rows into the table. For your convenience you can use . Export the data as csv (File >> Download>> CSV).