@solsdk/tokenflow_sdk
Version:
A simple SDK for interacting with tokenflow
104 lines (100 loc) • 4.29 kB
JavaScript
'use strict';
var borsh = require('@coral-xyz/borsh');
class BondingCurveAccount {
discriminator;
virtualSolReserves;
virtualTokenReserves;
realSolReserves;
realTokenReserves;
tokenTotalSupply;
active;
creator;
tradeAuthority;
complete;
constructor(discriminator, virtualSolReserves, virtualTokenReserves, realSolReserves, realTokenReserves, tokenTotalSupply, active, creator, tradeAuthority, complete) {
this.discriminator = discriminator;
this.virtualSolReserves = virtualSolReserves;
this.virtualTokenReserves = virtualTokenReserves;
this.realSolReserves = realSolReserves;
this.realTokenReserves = realTokenReserves;
this.tokenTotalSupply = tokenTotalSupply;
this.active = active;
this.creator = creator;
this.tradeAuthority = tradeAuthority;
this.complete = complete;
}
getBuyPrice(amount) {
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, feeBasisPoints) {
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() {
if (this.virtualTokenReserves === 0n) {
return 0n;
}
return ((this.tokenTotalSupply * this.virtualSolReserves) /
this.virtualTokenReserves);
}
getFinalMarketCapSOL(feeBasisPoints) {
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, feeBasisPoints) {
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;
}
static fromBuffer(buffer) {
const discriminator = buffer.subarray(0, 8).readBigInt64LE();
const layout = borsh.struct([
borsh.u64("virtualSolReserves"),
borsh.u64("virtualTokenReserves"),
borsh.u64("realSolReserves"),
borsh.u64("realTokenReserves"),
borsh.u64("tokenTotalSupply"),
borsh.bool("active"),
borsh.publicKey("creator"),
borsh.option(borsh.publicKey(), "tradeAuthority"),
borsh.bool("complete"),
]);
const value = layout.decode(buffer.subarray(8));
return new BondingCurveAccount(BigInt(discriminator), BigInt(value.virtualSolReserves), BigInt(value.virtualTokenReserves), BigInt(value.realSolReserves), BigInt(value.realTokenReserves), BigInt(value.tokenTotalSupply), value.active, value.creator, value.tradeAuthority, value.complete);
}
}
exports.BondingCurveAccount = BondingCurveAccount;
//# sourceMappingURL=bondingCurveAccount.cjs.map