-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathStakeCAP.svelte
More file actions
101 lines (75 loc) · 2.2 KB
/
Copy pathStakeCAP.svelte
File metadata and controls
101 lines (75 loc) · 2.2 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
<script>
import Modal from './Modal.svelte'
import Input from '@components/layout/Input.svelte'
import Button from '@components/layout/Button.svelte'
import { onMount } from 'svelte'
import { depositCAP, getCAPWalletBalance } from '@api/cap'
import { approveAsset, getAllowance } from '@api/assets'
import { formatCAPForDisplay } from "@lib/formatters"
import { allowances } from '@lib/stores'
import { focusInput, hideModal } from '@lib/ui'
import LabelValue from '../layout/LabelValue.svelte'
const CAP_STAKING_SPENDER = 'Staking';
let amount, isSubmitting, isApproving, isCheckingAllowance = true, walletBalance = "0.0";
$: formattedWalletBalance = formatCAPForDisplay(walletBalance);
async function submit() {
if (!amount) return focusInput('Amount');
isSubmitting = true;
const success = await depositCAP(amount);
if (success) {
hideModal();
}
isSubmitting = false;
}
async function checkAllowance() {
isCheckingAllowance = true;
try {
await getAllowance('CAP', CAP_STAKING_SPENDER);
} finally {
isCheckingAllowance = false;
}
}
async function _approveAsset() {
isApproving = true;
try {
await approveAsset('CAP', CAP_STAKING_SPENDER);
} finally {
isApproving = false;
}
}
async function getBalance() {
walletBalance = await getCAPWalletBalance();
}
checkAllowance();
getBalance();
onMount(() => {
focusInput('Amount');
});
</script>
<style>
</style>
<Modal title='Stake CAP' width={280}>
<div class='container'>
<form on:submit|preventDefault={submit}>
<div class="group">
<Input label='Amount' bind:value={amount} />
<LabelValue
label="Wallet Balance"
value={formattedWalletBalance}
isClickable={true}
hasSemiPadding={true}
on:click={() => { amount = formattedWalletBalance; }}
/>
</div>
<div>
{#if isCheckingAllowance}
<Button noSubmit={true} isLoading={true} label={`Approve CAP`} />
{:else if ($allowances['CAP']?.[CAP_STAKING_SPENDER] || 0) * 1 <= amount * 1}
<Button noSubmit={true} isLoading={isApproving} label={`Approve CAP`} on:click={_approveAsset} />
{:else}
<Button isLoading={isSubmitting} label={`Stake`} />
{/if}
</div>
</form>
</div>
</Modal>