-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbondingCurveAccount.ts
134 lines (112 loc) · 3.8 KB
/
bondingCurveAccount.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
import { struct, bool, u64, Layout } from "@coral-xyz/borsh";
export class BondingCurveAccount {
public discriminator: bigint;
public virtualTokenReserves: bigint;
public virtualSolReserves: bigint;
public realTokenReserves: bigint;
public realSolReserves: bigint;
public tokenTotalSupply: bigint;
public complete: boolean;
constructor(
discriminator: bigint,
virtualTokenReserves: bigint,
virtualSolReserves: bigint,
realTokenReserves: bigint,
realSolReserves: bigint,
tokenTotalSupply: bigint,
complete: boolean
) {
this.discriminator = discriminator;
this.virtualTokenReserves = virtualTokenReserves;
this.virtualSolReserves = virtualSolReserves;
this.realTokenReserves = realTokenReserves;
this.realSolReserves = realSolReserves;
this.tokenTotalSupply = tokenTotalSupply;
this.complete = complete;
}
getBuyPrice(amount: bigint): bigint {
if (this.complete) {
throw new Error("Curve is complete");
}
if (amount <= 0n) {
return 0n;
}
// Calculate the product of virtual reserves
let n = this.virtualSolReserves * this.virtualTokenReserves;
// Calculate the new virtual sol reserves after the purchase
let i = this.virtualSolReserves + amount;
// Calculate the new virtual token reserves after the purchase
let r = n / i + 1n;
// Calculate the amount of tokens to be purchased
let s = this.virtualTokenReserves - r;
// Return the minimum of the calculated tokens and real token reserves
return s < this.realTokenReserves ? s : this.realTokenReserves;
}
getSellPrice(amount: bigint, feeBasisPoints: bigint): bigint {
if (this.complete) {
throw new Error("Curve is complete");
}
if (amount <= 0n) {
return 0n;
}
// Calculate the proportional amount of virtual sol reserves to be received
let n =
(amount * this.virtualSolReserves) / (this.virtualTokenReserves + amount);
// Calculate the fee amount in the same units
let a = (n * feeBasisPoints) / 10000n;
// Return the net amount after deducting the fee
return n - a;
}
getMarketCapSOL(): bigint {
if (this.virtualTokenReserves === 0n) {
return 0n;
}
return (
(this.tokenTotalSupply * this.virtualSolReserves) /
this.virtualTokenReserves
);
}
getFinalMarketCapSOL(feeBasisPoints: bigint): bigint {
let totalSellValue = this.getBuyOutPrice(
this.realTokenReserves,
feeBasisPoints
);
let totalVirtualValue = this.virtualSolReserves + totalSellValue;
let totalVirtualTokens = this.virtualTokenReserves - this.realTokenReserves;
if (totalVirtualTokens === 0n) {
return 0n;
}
return (this.tokenTotalSupply * totalVirtualValue) / totalVirtualTokens;
}
getBuyOutPrice(amount: bigint, feeBasisPoints: bigint): bigint {
let solTokens =
amount < this.realSolReserves ? this.realSolReserves : amount;
let totalSellValue =
(solTokens * this.virtualSolReserves) /
(this.virtualTokenReserves - solTokens) +
1n;
let fee = (totalSellValue * feeBasisPoints) / 10000n;
return totalSellValue + fee;
}
public static fromBuffer(buffer: Buffer): BondingCurveAccount {
const structure: Layout<BondingCurveAccount> = struct([
u64("discriminator"),
u64("virtualTokenReserves"),
u64("virtualSolReserves"),
u64("realTokenReserves"),
u64("realSolReserves"),
u64("tokenTotalSupply"),
bool("complete"),
]);
let value = structure.decode(buffer);
return new BondingCurveAccount(
BigInt(value.discriminator),
BigInt(value.virtualTokenReserves),
BigInt(value.virtualSolReserves),
BigInt(value.realTokenReserves),
BigInt(value.realSolReserves),
BigInt(value.tokenTotalSupply),
value.complete
);
}
}