/
OptIn.vue
360 lines (346 loc) · 10.9 KB
/
OptIn.vue
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<script setup lang="ts">
import { onMounted, reactive } from "vue";
import MainLayout from "../../layouts/Main.vue";
import algosdk from "algosdk";
import Contract from "arc200js";
import { useStore } from "vuex";
import { useRoute, useRouter } from "vue-router";
import { getArc200Client } from "arc200-client";
const store = useStore();
const route = useRoute();
const router = useRouter();
const state = reactive({
account: {
name: "",
addr: route.params.account as string,
},
arc200id: "",
arc200Info: {
arc200id: 0,
name: "",
symbol: "",
decimals: BigInt(0),
totalSupply: BigInt(0),
balance: BigInt(0),
},
boxNotFound: false,
loading: false,
});
onMounted(async () => {
await store.dispatch("wallet/prolong");
});
const fetchAsset = async () => {
try {
const algodClient: algosdk.Algodv2 = await store.dispatch("algod/getAlgod");
const indexerClient = await store.dispatch("indexer/getIndexer");
state.arc200Info.arc200id = parseInt(state.arc200id);
const contract = new Contract(
state.arc200Info.arc200id,
algodClient,
indexerClient
);
state.arc200Info.name = "";
state.loading = true;
var name = await contract.arc200_name();
if (!name.success) {
store.dispatch("toast/openError", "Failed to fetch ARC200 name");
state.loading = false;
return;
}
state.arc200Info.name = name.returnValue;
var symbol = await contract.arc200_symbol();
if (!symbol.success) {
store.dispatch("toast/openError", "Failed to fetch ARC200 symbol");
state.loading = false;
return;
}
state.arc200Info.symbol = symbol.returnValue;
var decimals = await contract.arc200_decimals();
if (!decimals.success) {
store.dispatch("toast/openError", "Failed to fetch ARC200 decimals");
state.loading = false;
return;
}
state.arc200Info.decimals = decimals.returnValue;
var totalSupply = await contract.arc200_totalSupply();
if (!totalSupply.success) {
store.dispatch("toast/openError", "Failed to fetch ARC200 totalSupply");
state.loading = false;
return;
}
state.arc200Info.totalSupply = totalSupply.returnValue;
var balance = await contract.arc200_balanceOf(state.account.addr);
console.log("arc200_balanceOf", balance);
if (!balance.success) {
console.error("balance request was not successful");
store.dispatch("toast/openError", "Failed to fetch ARC200 balance");
state.loading = false;
return;
}
state.arc200Info.balance = balance.returnValue;
state.boxNotFound = !(await accountIsOptedInToArc200Asset(
state.account.addr
));
state.loading = false;
} catch (err: any) {
const error = err.message ?? err;
console.error("failed to fetch arc200", error, err);
await store.dispatch("toast/openError", error);
}
};
const accountIsOptedInToArc200Asset = async (addr: string) => {
const indexerClient = await store.dispatch("indexer/getIndexer");
const fromDecoded = algosdk.decodeAddress(addr);
const boxName = new Uint8Array(
Buffer.concat([Buffer.from([0x00]), Buffer.from(fromDecoded.publicKey)])
);
try {
const box = await indexerClient
.lookupApplicationBoxByIDandName(state.arc200Info.arc200id, boxName)
.do();
return true;
} catch (exc: any) {
if (exc.message?.indexOf("no application boxes found")) {
return false;
} else {
console.error(exc);
throw exc;
}
}
};
const save = async () => {
try {
await store.dispatch("wallet/addArc200Asset", {
addr: state.account.addr,
arc200Info: state.arc200Info,
});
router.push({ name: "Accounts" });
} catch (err: any) {
const error = err.message ?? err;
console.error("failed to addArc200Asset", error, err);
await store.dispatch("toast/openError", error);
}
};
const makeOptInTxs = async () => {
const algod = await store.dispatch("algod/getAlgod");
const appId = Number(state.arc200Info.arc200id);
const client = getArc200Client({
algod,
appId: appId,
sender: { addr: state.account.addr },
});
const fromDecoded = algosdk.decodeAddress(state.account.addr);
var boxFrom = {
// : algosdk.BoxReference
appIndex: appId,
name: new Uint8Array(
Buffer.concat([Buffer.from([0x00]), Buffer.from(fromDecoded.publicKey)])
), // data box
};
const compose = client.compose().arc200Transfer(
{ to: state.account.addr, value: BigInt(0) },
{
boxes: [boxFrom],
}
);
const enc = new TextEncoder();
let noteEnc = enc.encode("o");
const payTx = await store.dispatch("algod/preparePayment", {
payTo: algosdk.getApplicationAddress(appId),
payFrom: state.account.addr,
amount: 28500,
noteEnc: noteEnc,
fee: undefined,
asset: undefined,
reKeyTo: undefined,
});
const atc = await compose.atc();
const txsToSignArc200 = atc.buildGroup().map((tx) => tx.txn);
const txsToSign = [];
txsToSign.push(payTx);
txsToSignArc200.forEach((tx) => {
txsToSign.push(tx);
});
algosdk.assignGroupID(txsToSign);
console.log("txsToSign", txsToSign);
return txsToSign;
};
const createBoxClick = async () => {
try {
await store.dispatch("signer/toSignArray", {
txs: await makeOptInTxs(),
});
await router.push("/signAll");
} catch (err: any) {
const error = err.message ?? err;
console.error("failed to addArc200Asset", error, err);
await store.dispatch("toast/openError", error);
}
};
</script>
<template>
<MainLayout v-if="state">
<h1>Add ARC 200 asset to account {{ state?.account?.name }}</h1>
<Card>
<template #content>
<p>{{ $t("acc_overview.asset_optinArc200_help") }}</p>
<div class="field grid">
<label for="arc200id" class="col-12 mb-2 md:col-2 md:mb-0">
ARC200 App ID
</label>
<div class="col-12 md:col-10">
<InputText id="arc200id" v-model="state.arc200id" class="w-full" />
</div>
</div>
<div class="field grid">
<label class="col-12 mb-2 md:col-2 md:mb-0"></label>
<div class="col-12 md:col-10">
<Button
class="my-2 mr-2"
@click="fetchAsset"
:severity="state.arc200Info.name ? 'secondary' : 'primary'"
:disabled="state.loading || !state.arc200id"
>
Fetch asset information
<ProgressSpinner
class="ml-2"
v-if="state.loading"
style="width: 1em; height: 1em"
strokeWidth="10"
/>
</Button>
<Button
class="my-2"
:disabled="!state.arc200id"
@click="
state.arc200id = '';
state.arc200Info.name = '';
state.loading = false;
"
severity="secondary"
>
Reset
</Button>
</div>
</div>
<div
class="field grid my-5"
v-if="
!state.arc200Info.name &&
!state.loading &&
store.state.config.env == 'voitest-v1'
"
>
<label class="col-12 mb-2 md:col-2 md:mb-0">Example assets</label>
<div class="col-12 md:col-10">
<Button
class="mr-1"
@click="state.arc200id = '6779767'"
severity="secondary"
>
Via
</Button>
<Button
class="mr-1"
@click="state.arc200id = '6795477'"
severity="secondary"
>
TacoCoin
</Button>
<Button
class="mr-1"
@click="state.arc200id = '23214349'"
severity="secondary"
>
NFT Rewards
</Button>
<Button
class="mr-1"
@click="state.arc200id = '24590664'"
severity="secondary"
>
Wrapped Voi
</Button>
</div>
</div>
<div
class="field grid mt-5"
v-if="state.arc200Info.name && !state.loading"
>
<label class="col-12 mb-2 md:col-2 md:mb-0">Name</label>
<div class="col-12 md:col-10">
{{ state.arc200Info.name }}
</div>
</div>
<div class="field grid" v-if="state.arc200Info.name && !state.loading">
<label class="col-12 mb-2 md:col-2 md:mb-0">Symbol</label>
<div class="col-12 md:col-10">
{{ state.arc200Info.symbol }}
</div>
</div>
<div class="field grid" v-if="state.arc200Info.name && !state.loading">
<label class="col-12 mb-2 md:col-2 md:mb-0">Decimals</label>
<div class="col-12 md:col-10">
{{ state.arc200Info.decimals }}
</div>
</div>
<div class="field grid" v-if="state.arc200Info.name && !state.loading">
<label class="col-12 mb-2 md:col-2 md:mb-0">Total supply</label>
<div class="col-12 md:col-10">
{{
$filters.formatCurrencyBigInt(
state.arc200Info.totalSupply,
state.arc200Info.symbol,
state.arc200Info.decimals
)
}}
</div>
</div>
<div class="field grid" v-if="state.arc200Info.name && !state.loading">
<label class="col-12 mb-2 md:col-2 md:mb-0">Account balance</label>
<div class="col-12 md:col-10">
{{
$filters.formatCurrencyBigInt(
state.arc200Info.balance,
state.arc200Info.symbol,
state.arc200Info.decimals
)
}}
</div>
</div>
<div class="field grid" v-if="state.arc200Info.name && !state.loading">
<label class="col-12 mb-2 md:col-2 md:mb-0"
>Account can receive asset</label
>
<div class="col-12 md:col-10">
{{ !state.boxNotFound }}
{{
state.boxNotFound ? "- Box does not exists yet" : "- Box exists"
}}
</div>
</div>
<div class="field grid" v-if="state.arc200Info.name && !state.loading">
<label class="col-12 mb-2 md:col-2 md:mb-0"></label>
<div class="col-12 md:col-10">
<Button
class="my-2 mr-2"
@click="createBoxClick"
v-if="state.boxNotFound"
:severity="primary"
>
Create box for this asset for this account
</Button>
<Button
class="my-2"
@click="save"
:disabled="state.boxNotFound"
:severity="state.boxNotFound ? 'secondary' : 'primary'"
>
Save asset to your account
</Button>
</div>
</div>
</template>
</Card>
</MainLayout>
</template>