Skip to content

Commit

Permalink
Merge pull request #78 from Hipo/main
Browse files Browse the repository at this point in the history
v3.0.0
  • Loading branch information
jamcry authored Apr 13, 2023
2 parents 6312b31 + bd02049 commit e7b968b
Show file tree
Hide file tree
Showing 49 changed files with 1,732 additions and 540 deletions.
10 changes: 8 additions & 2 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import SwapQuoteError from "./util/error/SwapQuoteError";
export type { InitiatorSigner, SignerTransaction, SupportedNetwork } from "./util/commonTypes";
export { BASE_MINIMUM_BALANCE, MINIMUM_BALANCE_REQUIRED_PER_ASSET, MINIMUM_BALANCE_REQUIRED_PER_APP, MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA, MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE, MINIMUM_ADD_LIQUIDITY_AMOUNT } from "./util/constant";
export * from "./swap/v2/router";
export * from "./swap/common/utils";
export { applySlippageToAmount, ASSET_OPT_IN_PROCESS_TXN_COUNT, convertFromBaseUnits, convertToBaseUnits, sendAndWaitRawTransaction, getTxnGroupID, sumUpTxnFees } from "./util/util";
export { generateOptIntoAssetTxns } from "./util/asset/assetUtils";
export type { AccountAsset, TinymanAnalyticsApiAsset, IndexerAssetInformation } from "./util/asset/assetModels";
Expand All @@ -26,10 +29,13 @@ export type { V2RemoveLiquidityQuote, V2SingleAssetRemoveLiquidityQuote, V2Remov
export { V1_1_REMOVE_LIQUIDITY_TXN_COUNT } from "./remove-liquidity/v1_1/constants";
export { V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT } from "./remove-liquidity/v2/constants";
export { RemoveLiquidity } from "./remove-liquidity";
export type { SwapQuote, SwapQuoteWithPool, V1SwapExecution, V2SwapExecution } from "./swap/types";
export { getSwapTotalFee } from "./swap/utils";
export type { SwapQuote, V1SwapExecution, V2SwapExecution, DirectSwapQuote, SwapRoute, GenerateSwapTxnsParams } from "./swap/types";
export * from "./swap/v2/util";
export { SwapType } from "./swap/constants";
export { Swap } from "./swap";
export { SwapQuoteType } from "./swap/types";
export { SwapQuoteError };
export { SwapQuoteErrorType } from "./util/error/SwapQuoteError";
export { redeemExcessAsset, redeemAllExcessAsset, generateRedeemTxns, REDEEM_PROCESS_TXN_COUNT } from "./redeem";
export { prepareCommitTransactions, getStakingAppID } from "./stake";
export { tinymanJSSDKConfig } from "./config";
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

35 changes: 33 additions & 2 deletions dist/swap/common/utils.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { AssetWithAmountAndDecimals } from "../../util/asset/assetModels";
import { CONTRACT_VERSION } from "../../contract/constants";
import { ContractVersionValue } from "../../contract/types";
import { AssetWithAmountAndDecimals, AssetWithIdAndAmount } from "../../util/asset/assetModels";
import { SwapType } from "../constants";
import { SwapQuote } from "../types";
declare function calculateSwapRate({ assetIn, assetOut }: {
assetIn: AssetWithAmountAndDecimals;
assetOut: AssetWithAmountAndDecimals;
Expand All @@ -9,4 +13,31 @@ declare function calculatePriceImpact({ inputSupply, outputSupply, assetIn, asse
assetIn: AssetWithAmountAndDecimals;
assetOut: AssetWithAmountAndDecimals;
}): number;
export { calculateSwapRate, calculatePriceImpact };
declare function getSwapQuotePriceImpact(quote: SwapQuote): number;
declare function getAssetInFromSwapQuote(quote: SwapQuote): AssetWithIdAndAmount;
declare function getAssetOutFromSwapQuote(quote: SwapQuote): AssetWithIdAndAmount;
declare function getAssetInAndAssetOutFromSwapQuote(quote: SwapQuote): {
assetIn: AssetWithIdAndAmount;
assetOut: AssetWithIdAndAmount;
};
declare function getSwapQuoteContractVersion(quote: SwapQuote): ContractVersionValue;
/**
* @returns the total fee that will be paid by the user
* for the swap transaction with given parameters
*/
declare function getSwapTotalFee(params: {
version: typeof CONTRACT_VERSION.V1_1;
} | {
version: typeof CONTRACT_VERSION.V2;
type: SwapType;
}): number;
/**
* @returns The asset amount ratio for the given quote
*/
declare function getSwapQuoteRate(quote: SwapQuote): number;
/**
* Compares the given quotes and returns the best one (with the highest rate).
*/
declare function getBestQuote(quotes: SwapQuote[]): SwapQuote;
declare function isSwapQuoteErrorCausedByAmount(error: Error): boolean;
export { calculateSwapRate, calculatePriceImpact, getSwapQuotePriceImpact, getAssetInFromSwapQuote, getAssetOutFromSwapQuote, getAssetInAndAssetOutFromSwapQuote, getSwapQuoteContractVersion, getSwapTotalFee, getSwapQuoteRate, getBestQuote, isSwapQuoteErrorCausedByAmount };
1 change: 1 addition & 0 deletions dist/swap/constants.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export declare enum SwapType {
FixedInput = "fixed-input",
FixedOutput = "fixed-output"
}
export declare const DEFAULT_SWAP_FEE_RATE = 0.003;
79 changes: 39 additions & 40 deletions dist/swap/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,7 @@ export declare const Swap: {
assetOut: number;
};
}) => import("./types").SwapQuote;
generateTxns: ({ client, pool, swapType, assetIn, assetOut, slippage, initiatorAddr }: {
client: import("algosdk").Algodv2;
pool: import("..").V1PoolInfo;
swapType: import("./constants").SwapType;
assetIn: import("../util/asset/assetModels").AssetWithIdAndAmount;
assetOut: import("../util/asset/assetModels").AssetWithIdAndAmount;
slippage: number;
initiatorAddr: string;
}) => Promise<import("..").SignerTransaction[]>;
generateTxns: ({ client, quoteAndPool, swapType, slippage, initiatorAddr }: import("./types").GenerateV1_1SwapTxnsParams) => Promise<import("..").SignerTransaction[]>;
signTxns: ({ pool, txGroup, initiatorSigner }: {
pool: import("..").V1PoolInfo;
txGroup: import("..").SignerTransaction[];
Expand All @@ -55,46 +47,53 @@ export declare const Swap: {
}) => Promise<Omit<import("./types").V1SwapExecution, "fees" | "groupID">>;
};
v2: {
getQuote: (type: import("./constants").SwapType, pool: import("..").V2PoolInfo, asset: import("../util/asset/assetModels").AssetWithIdAndAmount, decimals: {
assetIn: number;
assetOut: number;
}) => import("./types").SwapQuote;
getFixedInputSwapQuote: ({ pool, assetIn, decimals }: {
pool: import("..").V2PoolInfo;
assetIn: import("../util/asset/assetModels").AssetWithIdAndAmount;
decimals: {
assetIn: number;
assetOut: number;
};
}) => import("./types").SwapQuote;
getFixedOutputSwapQuote: ({ pool, assetOut, decimals }: {
getQuote: ({ type, amount, assetIn, assetOut, network, isSwapRouterEnabled, pool }: {
type: import("./constants").SwapType;
amount: number | bigint;
assetIn: import("../util/asset/assetModels").AssetWithIdAndDecimals;
assetOut: import("../util/asset/assetModels").AssetWithIdAndDecimals;
pool: import("..").V2PoolInfo | null;
network: import("..").SupportedNetwork;
isSwapRouterEnabled?: boolean | undefined;
}) => Promise<import("./types").SwapQuote>;
getFixedInputSwapQuote: ({ amount, assetIn, assetOut, isSwapRouterEnabled, network, pool }: {
amount: number | bigint;
assetIn: import("../util/asset/assetModels").AssetWithIdAndDecimals;
assetOut: import("../util/asset/assetModels").AssetWithIdAndDecimals;
network: import("..").SupportedNetwork;
pool: import("..").V2PoolInfo | null;
isSwapRouterEnabled?: boolean | undefined;
}) => Promise<import("./types").SwapQuote>;
getFixedInputDirectSwapQuote: ({ amount, assetIn, assetOut, pool }: {
pool: import("..").V2PoolInfo;
assetOut: import("../util/asset/assetModels").AssetWithIdAndAmount;
decimals: {
assetIn: number;
assetOut: number;
};
amount: number | bigint;
assetIn: import("../util/asset/assetModels").AssetWithIdAndDecimals;
assetOut: import("../util/asset/assetModels").AssetWithIdAndDecimals;
}) => import("./types").DirectSwapQuote;
getFixedOutputDirectSwapQuote: ({ amount, assetIn, assetOut, pool }: {
pool: import("..").V2PoolInfo | null;
amount: number | bigint;
assetIn: import("../util/asset/assetModels").AssetWithIdAndDecimals;
assetOut: import("../util/asset/assetModels").AssetWithIdAndDecimals;
}) => import("./types").SwapQuote;
generateTxns: ({ client, pool, swapType, assetIn, assetOut, initiatorAddr, slippage }: {
client: import("algosdk").Algodv2;
pool: import("..").V2PoolInfo;
swapType: import("./constants").SwapType;
assetIn: import("../util/asset/assetModels").AssetWithIdAndAmount;
assetOut: import("../util/asset/assetModels").AssetWithIdAndAmount;
initiatorAddr: string;
slippage: number;
}) => Promise<import("..").SignerTransaction[]>;
getFixedOutputSwapQuote: ({ amount, assetIn, assetOut, isSwapRouterEnabled, network, pool }: {
amount: number | bigint;
assetIn: import("../util/asset/assetModels").AssetWithIdAndDecimals;
assetOut: import("../util/asset/assetModels").AssetWithIdAndDecimals;
pool: import("..").V2PoolInfo | null;
network: import("..").SupportedNetwork;
isSwapRouterEnabled?: boolean | undefined;
}) => Promise<import("./types").SwapQuote>;
generateTxns: (params: import("./types").GenerateSwapTxnsParams) => Promise<import("..").SignerTransaction[]>;
signTxns: ({ txGroup, initiatorSigner }: {
txGroup: import("..").SignerTransaction[];
initiatorSigner: import("..").InitiatorSigner;
}) => Promise<Uint8Array[]>;
execute: ({ client, pool, txGroup, signedTxns, network, assetIn }: {
execute: ({ client, quote, txGroup, signedTxns }: {
client: import("algosdk").Algodv2;
pool: import("..").V2PoolInfo;
network: import("..").SupportedNetwork;
quote: import("./types").SwapQuote;
txGroup: import("..").SignerTransaction[];
signedTxns: Uint8Array[];
assetIn: import("../util/asset/assetModels").AssetWithIdAndAmount;
}) => Promise<import("./types").V2SwapExecution>;
calculateFixedInputSwap: ({ inputSupply, outputSupply, swapInputAmount, totalFeeShare, decimals }: {
inputSupply: bigint;
Expand Down
107 changes: 97 additions & 10 deletions dist/swap/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { AssetWithIdAndAmount } from "../util/asset/assetModels";
import { Algodv2 } from "algosdk";
import { AssetWithIdAndAmount, TinymanAnalyticsApiAsset } from "../util/asset/assetModels";
import { SignerTransaction, SupportedNetwork } from "../util/commonTypes";
import { PoolReserves, V1PoolInfo, V2PoolInfo } from "../util/pool/poolTypes";
/** An object containing information about a swap quote. */
export interface SwapQuote {
import { SwapType } from "./constants";
export declare enum SwapQuoteType {
Direct = "direct",
Router = "router"
}
export interface DirectSwapQuote {
/** The ID of the input asset in this quote. */
assetInID: number;
/** The quantity of the input asset in this quote. */
Expand All @@ -19,6 +25,89 @@ export interface SwapQuote {
/** The round that this quote is based on. */
round?: number;
}
export interface DirectSwapQuoteAndPool {
quote: DirectSwapQuote;
pool: V1PoolInfo | V2PoolInfo;
}
export interface SwapRouteAsset {
id: string;
name: string;
unit_name: string;
decimals: number;
}
export interface SwapRoutePool {
address: string;
asset_1: SwapRouteAsset;
asset_2: SwapRouteAsset;
version: "2.0";
}
export type SwapRoute = {
quote: SwapRouterQuote;
pool: SwapRoutePool;
}[];
export interface SwapRouterQuote {
swap_type: SwapType;
amount_in: {
asset: SwapRouteAsset;
amount: string;
};
amount_out: {
asset: SwapRouteAsset;
amount: string;
};
swap_fees: {
amount: string;
asset: SwapRouteAsset;
};
price: number;
price_impact: number;
}
export interface FetchSwapRouteQuotesPayload {
asset_in_id: string;
asset_out_id: string;
amount: string;
swap_type: SwapType;
}
export type SwapRouterResponse = FetchSwapRouteQuotesPayload & {
route: SwapRoute;
price_impact: string;
status: {
round_number: string;
round_datetime: string;
};
};
export type GetSwapQuoteParams = {
assetIn: Pick<TinymanAnalyticsApiAsset, "id" | "decimals">;
assetOut: Pick<TinymanAnalyticsApiAsset, "id" | "decimals">;
pools: {
info: V1PoolInfo | V2PoolInfo;
reserves: PoolReserves;
}[];
amount: number | bigint;
type: SwapType;
network: SupportedNetwork;
/** If `true`, the function will also check the quotes that use swap route */
isSwapRouterEnabled?: boolean;
};
export type SwapQuote = {
data: DirectSwapQuoteAndPool;
type: SwapQuoteType.Direct;
} | {
data: SwapRouterResponse;
type: SwapQuoteType.Router;
};
export type GetSwapQuoteBySwapTypeParams = Omit<GetSwapQuoteParams, "type">;
export interface GenerateSwapTxnsParams {
client: Algodv2;
network: SupportedNetwork;
quote: SwapQuote;
swapType: SwapType;
slippage: number;
initiatorAddr: string;
}
export type GenerateV1_1SwapTxnsParams = Omit<GenerateSwapTxnsParams, "quote" | "network"> & {
quoteAndPool: DirectSwapQuoteAndPool;
};
/** An object containing information about a successfully executed swap. */
export interface V1SwapExecution {
/** The round that the swap occurred in. */
Expand Down Expand Up @@ -54,14 +143,12 @@ export interface V2SwapExecution {
/** Can be `undefined` if the execution was successful, but there was an issue while
* extracting the output asset data from the transaction response */
assetOut: AssetWithIdAndAmount | undefined;
pool: V2PoolInfo;
quote: SwapQuote;
txnID: string;
round: number;
}
export interface SwapQuoteWithPool {
quote: SwapQuote;
pool: {
info: V1PoolInfo | V2PoolInfo;
reserves: PoolReserves;
};
export interface ExecuteSwapCommonParams {
client: Algodv2;
txGroup: SignerTransaction[];
signedTxns: Uint8Array[];
}
Loading

0 comments on commit e7b968b

Please sign in to comment.