Skip to content
This repository was archived by the owner on Nov 15, 2024. It is now read-only.

Commit be00ceb

Browse files
authored
Merge pull request #26 from raydium-io/update-add-liquidity
Update add liquidity
2 parents 4b4ca41 + 60091e6 commit be00ceb

File tree

3 files changed

+84
-22
lines changed

3 files changed

+84
-22
lines changed

src/components/LiquidityPoolInfo.vue

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@
1515
<Icon type="swap" @click="() => (coinBasePrice = !coinBasePrice)" />
1616
</div>
1717

18+
<div class="fs-container">
19+
<span class="name">Base</span>
20+
<span>
21+
{{ fixedCoinSymbol }}
22+
</span>
23+
</div>
24+
25+
<div class="fs-container">
26+
<span class="name">Max Amount</span>
27+
<span>
28+
{{ maxAmount }}
29+
</span>
30+
</div>
31+
1832
<div class="fs-container">
1933
<span class="name">Pool liquidity</span>
2034
<div class="info">
@@ -58,6 +72,14 @@ export default Vue.extend({
5872
poolInfo: {
5973
type: [Object || null],
6074
default: null
75+
},
76+
fixedCoinSymbol: {
77+
type: String,
78+
default: 'false'
79+
},
80+
maxAmount: {
81+
type: Number,
82+
default: 0
6183
}
6284
},
6385

src/pages/liquidity/index.vue

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@
129129
<div class="card">
130130
<div class="card-body">
131131
<CoinInput
132-
v-model="fromCoinAmount"
132+
v-model="fromCoinAmountMin"
133133
label="Input"
134134
:mint-address="fromCoin ? fromCoin.mintAddress : ''"
135135
:coin-name="fromCoin ? fromCoin.symbol : ''"
@@ -156,7 +156,7 @@
156156
</div>
157157

158158
<CoinInput
159-
v-model="toCoinAmount"
159+
v-model="toCoinAmountMin"
160160
label="Input"
161161
:mint-address="toCoin ? toCoin.mintAddress : ''"
162162
:coin-name="toCoin ? toCoin.symbol : ''"
@@ -176,7 +176,30 @@
176176
@onSelect="openToCoinSelect"
177177
/>
178178

179-
<LiquidityPoolInfo :initialized="liquidity.initialized" :pool-info="liquidity.infos[lpMintAddress]" />
179+
<LiquidityPoolInfo
180+
:fixed-coin-symbol="
181+
toCoin !== null && fixedCoin === toCoin.mintAddress
182+
? toCoin.symbol
183+
: fromCoin !== null && fixedCoin === fromCoin.mintAddress
184+
? fromCoin.symbol
185+
: ''
186+
"
187+
:max-amount="`${
188+
toCoin !== null && fixedCoin === toCoin.mintAddress
189+
? fromCoinAmount
190+
: fromCoin !== null && fixedCoin === fromCoin.mintAddress
191+
? toCoinAmount
192+
: 0
193+
} ${
194+
toCoin !== null && fromCoin !== null && fixedCoin === toCoin.mintAddress
195+
? fromCoin.symbol
196+
: toCoin !== null && fromCoin !== null && fixedCoin === fromCoin.mintAddress
197+
? toCoin.symbol
198+
: ''
199+
}`"
200+
:initialized="liquidity.initialized"
201+
:pool-info="liquidity.infos[lpMintAddress]"
202+
/>
180203
<!-- <div v-if="officialPool === false">
181204
<div style="margin: 10px">
182205
<div>AMM ID:</div>
@@ -225,8 +248,8 @@
225248
!lpMintAddress ||
226249
!liquidity.initialized ||
227250
liquidity.loading ||
228-
gt(fromCoinAmount, fromCoin.balance ? fromCoin.balance.fixed() : '0') ||
229-
gt(toCoinAmount, toCoin.balance ? toCoin.balance.fixed() : '0') ||
251+
gt(fromCoinAmountMin, fromCoin.balance ? fromCoin.balance.fixed() : '0') ||
252+
gt(toCoinAmountMin, toCoin.balance ? toCoin.balance.fixed() : '0') ||
230253
suppling ||
231254
(fromCoin.mintAddress === TOKENS.xCOPE.mintAddress && gt(5, fromCoinAmount)) ||
232255
(toCoin.mintAddress === TOKENS.xCOPE.mintAddress && gt(5, toCoinAmount))
@@ -238,10 +261,10 @@
238261
<template v-else-if="!lpMintAddress || !liquidity.initialized"> Invalid pair </template>
239262
<template v-else-if="!fromCoinAmount"> Enter an amount </template>
240263
<template v-else-if="liquidity.loading"> Updating pool information </template>
241-
<template v-else-if="gt(fromCoinAmount, fromCoin.balance ? fromCoin.balance.fixed() : '0')">
264+
<template v-else-if="gt(fromCoinAmountMin, fromCoin.balance ? fromCoin.balance.fixed() : '0')">
242265
Insufficient {{ fromCoin.symbol }} balance
243266
</template>
244-
<template v-else-if="gt(toCoinAmount, toCoin.balance ? toCoin.balance.fixed() : '')">
267+
<template v-else-if="gt(toCoinAmountMin, toCoin.balance ? toCoin.balance.fixed() : '0')">
245268
Insufficient {{ toCoin.symbol }} balance
246269
</template>
247270
<template v-else-if="fromCoin.mintAddress === TOKENS.xCOPE.mintAddress && gt(50, fromCoinAmount)">
@@ -321,6 +344,8 @@ export default Vue.extend({
321344
322345
fromCoin: RAY as TokenInfo | null,
323346
toCoin: null as TokenInfo | null,
347+
fromCoinAmountMin: '',
348+
toCoinAmountMin: '',
324349
fromCoinAmount: '',
325350
toCoinAmount: '',
326351
@@ -456,6 +481,8 @@ export default Vue.extend({
456481
this.findLiquidityPool()
457482
this.fromCoinAmount = ''
458483
this.toCoinAmount = ''
484+
this.fromCoinAmountMin = ''
485+
this.toCoinAmountMin = ''
459486
}
460487
},
461488
@@ -468,6 +495,8 @@ export default Vue.extend({
468495
this.findLiquidityPool()
469496
this.fromCoinAmount = ''
470497
this.toCoinAmount = ''
498+
this.fromCoinAmountMin = ''
499+
this.toCoinAmountMin = ''
471500
}
472501
},
473502
@@ -675,9 +704,14 @@ export default Vue.extend({
675704
changeCoinAmountPosition() {
676705
const tempFromCoinAmount = this.fromCoinAmount
677706
const tempToCoinAmount = this.toCoinAmount
707+
const tempFromCoinAmountMin = this.fromCoinAmountMin
708+
const tempToCoinAmountMin = this.toCoinAmountMin
678709
679710
this.fromCoinAmount = tempToCoinAmount
680711
this.toCoinAmount = tempFromCoinAmount
712+
713+
this.fromCoinAmountMin = tempToCoinAmountMin
714+
this.toCoinAmountMin = tempFromCoinAmountMin
681715
},
682716
683717
updateCoinInfo(tokenAccounts: any) {
@@ -780,34 +814,38 @@ export default Vue.extend({
780814
const poolInfo = this.liquidity.infos[this.lpMintAddress]
781815
782816
if (this.fixedCoin === this.fromCoin.mintAddress) {
783-
const amount = (poolInfo.version === 5 ? getOutAmountStable : getOutAmount)(
817+
const { outAmount, outMinAmount } = (poolInfo.version === 5 ? getOutAmountStable : getOutAmount)(
784818
poolInfo,
785819
this.fromCoinAmount,
786820
this.fromCoin.mintAddress,
787821
this.toCoin.mintAddress,
788822
this.setting.slippage
789823
)
790824
791-
if (amount.isNaN() || !amount.isFinite()) {
825+
if (outAmount.isNaN() || !outAmount.isFinite()) {
792826
this.toCoinAmount = ''
827+
this.toCoinAmountMin = ''
793828
} else {
794-
this.toCoinAmount = amount.toFixed(this.toCoin.decimals)
829+
this.toCoinAmountMin = outMinAmount.toFixed(this.toCoin.decimals)
830+
this.toCoinAmount = outAmount.toFixed(this.toCoin.decimals)
795831
}
796832
} else {
797833
const poolInfo = this.liquidity.infos[this.lpMintAddress]
798834
799-
const amount = (poolInfo.version === 5 ? getOutAmountStable : getOutAmount)(
835+
const { outAmount, outMinAmount } = (poolInfo.version === 5 ? getOutAmountStable : getOutAmount)(
800836
poolInfo,
801837
this.toCoinAmount,
802838
this.toCoin.mintAddress,
803839
this.fromCoin.mintAddress,
804840
this.setting.slippage
805841
)
806842
807-
if (amount.isNaN() || !amount.isFinite()) {
843+
if (outAmount.isNaN() || !outAmount.isFinite()) {
808844
this.fromCoinAmount = ''
845+
this.fromCoinAmountMin = ''
809846
} else {
810-
this.fromCoinAmount = amount.toFixed(this.toCoin.decimals)
847+
this.fromCoinAmountMin = outMinAmount.toFixed(this.toCoin.decimals)
848+
this.fromCoinAmount = outAmount.toFixed(this.toCoin.decimals)
811849
}
812850
}
813851
}

src/utils/liquidity.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,21 @@ export function getOutAmount(
6868
const percent = new BigNumber(100).plus(slippage).dividedBy(100)
6969

7070
if (!coin.balance || !pc.balance) {
71-
return outAmount
71+
return { outAmount, outMinAmount: outAmount }
7272
}
7373

7474
if (fromCoinMint === coin.mintAddress && toCoinMint === pc.mintAddress) {
7575
// outcoin is pc
7676
outAmount = fromAmount.multipliedBy(price)
77-
outAmount = outAmount.multipliedBy(percent)
7877
} else if (fromCoinMint === pc.mintAddress && toCoinMint === coin.mintAddress) {
7978
// outcoin is coin
8079
outAmount = fromAmount.dividedBy(price)
81-
outAmount = outAmount.multipliedBy(percent)
8280
}
8381

84-
return outAmount
82+
return {
83+
outAmount: outAmount.multipliedBy(percent),
84+
outMinAmount: outAmount
85+
}
8586
}
8687

8788
export function getOutAmountStable(
@@ -95,7 +96,7 @@ export function getOutAmountStable(
9596

9697
const x = poolInfo.coin.balance?.toEther()
9798
const y = poolInfo.pc.balance?.toEther()
98-
if (!x || !y) return new BigNumber(0)
99+
if (!x || !y) return { outAmount: new BigNumber(0), outMinAmount: new BigNumber(0) }
99100

100101
const price = y.dividedBy(x).toNumber()
101102
// getStablePrice(currentK.toNumber(), x.toNumber(), y.toNumber(), true)
@@ -105,20 +106,21 @@ export function getOutAmountStable(
105106
const percent = new BigNumber(100).plus(slippage).dividedBy(100)
106107

107108
if (!coin.balance || !pc.balance) {
108-
return outAmount
109+
return { outAmount, outMinAmount: outAmount }
109110
}
110111

111112
if (fromCoinMint === coin.mintAddress && toCoinMint === pc.mintAddress) {
112113
// outcoin is pc
113114
outAmount = fromAmount.multipliedBy(price)
114-
outAmount = outAmount.multipliedBy(percent)
115115
} else if (fromCoinMint === pc.mintAddress && toCoinMint === coin.mintAddress) {
116116
// outcoin is coin
117117
outAmount = fromAmount.dividedBy(price)
118-
outAmount = outAmount.multipliedBy(percent)
119118
}
120119

121-
return outAmount
120+
return {
121+
outAmount: outAmount.multipliedBy(percent),
122+
outMinAmount: outAmount
123+
}
122124
}
123125

124126
/* eslint-disable */

0 commit comments

Comments
 (0)