-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- confirm_tags to use transfers to sendtag checkout contract - update tag receipt activity - updates tests
- Loading branch information
Showing
14 changed files
with
470 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 23 additions & 1 deletion
24
packages/contracts/script/anvil-add-send-merkle-drop-fixtures.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
supabase/migrations/20240716043643_update_confirm_tags_to_use_sendtag_checkout_contract.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
set check_function_bodies = off; | ||
|
||
create table sendtag_checkout_contracts ( | ||
id serial primary key, | ||
address bytea not null, | ||
chain_id integer not null, | ||
created_at timestamp with time zone default now(), | ||
updated_at timestamp with time zone default now() | ||
); | ||
|
||
insert into sendtag_checkout_contracts (address, chain_id) values ( | ||
'\x3936f906910C0f74b6d1536614068368B94CDa85', 8453 | ||
); | ||
|
||
alter table sendtag_checkout_contracts enable row level security; | ||
|
||
CREATE OR REPLACE FUNCTION public.confirm_tags(tag_names citext[], event_id text, referral_code_input text) | ||
RETURNS void | ||
LANGUAGE plpgsql | ||
SECURITY DEFINER | ||
SET search_path TO 'public' | ||
AS $function$ | ||
declare tag_owner_ids uuid []; | ||
|
||
distinct_user_ids INT; | ||
|
||
tag_owner_id uuid; | ||
|
||
referrer_id uuid; | ||
|
||
_event_id alias for $2; | ||
|
||
checkout_contracts bytea []; | ||
begin | ||
-- Check if the tags exist and fetch their owners. | ||
select array_agg(user_id) into tag_owner_ids | ||
from public.tags | ||
where name = any (tag_names) | ||
and status = 'pending'::public.tag_status; | ||
|
||
-- If any of the tags do not exist or are not in pending status, throw an error. | ||
if array_length(tag_owner_ids, 1) <> array_length(tag_names, 1) then raise exception 'One or more tags do not exist or are not in pending status.'; | ||
|
||
end if; | ||
|
||
-- Check if all tags belong to the same user | ||
select count(distinct user_id) into distinct_user_ids | ||
from unnest(tag_owner_ids) as user_id; | ||
|
||
if distinct_user_ids <> 1 then raise exception 'Tags must belong to the same user.'; | ||
|
||
end if; | ||
|
||
-- Fetch single user_id | ||
select distinct user_id into tag_owner_id | ||
from unnest(tag_owner_ids) as user_id; | ||
|
||
if event_id is null | ||
or event_id = '' then raise exception 'Receipt event ID is required for paid tags.'; | ||
|
||
end if; | ||
|
||
-- Ensure we have sendtag_checkout_contracts | ||
select array_agg(address) into checkout_contracts | ||
from sendtag_checkout_contracts | ||
limit 1; | ||
if (checkout_contracts is null) | ||
then raise exception 'Sendtag checkout contract not found.'; | ||
end if; | ||
|
||
-- Ensure event_id matches the sender | ||
if ( | ||
select count(distinct sat.f) | ||
from public.send_account_transfers sat | ||
join send_accounts sa on decode(substring(sa.address, 3), 'hex') = sat.f | ||
where sat.event_id = _event_id | ||
and sa.user_id = tag_owner_id | ||
and sat.t = any(checkout_contracts) | ||
) <> 1 then raise exception 'Receipt event ID does not match the sender'; | ||
end if; | ||
|
||
-- save receipt event_id | ||
insert into public.receipts (event_id, user_id) values (_event_id, tag_owner_id); | ||
|
||
-- Associate the tags with the onchain event | ||
insert into public.tag_receipts (tag_name, event_id) | ||
select unnest(tag_names), | ||
event_id; | ||
|
||
-- Confirm the tags | ||
update public.tags | ||
set status = 'confirmed'::public.tag_status | ||
where name = any (tag_names) | ||
and status = 'pending'::public.tag_status; | ||
|
||
-- Create referral code redemptions | ||
if referral_code_input is not null | ||
and referral_code_input <> '' then | ||
SELECT id INTO referrer_id | ||
FROM public.profiles | ||
WHERE referral_code = referral_code_input; | ||
|
||
if referrer_id is not null -- 'Referral code is not valid.' | ||
and referrer_id <> tag_owner_id then -- Referrer cannot be the tag owner. | ||
INSERT INTO public.referrals (referrer_id, referred_id, tag) | ||
select referrer_id, | ||
tag_owner_id, | ||
unnest(tag_names); | ||
|
||
end if; | ||
|
||
end if; | ||
|
||
end; | ||
|
||
$function$ | ||
; |
47 changes: 47 additions & 0 deletions
47
supabase/migrations/20240716212645_update_tag_receipt_activity_triggers.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
set check_function_bodies = off; | ||
|
||
CREATE OR REPLACE FUNCTION public.tag_receipts_insert_activity_trigger() | ||
RETURNS trigger | ||
LANGUAGE plpgsql | ||
SECURITY DEFINER | ||
AS $function$ | ||
begin | ||
delete from activity | ||
where event_name = 'tag_receipt_usdc' | ||
and event_id in (select event_id from NEW_TABLE); | ||
|
||
insert into activity (event_name, event_id, from_user_id, to_user_id, data, created_at) | ||
select | ||
'tag_receipt_usdc', | ||
NEW_TABLE.event_id, | ||
t.user_id, | ||
null, | ||
json_build_object( | ||
'log_addr', | ||
sat.log_addr, | ||
'block_num', | ||
sat.block_num, | ||
'tx_idx', | ||
sat.tx_idx, | ||
'log_idx', | ||
sat.log_idx, | ||
'tx_hash', | ||
sat.tx_hash, | ||
'tags', | ||
array_agg(t.name), | ||
'value', | ||
-- cast v to text to avoid losing precision when converting to json when sending to clients | ||
sat.v::text | ||
), | ||
current_timestamp | ||
from NEW_TABLE | ||
join tags t on t.name = NEW_TABLE.tag_name | ||
join send_account_transfers sat ON NEW_TABLE.event_id = sat.event_id | ||
group by t.user_id, NEW_TABLE.event_id, sat.event_id, sat.log_addr, sat.block_num, sat.tx_idx, sat.log_idx, sat.tx_hash, sat.v; | ||
|
||
return NULL; | ||
end; | ||
$function$ | ||
; | ||
|
||
|
Oops, something went wrong.