Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/flat curve #20

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wen-moon-ser/moonshot-sdk",
"version": "4.0.0-alpha-0.0.2",
"version": "4.1.0-alpha-0.0.8",
"main": "dist/index.js",
"types": "dist/types/index.d.ts",
"scripts": {
Expand Down Expand Up @@ -32,8 +32,8 @@
"typescript": "^5.5.2"
},
"dependencies": {
"@coral-xyz/anchor": "^0.29.0",
"@heliofi/launchpad-common": "1.1.2",
"@coral-xyz/anchor": "^0.30.1",
"@heliofi/launchpad-common": "1.1.6",
"@solana/spl-token": "0.3.9",
"@solana/web3.js": "^1.68.0",
"@types/bn.js": "^5.1.5",
Expand Down
75 changes: 75 additions & 0 deletions src/domain/model/curve/ConstantProductCurveV2Adapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import {
GetCollateralPriceOptions,
GetCollateralAmountOptions,
GetTokenAmountOptions,
} from '../token';
import { GetTokenAmountSyncOptions } from '../token/GetTokenAmountSyncOptions';
import { AbstractCurveAdapter } from './AbstractCurveAdapter';
import { ConstantProductCurveV2 } from '@heliofi/launchpad-common';
import { GetCollateralAmountSyncOptions } from '../token/GetCollateralAmountSyncOptions';

export class ConstantProductCurveV2Adapter extends AbstractCurveAdapter {
private readonly platformFeeBps: number = 100;

private readonly curve = new ConstantProductCurveV2();

getCollateralPrice(options: GetCollateralPriceOptions): Promise<bigint> {
return this.getCollateralAmountByTokens({
tokenAmount: options.tokenAmount,
tradeDirection: 'BUY',
curvePosition: options.curvePosition,
});
}

async getCollateralAmountByTokens(
options: GetCollateralAmountOptions,
): Promise<bigint> {
const curvePosition =
options.curvePosition ?? (await this.getCurvePosition());

return this.curve.getCollateralAmountFromTokens({
amount: options.tokenAmount,
curvePosition,
platformFeeBps: this.platformFeeBps,
tradeDirection: options.tradeDirection,
});
}

getCollateralAmountByTokensSync(
options: GetCollateralAmountSyncOptions,
): bigint {
const curvePosition = options.curvePosition;

return this.curve.getCollateralAmountFromTokens({
amount: options.tokenAmount,
curvePosition,
platformFeeBps: this.platformFeeBps,
tradeDirection: options.tradeDirection,
});
}

async getTokenAmountByCollateral(
options: GetTokenAmountOptions,
): Promise<bigint> {
const curvePosition =
options.curvePosition ?? (await this.getCurvePosition());

return this.curve.getTokensAmountFromCollateral({
amount: options.collateralAmount,
curvePosition,
platformFeeBps: this.platformFeeBps,
tradeDirection: options.tradeDirection,
});
}

getTokenAmountByCollateralSync(options: GetTokenAmountSyncOptions): bigint {
const curvePosition = options.curvePosition;

return this.curve.getTokensAmountFromCollateral({
amount: options.collateralAmount,
curvePosition,
platformFeeBps: this.platformFeeBps,
tradeDirection: options.tradeDirection,
});
}
}
2 changes: 2 additions & 0 deletions src/domain/model/curve/CurveAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ export interface CurveAccount {
migrationFee: bigint;
coefB: number;
bump: number;
migrationTarget: number;
priceIncrease: number;
}
87 changes: 87 additions & 0 deletions src/domain/model/curve/FlatCurveV1Adapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import {
GetCollateralPriceOptions,
GetCollateralAmountOptions,
GetTokenAmountOptions,
} from '../token';
import { GetTokenAmountSyncOptions } from '../token/GetTokenAmountSyncOptions';
import { AbstractCurveAdapter } from './AbstractCurveAdapter';
import { FlatCurveV1 } from '@heliofi/launchpad-common';
import { GetCollateralAmountSyncOptions } from '../token/GetCollateralAmountSyncOptions';
import { TokenLaunchpadIdl } from '../../../solana';
import { BaseAnchorProvider } from '../../../solana';

export class FlatCurveV1Adapter extends AbstractCurveAdapter {
private readonly platformFeeBps: number = 100;

private readonly curve: FlatCurveV1;

constructor(
moonshotProgram: BaseAnchorProvider<TokenLaunchpadIdl>,
mintAddress: string,
collateralCollected: bigint,
priceIncrease?: number,
) {
super(moonshotProgram, mintAddress);
this.curve = new FlatCurveV1(collateralCollected, priceIncrease);
}

getCollateralPrice(options: GetCollateralPriceOptions): Promise<bigint> {
return this.getCollateralAmountByTokens({
tokenAmount: options.tokenAmount,
tradeDirection: 'BUY',
curvePosition: options.curvePosition,
});
}

async getCollateralAmountByTokens(
options: GetCollateralAmountOptions,
): Promise<bigint> {
const curvePosition =
options.curvePosition ?? (await this.getCurvePosition());

return this.curve.getCollateralAmountFromTokens({
amount: options.tokenAmount,
curvePosition,
platformFeeBps: this.platformFeeBps,
tradeDirection: options.tradeDirection,
});
}

getCollateralAmountByTokensSync(
options: GetCollateralAmountSyncOptions,
): bigint {
const curvePosition = options.curvePosition;

return this.curve.getCollateralAmountFromTokens({
amount: options.tokenAmount,
curvePosition,
platformFeeBps: this.platformFeeBps,
tradeDirection: options.tradeDirection,
});
}

async getTokenAmountByCollateral(
options: GetTokenAmountOptions,
): Promise<bigint> {
const curvePosition =
options.curvePosition ?? (await this.getCurvePosition());

return this.curve.getTokensAmountFromCollateral({
amount: options.collateralAmount,
curvePosition,
platformFeeBps: this.platformFeeBps,
tradeDirection: options.tradeDirection,
});
}

getTokenAmountByCollateralSync(options: GetTokenAmountSyncOptions): bigint {
const curvePosition = options.curvePosition;

return this.curve.getTokensAmountFromCollateral({
amount: options.collateralAmount,
curvePosition,
platformFeeBps: this.platformFeeBps,
tradeDirection: options.tradeDirection,
});
}
}
16 changes: 16 additions & 0 deletions src/domain/model/curve/getCurveAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,33 @@ import { ContractCurveType } from '@heliofi/launchpad-common';
import { ConstantProductCurveV1Adapter } from './ConstantProductCurveV1Adapter';
import { LinearCurveV1Adapter } from './LinearCurveV1Adapter';
import { BaseAnchorProvider, TokenLaunchpadIdl } from '../../../solana';
import { FlatCurveV1Adapter } from './FlatCurveV1Adapter';
import { ConstantProductCurveV2Adapter } from './ConstantProductCurveV2Adapter';

export const getCurveAdapter = (
curveAccount: CurveAccount,
programProvider: BaseAnchorProvider<TokenLaunchpadIdl>,
mintAddress: string,
collateralCollected?: bigint,
priceIncrease?: number,
): AbstractCurveAdapter => {
switch (curveAccount.curveType) {
case ContractCurveType.CONSTANT_PRODUCT_V1:
return new ConstantProductCurveV1Adapter(programProvider, mintAddress);
case ContractCurveType.CONSTANT_PRODUCT_V2:
return new ConstantProductCurveV2Adapter(programProvider, mintAddress);
case ContractCurveType.LINEAR_V1:
return new LinearCurveV1Adapter(programProvider, mintAddress);
case ContractCurveType.FLAT_V1:
if (!collateralCollected) {
throw new Error('Collateral collected is required for flat curve');
}
return new FlatCurveV1Adapter(
programProvider,
mintAddress,
collateralCollected,
priceIncrease,
);
default:
throw new Error('Unsupported curve type');
}
Expand Down
2 changes: 2 additions & 0 deletions src/domain/model/curve/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export * from './CurveAccount';
export * from './LinearCurveV1Adapter';
export * from './ConstantProductCurveV1Adapter';
export * from './ConstantProductCurveV2Adapter';
export * from './FlatCurveV1Adapter';
export * from './getCurveAdapter';
11 changes: 11 additions & 0 deletions src/domain/model/moonshot/PrepareMintTxOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,15 @@ export interface PrepareMintTxOptions {
affiliate?: {
wallet: string;
};

/**
* Percentage increase in price after migration, only for flat curve
*/

priceIncrease?: number;

/**
* Amount of collateral collected before migration, only for flat curve
*/
collateralCollected?: string;
}
8 changes: 8 additions & 0 deletions src/domain/model/token/BaseToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ export class BaseToken {

protected readonly mintAddress: string;

protected collateralCollected?: bigint;

protected priceIncrease?: number;

protected _curveAdapter?: AbstractCurveAdapter;

constructor(options: InitTokenOptions) {
this.moonshot = options.moonshot;
this.mintAddress = options.mintAddress;
this.collateralCollected = options.collateralCollected;
this.priceIncrease = options.priceIncrease;
}

protected async curveAdapter(): Promise<AbstractCurveAdapter> {
Expand All @@ -34,6 +40,8 @@ export class BaseToken {
curveAccount,
this.moonshot.provider,
this.mintAddress,
this.collateralCollected,
this.priceIncrease,
);
}

Expand Down
2 changes: 2 additions & 0 deletions src/domain/model/token/InitTokenOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ export interface InitTokenOptions {
mintAddress: string;
moonshot: Moonshot;
curveType?: CurveType;
collateralCollected?: bigint;
priceIncrease?: number;
}
2 changes: 2 additions & 0 deletions src/domain/model/token/PreloadedToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export class PreloadedToken
curveAccount,
options.moonshot.provider,
options.mintAddress,
options.collateralCollected,
options.priceIncrease,
);
return new PreloadedToken(options, curveAdapter);
}
Expand Down
2 changes: 2 additions & 0 deletions src/domain/model/token/Token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export class Token extends BaseToken {
return PreloadedToken.init({
mintAddress: this.mintAddress,
moonshot: this.moonshot,
collateralCollected: this.collateralCollected,
priceIncrease: this.priceIncrease,
});
}
}
8 changes: 2 additions & 6 deletions src/domain/services/utils.ts
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@martin, here the fs was used for uploading image so it doesn't work in the browser, we have to sync about it

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, missed that we can think of a solution for nodejs and browser envs

Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import { readFileSync } from 'fs';

export const imagePathToBase64 = (filePath: string): string => {
const imageBuffer = readFileSync(filePath);

return imageBuffer.toString('base64');
export const imagePathToBase64 = (input: string): string => {
return Buffer.from(input).toString('base64'); // TODO: check how to do file without fs so it is browser compatible
};
12 changes: 4 additions & 8 deletions src/solana/program/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { Idl } from '@coral-xyz/anchor';
import { PublicKey } from '@solana/web3.js';
import idl from './tokenLaunchpadIdlV1';
import idl from './tokenLaunchpadIdlV4';

export const programId = new PublicKey(idl.metadata.address);
export const tokenLaunchpadIdlV1 = idl as TokenLaunchpadIdl;
export const programId = new PublicKey(idl.address);
export const tokenLaunchpadIdlV4 = idl as TokenLaunchpadIdl;

export type TokenLaunchpadIdl = Idl & {
metadata: {
address: string;
};
};
export type TokenLaunchpadIdl = Idl;
Loading