-
Notifications
You must be signed in to change notification settings - Fork 0
/
make-provide-liquidity.ts
142 lines (124 loc) · 5.63 KB
/
make-provide-liquidity.ts
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
import { Bool, Field, Poseidon, PublicKey, SelfProof, Signature, UInt64 } from "o1js"
import {
Balance,
DoubleBalanceWitness,
Liquidity,
Pool,
SingleLiquidityWitness,
SinglePoolWitness,
StateRoots,
choose,
normalDiv,
} from "nacho-common-o1js"
export const makeProvideLiquidity = async (
stateRoots: StateRoots,
earlierProof: SelfProof<StateRoots, StateRoots>,
singlePoolWitness: SinglePoolWitness,
singleLiquidityWitness: SingleLiquidityWitness,
doubleBalanceWitness: DoubleBalanceWitness,
baseTokenId: Field,
quoteTokenId: Field,
userAddress: PublicKey,
userLiquidityPoints: Field,
userBalanceBaseTokenAmount: UInt64,
userBalanceQuoteTokenAmount: UInt64,
poolBaseTokenAmount: UInt64,
poolQuoteTokenAmount: UInt64,
poolTotalLiquidityPoints: Field,
userBaseTokenAmountToProvide: UInt64,
userQuoteTokenAmountLimitToProvide: UInt64,
userSignature: Signature,
): Promise<StateRoots> => {
stateRoots.assertEquals(earlierProof.publicOutput)
earlierProof.verify()
// The signature message is base and quote tokens IDs, base token amount and quote token amount limit.
// NOTE: A compromised signature might be used to make multiple unwanted transactions.
userSignature
.verify(userAddress, [
baseTokenId,
quoteTokenId,
userBaseTokenAmountToProvide.value,
userQuoteTokenAmountLimitToProvide.value,
])
.assertTrue()
const userBaseTokenBalance = new Balance({
owner: userAddress,
tokenId: baseTokenId,
tokenAmount: userBalanceBaseTokenAmount,
})
const userQuoteTokenBalance = new Balance({
owner: userAddress,
tokenId: quoteTokenId,
tokenAmount: userBalanceQuoteTokenAmount,
})
const userLiquidity = new Liquidity({
provider: userAddress,
baseTokenId,
quoteTokenId,
points: userLiquidityPoints,
})
const pool = new Pool({
baseTokenId,
quoteTokenId,
baseTokenAmount: poolBaseTokenAmount,
quoteTokenAmount: poolQuoteTokenAmount,
totalLiquidityPoints: poolTotalLiquidityPoints,
})
// NOTE: We make sure that this double Merkle witness is correct to prevent mistaken root calculation.
doubleBalanceWitness.isCorrect().assertTrue()
stateRoots.pools.assertEquals(singlePoolWitness.calculateRoot(Poseidon.hash(pool.toFields())))
const liquiditiesRootIfFirstProviding = singleLiquidityWitness.calculateRoot(Field(0))
const isFirstProviding = stateRoots.liquidities.equals(liquiditiesRootIfFirstProviding)
Bool.or(
stateRoots.liquidities.equals(liquiditiesRootIfFirstProviding),
stateRoots.liquidities.equals(
singleLiquidityWitness.calculateRoot(Poseidon.hash(userLiquidity.toFields())),
),
).assertTrue()
stateRoots.balances.assertEquals(
doubleBalanceWitness.calculateRoot(
Poseidon.hash(userBaseTokenBalance.toFields()),
Poseidon.hash(userQuoteTokenBalance.toFields()),
),
)
// NOTE: We make sure that the given user liquidity points are zero, if it is the user's first liquidity providing in this pool.
choose(isFirstProviding, userLiquidityPoints, Field(0)).assertEquals(Field(0))
userBaseTokenAmountToProvide.equals(UInt64.zero).assertFalse()
poolBaseTokenAmount.equals(UInt64.zero).assertFalse()
// NOTE: We don't have to check overflow because the total liquidity points in a pool is always less than 2^128,
// the base token amount to provide as a liquidity is always less than 2^64 and the target can store up to 2^254.
// Plus the base token amount in the pool is never equal to zero.
const liquidityPointsToBeCreated = normalDiv(
poolTotalLiquidityPoints.mul(userBaseTokenAmountToProvide.value),
poolBaseTokenAmount.value,
)
// NOTE: We don't have to check overflow because both the base token amount to provide as a liquidity
// and the quote token amount in the pool are always less than 2^64
// and the target can store up to 2^254.
// Plus the base token amount in the pool is never equal to zero.
// And `UInt64.from` checks overflows.
const quoteTokenAmountToProvide = UInt64.fromFields([
normalDiv(
userBaseTokenAmountToProvide.value.mul(poolQuoteTokenAmount.value),
poolBaseTokenAmount.value,
),
])
quoteTokenAmountToProvide.assertLessThanOrEqual(userQuoteTokenAmountLimitToProvide)
// NOTE: We don't have to check if the user has enough balance because it throws an error if there is an underflow.
userBaseTokenBalance.tokenAmount = userBalanceBaseTokenAmount.sub(userBaseTokenAmountToProvide)
// NOTE: We don't have to check if the user has enough balance because it throws an error if there is an underflow.
userQuoteTokenBalance.tokenAmount = userBalanceQuoteTokenAmount.sub(quoteTokenAmountToProvide)
userLiquidity.points = userLiquidityPoints.add(liquidityPointsToBeCreated)
pool.totalLiquidityPoints = poolTotalLiquidityPoints.add(liquidityPointsToBeCreated)
pool.baseTokenAmount = poolBaseTokenAmount.add(userBaseTokenAmountToProvide)
pool.quoteTokenAmount = poolQuoteTokenAmount.add(quoteTokenAmountToProvide)
stateRoots.pools = singlePoolWitness.calculateRoot(Poseidon.hash(pool.toFields()))
stateRoots.liquidities = singleLiquidityWitness.calculateRoot(
Poseidon.hash(userLiquidity.toFields()),
)
stateRoots.balances = doubleBalanceWitness.calculateRoot(
Poseidon.hash(userBaseTokenBalance.toFields()),
Poseidon.hash(userQuoteTokenBalance.toFields()),
)
return stateRoots
}