-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1341 from madfish-solutions/QUIPU-831-implement-3…
…-route-for-swaps Implement swaps with 3route
- Loading branch information
Showing
53 changed files
with
1,110 additions
and
277 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,9 @@ REACT_APP_POOLS_URL=wss://dexes-api-mainnet.prod.templewallet.com/ | |
REACT_APP_FARMING_API_URL=https://staking-api-mainnet.prod.quipuswap.com | ||
REACT_APP_STABLESWAP_API_URL=https://stableswap-api-mainnet.prod.quipuswap.com | ||
REACT_APP_LIQUIDITY_API_URL=https://liquidity-api-mainnet.prod.quipuswap.com | ||
REACT_APP_THREE_ROUTE_API_URL=https://quipuswap.3route.io | ||
REACT_APP_THREE_ROUTE_API_AUTH_TOKEN=UXVpcHVzd2FwOhXazKpW5//4RC+Oa9TdpQrmLOwl284os85upVGa65uA | ||
REACT_APP_THREE_ROUTE_CONTRACT_ADDRESS=KT1Tuta6vbpHhZ15ixsYD3qJdhnpEAuogLQ9 | ||
|
||
#sentry | ||
REACT_APP_SENTRY_DSN=https://[email protected]/6676545 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './three-route'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import BigNumber from 'bignumber.js'; | ||
|
||
import { THREE_ROUTE_API_AUTH_TOKEN, THREE_ROUTE_API_URL } from '@config/environment'; | ||
|
||
import { ThreeRouteDex, ThreeRouteSwapResponse, ThreeRouteToken } from '../../types'; | ||
|
||
export class ThreeRouteBackendApi { | ||
private static NUMBER_DISCRIMINATION_PREFIX = 'uniqueprefix'; | ||
|
||
private static jsonWithBigNumberParser(origJSON: string): ReturnType<typeof JSON['parse']> { | ||
const stringedJSON = origJSON.replace( | ||
/:\s*([-+Ee0-9.]+)/g, | ||
`: "${ThreeRouteBackendApi.NUMBER_DISCRIMINATION_PREFIX}$1"` | ||
); | ||
|
||
return JSON.parse(stringedJSON, (_, value) => { | ||
if (typeof value !== 'string' || !value.startsWith(ThreeRouteBackendApi.NUMBER_DISCRIMINATION_PREFIX)) { | ||
return value; | ||
} | ||
|
||
value = value.slice('uniqueprefix'.length); | ||
|
||
return new BigNumber(value); | ||
}); | ||
} | ||
|
||
private static async getThreeRouteResponse(path: string) { | ||
const response = await fetch(`${THREE_ROUTE_API_URL}${path}`, { | ||
headers: { Authorization: `Basic ${THREE_ROUTE_API_AUTH_TOKEN}` } | ||
}); | ||
const rawJSON = await response.text(); | ||
|
||
return ThreeRouteBackendApi.jsonWithBigNumberParser(rawJSON); | ||
} | ||
|
||
static async getSwap( | ||
inputTokenSymbol: string, | ||
outputTokenSymbol: string, | ||
realAmount: BigNumber | ||
): Promise<ThreeRouteSwapResponse> { | ||
return await ThreeRouteBackendApi.getThreeRouteResponse( | ||
`/swap/${inputTokenSymbol}/${outputTokenSymbol}/${realAmount.toFixed()}` | ||
); | ||
} | ||
|
||
static async getTokens(): Promise<ThreeRouteToken[]> { | ||
return await ThreeRouteBackendApi.getThreeRouteResponse('/tokens'); | ||
} | ||
|
||
static async getDexes(): Promise<ThreeRouteDex[]> { | ||
return await ThreeRouteBackendApi.getThreeRouteResponse('/dexes'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './no-mediators-swap'; | ||
export * from './three-route'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { TezosToolkit } from '@taquito/taquito'; | ||
import { BigNumber } from 'bignumber.js'; | ||
import { getTradeOpParams, parseTransferParamsToParamsWithKind, Trade } from 'swap-router-sdk'; | ||
|
||
import { STABLESWAP_REFERRAL } from '@config/config'; | ||
import { QUIPUSWAP_REFERRAL_CODE } from '@config/constants'; | ||
import { estimateFee } from '@shared/api'; | ||
|
||
export class NoMediatorsSwapBlockchainApi { | ||
static async getSwapTransferParams( | ||
tezos: TezosToolkit, | ||
accountPkh: string, | ||
trade: Trade, | ||
recipientPkh = accountPkh, | ||
deadlineTimespan?: BigNumber | ||
) { | ||
return await getTradeOpParams( | ||
trade, | ||
accountPkh, | ||
tezos, | ||
STABLESWAP_REFERRAL, | ||
recipientPkh, | ||
deadlineTimespan?.toNumber(), | ||
QUIPUSWAP_REFERRAL_CODE.toNumber() | ||
); | ||
} | ||
|
||
static async estimateSwapFee( | ||
tezos: TezosToolkit, | ||
accountPkh: string, | ||
trade: Trade, | ||
recipientPkh = accountPkh, | ||
deadlineTimespan?: BigNumber | ||
) { | ||
return await estimateFee( | ||
tezos, | ||
accountPkh, | ||
await NoMediatorsSwapBlockchainApi.getSwapTransferParams(tezos, accountPkh, trade, recipientPkh, deadlineTimespan) | ||
); | ||
} | ||
|
||
static async doSwap( | ||
tezos: TezosToolkit, | ||
accountPkh: string, | ||
trade: Trade, | ||
recipientPkh = accountPkh, | ||
deadlineTimespan?: BigNumber | ||
) { | ||
const tradeTransferParams = await NoMediatorsSwapBlockchainApi.getSwapTransferParams( | ||
tezos, | ||
accountPkh, | ||
trade, | ||
recipientPkh, | ||
deadlineTimespan | ||
); | ||
|
||
const walletParamsWithKind = tradeTransferParams.map(tradeTransferParam => | ||
parseTransferParamsToParamsWithKind(tradeTransferParam) | ||
); | ||
|
||
return await tezos.wallet.batch(walletParamsWithKind).send(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { TezosToolkit } from '@taquito/taquito'; | ||
import { BigNumber } from 'bignumber.js'; | ||
import { parseTransferParamsToParamsWithKind } from 'swap-router-sdk'; | ||
|
||
import { getApproveParams } from '@blockchain'; | ||
import { THREE_ROUTE_APP_ID } from '@config/config'; | ||
import { FIRST_INDEX, ZERO_AMOUNT } from '@config/constants'; | ||
import { THREE_ROUTE_CONTRACT_ADDRESS } from '@config/environment'; | ||
import { threeRouteTokenMatches } from '@modules/swap/helpers'; | ||
import { ThreeRouteSwapResponse, ThreeRouteToken } from '@modules/swap/types'; | ||
import { estimateFee } from '@shared/api'; | ||
import { getContract } from '@shared/dapp/get-storage-info'; | ||
import { decreaseByPercentage, toAtomic, isTezosToken } from '@shared/helpers'; | ||
import { Token } from '@shared/types'; | ||
|
||
export class ThreeRouteBlockchainApi { | ||
static async getSwapTransferParams( | ||
tezos: TezosToolkit, | ||
accountPkh: string, | ||
receiver: string, | ||
inputToken: Token, | ||
outputToken: Token, | ||
threeRouteTokens: ThreeRouteToken[], | ||
swap: ThreeRouteSwapResponse, | ||
slippageTolerance: BigNumber | ||
) { | ||
const threeRouteContract = await getContract(tezos, THREE_ROUTE_CONTRACT_ADDRESS!); | ||
const [threeRouteInputToken, threeRouteOutputToken] = [inputToken, outputToken].map(token => | ||
threeRouteTokens.find(threeRouteToken => threeRouteTokenMatches(threeRouteToken, token)) | ||
); | ||
const atomicInputAmount = toAtomic(swap.input, inputToken); | ||
|
||
const hops = swap.chains | ||
.map(chain => | ||
chain.hops.map(({ dex: dex_id, forward }, index) => ({ | ||
// eslint-disable-next-line @typescript-eslint/no-magic-numbers | ||
code: (index === FIRST_INDEX ? 1 : 0) + (forward ? 2 : 0), | ||
dex_id, | ||
amount_opt: index === FIRST_INDEX ? toAtomic(chain.input, inputToken) : null | ||
})) | ||
) | ||
.flat(); | ||
const tezAmount = isTezosToken(inputToken) ? swap.input.toNumber() : ZERO_AMOUNT; | ||
|
||
return getApproveParams(tezos, THREE_ROUTE_CONTRACT_ADDRESS!, inputToken, accountPkh, atomicInputAmount, [ | ||
threeRouteContract.methods | ||
.execute( | ||
threeRouteInputToken!.id, | ||
threeRouteOutputToken!.id, | ||
toAtomic(decreaseByPercentage(swap.output, slippageTolerance), outputToken).integerValue( | ||
BigNumber.ROUND_DOWN | ||
), | ||
receiver, | ||
hops, | ||
THREE_ROUTE_APP_ID | ||
) | ||
.toTransferParams({ amount: tezAmount, mutez: false }) | ||
]); | ||
} | ||
|
||
static async estimateSwapFee( | ||
tezos: TezosToolkit, | ||
accountPkh: string, | ||
receiver: string, | ||
inputToken: Token, | ||
outputToken: Token, | ||
threeRouteTokens: ThreeRouteToken[], | ||
swap: ThreeRouteSwapResponse, | ||
slippageTolerance: BigNumber | ||
) { | ||
return await estimateFee( | ||
tezos, | ||
accountPkh, | ||
await ThreeRouteBlockchainApi.getSwapTransferParams( | ||
tezos, | ||
accountPkh, | ||
receiver, | ||
inputToken, | ||
outputToken, | ||
threeRouteTokens, | ||
swap, | ||
slippageTolerance | ||
) | ||
); | ||
} | ||
|
||
static async doSwap( | ||
tezos: TezosToolkit, | ||
accountPkh: string, | ||
receiver: string, | ||
inputToken: Token, | ||
outputToken: Token, | ||
threeRouteTokens: ThreeRouteToken[], | ||
swap: ThreeRouteSwapResponse, | ||
slippageTolerance: BigNumber | ||
) { | ||
const tradeTransferParams = await ThreeRouteBlockchainApi.getSwapTransferParams( | ||
tezos, | ||
accountPkh, | ||
receiver, | ||
inputToken, | ||
outputToken, | ||
threeRouteTokens, | ||
swap, | ||
slippageTolerance | ||
); | ||
|
||
const walletParamsWithKind = tradeTransferParams.map(tradeTransferParam => | ||
parseTransferParamsToParamsWithKind(tradeTransferParam) | ||
); | ||
|
||
return await tezos.wallet.batch(walletParamsWithKind).send(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './backend'; | ||
export * from './blockchain'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export * from './three-route-swap-chain.dto'; | ||
export * from './three-route-hop.dto'; | ||
export * from './three-route-swap-response.dto'; | ||
export * from './three-route-token.dto'; | ||
export * from './three-route-tokens-response.dto'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import BigNumber from 'bignumber.js'; | ||
|
||
import { Typed } from '@shared/decorators'; | ||
|
||
export class ThreeRouteHopDto { | ||
@Typed() | ||
dex: BigNumber; | ||
|
||
@Typed() | ||
forward: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import BigNumber from 'bignumber.js'; | ||
|
||
import { Typed } from '@shared/decorators'; | ||
|
||
import { ThreeRouteHopDto } from './three-route-hop.dto'; | ||
|
||
export class ThreeRouteSwapChainDto { | ||
@Typed() | ||
input: BigNumber; | ||
|
||
@Typed() | ||
output: BigNumber; | ||
|
||
@Typed({ type: ThreeRouteHopDto, isArray: true }) | ||
hops: ThreeRouteHopDto[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import BigNumber from 'bignumber.js'; | ||
|
||
import { Typed } from '@shared/decorators'; | ||
|
||
import { ThreeRouteSwapChainDto } from './three-route-swap-chain.dto'; | ||
|
||
export class ThreeRouteSwapResponseDto { | ||
@Typed() | ||
input: BigNumber; | ||
|
||
@Typed() | ||
output: BigNumber; | ||
|
||
@Typed({ type: ThreeRouteSwapChainDto, isArray: true }) | ||
chains: ThreeRouteSwapChainDto[]; | ||
} |
Oops, something went wrong.