-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuris.ts
120 lines (117 loc) · 4.97 KB
/
uris.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { init, __wasm } from "@tableland/sqlparser";
export async function normalize(sql: string) {
if (__wasm == null) {
await init();
}
return (await globalThis.sqlparser.normalize(sql)).statements[0];
}
export async function getURITemplate(
tablelandHost: string,
rigsTable: string,
attributesTable: string,
dealsTable: string,
lookupsTable: string,
pilotsTable: string,
displayAttributes: boolean
): Promise<string[]> {
if (!displayAttributes) {
const uri =
tablelandHost +
"/api/v1/query?format=objects&extract=true&unwrap=true&statement=" +
encodeURIComponent(
await normalize(
`select
json_object(
'name','Rig #'||id,
'external_url','https://garage.tableland.xyz/rigs/'||id,
'image','ipfs://'||renders_cid||'/'||(select value from ${lookupsTable} where label = 'image_full_name'),
'image_alpha','ipfs://'||renders_cid||'/'||(select value from ${lookupsTable} where label = 'image_full_alpha_name'),
'image_medium','ipfs://'||renders_cid||'/'||(select value from ${lookupsTable} where label = 'image_medium_name'),
'image_medium_alpha','ipfs://'||renders_cid||'/'||(select value from ${lookupsTable} where label = 'image_medium_alpha_name'),
'thumb','ipfs://'||renders_cid||'/'||(select value from ${lookupsTable} where label = 'image_thumb_name'),
'thumb_alpha','ipfs://'||renders_cid||'/'||(select value from ${lookupsTable} where label = 'image_thumb_alpha_name'),
'animation_url',(select value from ${lookupsTable} where label = 'animation_base_url')||rig_id||'.html',
'attributes',json_array(
json_object(
'trait_type','status',
'value','pre-reveal'
)
)
)
from
${rigsTable}
inner join ${attributesTable} on id = rig_id
where id=ID
group by id;`
)
);
return uri.split("ID");
} else {
const uri =
tablelandHost +
"/api/v1/query?format=objects&extract=true&unwrap=true&statement=" +
encodeURIComponent(
await normalize(
`select
json_object(
'name',case when exists(select * from ${pilotsTable} where rig_id = r.id and end_time is null) then 'Rig #'||id||' ✈️' else 'Rig #'||id end,
'external_url','https://garage.tableland.xyz/rigs/'||id,
'image','ipfs://'||renders_cid||'/'||(select value from ${lookupsTable} where label = 'image_full_name'),
'image_alpha','ipfs://'||renders_cid||'/'||(select value from ${lookupsTable} where label = 'image_full_alpha_name'),
'image_medium','ipfs://'||renders_cid||'/'||(select value from ${lookupsTable} where label = 'image_medium_name'),
'image_medium_alpha','ipfs://'||renders_cid||'/'||(select value from ${lookupsTable} where label = 'image_medium_alpha_name'),
'thumb','ipfs://'||renders_cid||'/'||(select value from ${lookupsTable} where label = 'image_thumb_name'),
'thumb_alpha','ipfs://'||renders_cid||'/'||(select value from ${lookupsTable} where label = 'image_thumb_alpha_name'),
'animation_url',(select value from ${lookupsTable} where label = 'animation_base_url')||rig_id||'.html',
'attributes', json_group_array(
json_object('display_type',display_type,'trait_type',trait_type,'value',value)
)
)
from
${rigsTable} r
inner join (
select *
from ${attributesTable}
union
select
a.rig_id,
'string' display_type,
'Garage Status' trait_type,
case when start_time is null then 'parked' else 'in-flight' end value
from
${attributesTable} a
left join (select * from ${pilotsTable} where end_time is null) s on a.rig_id = s.rig_id
union
select
rig_id,
'string' display_type,
'Filecoin Deal '||deal_number trait_type,
(select value from ${lookupsTable} where label = 'filecoin_base_url')||deal_id value
from ${dealsTable}
) on id = rig_id
where rig_id=ID
group by id;`
)
);
return uri.split("ID");
}
}
export function getContractURI(
tablelandHost: string,
contractTable: string
): string {
return (
tablelandHost +
"/api/v1/query?format=objects&extract=true&unwrap=true&statement=" +
encodeURIComponent(
`select json_object(
'name',name,
'description',description,
'image',image,
'external_link',external_link,
'seller_fee_basis_points',seller_fee_basis_points,
'fee_recipient',fee_recipient
) from ${contractTable} limit 1;`.replace(/(\r\n|\n|\r|\s\s+)/gm, "")
)
);
}