Skip to content

Commit

Permalink
Merge pull request #333 from oraichain/feat/zapper-2
Browse files Browse the repository at this point in the history
Feat/zapper 2
  • Loading branch information
vuonghuuhung authored Oct 24, 2024
2 parents 9df748d + 73270b8 commit 4836323
Show file tree
Hide file tree
Showing 23 changed files with 1,525 additions and 525 deletions.
2 changes: 1 addition & 1 deletion packages/contracts-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@oraichain/oraidex-contracts-sdk",
"version": "1.0.52",
"version": "1.0.53",
"main": "build/index.js",
"files": [
"build/",
Expand Down
90 changes: 90 additions & 0 deletions packages/contracts-sdk/src/IncentivesFundManager.client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* This file was automatically generated by @oraichain/[email protected].
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
* and run the @oraichain/ts-codegen generate command to regenerate this file.
*/

import { CosmWasmClient, SigningCosmWasmClient, ExecuteResult } from "@cosmjs/cosmwasm-stargate";
import { Coin, StdFee } from "@cosmjs/amino";
import {Addr, InstantiateMsg, ExecuteMsg, Uint128, AssetInfo, Asset, QueryMsg, MigrateMsg, ConfigResponse} from "./IncentivesFundManager.types";
export interface IncentivesFundManagerReadOnlyInterface {
contractAddress: string;
config: () => Promise<ConfigResponse>;
}
export class IncentivesFundManagerQueryClient implements IncentivesFundManagerReadOnlyInterface {
client: CosmWasmClient;
contractAddress: string;

constructor(client: CosmWasmClient, contractAddress: string) {
this.client = client;
this.contractAddress = contractAddress;
this.config = this.config.bind(this);
}

config = async (): Promise<ConfigResponse> => {
return this.client.queryContractSmart(this.contractAddress, {
config: {}
});
};
}
export interface IncentivesFundManagerInterface extends IncentivesFundManagerReadOnlyInterface {
contractAddress: string;
sender: string;
updateConfig: ({
oraiswapV3,
owner
}: {
oraiswapV3?: Addr;
owner?: Addr;
}, _fee?: number | StdFee | "auto", _memo?: string, _funds?: Coin[]) => Promise<ExecuteResult>;
sendFund: ({
asset,
receiver
}: {
asset: Asset;
receiver: Addr;
}, _fee?: number | StdFee | "auto", _memo?: string, _funds?: Coin[]) => Promise<ExecuteResult>;
}
export class IncentivesFundManagerClient extends IncentivesFundManagerQueryClient implements IncentivesFundManagerInterface {
client: SigningCosmWasmClient;
sender: string;
contractAddress: string;

constructor(client: SigningCosmWasmClient, sender: string, contractAddress: string) {
super(client, contractAddress);
this.client = client;
this.sender = sender;
this.contractAddress = contractAddress;
this.updateConfig = this.updateConfig.bind(this);
this.sendFund = this.sendFund.bind(this);
}

updateConfig = async ({
oraiswapV3,
owner
}: {
oraiswapV3?: Addr;
owner?: Addr;
}, _fee: number | StdFee | "auto" = "auto", _memo?: string, _funds?: Coin[]): Promise<ExecuteResult> => {
return await this.client.execute(this.sender, this.contractAddress, {
update_config: {
oraiswap_v3: oraiswapV3,
owner
}
}, _fee, _memo, _funds);
};
sendFund = async ({
asset,
receiver
}: {
asset: Asset;
receiver: Addr;
}, _fee: number | StdFee | "auto" = "auto", _memo?: string, _funds?: Coin[]): Promise<ExecuteResult> => {
return await this.client.execute(this.sender, this.contractAddress, {
send_fund: {
asset,
receiver
}
}, _fee, _memo, _funds);
};
}
38 changes: 38 additions & 0 deletions packages/contracts-sdk/src/IncentivesFundManager.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export type Addr = string;
export interface InstantiateMsg {
oraiswap_v3: Addr;
owner?: Addr | null;
}
export type ExecuteMsg = {
update_config: {
oraiswap_v3?: Addr | null;
owner?: Addr | null;
};
} | {
send_fund: {
asset: Asset;
receiver: Addr;
};
};
export type Uint128 = string;
export type AssetInfo = {
token: {
contract_addr: Addr;
};
} | {
native_token: {
denom: string;
};
};
export interface Asset {
amount: Uint128;
info: AssetInfo;
}
export type QueryMsg = {
config: {};
};
export interface MigrateMsg {}
export interface ConfigResponse {
oraiswap_v3: Addr;
owner: Addr;
}
9 changes: 8 additions & 1 deletion packages/contracts-sdk/src/OraiswapV3.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@

import { CosmWasmClient, SigningCosmWasmClient, ExecuteResult } from "@cosmjs/cosmwasm-stargate";
import { Coin, StdFee } from "@cosmjs/amino";
import {Percentage, InstantiateMsg, ExecuteMsg, Addr, Liquidity, SqrtPrice, TokenAmount, Binary, Expiration, Timestamp, Uint64, AssetInfo, PoolKey, FeeTier, SwapHop, NftExtensionMsg, QueryMsg, MigrateMsg, FeeGrowth, AllNftInfoResponse, OwnerOfResponse, Approval, NftInfoResponse, Position, PositionIncentives, ArrayOfPosition, TokensResponse, ApprovedForAllResponse, Boolean, ArrayOfFeeTier, ArrayOfLiquidityTick, LiquidityTick, Uint32, NumTokensResponse, Pool, IncentiveRecord, ArrayOfPoolWithPoolKey, PoolWithPoolKey, Uint128, ArrayOfAsset, Asset, ArrayOfPositionTick, PositionTick, QuoteResult, Tick, TickIncentive, ArrayOfTupleOfUint16AndUint64} from "./OraiswapV3.types";
import {Addr, Percentage, InstantiateMsg, ExecuteMsg, Liquidity, SqrtPrice, TokenAmount, Binary, Expiration, Timestamp, Uint64, AssetInfo, PoolKey, FeeTier, SwapHop, NftExtensionMsg, QueryMsg, MigrateMsg, FeeGrowth, AllNftInfoResponse, OwnerOfResponse, Approval, NftInfoResponse, Position, PositionIncentives, ArrayOfPosition, TokensResponse, ApprovedForAllResponse, Boolean, ArrayOfFeeTier, ArrayOfLiquidityTick, LiquidityTick, Uint32, NumTokensResponse, Pool, IncentiveRecord, ArrayOfPoolWithPoolKey, PoolWithPoolKey, Uint128, ArrayOfAsset, Asset, ArrayOfPositionTick, PositionTick, QuoteResult, Tick, TickIncentive, ArrayOfTupleOfUint16AndUint64} from "./OraiswapV3.types";
export interface OraiswapV3ReadOnlyInterface {
contractAddress: string;
admin: () => Promise<Addr>;
protocolFee: () => Promise<Percentage>;
incentivesFundManager: () => Promise<Addr>;
position: ({
index,
ownerId
Expand Down Expand Up @@ -205,6 +206,7 @@ export class OraiswapV3QueryClient implements OraiswapV3ReadOnlyInterface {
this.contractAddress = contractAddress;
this.admin = this.admin.bind(this);
this.protocolFee = this.protocolFee.bind(this);
this.incentivesFundManager = this.incentivesFundManager.bind(this);
this.position = this.position.bind(this);
this.positions = this.positions.bind(this);
this.allPosition = this.allPosition.bind(this);
Expand Down Expand Up @@ -243,6 +245,11 @@ export class OraiswapV3QueryClient implements OraiswapV3ReadOnlyInterface {
protocol_fee: {}
});
};
incentivesFundManager = async (): Promise<Addr> => {
return this.client.queryContractSmart(this.contractAddress, {
incentives_fund_manager: {}
});
};
position = async ({
index,
ownerId
Expand Down
5 changes: 4 additions & 1 deletion packages/contracts-sdk/src/OraiswapV3.types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export type Addr = string;
export type Percentage = number;
export interface InstantiateMsg {
incentives_fund_manager: Addr;
protocol_fee: Percentage;
}
export type ExecuteMsg = {
Expand Down Expand Up @@ -132,7 +134,6 @@ export type ExecuteMsg = {
index: number;
};
};
export type Addr = string;
export type Liquidity = string;
export type SqrtPrice = string;
export type TokenAmount = string;
Expand Down Expand Up @@ -180,6 +181,8 @@ export type QueryMsg = {
admin: {};
} | {
protocol_fee: {};
} | {
incentives_fund_manager: {};
} | {
position: {
index: number;
Expand Down
2 changes: 2 additions & 0 deletions packages/contracts-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ export * as OraiswapMixedRouterTypes from "./OraiswapMixedRouter.types";
export * from "./OraiswapMixedRouter.client";
export * as ZapperTypes from "./Zapper.types";
export * from "./Zapper.client";
export * as IncentivesFundManagerTypes from "./IncentivesFundManager.types";
export * from "./IncentivesFundManager.client";
export * from "./types";
6 changes: 3 additions & 3 deletions packages/oraiswap-v3/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@oraichain/oraiswap-v3",
"version": "0.1.20",
"version": "0.1.21",
"main": "build/index.js",
"files": [
"build/"
Expand All @@ -12,7 +12,7 @@
"license": "MIT",
"dependencies": {
"@cosmjs/cosmwasm-stargate": "^0.31.0",
"@oraichain/oraidex-contracts-sdk": "^1.0.52",
"@oraichain/oraidex-common": "^1.1.21"
"@oraichain/oraidex-contracts-sdk": "^1.0.53",
"@oraichain/oraidex-common": "^1.1.23"
}
}
12 changes: 12 additions & 0 deletions packages/oraiswap-v3/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,16 @@ export class RouteNotFoundError extends Error {
constructor(route: string) {
super(`Route not found: ${route}`);
}
}

export class RouteNoLiquidity extends Error {
constructor() {
super(`Route has no liquidity`);
}
}

export class SpamTooManyRequestsError extends Error {
constructor() {
super(`Too many requests`);
}
}
56 changes: 44 additions & 12 deletions packages/oraiswap-v3/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,30 +496,52 @@ export const extractOraidexV3Actions = (routes: RouteResponse[]): ActionRoute[]
return routesArray;
};

export type buildZapInMessageOptions = {
isSingleSide: boolean;
isTokenX: boolean;
};

export const populateMessageZapIn = (
message: ZapInLiquidityResponse,
tokenIn: TokenItemType,
amountIn: string,
amountInToX: bigint,
amountInToY: bigint,
actualAmountXReceived: SmartRouteResponse,
actualAmountYReceived: SmartRouteResponse,
sqrtPrice: bigint,
poolKey: PoolKey,
pool: Pool,
lowerTick: number,
upperTick: number,
slippage: number
slippage: number,
buildZapInMessageOptions?: buildZapInMessageOptions
) => {
message.assetIn = parseAsset(tokenIn, amountIn);
message.amountToX = amountInToX.toString();
message.amountToY = amountInToY.toString();
message.amountX = actualAmountXReceived.returnAmount;
message.amountY = actualAmountYReceived.returnAmount;
message.poolKey = poolKey;
message.sqrtPrice = BigInt(pool.sqrt_price);
message.tickLowerIndex = lowerTick;
message.tickUpperIndex = upperTick;
message.routes = generateMessageSwapOperation([actualAmountXReceived, actualAmountYReceived], slippage);
message.currentSqrtPrice = sqrtPrice;

if (buildZapInMessageOptions) {
if (buildZapInMessageOptions.isTokenX) {
message.amountY = "0";
} else {
message.amountX = "0";
}
message.routes = generateMessageSwapOperation([actualAmountXReceived], slippage);
} else {
message.routes = generateMessageSwapOperation([actualAmountXReceived, actualAmountYReceived], slippage);
}

calculateSwapFee(message);

calculateMinimumLiquidity(
message,
actualAmountXReceived,
actualAmountYReceived,
lowerTick,
upperTick,
sqrtPrice,
slippage,
buildZapInMessageOptions
);
};

export const calculateSwapFee = (message: ZapInLiquidityResponse) => {
Expand All @@ -538,8 +560,18 @@ export const calculateMinimumLiquidity = (
lowerTick: number,
upperTick: number,
sqrtPrice: bigint,
slippage: number
slippage: number,
buildZapInMessageOptions?: buildZapInMessageOptions
) => {
if (buildZapInMessageOptions) {
const res = buildZapInMessageOptions.isTokenX
? getLiquidityByX(BigInt(actualAmountXReceived.returnAmount), lowerTick, upperTick, sqrtPrice, true)
: getLiquidityByY(BigInt(actualAmountYReceived.returnAmount), lowerTick, upperTick, sqrtPrice, true);
const slippageMultiplier = BigInt(Math.floor((100 - slippage) * 1000));
message.minimumLiquidity = res.l ? (BigInt(res.l) * slippageMultiplier) / 100_000n : 0n;
return;
}

const res1 = getLiquidityByX(BigInt(actualAmountXReceived.returnAmount), lowerTick, upperTick, sqrtPrice, true);
const res2 = getLiquidityByY(BigInt(actualAmountYReceived.returnAmount), lowerTick, upperTick, sqrtPrice, true);
message.minimumLiquidity =
Expand Down
Loading

0 comments on commit 4836323

Please sign in to comment.