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

add pool v2 price function #35

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
36 changes: 36 additions & 0 deletions src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ export type GetPoolPriceParams = {
decimalsB?: number;
};

export type GetV2PoolPriceParams = {
pool: PoolV2.State;
decimalsA?: number;
decimalsB?: number;
};

export type GetPoolHistoryParams = PaginationOptions & {
id: string;
};
Expand Down Expand Up @@ -326,6 +332,36 @@ export class BlockfrostAdapter {
);
}

/**
* Get pool price.
* @param {Object} params - The parameters to calculate pool price.
* @param {string} params.pool - The pool we want to get price.
* @param {string} [params.decimalsA] - The decimals of assetA in pool, if undefined then query from Blockfrost.
* @param {string} [params.decimalsB] - The decimals of assetB in pool, if undefined then query from Blockfrost.
* @returns {[string, string]} - Returns a pair of asset A/B price and B/A price, adjusted to decimals.
*/
public async getV2PoolPrice({
pool,
decimalsA,
decimalsB,
}: GetV2PoolPriceParams): Promise<[Big, Big]> {
if (decimalsA === undefined) {
decimalsA = await this.getAssetDecimals(pool.assetA);
}
if (decimalsB === undefined) {
decimalsB = await this.getAssetDecimals(pool.assetB);
}
const adjustedReserveA = Big(pool.reserveA.toString()).div(
Big(10).pow(decimalsA)
);
const adjustedReserveB = Big(pool.reserveB.toString()).div(
Big(10).pow(decimalsB)
);
const priceAB = adjustedReserveA.div(adjustedReserveB);
const priceBA = adjustedReserveB.div(adjustedReserveA);
return [priceAB, priceBA];
}

public async getAllStablePools(): Promise<{
pools: StablePool.State[];
errors: unknown[];
Expand Down
44 changes: 24 additions & 20 deletions src/dex-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export type BulkOrdersOption = {
expiredOptions?: OrderV2.ExpirySetting;
availableUtxos: UTxO[];
composeTx?: Tx;
authorizationMethodType? : OrderV2.AuthorizationMethodType;
authorizationMethodType?: OrderV2.AuthorizationMethodType;
};

export type OrderV2SwapRouting = {
Expand Down Expand Up @@ -167,8 +167,8 @@ export type OrderOptions = (

export type CancelBulkOrdersOptions = {
orderOutRefs: OutRef[];
composeTx? : Tx,
AuthorizationMethodType? : OrderV2.AuthorizationMethodType;
composeTx?: Tx;
AuthorizationMethodType?: OrderV2.AuthorizationMethodType;
};

export class DexV2 {
Expand Down Expand Up @@ -737,8 +737,8 @@ export class DexV2 {
expiredOptions,
availableUtxos,
composeTx,
authorizationMethodType
}: BulkOrdersOption): Promise<TxComplete > {
authorizationMethodType,
}: BulkOrdersOption): Promise<TxComplete> {
// calculate total order value
const totalOrderAssets: Record<string, bigint> = {};
for (const option of orderOptions) {
Expand Down Expand Up @@ -780,16 +780,22 @@ export class DexV2 {
orderAssets["lovelace"] = totalBatcherFee;
}

const senderPaymentCred = this.lucid.utils.getAddressDetails(sender).paymentCredential;
invariant( senderPaymentCred, "sender address payment credentials not found");
const senderPaymentCred =
this.lucid.utils.getAddressDetails(sender).paymentCredential;
invariant(
senderPaymentCred,
"sender address payment credentials not found"
);

const canceller = authorizationMethodType ? {
type: authorizationMethodType,
hash: senderPaymentCred.hash,
} : {
type: OrderV2.AuthorizationMethodType.SIGNATURE,
hash: senderPaymentCred.hash,
};
const canceller = authorizationMethodType
? {
type: authorizationMethodType,
hash: senderPaymentCred.hash,
}
: {
type: OrderV2.AuthorizationMethodType.SIGNATURE,
hash: senderPaymentCred.hash,
};
const orderDatum: OrderV2.Datum = {
canceller: canceller,
refundReceiver: sender,
Expand Down Expand Up @@ -834,17 +840,15 @@ export class DexV2 {
limitOrders: limitOrderMessage,
});
lucidTx.payToAddress(sender, reductionAssets);
if (composeTx){
if (composeTx) {
lucidTx.compose(composeTx);
}
return lucidTx.complete();

}

async cancelOrder({
orderOutRefs,
composeTx,

}: CancelBulkOrdersOptions): Promise<TxComplete> {
const v2OrderScriptHash = this.getOrderScriptHash();
const orderUtxos = await this.lucid.utxosByOutRef(orderOutRefs);
Expand Down Expand Up @@ -890,8 +894,8 @@ export class DexV2 {
);
}

if(datum.canceller.type === OrderV2.AuthorizationMethodType.SIGNATURE)
requiredPubKeyHashSet.add(datum.canceller.hash);
if (datum.canceller.type === OrderV2.AuthorizationMethodType.SIGNATURE)
requiredPubKeyHashSet.add(datum.canceller.hash);
}
const redeemer = Data.to(
new Constr(OrderV2.Redeemer.CANCEL_ORDER_BY_OWNER, [])
Expand All @@ -904,7 +908,7 @@ export class DexV2 {
lucidTx.attachMetadata(674, {
msg: [MetadataMessage.CANCEL_ORDER],
});
if (composeTx){
if (composeTx) {
lucidTx.compose(composeTx);
}
return lucidTx.complete();
Expand Down