-
Notifications
You must be signed in to change notification settings - Fork 0
/
storeTransferSingle.ts
105 lines (89 loc) · 3.04 KB
/
storeTransferSingle.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { supabase } from "@/clients/supabaseClient.js";
import { getHypercertTokenId } from "@/utils/tokenIds.js";
import { chainId } from "@/utils/constants.js";
import { getAddress } from "viem";
import { getLowestValue } from "@/utils/getMostRecentOrDefined.js";
import { ParsedTransferSingle } from "@/parsing/transferSingleEvent.js";
import { StorageMethod } from "@/indexer/processLogs.js";
import _ from "lodash";
/*
This function stores the hypercert token and the ownership of the token in the database.
@param token The token to store.
@returns The stored data.
@example
```js
const token = {
token_id: 1n,
const storedData = await ("0x1234...5678", 1n, metadata, cid);
```
*/
export const storeTransferSingle: StorageMethod<ParsedTransferSingle> = async ({
data,
}) => {
if (_.isArray(data)) return;
const hypercertTokenId = getHypercertTokenId(data.token_id);
if (hypercertTokenId.toString() === data.token_id.toString()) return;
let claims_id;
try {
const { data: claim_id, error: claimError } = await supabase.rpc(
"get_or_create_claim",
{
p_chain_id: chainId,
p_contract_address: getAddress(data.contract_address),
p_token_id: hypercertTokenId.toString(),
p_creation_block_number: data.block_number,
p_creation_block_timestamp: data.block_timestamp,
p_last_update_block_timestamp: data.block_timestamp,
p_last_update_block_number: data.block_number,
},
);
if (claimError || !claim_id) {
console.error(
`[storeTransferSingle] Error while getting or creating claim for token ${data.token_id}.`,
claimError,
claim_id,
);
return;
}
claims_id = claim_id;
} catch (e: unknown) {
console.error(`[StoreUnitTransfer] Could net create or get claim_id.`, e);
return;
}
const { data: token, error } = await supabase
.from("fractions")
.select("*, token_id::text")
.eq("token_id", data.token_id.toString())
.ilike("fraction_id", `${chainId}-${getAddress(data.contract_address)}-%`)
.maybeSingle();
if (error) {
console.error(`[storeTransferSingle] Error while getting token.`, error);
return;
}
const _token = {
...token,
claims_id: token?.claims_id ?? claims_id,
fraction_id: `${chainId}-${getAddress(data.contract_address)}-${data.token_id}`,
token_id: token?.token_id.toString() ?? data.token_id.toString(),
creation_block_timestamp: getLowestValue(
token?.creation_block_timestamp,
data.block_timestamp,
),
creation_block_number: getLowestValue(
token?.creation_block_number,
data.block_number,
),
last_update_block_timestamp: data.block_timestamp,
last_update_block_number: data.block_number,
owner_address: getAddress(data.to_owner_address),
value: data.value.toString(),
};
await supabase
.from("fractions")
.upsert(_token, {
onConflict: "claims_id, token_id",
ignoreDuplicates: false,
defaultToNull: true,
})
.throwOnError();
};