Skip to content

Commit

Permalink
feat: add swap portal
Browse files Browse the repository at this point in the history
  • Loading branch information
AnshuJalan committed Sep 7, 2023
1 parent 2ea4f70 commit 8a51272
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 7 deletions.
92 changes: 92 additions & 0 deletions src/swapPortal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import BigNumber from "bignumber.js";
import { TransferParams } from "@taquito/taquito";

import { Contract } from "./types";

export interface SwapXToYOptions {
/**
* Amount of token x being swapped for token y
*/
tokenXIn: BigNumber;

/**
* Operation reverts if amount of token y received is less than this number
*/
minTokenYOut: BigNumber;

/**
* Operation reverts beyond this timestamp
* Default value is 15 minutes from current time
*/
deadline?: Date;

/**
* Tezos address to which the output tokens should be sent
*/
recepient: string;
}

export interface SwapYToXOptions {
/**
* Amount of token y being swapped for token x
*/
tokenYIn: BigNumber;

/**
* Operation reverts if amount of token x received is less than this number
*/
minTokenXOut: BigNumber;

/**
* Operation reverts beyond this timestamp
* Default value is 15 minutes from current time
*/
deadline?: Date;

/**
* Tezos address to which the output tokens should be sent
*/
recepient: string;
}

export abstract class SwapPortal {
/**
* Builds transaction params for perform a swap from token x to token y
* @param pool A taquito contract instance of the pool through which the swap happens
* @param options Options for performing the token x to y swap
*/
static swapXToY(pool: Contract, options: SwapXToYOptions): TransferParams {
if (!options.deadline) {
options.deadline = new Date(Date.now() + 900000); // 15 minutes
}

return pool.methodsObject
.x_to_y({
dx: options.tokenXIn,
deadline: Math.floor(options.deadline.getTime() / 1000),
min_dy: options.minTokenYOut,
to_dy: options.recepient,
})
.toTransferParams();
}

/**
* Builds transaction params for perform a swap from token y to token x
* @param pool A taquito contract instance of the pool through which the swap happens
* @param options Options for performing the token y to x swap
*/
static swapYToX(pool: Contract, options: SwapYToXOptions): TransferParams {
if (!options.deadline) {
options.deadline = new Date(Date.now() + 900000); // 15 minutes
}

return pool.methodsObject
.y_to_x({
dy: options.tokenYIn,
deadline: Math.floor(options.deadline.getTime() / 1000),
min_dx: options.minTokenXOut,
to_dx: options.recepient,
})
.toTransferParams();
}
}
18 changes: 11 additions & 7 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ export abstract class Helpers {
* Orders given tokens following the criteria specified in the factory during deployment of a pair.
*/
static orderTokens(tokenX: Token, tokenY: Token): [Token, Token] {
return tokenX.address === tokenY.address
? (tokenX.tokenId ?? 0) < (tokenY.tokenId ?? 0)
? [tokenX, tokenY]
: [tokenY, tokenX]
: tokenX.address < tokenY.address
? [tokenX, tokenY]
: [tokenY, tokenX];
let [tx, ty] = [tokenX, tokenY];

const yIsFa12 = !tokenY.tokenId && tokenX.tokenId;
const yAddressIsSmaller = tokenY.address < tokenX.address;
const yTokenIdIsSmaller = tokenX.address === tokenY.address && (tokenY.tokenId ?? 0) < (tokenX.tokenId ?? 0);

if (yIsFa12 || yAddressIsSmaller || yTokenIdIsSmaller) {
[tx, ty] = [tokenY, tokenX];
}

return [tx, ty];
}
}

0 comments on commit 8a51272

Please sign in to comment.