-
Notifications
You must be signed in to change notification settings - Fork 296
/
InitialLiquidity.vue
336 lines (308 loc) · 9.65 KB
/
InitialLiquidity.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
<script setup lang="ts">
import { onMounted, onBeforeMount, ref, computed } from 'vue';
import { bnum } from '@/lib/utils';
import useWeb3 from '@/services/web3/useWeb3';
import useTokens from '@/composables/useTokens';
import usePoolCreation from '@/composables/pools/usePoolCreation';
import useNumbers from '@/composables/useNumbers';
import TokenInput from '@/components/inputs/TokenInput/TokenInput.vue';
import AnimatePresence from '@/components/animate/AnimatePresence.vue';
import { useI18n } from 'vue-i18n';
const emit = defineEmits(['update:height']);
/**
* STATE
*/
const isOptimised = ref(false);
const cardWrapper = ref<HTMLElement>();
/**
* COMPOSBALES
*/
const { userNetworkConfig } = useWeb3();
const { balanceFor, priceFor, nativeAsset, wrappedNativeAsset } = useTokens();
const { fNum } = useNumbers();
const {
seedTokens,
tokensList,
optimisedLiquidity,
totalLiquidity,
scaledLiquidity,
manuallySetToken,
autoOptimiseBalances,
currentLiquidity,
isWethPool,
useNativeAsset,
poolLiquidity,
goBack,
updateManuallySetToken,
proceed,
clearAmounts,
setAmountsToMaxBalances,
saveState
} = usePoolCreation();
const { t } = useI18n();
const tokenAddresses = ref([] as string[]);
/**
* COMPUTED
*/
const areAmountsMaxed = computed(() => {
const isMaxed = seedTokens.value.every(t =>
bnum(t.amount).eq(balanceFor(t.tokenAddress))
);
return isMaxed;
});
const isExceedingWalletBalance = computed(() => {
// need to perform rounding here as JS cuts off those
// really long numbers which makes it impossible to compare
const isExceeding = tokenAddresses.value.some((t, i) =>
bnum(seedTokens.value[i].amount).gt(balanceFor(t))
);
return isExceeding;
});
const arbitrageDelta = computed(() => {
let totalPctDelta = bnum(0);
for (const token of seedTokens.value) {
const initialPct = bnum(token.amount)
.times(priceFor(token.tokenAddress))
.div(poolLiquidity.value);
const expectedPct = token.weight / 100;
const delta = initialPct.minus(expectedPct).abs();
totalPctDelta = totalPctDelta.plus(delta);
}
return {
delta: totalPctDelta,
value: totalPctDelta.times(poolLiquidity.value)
};
});
/**
* LIFECYCLE
*/
onBeforeMount(() => {
tokenAddresses.value = [...tokensList.value];
if (isWethPool.value) setNativeAssetIfRequired();
});
onMounted(() => {
optimiseLiquidity();
scaleLiquidity();
});
/**
* METHODS
*/
function optimiseLiquidity(force = false) {
if (manuallySetToken.value && !force) return;
isOptimised.value = true;
for (const token of seedTokens.value) {
token.amount = optimisedLiquidity.value[token.tokenAddress].balanceRequired;
}
}
function scaleLiquidity() {
if (!autoOptimiseBalances.value || !manuallySetToken.value) return;
for (const token of seedTokens.value) {
if (token.tokenAddress !== manuallySetToken.value) {
token.amount = scaledLiquidity.value[token.tokenAddress].balanceRequired;
}
}
}
function toggleAutoOptimise() {
autoOptimiseBalances.value = !autoOptimiseBalances.value;
checkLiquidityScaling();
}
function checkLiquidityScaling() {
if (!autoOptimiseBalances.value) return;
scaleLiquidity();
}
function handleMax() {
setAmountsToMaxBalances();
isOptimised.value = false;
}
function handleAmountChange(tokenAddress) {
updateManuallySetToken(tokenAddress);
checkLiquidityScaling();
isOptimised.value = false;
}
function handleAddressChange(newAddress: string): void {
useNativeAsset.value = newAddress === nativeAsset.address;
}
function tokenOptions(index: number): string[] {
if (tokenAddresses.value[index] === wrappedNativeAsset.value.address)
return [wrappedNativeAsset.value.address, nativeAsset.address];
if (tokenAddresses.value[index] === nativeAsset.address)
return [nativeAsset.address, wrappedNativeAsset.value.address];
return [];
}
// If useNativeAsset is set, or ETH has a higher balance than WETH, then use it for the input.
function setNativeAssetIfRequired(): void {
const nativeAssetBalance = balanceFor(nativeAsset.address);
const wrappedNativeAssetBalance = balanceFor(
wrappedNativeAsset.value.address
);
if (
useNativeAsset.value ||
bnum(nativeAssetBalance).gt(wrappedNativeAssetBalance)
) {
tokenAddresses.value = tokenAddresses.value.map(address => {
if (address == wrappedNativeAsset.value.address) {
return nativeAsset.address;
}
return address;
});
}
}
function handleClearAll() {
clearAmounts();
isOptimised.value = false;
}
function onAlertMountChange() {
emit('update:height', {
height: cardWrapper.value?.offsetHeight || 0
});
}
function saveAndProceed() {
saveState();
proceed();
}
</script>
<template>
<div ref="cardWrapper">
<BalCard shadow="xl" noBorder>
<BalStack vertical>
<BalStack vertical spacing="xs">
<span class="text-xs text-gray-700 dark:text-gray-500">{{
userNetworkConfig?.name
}}</span>
<BalStack horizontal spacing="xs" align="center">
<button
@click="goBack"
class="text-blue-500 hover:text-blue-700 flex"
>
<BalIcon class="flex" name="chevron-left" />
</button>
<h5 class="font-bold dark:text-gray-300">Set initial liquidity</h5>
</BalStack>
<AnimatePresence :isVisible="isOptimised" unmountInstantly>
<BalStack
horizontal
align="center"
spacing="sm"
class="border rounded-lg p-2 mt-2"
>
<BalIcon name="zap" size="sm" class="mt-1 text-gray-500" />
<span class="dark:text-gray-400 font-medium">
{{ t('optimizedPrefilled') }}
</span>
<button
@click="handleClearAll"
class="text-sm font-medium text-gray-400 hover:text-blue-500"
>
Clear all
</button>
</BalStack>
</AnimatePresence>
</BalStack>
<BalStack vertical>
<TokenInput
v-for="(address, i) in tokenAddresses"
:key="i"
v-model:amount="seedTokens[i].amount"
v-model:address="tokenAddresses[i]"
:weight="seedTokens[i].weight / 100"
:name="`initial-token-${seedTokens[i].tokenAddress}`"
fixedToken
:options="tokenOptions(i)"
@update:amount="handleAmountChange(address)"
@update:address="handleAddressChange($event)"
/>
</BalStack>
<BalStack horizontal spacing="sm" align="center">
<div>
<span class="text-sm pl-2">{{
t('autoOptimiseLiquidityToggle.label')
}}</span>
<BalTooltip width="64">
<template v-slot:activator>
<BalIcon
name="info"
size="xs"
class="text-gray-400 ml-1 flex"
/>
</template>
<div v-html="t('autoOptimiseLiquidityToggle.tooltip')" />
</BalTooltip>
</div>
<div>
<BalToggle
name="autoOptimise"
:checked="autoOptimiseBalances"
@toggle="toggleAutoOptimise"
/>
</div>
</BalStack>
<div class="p-3 border rounded-lg">
<BalStack horizontal justify="between">
<BalStack vertical spacing="none">
<h6>{{ t('total') }}</h6>
<BalStack horizontal spacing="xs" class="font-medium">
<span class="text-sm">
{{ t('available') }}: {{ fNum(totalLiquidity, 'usd') }}
</span>
<button
:disabled="areAmountsMaxed"
:class="[
'text-sm font-semibold3',
{
'text-gray-400 dark:text-gray-600': areAmountsMaxed,
'text-blue-500 hover:text-blue-50': !areAmountsMaxed
}
]"
@click="handleMax"
>
{{ areAmountsMaxed ? t('maxed') : t('max') }}
</button>
</BalStack>
</BalStack>
<BalStack vertical spacing="none">
<h6>{{ fNum(currentLiquidity, 'usd') }}</h6>
<AnimatePresence
:isVisible="!isOptimised"
@on-presence="onAlertMountChange"
@on-exit="onAlertMountChange"
unmountInstantly
>
<button
@click="optimiseLiquidity(true)"
class="bg-clip-text text-sm text-transparent font-medium bg-gradient-to-tr from-blue-500 to-pink-500 hover:from-blue-800 hover:to-pink-800"
>
{{ t('optimize') }}
</button>
</AnimatePresence>
</BalStack>
</BalStack>
</div>
<AnimatePresence
:isVisible="arbitrageDelta.delta > 0.05"
@on-presence="onAlertMountChange"
@on-exit="onAlertMountChange"
unmountInstantly
>
<BalAlert
type="warning"
:title="
t('createAPool.arbTitle', [
fNum(arbitrageDelta.value, 'usd'),
fNum(arbitrageDelta.delta, 'percent')
])
"
>
{{ t('createAPool.arbReason') }}
</BalAlert>
</AnimatePresence>
<BalBtn
:disabled="isExceedingWalletBalance"
@click="saveAndProceed"
block
color="gradient"
>{{ t('preview') }}</BalBtn
>
</BalStack>
</BalCard>
</div>
</template>