-
Notifications
You must be signed in to change notification settings - Fork 15
/
ApproveAllowanceDialog.vue
889 lines (812 loc) · 33.1 KB
/
ApproveAllowanceDialog.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
<!--
-
- Hedera Mirror Node Explorer
-
- Copyright (C) 2021 - 2024 Hedera Hashgraph, LLC
-
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
-
-->
<!-- --------------------------------------------------------------------------------------------------------------- -->
<!-- TEMPLATE -->
<!-- --------------------------------------------------------------------------------------------------------------- -->
<template>
<div :class="{'is-active': showDialog}" class="modal has-text-white">
<div class="modal-background"/>
<div class="modal-content" style="width: 768px; border-radius: 16px">
<div class="box">
<span class="h-is-primary-title">
<span v-if="isEditing">Modify allowance</span>
<span v-else>Approve allowance</span>
<span v-if="ownerAccountId"> for account </span>
<span v-if="ownerAccountId" class="h-is-secondary-text has-text-weight-light mr-3">{{ ownerAccountId }}</span>
</span>
<hr class="h-card-separator"/>
<div class="dialog-grid">
<div class="has-text-weight-light">
Spender
</div>
<div/>
<input :value="selectedSpender"
class="input is-small has-text-right has-text-white"
placeholder="Account or Contract ID (0.0.1234)"
style="height:26px; margin-top: 1px; border-radius: 4px; border-width: 1px;
background-color: var(--h-theme-box-background-color)"
type="text"
@input="event => handleSpenderInput(event)">
<div v-if="spenderFeedback" id="spenderFeedback"
:class="{'has-text-grey': isSpenderValid, 'has-text-danger': !isSpenderValid}"
class="is-inline-block h-is-text-size-2"
style="line-height:26px;">
{{ spenderFeedback }}
</div>
</div>
<div class="dialog-grid mt-4">
<div class="has-text-weight-light">
Allowance
</div>
<div class="control" style="">
<label class="radio h-radio-button">
<input v-model="allowanceChoice" name="allowanceType" type="radio" value="hbar">
HBAR
</label>
</div>
<input :class="{'has-text-grey': allowanceChoice !== 'hbar'}"
:value="selectedHbarAmount"
class="input is-small has-text-right has-text-white"
placeholder="HBAR Amount"
style="height:26px; margin-top: 1px; border-radius: 4px; border-width: 1px;
background-color: var(--h-theme-box-background-color)"
type="text"
@focus="allowanceChoice='hbar'"
@input="event => handleHbarAmountInput(event)">
<div/>
</div>
<div class="dialog-grid mt-2">
<div/>
<div class="control" style="">
<label class="radio h-radio-button">
<input v-model="allowanceChoice" name="allowanceType" type="radio" value="token">
Fungible Token
</label>
</div>
<input :class="{'has-text-grey': allowanceChoice !== 'token'}"
:value="selectedToken"
class="input is-small has-text-right has-text-white"
placeholder="Token ID (0.0.1234)"
style="height:26px; margin-top: 1px; border-radius: 4px; border-width: 1px;
background-color: var(--h-theme-box-background-color)"
type="text"
@focus="allowanceChoice='token'"
@input="event => handleTokenInput(event)">
<input v-if="allowanceChoice === 'token' && isTokenValid"
:class="{'has-text-grey': allowanceChoice !== 'token'}"
:value="selectedTokenAmount"
class="input is-small has-text-right has-text-white"
placeholder="Token Amount"
style="height:26px; margin-top: 1px; border-radius: 4px; border-width: 1px;
background-color: var(--h-theme-box-background-color)"
type="text"
@focus="allowanceChoice='token'"
@input="event => handleTokenAmountInput(event)">
<div v-else-if="allowanceChoice === 'token'" id="tokenFeedback"
:class="{'has-text-grey': isTokenValid, 'has-text-danger': !isTokenValid}"
class="is-inline-block h-is-text-size-2"
style="line-height:26px;">
{{ tokenFeedback }}
</div>
</div>
<div class="dialog-grid mt-2">
<div/>
<div class="control" style="">
<label class="radio h-radio-button">
<input v-model="allowanceChoice" name="allowanceType" type="radio" value="nft">
NFT
</label>
</div>
<input :class="{'has-text-grey': allowanceChoice !== 'nft'}"
:value="selectedNft"
class="input is-small has-text-right has-text-white"
placeholder="Token ID (0.0.1234)"
style="height:26px; margin-top: 1px; border-radius: 4px; border-width: 1px;
background-color: var(--h-theme-box-background-color)"
type="text"
@focus="allowanceChoice='nft'"
@input="event => handleNftInput(event)">
<input v-if="allowanceChoice === 'nft' && isNftValid"
:class="{'has-text-grey': allowanceChoice !== 'nft'}"
:value="selectedNftSerials"
class="input is-small has-text-right has-text-white"
placeholder="serial numbers (1, 2, 3…)"
style="height:26px; margin-top: 1px; border-radius: 4px; border-width: 1px;
background-color: var(--h-theme-box-background-color)"
type="text"
@focus="allowanceChoice='nft'"
@input="event => handleNftSerialsInput(event)">
<div v-else-if="allowanceChoice === 'nft'" id="nftFeedback"
:class="{'has-text-grey': isNftValid, 'has-text-danger': !isNftValid}"
class="is-inline-block h-is-text-size-2"
style="line-height:26px;">
{{ nftFeedback }}
</div>
</div>
<div class="dialog-grid mt-2">
<div/>
<div/>
<div/>
<div id="nftSerialsFeedback"
:class="{
'has-text-grey': isNftSerialsValid,
'has-text-danger': !isNftSerialsValid,
'is-invisible': allowanceChoice !== 'nft' || !isNftValid}"
class="is-inline-block h-is-text-size-2 has-text-right">
{{ nftSerialsFeedback }}
</div>
</div>
<div v-if="isEditing" id="editingFeedback"
class="has-text-grey h-is-text-size-3 has-text-centered mt-1"
:class="{'is-invisible': !enableApproveButton}">
{{ editingFeedback }}
</div>
<div class="is-flex is-justify-content-flex-end mt-5">
<button class="button is-white is-small" @click="handleCancel">CANCEL</button>
<button :disabled="!enableApproveButton"
class="button is-info is-small ml-4" @click="handleApprove">APPROVE
</button>
</div>
</div>
</div>
</div>
<ConfirmDialog v-model:show-dialog="showConfirmDialog" :main-message="confirmMessage"
@onConfirm="handleConfirmApprove">
<template v-slot:dialogTitle>
<span v-if="isEditing" class="h-is-primary-title">Modify allowance </span>
<span v-else class="h-is-primary-title">Approve allowance </span>
<span v-if="ownerAccountId"> for account </span>
<span v-if="ownerAccountId" class="h-is-secondary-text has-text-weight-light mr-3"
style="line-height: 36px">{{ ownerAccountId }}</span>
</template>
</ConfirmDialog>
<ProgressDialog v-model:show-dialog="showProgressDialog"
:extra-message="progressExtraMessage"
:extra-transaction-id="progressExtraTransactionId"
:main-message="progressMainMessage"
:mode="progressDialogMode"
:show-spinner="showProgressSpinner"
>
<template v-slot:dialogTitle>
<span class="h-is-primary-title">{{ progressDialogTitle }}</span>
</template>
</ProgressDialog>
</template>
<!-- --------------------------------------------------------------------------------------------------------------- -->
<!-- SCRIPT -->
<!-- --------------------------------------------------------------------------------------------------------------- -->
<script lang="ts">
import {computed, defineComponent, PropType, Ref, ref, watch} from "vue";
import router, {walletManager} from "@/router";
import {EntityID} from "@/utils/EntityID";
import {networkRegistry} from "@/schemas/NetworkRegistry";
import ConfirmDialog from "@/components/ConfirmDialog.vue";
import axios from "axios";
import {
CryptoAllowance,
Nfts,
TokenAllowance,
TokenInfo,
TokenRelationshipResponse,
Transaction,
TransactionByIdResponse
} from "@/schemas/HederaSchemas";
import ProgressDialog, {Mode} from "@/components/staking/ProgressDialog.vue";
import {normalizeTransactionId} from "@/utils/TransactionID";
import {waitFor} from "@/utils/TimerUtils";
import {WalletDriverCancelError, WalletDriverError} from "@/utils/wallet/WalletDriverError";
import {TokenInfoCache} from "@/utils/cache/TokenInfoCache";
import {AccountByIdCache} from "@/utils/cache/AccountByIdCache";
import {ContractByIdCache} from "@/utils/cache/ContractByIdCache";
const VALID_ACCOUNT_MESSAGE = "Account found"
const VALID_CONTRACT_MESSAGE = "Contract found"
const UNKNOWN_ACCOUNT_MESSAGE = "Unknown account"
const INVALID_ACCOUNTID_MESSAGE = "Invalid account ID"
const INVALID_CHECKSUM_MESSAGE = "Invalid checksum"
const SAME_AS_OWNER_ACCOUNT_MESSAGE = "Same as owner's account"
const INVALID_TOKENID_MESSAGE = "Invalid token ID"
const TOKEN_NOT_FUNGIBLE_MESSAGE = "Token is not fungible"
const TOKEN_NOT_NFT_MESSAGE = "Token is fungible"
const TOKEN_NOT_FOUND_MESSAGE = "Not associated with this account"
const NFT_SERIAL_PROMPT_MESSAGE = "Leave empty to approve for ALL"
const SERIAL_NOT_FOUND_MESSAGE = "Not associated with this account"
export default defineComponent({
name: "ApproveAllowanceDialog",
components: {ProgressDialog, ConfirmDialog},
props: {
ownerAccountId: {
type: String as PropType<string|null>,
default: null
},
showDialog: {
type: Boolean,
default: false
},
currentHbarAllowance: Object as PropType<CryptoAllowance|null>,
currentTokenAllowance: Object as PropType<TokenAllowance|null>,
polling: { // For testing purpose
type: Number,
default: 3000
}
},
emits: ["update:showDialog", "allowanceApproved"],
setup(props, context) {
const nr = networkRegistry
const network = router.currentRoute.value.params.network as string
const selectedSpender = ref<string | null>(null)
const normalizedSpender = computed(() => EntityID.normalize(nr.stripChecksum(selectedSpender.value ?? "")))
const selectedHbarAmount = ref<string | null>(null)
const selectedToken = ref<string | null>(null)
const normalizedToken = computed(() => EntityID.normalize(nr.stripChecksum(selectedToken.value ?? "")))
const selectedTokenAmount = ref<string | null>(null)
const selectedNft = ref<string | null>(null)
const normalizedNFT = computed(() => EntityID.normalize(nr.stripChecksum(selectedNft.value ?? "")))
const selectedNftSerials = ref<string | null>(null)
const nftSerials = ref<number[]>([])
const allowanceChoice = ref("hbar")
const isSpenderValid = ref(false)
const spenderFeedback = ref<string | null>(null)
let spenderValidationTimerId = -1
const isHbarAmountValid = computed(() =>
selectedHbarAmount.value !== null
&& parseFloat(selectedHbarAmount.value) >= 0
)
const isTokenValid = ref(false)
const tokenFeedback = ref<string | null>(null)
let tokenValidationTimerId = -1
const isTokenAmountValid = computed(() =>
selectedTokenAmount.value !== null &&
parseFloat(selectedTokenAmount.value) >= 0)
const isNftValid = ref(false)
const nftFeedback = ref<string | null>(null)
let nftValidationTimerId = -1
const isNftSerialsValid = ref(true)
const nftSerialsFeedback = ref<string | null>(NFT_SERIAL_PROMPT_MESSAGE)
let nftSerialsValidationTimer = -1
const isEditing = ref(false)
const edited = computed(() => {
let result = false
if (props.currentHbarAllowance) {
result ||= allowanceChoice.value !== "hbar"
result ||= selectedSpender.value !== props.currentHbarAllowance?.spender
result ||= selectedHbarAmount.value !==
(props.currentHbarAllowance ? (props.currentHbarAllowance.amount_granted / 100000000).toString() : null)
} else if (props.currentTokenAllowance) {
result ||= allowanceChoice.value !== "token"
result ||= selectedSpender.value !== props.currentTokenAllowance?.spender
result ||= selectedToken.value !== props.currentTokenAllowance.token_id
result ||= selectedTokenAmount.value !== (props.currentTokenAllowance?.amount_granted.toString() ?? null)
} else {
result = true
}
return result
})
const editingFeedback = computed(() => {
let result: string | null
if (props.currentHbarAllowance) {
result = "Previous allowance was to "
+ props.currentHbarAllowance.spender + " for "
+ props.currentHbarAllowance.amount_granted/100000000 + " hbars"
} else if (props.currentTokenAllowance) {
result = "Previous allowance was to "
+ props.currentTokenAllowance.spender + " for "
+ props.currentTokenAllowance.amount_granted
+ " tokens (" + props.currentTokenAllowance.token_id + ")"
} else {
result = null
}
return result
})
const enableApproveButton = computed(() => {
return isSpenderValid.value && (
(allowanceChoice.value === 'hbar' && isHbarAmountValid.value)
|| (allowanceChoice.value === 'token' && isTokenValid.value && isTokenAmountValid.value)
|| (allowanceChoice.value === 'nft' && isNftValid.value && isNftSerialsValid.value)
) && edited.value
})
watch(() => props.showDialog, (newValue) => {
if (newValue) {
if (props.currentHbarAllowance) {
isEditing.value = true
allowanceChoice.value = "hbar"
selectedSpender.value = props.currentHbarAllowance?.spender ?? null
selectedHbarAmount.value =
(props.currentHbarAllowance) ? (props.currentHbarAllowance.amount_granted / 100000000).toString() : null
selectedToken.value = null
selectedTokenAmount.value = null
selectedNft.value = null
selectedNftSerials.value = null
} else if (props.currentTokenAllowance) {
isEditing.value = true
allowanceChoice.value = "token"
selectedSpender.value = props.currentTokenAllowance?.spender ?? null
selectedHbarAmount.value = null
selectedToken.value = props.currentTokenAllowance.token_id
selectedTokenAmount.value = props.currentTokenAllowance?.amount_granted.toString() ?? null
selectedNft.value = null
selectedNftSerials.value = null
} else {
isEditing.value = false
allowanceChoice.value = "hbar"
selectedSpender.value = null
selectedHbarAmount.value = null
selectedToken.value = null
selectedTokenAmount.value = null
selectedNft.value = null
selectedNftSerials.value = null
}
}
})
watch(selectedSpender, (newValue) => {
isSpenderValid.value = false
spenderFeedback.value = null
if (spenderValidationTimerId != -1) {
window.clearTimeout(spenderValidationTimerId)
spenderValidationTimerId = -1
}
if (newValue?.length) {
spenderValidationTimerId = window.setTimeout(() => validateSpender(), 500)
} else {
selectedSpender.value = null
}
})
const validateSpender =
() => validateAccount(selectedSpender.value, isSpenderValid, spenderFeedback)
watch(selectedToken, () => {
isTokenValid.value = false
tokenFeedback.value = null
if (tokenValidationTimerId != -1) {
window.clearTimeout(tokenValidationTimerId)
tokenValidationTimerId = -1
}
if (selectedToken.value?.length) {
tokenValidationTimerId = window.setTimeout(() => validateToken(), 500)
} else {
selectedToken.value = null
}
})
const validateToken = () => validateTokenAssociation(
walletManager.accountId.value,
selectedToken.value,
'FUNGIBLE_COMMON',
isTokenValid,
tokenFeedback)
watch(selectedNft, () => {
isNftValid.value = false
nftFeedback.value = null
if (nftValidationTimerId != -1) {
window.clearTimeout(nftValidationTimerId)
nftValidationTimerId = -1
}
if (selectedNft.value?.length) {
nftValidationTimerId = window.setTimeout(() => validateNft(), 500)
} else {
selectedNft.value = null
}
})
const validateNft = () => validateTokenAssociation(
walletManager.accountId.value,
selectedNft.value,
'NON_FUNGIBLE_UNIQUE',
isNftValid,
nftFeedback)
watch([selectedNftSerials, selectedNft], () => {
isNftSerialsValid.value = true
nftSerialsFeedback.value = NFT_SERIAL_PROMPT_MESSAGE
if (nftSerialsValidationTimer != -1) {
window.clearTimeout(nftSerialsValidationTimer)
nftSerialsValidationTimer = -1
}
if (selectedNftSerials.value?.length) {
nftSerialsValidationTimer = window.setTimeout(() => validateNftSerials(), 500)
} else {
selectedNftSerials.value = null
}
})
const validateNftSerials = () => validateSerialsAssociation(
walletManager.accountId.value,
normalizedNFT.value,
selectedNftSerials.value,
isNftSerialsValid,
nftSerials,
nftSerialsFeedback)
const showConfirmDialog = ref(false)
const confirmMessage = computed(() => {
let result: string
const toAccount = normalizedSpender.value
if (allowanceChoice.value === 'hbar') {
result = "Do you want to approve an allowance to account " + toAccount
+ " for " + (selectedHbarAmount.value??0/100000000) + " hbars?"
} else if (allowanceChoice.value === 'token') {
const token = normalizedToken.value
result = "Do you want to approve an allowance to account " + toAccount
+ " for " + selectedTokenAmount.value + " fungible tokens (" + token + ")?"
} else { // 'nft'
const nFT = normalizedNFT.value
result = "Do you want to approve an allowance to account " + toAccount
+ " for NFT " + nFT
if (nftSerials.value.length) {
result += " ("
for (let i = 0; i < nftSerials.value.length; i++) {
if (i > 0) {
result += ', '
}
result += '#' + nftSerials.value[i]
}
result += ')'
}
result += '?'
}
return result
})
const showProgressDialog = ref(false)
const progressDialogMode = ref(Mode.Busy)
const progressDialogTitle = ref("Approving allowance")
const progressMainMessage = ref<string | null>(null)
const progressExtraMessage = ref<string | null>(null)
const progressExtraTransactionId = ref<string | null>(null)
const showProgressSpinner = ref(false)
const handleSpenderInput = (event: Event) => handleEntityIDInput(selectedSpender, event)
const handleHbarAmountInput = (event: Event) => handleAmountInput(selectedHbarAmount, event)
const handleTokenInput = (event: Event) => handleEntityIDInput(selectedToken, event)
const handleTokenAmountInput = (event: Event) => handleAmountInput(selectedTokenAmount, event)
const handleNftInput = (event: Event) => handleEntityIDInput(selectedNft, event)
const handleNftSerialsInput = (event: Event) => handleIntListInput(selectedNftSerials, event)
const handleCancel = () => {
context.emit('update:showDialog', false)
}
const handleApprove = () => {
context.emit('update:showDialog', false)
showConfirmDialog.value = true
}
const handleConfirmApprove = async () => {
try {
showProgressDialog.value = true
progressDialogMode.value = Mode.Busy
progressDialogTitle.value = isEditing.value ? "Modifying allowance" : "Approving allowance"
progressMainMessage.value = "Connecting to Hedera Network using your wallet…"
progressExtraMessage.value = "Check your wallet for any approval request"
progressExtraTransactionId.value = null
showProgressSpinner.value = true
if (normalizedSpender.value) {
let tid = null
if (allowanceChoice.value === 'hbar') {
if (selectedHbarAmount.value) {
tid = normalizeTransactionId(await walletManager.approveHbarAllowance(
normalizedSpender.value, parseFloat(selectedHbarAmount.value)))
}
} else if (allowanceChoice.value === 'token') {
if (normalizedToken.value && selectedTokenAmount.value) {
tid = normalizeTransactionId(await walletManager.approveTokenAllowance(
normalizedToken.value, normalizedSpender.value, parseFloat(selectedTokenAmount.value)))
}
} else { // 'nft'
if (normalizedNFT.value) {
tid = normalizeTransactionId(await walletManager.approveNFTAllowance(
normalizedNFT.value, normalizedSpender.value, nftSerials.value))
}
}
console.log("Transaction ID=" + tid)
if (tid) {
progressMainMessage.value = "Completing operation…"
progressExtraMessage.value = "This may take a few seconds"
showProgressSpinner.value = true
await waitForTransactionRefresh(tid, 10)
}
progressDialogMode.value = Mode.Success
progressMainMessage.value = "Operation completed"
showProgressSpinner.value = false
progressExtraMessage.value = "with transaction ID:"
progressExtraTransactionId.value = tid
context.emit('allowanceApproved')
}
} catch (reason) {
console.log("Transaction Error: " + reason)
if (reason instanceof WalletDriverCancelError) {
showProgressDialog.value = false
} else {
progressDialogMode.value = Mode.Error
if (reason instanceof WalletDriverError) {
progressMainMessage.value = reason.message
progressExtraMessage.value = reason.extra
} else {
progressMainMessage.value = "Operation did not complete"
progressExtraMessage.value = reason instanceof Error ? JSON.stringify(reason.message) : JSON.stringify(reason)
}
progressExtraTransactionId.value = null
showProgressSpinner.value = false
}
}
}
const handleEntityIDInput = (entityID: Ref<string | null>, event: Event) => {
const previousValue = entityID.value
let isValidInput = true
let isValidID = false
let isPastDash = false
const value = (event.target as HTMLInputElement).value
for (const c of value) {
if ((c >= '0' && c <= '9') || c === '.') {
if (isPastDash) {
isValidInput = false
break
} else {
isValidID = EntityID.parse(nr.stripChecksum(value)) !== null
}
} else if (c === '-') {
if (!isValidID || isPastDash) {
isValidInput = false
break
} else {
isPastDash = true
}
} else if (c < 'a' || c > 'z' || !isPastDash) {
isValidInput = false
break
}
}
if (isValidInput) {
entityID.value = value
} else {
entityID.value = ""
entityID.value = previousValue
}
}
const handleAmountInput = (amount: Ref<string | null>, event: Event) => {
const previousValue = amount.value
let isValidInput = true
let isDecimal = false
const value = (event.target as HTMLInputElement).value
for (const c of value) {
if ((c >= '0' && c <= '9') || c === '.') {
if (c === '.') {
isValidInput = !isDecimal
isDecimal = true
}
} else {
isValidInput = false
break
}
}
if (isValidInput) {
amount.value = value
} else {
amount.value = ""
amount.value = previousValue
}
}
const handleIntListInput = (list: Ref<string | null>, event: Event) => {
const previousValue = list.value
let isValidInput = true
let previousWasComma = false
const value = (event.target as HTMLInputElement).value
for (const c of value) {
if ((c >= '0' && c <= '9') || c === ',') {
if (c === ',') {
isValidInput = !previousWasComma
previousWasComma = true
} else {
previousWasComma = false
}
} else {
isValidInput = false
break
}
}
if (isValidInput) {
list.value = value
} else {
list.value = ""
list.value = previousValue
}
}
const validateAccount = (accountId: string | null, isValid: Ref<boolean>, message: Ref<string | null>) => {
const checksum = nr.extractChecksum(accountId ?? "")
const entity = EntityID.normalize(nr.stripChecksum(accountId ?? ""))
const isValidChecksum = checksum ? nr.isValidChecksum(entity ?? "", checksum, network) : true
if (entity === null) {
message.value = INVALID_ACCOUNTID_MESSAGE
} else if (isValidChecksum) {
if (entity === walletManager.accountId.value) {
message.value = SAME_AS_OWNER_ACCOUNT_MESSAGE
} else {
AccountByIdCache.instance.lookup(entity)
.then((r) => {
if (r) {
isValid.value = true
ContractByIdCache.instance.lookup(entity)
.then((r) => {
if (r) {
message.value = VALID_CONTRACT_MESSAGE
} else {
message.value = VALID_ACCOUNT_MESSAGE
}
})
} else {
message.value = UNKNOWN_ACCOUNT_MESSAGE
}
})
.catch(() => message.value = UNKNOWN_ACCOUNT_MESSAGE)
}
} else {
message.value = INVALID_CHECKSUM_MESSAGE
}
}
const validateTokenAssociation = (
accountId: string | null,
tokenId: string | null,
type: string | null,
isValid: Ref<boolean>,
message: Ref<string | null>) => {
const checksum = nr.extractChecksum(tokenId ?? "")
const entity = EntityID.normalize(nr.stripChecksum(tokenId ?? ""))
const isValidChecksum = checksum ? nr.isValidChecksum(entity ?? "", checksum, network) : true
if (entity === null) {
message.value = INVALID_TOKENID_MESSAGE
} else if (isValidChecksum) {
if (accountId && tokenId) {
const uRL = "api/v1/accounts/" + accountId + "/tokens"
const params = {
'token.id': entity,
}
axios
.get<TokenRelationshipResponse>(uRL, {params: params})
.then((response) => {
const tokens = response.data.tokens
if (tokens && tokens.length > 0) {
if (type !== null) {
TokenInfoCache.instance.lookup(entity)
.then((t: TokenInfo | null) => {
if (t?.type === type) {
isValid.value = true
} else {
message.value = (type === 'FUNGIBLE_COMMON') ? TOKEN_NOT_FUNGIBLE_MESSAGE : TOKEN_NOT_NFT_MESSAGE
}
})
.catch(() => message.value = TOKEN_NOT_FOUND_MESSAGE)
} else {
isValid.value = true
}
} else {
message.value = TOKEN_NOT_FOUND_MESSAGE
}
})
.catch(() => message.value = TOKEN_NOT_FOUND_MESSAGE)
}
} else {
message.value = INVALID_CHECKSUM_MESSAGE
}
}
const validateSerialsAssociation = (
accountId: string | null,
tokenId: string | null,
inputSerials: string | null,
isValid: Ref<boolean>,
resultSerials: Ref<number[]>,
message: Ref<string | null>) => {
const inputSerialsSplit = inputSerials ? inputSerials.split(',') : []
if (accountId && tokenId && inputSerialsSplit.length) {
const uRL = "api/v1/accounts/" + accountId + "/nfts"
const params = {
'token.id': tokenId,
}
axios
.get<Nfts>(uRL, {params: params})
.then((response) => {
const nfts = response.data.nfts
if (nfts && nfts.length > 0) {
const serialsInAccount = Array<number>()
for (const nft of nfts) {
if (nft.serial_number) {
serialsInAccount.push(nft.serial_number)
}
}
for (const s of inputSerialsSplit) {
if (s.length) {
const i = parseInt(s)
if (serialsInAccount.includes(i)) {
if (!resultSerials.value.includes(i)) {
resultSerials.value.push(i)
}
} else {
isValid.value = false
break
}
}
}
} else {
isValid.value = false
}
if (!isValid.value) {
message.value = SERIAL_NOT_FOUND_MESSAGE
}
})
.catch(() => {
isValid.value = false
message.value = SERIAL_NOT_FOUND_MESSAGE
})
}
}
const waitForTransactionRefresh = async (transactionId: string, attemptIndex: number) => {
let result: Promise<Transaction | string>
if (attemptIndex >= 0) {
await waitFor(props.polling)
try {
const response = await axios.get<TransactionByIdResponse>("api/v1/transactions/" + transactionId)
const transactions = response.data.transactions ?? []
result = Promise.resolve(transactions.length >= 1 ? transactions[0] : transactionId)
} catch {
result = waitForTransactionRefresh(transactionId, attemptIndex - 1)
}
} else {
result = Promise.resolve(transactionId)
}
return result
}
return {
editingFeedback,
enableApproveButton,
selectedSpender,
selectedHbarAmount,
selectedToken,
selectedTokenAmount,
selectedNft,
selectedNftSerials,
allowanceChoice,
isSpenderValid,
spenderFeedback,
isTokenValid,
tokenFeedback,
isNftValid,
nftFeedback,
isNftSerialsValid,
nftSerialsFeedback,
isEditing,
showConfirmDialog,
confirmMessage,
showProgressDialog,
progressDialogMode,
progressDialogTitle,
progressMainMessage,
progressExtraMessage,
progressExtraTransactionId,
showProgressSpinner,
handleSpenderInput,
handleHbarAmountInput,
handleTokenInput,
handleTokenAmountInput,
handleNftInput,
handleNftSerialsInput,
handleCancel,
handleApprove,
handleConfirmApprove,
}
}
});
</script>
<!-- --------------------------------------------------------------------------------------------------------------- -->
<!-- STYLE -->
<!-- --------------------------------------------------------------------------------------------------------------- -->
<style scoped>
.dialog-grid {
display: grid;
grid-template-columns: 2fr 3fr 5fr 4fr;
grid-column-gap: 1rem;
}
</style>