-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy paththirdweb.ts
More file actions
179 lines (153 loc) · 4.08 KB
/
thirdweb.ts
File metadata and controls
179 lines (153 loc) · 4.08 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { env } from "./env";
import { SCORE_CONTRACT_ADDRESS } from "./constants";
import {
ContractReadCall,
ContractWriteCall,
} from "./types";
const THIRDWEB_API_URL = env.THIRDWEB_API_BASE_URL;
async function makeThirdwebRequest(
endpoint: string,
options: RequestInit = {}
): Promise<any> {
const response = await fetch(`${THIRDWEB_API_URL}${endpoint}`, {
...options,
headers: {
"Content-Type": "application/json",
"x-secret-key": env.THIRDWEB_SECRET_KEY,
...options.headers,
},
});
if (!response.ok) {
const error = await response.text();
throw new Error(
`Thirdweb API error: ${response.status} ${response.statusText} ${error}`
);
}
const data = await response.json();
return data.result;
}
export async function createWallet(identifier: string) {
const response = await makeThirdwebRequest("/v1/wallets", {
method: "POST",
body: JSON.stringify({ identifier }),
});
console.log("response", response);
return response;
}
export async function getUserDetails(authToken: string) {
const response = await makeThirdwebRequest("/v1/wallets/me", {
method: "GET",
headers: {
"Authorization": `Bearer ${authToken}`,
},
});
return response;
}
export async function readContract(calls: ContractReadCall[], chainId: number) {
const response = await makeThirdwebRequest("/v1/contracts/read", {
method: "POST",
body: JSON.stringify({
calls,
chainId,
}),
});
return response;
}
export async function writeContract(
calls: ContractWriteCall[],
chainId: number,
from: string,
authToken?: string
) {
const response = await makeThirdwebRequest("/v1/contracts/write", {
method: "POST",
body: JSON.stringify({
calls,
chainId,
from,
}),
headers: authToken ? {
"Authorization": `Bearer ${authToken}`,
} : {},
});
return response;
}
export async function getTransaction(transactionId: string) {
const response = await makeThirdwebRequest(
`/v1/transactions/${transactionId}`,
{
method: "GET",
}
);
return response;
}
export async function getTokenOwners(
chainId: number,
tokenAddress: string,
limit: number = 3
) {
const response = await makeThirdwebRequest(
`/v1/tokens/${chainId}/${tokenAddress}/owners?limit=${limit}`,
{
method: "GET",
}
);
return response;
}
export async function listTransactions(page: number = 1, limit: number = 10) {
const response = await makeThirdwebRequest(
`/v1/transactions?page=${page}&limit=${limit}`,
{
method: "GET",
}
);
return response;
}
export async function transferTokens(
from: string,
to: string,
amount: string,
tokenAddress: string,
chainId: number,
authToken?: string
) {
const calls: ContractWriteCall[] = [];
// Always include the primary token transfer
calls.push({
contractAddress: tokenAddress,
method: "function transfer(address to, uint256 amount)",
params: [to, amount],
});
// If sending from treasury (reward), also mint score tokens to the recipient
if (from && env.TREASURY_WALLET_ADDRESS && from.toLowerCase() === env.TREASURY_WALLET_ADDRESS.toLowerCase()) {
calls.push({
contractAddress: SCORE_CONTRACT_ADDRESS,
method: "function mintTo(address to, uint256 amount)",
params: [to, amount],
});
}
// If sending to treasury (penalty), also burn score tokens from the sender
if (to && env.TREASURY_WALLET_ADDRESS && to.toLowerCase() === env.TREASURY_WALLET_ADDRESS.toLowerCase()) {
calls.push({
contractAddress: SCORE_CONTRACT_ADDRESS,
method: "function burn(uint256 amount)",
params: [amount],
});
}
return writeContract(calls, chainId, from, authToken);
}
export async function getTokenBalance(
walletAddress: string,
tokenAddress: string,
chainId: number
) {
const calls: ContractReadCall[] = [
{
contractAddress: tokenAddress,
method: "function balanceOf(address owner) view returns (uint256)",
params: [walletAddress],
},
];
const result = await readContract(calls, chainId);
return result[0];
}