@swapkit/plugins 0.0.0-nightly-20250304130539
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chainflip/index.cjs +3 -0
- package/dist/chainflip/index.cjs.map +11 -0
- package/dist/chainflip/index.js +3 -0
- package/dist/chainflip/index.js.map +11 -0
- package/dist/chunk-fazw0jvt.js +3 -0
- package/dist/chunk-fazw0jvt.js.map +9 -0
- package/dist/chunk-tvrdndbw.js +4 -0
- package/dist/chunk-tvrdndbw.js.map +9 -0
- package/dist/evm/index.cjs +3 -0
- package/dist/evm/index.cjs.map +10 -0
- package/dist/evm/index.js +3 -0
- package/dist/evm/index.js.map +10 -0
- package/dist/index.cjs +3 -0
- package/dist/index.cjs.map +9 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +9 -0
- package/dist/kado/index.cjs +20 -0
- package/dist/kado/index.cjs.map +12 -0
- package/dist/kado/index.js +20 -0
- package/dist/kado/index.js.map +12 -0
- package/dist/radix/index.cjs +3 -0
- package/dist/radix/index.cjs.map +10 -0
- package/dist/radix/index.js +3 -0
- package/dist/radix/index.js.map +10 -0
- package/dist/thorchain/index.cjs +3 -0
- package/dist/thorchain/index.cjs.map +11 -0
- package/dist/thorchain/index.js +3 -0
- package/dist/thorchain/index.js.map +11 -0
- package/package.json +64 -0
- package/src/chainflip/broker.ts +127 -0
- package/src/chainflip/index.ts +3 -0
- package/src/chainflip/plugin.ts +73 -0
- package/src/chainflip/types.ts +48 -0
- package/src/evm/index.ts +82 -0
- package/src/index.ts +17 -0
- package/src/kado/helpers.ts +117 -0
- package/src/kado/index.ts +3 -0
- package/src/kado/plugin.ts +234 -0
- package/src/kado/types.ts +225 -0
- package/src/radix/index.ts +36 -0
- package/src/thorchain/index.ts +3 -0
- package/src/thorchain/plugin.ts +492 -0
- package/src/thorchain/shared.ts +23 -0
- package/src/thorchain/types.ts +83 -0
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/thorchain/plugin.ts", "../src/thorchain/shared.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import {\n ApproveMode,\n type ApproveReturnType,\n AssetValue,\n Chain,\n type CryptoChain,\n type EVMChain,\n EVMChains,\n type ErrorKeys,\n FeeOption,\n MemoType,\n ProviderName,\n SwapKitError,\n type SwapKitPluginParams,\n type SwapParams,\n createPlugin,\n getMemoForDeposit,\n getMemoForLeaveAndBond,\n getMemoForNamePreferredAssetRegister,\n getMemoForNameRegister,\n getMemoForSaverDeposit,\n getMemoForSaverWithdraw,\n getMemoForUnbond,\n getMemoForWithdraw,\n getMinAmountByChain,\n wrapWithThrow,\n} from \"@swapkit/helpers\";\nimport { type QuoteResponseRoute, SwapKitApi, type THORNodeType } from \"@swapkit/helpers/api\";\nimport {\n MayaArbitrumVaultAbi,\n MayaEthereumVaultAbi,\n TCAvalancheDepositABI,\n TCBaseDepositABI,\n TCBscDepositABI,\n TCEthereumVaultAbi,\n} from \"@swapkit/helpers/contracts\";\n\nimport { prepareTxParams, validateAddressType } from \"./shared\";\nimport type {\n AddLiquidityParams,\n AddLiquidityPartParams,\n ApproveParams,\n CoreTxParams,\n CreateLiquidityParams,\n NodeActionParams,\n RegisterThornameParams,\n SavingsParams,\n WithdrawParams,\n} from \"./types\";\n\nconst gasFeeMultiplier: Record<FeeOption, number> = {\n [FeeOption.Average]: 1.2,\n [FeeOption.Fast]: 1.5,\n [FeeOption.Fastest]: 2,\n};\n\nconst TCSpecificAbi = {\n [Chain.Avalanche]: TCAvalancheDepositABI,\n [Chain.Base]: TCBaseDepositABI,\n [Chain.BinanceSmartChain]: TCBscDepositABI,\n [Chain.Ethereum]: TCEthereumVaultAbi,\n};\n\nconst MayaSpecificAbi = {\n [Chain.Arbitrum]: MayaArbitrumVaultAbi,\n [Chain.Ethereum]: MayaEthereumVaultAbi,\n};\n\nexport const ThorchainPlugin = createPlugin({\n name: \"thorchain\",\n methods: createTCBasedPlugin(Chain.THORChain),\n properties: {\n supportedSwapkitProviders: [ProviderName.THORCHAIN, ProviderName.THORCHAIN_STREAMING],\n },\n});\n\nexport const MayachainPlugin = createPlugin({\n name: \"mayachain\",\n methods: createTCBasedPlugin(Chain.Maya),\n properties: {\n supportedSwapkitProviders: [ProviderName.MAYACHAIN],\n },\n});\n\nfunction getInboundDataFunction(type?: THORNodeType) {\n return async function getInboundDataByChain<T extends Chain>(chain: T) {\n if (\n (type === \"thorchain\" && chain === Chain.THORChain) ||\n (type === \"mayachain\" && chain === Chain.Maya)\n ) {\n return { gas_rate: \"0\", router: \"\", address: \"\", halted: false, chain };\n }\n\n const inboundData = await SwapKitApi.thornode.getInboundAddresses(type);\n const chainAddressData = inboundData.find((item) => item.chain === chain);\n\n if (!chainAddressData) throw new SwapKitError(\"core_inbound_data_not_found\");\n if (chainAddressData?.halted) throw new SwapKitError(\"core_chain_halted\");\n\n return chainAddressData;\n };\n}\n\ntype PluginChain = Chain.Maya | Chain.THORChain;\n\nfunction createTCBasedPlugin<T extends PluginChain>(pluginChain: T) {\n return function plugin({ getWallet }: SwapKitPluginParams) {\n const pluginType = pluginChain === Chain.Maya ? \"mayachain\" : \"thorchain\";\n const getInboundDataByChain = getInboundDataFunction(pluginType);\n\n // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: TODO: refactor/split\n async function approve<T extends ApproveMode>({\n assetValue,\n type = \"checkOnly\" as T,\n }: { type: T; assetValue: AssetValue }) {\n const router = (await getInboundDataByChain(assetValue.chain)).router as string;\n\n const chain = assetValue.chain as EVMChain;\n\n const isEVMChain = EVMChains.includes(chain as EVMChain);\n const isNativeEVM = isEVMChain && assetValue.isGasAsset;\n\n if (isNativeEVM || !isEVMChain || assetValue.isSynthetic) {\n return Promise.resolve(type === \"checkOnly\" ? true : \"approved\") as ApproveReturnType<T>;\n }\n\n const wallet = getWallet(chain);\n\n if (!wallet) {\n throw new SwapKitError(\"core_wallet_connection_not_found\");\n }\n\n const walletAction = type === \"checkOnly\" ? wallet.isApproved : wallet.approve;\n\n if (!(assetValue.address && wallet.address)) {\n throw new SwapKitError(\"core_approve_asset_address_or_from_not_found\");\n }\n\n return walletAction({\n amount: assetValue.getBaseValue(\"bigint\"),\n assetAddress: assetValue.address,\n from: wallet.address,\n spenderAddress: router,\n });\n }\n\n // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: TODO refactor\n async function deposit({\n assetValue,\n recipient,\n router,\n ...rest\n }: CoreTxParams & { router?: string }) {\n const abis = pluginType === \"thorchain\" ? TCSpecificAbi : MayaSpecificAbi;\n const { chain, symbol, ticker } = assetValue;\n\n const wallet = getWallet(chain as CryptoChain);\n if (!wallet) {\n throw new SwapKitError(\"core_wallet_connection_not_found\");\n }\n const { address } = wallet;\n const isAddressValidated = validateAddressType({ address, chain });\n if (!isAddressValidated) {\n throw new SwapKitError(\"core_transaction_invalid_sender_address\");\n }\n\n const params = prepareTxParams({ from: address, assetValue, recipient, router, ...rest });\n\n try {\n const abi = abis?.[chain as keyof typeof abis];\n\n if (!abi) {\n const wallet = getWallet(chain as PluginChain);\n const shouldDeposit = pluginChain === chain && recipient === \"\";\n // TODO: Ask @Towan - that's the same action? :)\n return shouldDeposit ? wallet.deposit(params) : wallet.transfer(params);\n }\n\n const { getChecksumAddressFromAsset } = await import(\"@swapkit/toolboxes/evm\");\n const wallet = getWallet(chain as EVMChain);\n\n return wallet.call<string>({\n abi,\n contractAddress: router || ((await getInboundDataByChain(chain)).router as string),\n funcName: \"depositWithExpiry\",\n funcParams: [\n recipient,\n getChecksumAddressFromAsset({ chain, symbol, ticker }, chain as EVMChain),\n assetValue.getBaseValue(\"string\"),\n params.memo,\n rest.expiration || Number.parseInt(`${(Date.now() + 15 * 60 * 1000) / 1000}`),\n ],\n txOverrides: {\n from: params.from,\n value: assetValue.isGasAsset ? assetValue.getBaseValue(\"bigint\") : undefined,\n },\n });\n } catch (error) {\n const errorMessage =\n // @ts-expect-error Fine to use error as string\n typeof error === \"string\" ? error.toLowerCase() : error?.message.toLowerCase();\n const isInsufficientFunds = errorMessage?.includes(\"insufficient funds\");\n const isGas = errorMessage?.includes(\"gas\");\n const isServer = errorMessage?.includes(\"server\");\n const isUserRejected = errorMessage?.includes(\"user rejected\");\n const errorKey: ErrorKeys = isInsufficientFunds\n ? \"core_transaction_deposit_insufficient_funds_error\"\n : isGas\n ? \"core_transaction_deposit_gas_error\"\n : isServer\n ? \"core_transaction_deposit_server_error\"\n : isUserRejected\n ? \"core_transaction_user_rejected\"\n : \"core_transaction_deposit_error\";\n\n throw new SwapKitError(errorKey, error);\n }\n }\n\n async function depositToProtocol({\n memo,\n assetValue,\n }: { assetValue: AssetValue; memo: string }) {\n const mimir = await SwapKitApi.thornode.getMimirInfo(pluginType);\n\n // check if trading is halted or not\n if (mimir.HALTCHAINGLOBAL >= 1 || mimir.HALTTHORCHAIN >= 1) {\n throw new SwapKitError(\"thorchain_chain_halted\");\n }\n\n return deposit({ assetValue, recipient: \"\", memo });\n }\n\n async function depositToPool({\n assetValue,\n memo,\n feeOptionKey = FeeOption.Fast,\n }: { assetValue: AssetValue; memo: string; feeOptionKey?: FeeOption }) {\n const {\n gas_rate = \"0\",\n router,\n address: poolAddress,\n } = await getInboundDataByChain(assetValue.chain);\n\n return deposit({\n assetValue,\n recipient: poolAddress,\n memo,\n router,\n feeRate: Number.parseInt(gas_rate) * gasFeeMultiplier[feeOptionKey],\n });\n }\n\n function approveAssetValue(params: ApproveParams) {\n return approve({ ...params, type: ApproveMode.Approve });\n }\n\n function isAssetValueApproved(params: ApproveParams) {\n return approve({ ...params, type: ApproveMode.CheckOnly });\n }\n\n function registerName({ assetValue, ...params }: RegisterThornameParams) {\n return depositToProtocol({ assetValue, memo: getMemoForNameRegister(params) });\n }\n\n function registerPreferredAsset({\n assetValue,\n payoutAddress,\n name,\n ownerAddress,\n }: {\n assetValue: AssetValue;\n payoutAddress?: string;\n name: string;\n ownerAddress: string;\n }) {\n const payout = payoutAddress || getWallet(assetValue.chain as CryptoChain)?.address;\n\n if (!payout) {\n throw new SwapKitError(\"thorchain_preferred_asset_payout_required\");\n }\n\n return depositToProtocol({\n assetValue: AssetValue.from({ chain: pluginChain }),\n memo: getMemoForNamePreferredAssetRegister({\n asset: assetValue.toString(),\n chain: assetValue.chain,\n name,\n owner: ownerAddress,\n payout,\n }),\n });\n }\n\n function nodeAction({ type, assetValue, address }: NodeActionParams) {\n const memo =\n type === MemoType.UNBOND\n ? getMemoForUnbond({ address, unbondAmount: assetValue.getBaseValue(\"number\") })\n : getMemoForLeaveAndBond({ type, address });\n\n const assetToTransfer =\n type === MemoType.BOND ? assetValue : getMinAmountByChain(pluginChain);\n return depositToProtocol({ memo, assetValue: assetToTransfer });\n }\n\n async function createLiquidity({ baseAssetValue, assetValue }: CreateLiquidityParams) {\n if (baseAssetValue.lte(0) || assetValue.lte(0)) {\n throw new SwapKitError(\"core_transaction_create_liquidity_invalid_params\");\n }\n\n const assetAddress = getWallet(assetValue.chain as CryptoChain).address;\n const baseAssetAddress = getWallet(pluginChain).address;\n\n const baseAssetTx = await wrapWithThrow(() => {\n return depositToPool({\n assetValue: baseAssetValue,\n memo: getMemoForDeposit({ ...assetValue, address: assetAddress }),\n });\n }, \"core_transaction_create_liquidity_base_error\");\n\n const assetTx = await wrapWithThrow(() => {\n return depositToPool({\n assetValue,\n memo: getMemoForDeposit({ ...assetValue, address: baseAssetAddress }),\n });\n }, \"core_transaction_create_liquidity_asset_error\");\n\n return { baseAssetTx, assetTx };\n }\n\n function addLiquidityPart({\n assetValue,\n poolAddress,\n address,\n symmetric,\n }: AddLiquidityPartParams) {\n if (symmetric && !address) {\n throw new SwapKitError(\"core_transaction_add_liquidity_invalid_params\");\n }\n const memo = getMemoForDeposit({\n chain: poolAddress.split(\".\")[0] as Chain,\n symbol: poolAddress.split(\".\")[1] as string,\n address: symmetric ? address : \"\",\n });\n\n return depositToPool({ assetValue, memo });\n }\n\n // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: TODO: Refactor\n async function addLiquidity({\n baseAssetValue,\n assetValue,\n baseAssetAddr,\n assetAddr,\n isPendingSymmAsset,\n mode = \"sym\",\n }: AddLiquidityParams) {\n const { chain, symbol } = assetValue;\n const isSym = mode === \"sym\";\n const baseTransfer = baseAssetValue?.gt(0) && (isSym || mode === \"baseAsset\");\n const assetTransfer = assetValue?.gt(0) && (isSym || mode === \"asset\");\n const includeBaseAddress = isPendingSymmAsset || baseTransfer;\n const baseAssetWalletAddress = getWallet(pluginChain).address;\n\n const baseAddress = includeBaseAddress ? baseAssetAddr || baseAssetWalletAddress : \"\";\n const assetAddress =\n isSym || mode === \"asset\" ? assetAddr || getWallet(chain as CryptoChain).address : \"\";\n\n if (!(baseTransfer || assetTransfer)) {\n throw new SwapKitError(\"core_transaction_add_liquidity_invalid_params\");\n }\n if (includeBaseAddress && !baseAddress) {\n throw new SwapKitError(\"core_transaction_add_liquidity_base_address\");\n }\n\n const baseAssetTx =\n baseTransfer && baseAssetValue\n ? await wrapWithThrow(() => {\n return depositToPool({\n assetValue: baseAssetValue,\n memo: getMemoForDeposit({ chain, symbol, address: assetAddress }),\n });\n }, \"core_transaction_add_liquidity_base_error\")\n : undefined;\n\n const assetTx =\n assetTransfer && assetValue\n ? await wrapWithThrow(() => {\n return depositToPool({\n assetValue,\n memo: getMemoForDeposit({ chain, symbol, address: baseAddress }),\n });\n }, \"core_transaction_add_liquidity_asset_error\")\n : undefined;\n\n return { baseAssetTx, assetTx };\n }\n\n function savings({ assetValue, memo, percent, type }: SavingsParams) {\n const { chain, symbol } = assetValue;\n const isDeposit = type === \"add\";\n const memoString = isDeposit\n ? getMemoForSaverDeposit({ symbol, chain })\n : getMemoForSaverWithdraw({\n basisPoints: Math.min(10000, Math.round(percent * 100)),\n symbol,\n chain,\n });\n\n return depositToPool({\n memo: memo || memoString,\n assetValue: isDeposit ? assetValue : getMinAmountByChain(chain),\n });\n }\n\n function withdraw({ memo, assetValue, percent, from, to }: WithdrawParams) {\n const targetAsset =\n to === \"baseAsset\" && from !== \"baseAsset\"\n ? AssetValue.from({ chain: pluginChain })\n : (from === \"sym\" && to === \"sym\") || from === \"baseAsset\" || from === \"asset\"\n ? undefined\n : assetValue;\n\n const value = getMinAmountByChain(from === \"asset\" ? assetValue.chain : pluginChain);\n const memoString =\n memo ||\n getMemoForWithdraw({\n symbol: assetValue.symbol,\n chain: assetValue.chain,\n ticker: assetValue.ticker,\n basisPoints: Math.min(10000, Math.round(percent * 100)),\n targetAsset: targetAsset?.toString(),\n });\n\n return depositToPool({ assetValue: value, memo: memoString });\n }\n\n async function swap({\n feeOptionKey,\n route,\n }: SwapParams<typeof pluginType, QuoteResponseRoute>) {\n const { memo, expiration, targetAddress } = route;\n\n const assetValue = await AssetValue.from({\n asyncTokenLookup: true,\n asset: route.sellAsset,\n value: route.sellAmount,\n });\n\n if (!assetValue) {\n throw new SwapKitError(\"core_swap_asset_not_recognized\");\n }\n\n const isRecipientValidated = validateAddressType({\n address: route.destinationAddress,\n chain: AssetValue.from({ asset: route.buyAsset }).chain,\n });\n\n if (!isRecipientValidated) {\n throw new SwapKitError(\"core_transaction_invalid_recipient_address\");\n }\n\n const { address: recipient } = await getInboundDataByChain(assetValue.chain);\n\n return deposit({\n expiration: Number(expiration),\n assetValue,\n memo,\n feeOptionKey,\n router: targetAddress,\n recipient,\n });\n }\n\n return {\n addLiquidity,\n addLiquidityPart,\n approveAssetValue,\n createLiquidity,\n deposit,\n depositToPool,\n getInboundDataByChain,\n isAssetValueApproved,\n nodeAction,\n registerName,\n registerPreferredAsset,\n savings,\n swap,\n withdraw,\n };\n };\n}\n",
|
|
6
|
+
"import { Chain } from \"@swapkit/helpers\";\nimport type { CoreTxParams } from \"./types\";\n\nexport function validateAddressType({ chain, address }: { chain?: Chain; address?: string }) {\n if (!address) return false;\n\n switch (chain) {\n case Chain.Bitcoin:\n // filter out taproot addresses\n return !address.startsWith(\"bc1p\");\n default:\n return true;\n }\n}\n\nexport function prepareTxParams({\n assetValue,\n from,\n memo = \"\",\n ...restTxParams\n}: CoreTxParams & { from: string; router?: string }) {\n return { ...restTxParams, memo, from, assetValue };\n}\n"
|
|
7
|
+
],
|
|
8
|
+
"mappings": "kDAAA,sBACE,gBAEA,WACA,eAGA,gBAEA,cACA,kBACA,kBACA,kBAGA,uBACA,4BACA,2CACA,6BACA,6BACA,8BACA,uBACA,yBACA,0BACA,mBACA,yBAEF,qBAAkC,6BAClC,+BACE,2BACA,4BACA,uBACA,sBACA,yBACA,oCClCF,gBAAS,yBAGF,SAAS,CAAmB,EAAG,QAAO,WAAgD,CAC3F,IAAK,EAAS,MAAO,GAErB,OAAQ,QACD,EAAM,QAET,OAAQ,EAAQ,WAAW,MAAM,UAEjC,MAAO,IAIN,SAAS,CAAe,EAC7B,aACA,OACA,OAAO,MACJ,GACgD,CACnD,MAAO,IAAK,EAAc,OAAM,OAAM,YAAW,ED6BnD,IAAM,GAA8C,EACjD,EAAU,SAAU,KACpB,EAAU,MAAO,KACjB,EAAU,SAAU,CACvB,EAEM,GAAgB,EACnB,EAAM,WAAY,IAClB,EAAM,MAAO,IACb,EAAM,mBAAoB,IAC1B,EAAM,UAAW,EACpB,EAEM,GAAkB,EACrB,EAAM,UAAW,IACjB,EAAM,UAAW,EACpB,EAEa,GAAkB,EAAa,CAC1C,KAAM,YACN,QAAS,EAAoB,EAAM,SAAS,EAC5C,WAAY,CACV,0BAA2B,CAAC,EAAa,UAAW,EAAa,mBAAmB,CACtF,CACF,CAAC,EAEY,GAAkB,EAAa,CAC1C,KAAM,YACN,QAAS,EAAoB,EAAM,IAAI,EACvC,WAAY,CACV,0BAA2B,CAAC,EAAa,SAAS,CACpD,CACF,CAAC,EAED,SAAS,EAAsB,CAAC,EAAqB,CACnD,OAAO,eAAe,CAAsC,CAAC,EAAU,CACrE,GACG,IAAS,aAAe,IAAU,EAAM,WACxC,IAAS,aAAe,IAAU,EAAM,KAEzC,MAAO,CAAE,SAAU,IAAK,OAAQ,GAAI,QAAS,GAAI,OAAQ,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D,CAAC,EAGC,MAAM,IAAI,EAAa,4CAA4C,EAGrE,IAAQ,QAAS,GAAc,MAAM,EAAsB,EAAW,KAAK,EAE3E,OAAO,EAAQ,CACb,WAAY,OAAO,CAAU,EAC7B,aACA,OACA,eACA,OAAQ,EACR,WACF,CAAC,EAGH,MAAO,CACL,eACA,mBACA,oBACA,kBACA,UACA,gBACA,wBACA,uBACA,aACA,eACA,yBACA,UACA,OACA,UACF",
|
|
9
|
+
"debugId": "ECA6873294467FB264756E2164756E21",
|
|
10
|
+
"names": []
|
|
11
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
{
|
|
2
|
+
"author": "swapkit-oss",
|
|
3
|
+
"dependencies": {
|
|
4
|
+
"@polkadot/keyring": "13.4.3",
|
|
5
|
+
"@polkadot/util": "13.4.3",
|
|
6
|
+
"@swapkit/helpers": "0.0.0-nightly-20250304130539",
|
|
7
|
+
"@swapkit/toolboxes": "0.0.0-nightly-20250304130539"
|
|
8
|
+
},
|
|
9
|
+
"description": "SwapKit Plugin - ChainFlip",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"default": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs",
|
|
14
|
+
"types": "./src/index.ts"
|
|
15
|
+
},
|
|
16
|
+
"./chainflip": {
|
|
17
|
+
"default": "./dist/chainflip/index.js",
|
|
18
|
+
"require": "./dist/chainflip/index.cjs",
|
|
19
|
+
"types": "./src/chainflip/index.ts"
|
|
20
|
+
},
|
|
21
|
+
"./evm": {
|
|
22
|
+
"default": "./dist/evm/index.js",
|
|
23
|
+
"require": "./dist/evm/index.cjs",
|
|
24
|
+
"types": "./src/evm/index.ts"
|
|
25
|
+
},
|
|
26
|
+
"./kado": {
|
|
27
|
+
"default": "./dist/kado/index.js",
|
|
28
|
+
"require": "./dist/kado/index.cjs",
|
|
29
|
+
"types": "./src/kado/index.ts"
|
|
30
|
+
},
|
|
31
|
+
"./radix": {
|
|
32
|
+
"default": "./dist/radix/index.js",
|
|
33
|
+
"require": "./dist/radix/index.cjs",
|
|
34
|
+
"types": "./src/radix/index.ts"
|
|
35
|
+
},
|
|
36
|
+
"./thorchain": {
|
|
37
|
+
"default": "./dist/thorchain/index.js",
|
|
38
|
+
"require": "./dist/thorchain/index.cjs",
|
|
39
|
+
"types": "./src/thorchain/index.ts"
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"files": [
|
|
43
|
+
"src/",
|
|
44
|
+
"dist/"
|
|
45
|
+
],
|
|
46
|
+
"homepage": "https://github.com/thorswap/SwapKit",
|
|
47
|
+
"license": "Apache-2.0",
|
|
48
|
+
"name": "@swapkit/plugins",
|
|
49
|
+
"repository": {
|
|
50
|
+
"directory": "packages/plugins",
|
|
51
|
+
"type": "git",
|
|
52
|
+
"url": "git+https://github.com/thorswap/SwapKit.git"
|
|
53
|
+
},
|
|
54
|
+
"scripts": {
|
|
55
|
+
"build": "bun run ./build.ts",
|
|
56
|
+
"clean": "rm -rf dist node_modules *.tsbuildinfo",
|
|
57
|
+
"lint": "biome check --write ./src",
|
|
58
|
+
"test": "echo 'bun test'",
|
|
59
|
+
"test:ci": "bun test --coverage",
|
|
60
|
+
"type-check": "tsc --noEmit"
|
|
61
|
+
},
|
|
62
|
+
"type": "module",
|
|
63
|
+
"version": "0.0.0-nightly-20250304130539"
|
|
64
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { decodeAddress } from "@polkadot/keyring";
|
|
2
|
+
import { isHex, u8aToHex } from "@polkadot/util";
|
|
3
|
+
import { AssetValue, Chain, SwapKitError, wrapWithThrow } from "@swapkit/helpers";
|
|
4
|
+
import type { ETHToolbox } from "@swapkit/toolboxes/evm";
|
|
5
|
+
import type { ChainflipToolbox } from "@swapkit/toolboxes/substrate";
|
|
6
|
+
|
|
7
|
+
import type { WithdrawFeeResponse } from "./types";
|
|
8
|
+
|
|
9
|
+
export const assetIdentifierToChainflipTicker = new Map<string, string>([
|
|
10
|
+
["ARB.ETH", "ArbEth"],
|
|
11
|
+
["ARB.USDC-0XAF88D065E77C8CC2239327C5EDB3A432268E5831", "ArbUsdc"],
|
|
12
|
+
["BTC.BTC", "Btc"],
|
|
13
|
+
["DOT.DOT", "Dot"],
|
|
14
|
+
["ETH.ETH", "Eth"],
|
|
15
|
+
["ETH.FLIP-0X826180541412D574CF1336D22C0C0A287822678A", "Flip"],
|
|
16
|
+
["ETH.USDC-0XA0B86991C6218B36C1D19D4A2E9EB0CE3606EB48", "Usdc"],
|
|
17
|
+
["ETH.USDT-0XDAC17F958D2EE523A2206206994597C13D831EC7", "Usdt"],
|
|
18
|
+
["SOL.SOL", "Sol"],
|
|
19
|
+
["SOL.USDC-EPJFWDD5AUFQSSQEM2QN1XZYBAPC8G4WEGGKZWYTDT1V", "SolUsdc"],
|
|
20
|
+
]);
|
|
21
|
+
|
|
22
|
+
const registerAsBroker = (toolbox: Awaited<ReturnType<typeof ChainflipToolbox>>) => () => {
|
|
23
|
+
const extrinsic = toolbox.api.tx.swapping?.registerAsBroker?.();
|
|
24
|
+
|
|
25
|
+
if (!extrinsic) {
|
|
26
|
+
throw new SwapKitError("chainflip_broker_register");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return toolbox.signAndBroadcast({ tx: extrinsic, address: toolbox.getAddress() });
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const withdrawFee =
|
|
33
|
+
(toolbox: Awaited<ReturnType<typeof ChainflipToolbox>>) =>
|
|
34
|
+
({ feeAsset, recipient }: { feeAsset: AssetValue; recipient: string }) => {
|
|
35
|
+
const isFeeChainPolkadot = feeAsset.chain === Chain.Polkadot;
|
|
36
|
+
|
|
37
|
+
const recipientAddress = wrapWithThrow(() => {
|
|
38
|
+
return isFeeChainPolkadot
|
|
39
|
+
? toolbox.encodeAddress(toolbox.decodeAddress(recipient), "hex")
|
|
40
|
+
: recipient;
|
|
41
|
+
}, "chainflip_broker_recipient_error");
|
|
42
|
+
|
|
43
|
+
return new Promise<WithdrawFeeResponse>((resolve) => {
|
|
44
|
+
const extrinsic = toolbox.api.tx?.swapping?.withdraw?.(feeAsset.ticker.toLowerCase(), {
|
|
45
|
+
[feeAsset.chain.toLowerCase()]: recipientAddress,
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
if (!extrinsic) {
|
|
49
|
+
throw new SwapKitError("chainflip_broker_withdraw");
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
toolbox.signAndBroadcast({
|
|
53
|
+
tx: extrinsic,
|
|
54
|
+
callback: async (result) => {
|
|
55
|
+
if (!result.status?.isFinalized) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const withdrawEvent = result.events.find(
|
|
60
|
+
(event) => event.event.method === "WithdrawalRequested",
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
if (!withdrawEvent) {
|
|
64
|
+
throw new SwapKitError(
|
|
65
|
+
"chainflip_channel_error",
|
|
66
|
+
"Could not find 'WithdrawalRequested' event",
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
const {
|
|
70
|
+
event: {
|
|
71
|
+
data: { egressId, egressAsset, egressAmount, egressFee, destinationAddress },
|
|
72
|
+
},
|
|
73
|
+
} = withdrawEvent.toHuman() as any;
|
|
74
|
+
resolve({
|
|
75
|
+
egressId,
|
|
76
|
+
egressAsset,
|
|
77
|
+
egressAmount,
|
|
78
|
+
egressFee,
|
|
79
|
+
destinationAddress,
|
|
80
|
+
});
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const fundStateChainAccount =
|
|
87
|
+
(chainflipToolbox: Awaited<ReturnType<typeof ChainflipToolbox>>) =>
|
|
88
|
+
async ({
|
|
89
|
+
evmToolbox,
|
|
90
|
+
stateChainAccount,
|
|
91
|
+
assetValue,
|
|
92
|
+
}: {
|
|
93
|
+
evmToolbox: ReturnType<typeof ETHToolbox>;
|
|
94
|
+
stateChainAccount: string;
|
|
95
|
+
assetValue: AssetValue;
|
|
96
|
+
}) => {
|
|
97
|
+
const { chainflipGateway } = await import("@swapkit/helpers/contracts");
|
|
98
|
+
|
|
99
|
+
const flipAssetValue = AssetValue.from({ asset: "ETH.FLIP" });
|
|
100
|
+
|
|
101
|
+
if (!assetValue.eqAsset(flipAssetValue)) {
|
|
102
|
+
throw new SwapKitError("chainflip_broker_fund_only_flip_supported");
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (!chainflipToolbox.validateAddress(stateChainAccount)) {
|
|
106
|
+
throw new SwapKitError("chainflip_broker_fund_invalid_address");
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const hexAddress = isHex(stateChainAccount)
|
|
110
|
+
? stateChainAccount
|
|
111
|
+
: u8aToHex(decodeAddress(stateChainAccount));
|
|
112
|
+
|
|
113
|
+
return evmToolbox.call<string>({
|
|
114
|
+
abi: chainflipGateway,
|
|
115
|
+
contractAddress: "0x6995ab7c4d7f4b03f467cf4c8e920427d9621dbd",
|
|
116
|
+
funcName: "fundStateChainAccount",
|
|
117
|
+
funcParams: [hexAddress, assetValue.getBaseValue("string")],
|
|
118
|
+
});
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
export const ChainflipBroker = (
|
|
122
|
+
chainflipToolbox: Awaited<ReturnType<typeof ChainflipToolbox>>,
|
|
123
|
+
) => ({
|
|
124
|
+
registerAsBroker: registerAsBroker(chainflipToolbox),
|
|
125
|
+
fundStateChainAccount: fundStateChainAccount(chainflipToolbox),
|
|
126
|
+
withdrawFee: withdrawFee(chainflipToolbox),
|
|
127
|
+
});
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AssetValue,
|
|
3
|
+
type CryptoChain,
|
|
4
|
+
ProviderName,
|
|
5
|
+
SKConfig,
|
|
6
|
+
SwapKitError,
|
|
7
|
+
createPlugin,
|
|
8
|
+
} from "@swapkit/helpers";
|
|
9
|
+
import { SwapKitApi } from "@swapkit/helpers/api";
|
|
10
|
+
import type { RequestSwapDepositAddressParams } from "./types";
|
|
11
|
+
|
|
12
|
+
export const ChainflipPlugin = createPlugin({
|
|
13
|
+
name: "chainflip",
|
|
14
|
+
methods: ({ getWallet }) => ({
|
|
15
|
+
swap: async function chainflipSwap(swapParams: RequestSwapDepositAddressParams) {
|
|
16
|
+
const brokerUrl = SKConfig.get("integrations").chainflip?.brokerUrl;
|
|
17
|
+
|
|
18
|
+
if (!(swapParams?.route?.buyAsset && brokerUrl && swapParams.route.meta.chainflip)) {
|
|
19
|
+
throw new SwapKitError("core_swap_invalid_params", {
|
|
20
|
+
...swapParams,
|
|
21
|
+
chainflipBrokerUrl: brokerUrl,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const {
|
|
26
|
+
route: {
|
|
27
|
+
buyAsset: buyAssetString,
|
|
28
|
+
sellAsset: sellAssetString,
|
|
29
|
+
sellAmount,
|
|
30
|
+
destinationAddress: recipient,
|
|
31
|
+
meta: { chainflip },
|
|
32
|
+
},
|
|
33
|
+
maxBoostFeeBps = 0,
|
|
34
|
+
} = swapParams;
|
|
35
|
+
|
|
36
|
+
if (!(sellAssetString && buyAssetString)) {
|
|
37
|
+
throw new SwapKitError("core_swap_asset_not_recognized");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const sellAsset = await AssetValue.from({
|
|
41
|
+
asyncTokenLookup: true,
|
|
42
|
+
asset: sellAssetString,
|
|
43
|
+
value: sellAmount,
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const wallet = getWallet(sellAsset.chain as CryptoChain);
|
|
47
|
+
|
|
48
|
+
if (!wallet) {
|
|
49
|
+
throw new SwapKitError("core_wallet_connection_not_found");
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const { depositAddress } = await SwapKitApi.getChainflipDepositChannel({
|
|
53
|
+
...chainflip,
|
|
54
|
+
destinationAddress: recipient || chainflip.destinationAddress,
|
|
55
|
+
maxBoostFeeBps: maxBoostFeeBps || chainflip.maxBoostFeeBps,
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// @ts-expect-error TODO: right now it's inferred from toolboxes
|
|
59
|
+
// we need to simplify this to one object params
|
|
60
|
+
const tx = await wallet.transfer({
|
|
61
|
+
assetValue: sellAsset,
|
|
62
|
+
from: wallet.address,
|
|
63
|
+
recipient: depositAddress,
|
|
64
|
+
isProgramDerivedAddress: true,
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
return tx as string;
|
|
68
|
+
},
|
|
69
|
+
}),
|
|
70
|
+
properties: {
|
|
71
|
+
supportedSwapkitProviders: [ProviderName.CHAINFLIP, ProviderName.CHAINFLIP_STREAMING],
|
|
72
|
+
},
|
|
73
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { AssetValue, SwapParams } from "@swapkit/helpers";
|
|
2
|
+
import type { QuoteResponseRoute } from "@swapkit/helpers/api";
|
|
3
|
+
|
|
4
|
+
export type WithdrawFeeResponse = {
|
|
5
|
+
egressId: string;
|
|
6
|
+
egressAsset: string;
|
|
7
|
+
egressAmount: string;
|
|
8
|
+
egressFee: string;
|
|
9
|
+
destinationAddress: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type DepositChannelRequest = {
|
|
13
|
+
brokerCommissionBPS: number;
|
|
14
|
+
ccmMetadata: ccmMetadata | null;
|
|
15
|
+
maxBoostFeeBps?: number;
|
|
16
|
+
affiliateFees?: AffiliateBroker[];
|
|
17
|
+
refundParameters?: SwapRefundParameters;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export type ccmMetadata = {
|
|
21
|
+
message: string;
|
|
22
|
+
gasBudget: string;
|
|
23
|
+
cfParameters: string;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export type SwapDepositResponse = {
|
|
27
|
+
depositChannelId: string;
|
|
28
|
+
depositAddress: string;
|
|
29
|
+
srcChainExpiryBlock: number;
|
|
30
|
+
sellAsset: AssetValue;
|
|
31
|
+
buyAsset: AssetValue;
|
|
32
|
+
recipient: string;
|
|
33
|
+
brokerCommissionBPS: number;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export type AffiliateBroker = {
|
|
37
|
+
brokerAddress: string;
|
|
38
|
+
basisPoints: number;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export type SwapRefundParameters = {
|
|
42
|
+
retryDuration: number;
|
|
43
|
+
refundAddress: string;
|
|
44
|
+
minPrice: string;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export type RequestSwapDepositAddressParams = Partial<SwapParams<"chainflip", QuoteResponseRoute>> &
|
|
48
|
+
Partial<DepositChannelRequest>;
|
package/src/evm/index.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ApproveMode,
|
|
3
|
+
type ApproveReturnType,
|
|
4
|
+
AssetValue,
|
|
5
|
+
type EVMChain,
|
|
6
|
+
EVMChains,
|
|
7
|
+
ProviderName,
|
|
8
|
+
SwapKitError,
|
|
9
|
+
type SwapKitPluginParams,
|
|
10
|
+
type SwapParams,
|
|
11
|
+
createPlugin,
|
|
12
|
+
} from "@swapkit/helpers";
|
|
13
|
+
import type { EVMTransaction, QuoteResponseRoute } from "@swapkit/helpers/api";
|
|
14
|
+
|
|
15
|
+
function approve<T extends ApproveMode>({
|
|
16
|
+
approveMode,
|
|
17
|
+
getWallet,
|
|
18
|
+
}: { approveMode: T } & SwapKitPluginParams) {
|
|
19
|
+
return function approveEVM({
|
|
20
|
+
assetValue,
|
|
21
|
+
spenderAddress,
|
|
22
|
+
}: { spenderAddress: string; assetValue: AssetValue }) {
|
|
23
|
+
const evmChain = assetValue.chain as EVMChain;
|
|
24
|
+
const isEVMChain = EVMChains.includes(evmChain);
|
|
25
|
+
const isNativeEVM = isEVMChain && assetValue.isGasAsset;
|
|
26
|
+
|
|
27
|
+
if (isNativeEVM || !isEVMChain || assetValue.isSynthetic) {
|
|
28
|
+
const isApproved = approveMode === "checkOnly" || "approved";
|
|
29
|
+
return Promise.resolve(isApproved) as ApproveReturnType<T>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const wallet = getWallet(evmChain);
|
|
33
|
+
const walletAction = approveMode === "checkOnly" ? wallet.isApproved : wallet.approve;
|
|
34
|
+
|
|
35
|
+
if (!(assetValue.address && wallet.address)) {
|
|
36
|
+
throw new SwapKitError("core_approve_asset_address_or_from_not_found");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return walletAction({
|
|
40
|
+
amount: assetValue.getBaseValue("bigint"),
|
|
41
|
+
assetAddress: assetValue.address,
|
|
42
|
+
from: wallet.address,
|
|
43
|
+
spenderAddress,
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export const EVMPlugin = createPlugin({
|
|
49
|
+
name: "evm",
|
|
50
|
+
properties: {
|
|
51
|
+
supportedSwapkitProviders: [
|
|
52
|
+
ProviderName.CAMELOT_V3,
|
|
53
|
+
ProviderName.OPENOCEAN_V2,
|
|
54
|
+
ProviderName.ONEINCH,
|
|
55
|
+
ProviderName.PANCAKESWAP,
|
|
56
|
+
ProviderName.PANGOLIN_V1,
|
|
57
|
+
ProviderName.SUSHISWAP_V2,
|
|
58
|
+
ProviderName.TRADERJOE_V2,
|
|
59
|
+
ProviderName.UNISWAP_V2,
|
|
60
|
+
ProviderName.UNISWAP_V3,
|
|
61
|
+
],
|
|
62
|
+
},
|
|
63
|
+
methods: ({ getWallet }) => ({
|
|
64
|
+
approveAssetValue: approve({ approveMode: ApproveMode.Approve, getWallet }),
|
|
65
|
+
isAssetValueApproved: approve({ approveMode: ApproveMode.CheckOnly, getWallet }),
|
|
66
|
+
swap: async function evmSwap({
|
|
67
|
+
route: { tx, sellAsset },
|
|
68
|
+
feeOptionKey,
|
|
69
|
+
}: SwapParams<"evm", QuoteResponseRoute>) {
|
|
70
|
+
const assetValue = await AssetValue.from({ asset: sellAsset, asyncTokenLookup: true });
|
|
71
|
+
const evmChain = assetValue.chain as EVMChain;
|
|
72
|
+
const wallet = getWallet(evmChain);
|
|
73
|
+
|
|
74
|
+
if (!(EVMChains.includes(evmChain) && tx)) {
|
|
75
|
+
throw new SwapKitError("core_swap_invalid_params");
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const { from, to, data, value } = tx as EVMTransaction;
|
|
79
|
+
return wallet.sendTransaction({ from, to, data, value: BigInt(value), feeOptionKey });
|
|
80
|
+
},
|
|
81
|
+
}),
|
|
82
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ChainflipPlugin } from "./chainflip";
|
|
2
|
+
import type { EVMPlugin } from "./evm";
|
|
3
|
+
import type { KadoPlugin } from "./kado";
|
|
4
|
+
import type { RadixPlugin } from "./radix";
|
|
5
|
+
import type { ThorchainPlugin } from "./thorchain";
|
|
6
|
+
|
|
7
|
+
export type * from "./chainflip/types";
|
|
8
|
+
export type * from "./thorchain/types";
|
|
9
|
+
export type * from "./kado/types";
|
|
10
|
+
|
|
11
|
+
export type SKPlugins = typeof ChainflipPlugin &
|
|
12
|
+
typeof ThorchainPlugin &
|
|
13
|
+
typeof KadoPlugin &
|
|
14
|
+
typeof RadixPlugin &
|
|
15
|
+
typeof EVMPlugin;
|
|
16
|
+
|
|
17
|
+
export type PluginName = keyof SKPlugins;
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { type AssetValue, Chain, FeeTypeEnum, ProviderName, blockTimes } from "@swapkit/helpers";
|
|
2
|
+
import type { QuoteResponse, QuoteResponseRoute } from "@swapkit/helpers/api";
|
|
3
|
+
import type { KadoQuoteResponse } from "./types";
|
|
4
|
+
|
|
5
|
+
export const SupportedKadoChain = {
|
|
6
|
+
thorchain: Chain.THORChain,
|
|
7
|
+
solana: Chain.Solana,
|
|
8
|
+
polygon: Chain.Polygon,
|
|
9
|
+
Optimism: Chain.Optimism,
|
|
10
|
+
litecoin: Chain.Litecoin,
|
|
11
|
+
kujira: Chain.Kujira,
|
|
12
|
+
ethereum: Chain.Ethereum,
|
|
13
|
+
"cosmos hub": Chain.Cosmos,
|
|
14
|
+
bitcoin: Chain.Bitcoin,
|
|
15
|
+
base: Chain.Base,
|
|
16
|
+
Avalanche: Chain.Avalanche,
|
|
17
|
+
Arbitrum: Chain.Arbitrum,
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export function ChainToKadoChain(chain: Chain) {
|
|
21
|
+
const entries = Object.entries(SupportedKadoChain);
|
|
22
|
+
const found = entries.find(([_, value]) => value === chain);
|
|
23
|
+
if (!found) throw new Error(`Chain ${chain} not supported`);
|
|
24
|
+
return found[0];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function KadoChainToChain(kadoChain: string) {
|
|
28
|
+
const found = Object.keys(SupportedKadoChain).includes(kadoChain);
|
|
29
|
+
if (!found) throw new Error(`KadoChain ${kadoChain} not supported`);
|
|
30
|
+
return SupportedKadoChain[kadoChain as keyof typeof SupportedKadoChain];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function mapKadoQuoteToQuoteResponse({
|
|
34
|
+
quote,
|
|
35
|
+
sellAsset,
|
|
36
|
+
buyAsset,
|
|
37
|
+
}: { quote: KadoQuoteResponse; sellAsset: AssetValue; buyAsset: AssetValue }): QuoteResponse {
|
|
38
|
+
const sellAssetChain = sellAsset.chain;
|
|
39
|
+
const buyAssetChain = buyAsset.chain;
|
|
40
|
+
const isOnRamp = sellAssetChain === Chain.Fiat;
|
|
41
|
+
const { receive, price, baseAmount, totalFee, processingFee, networkFee } = quote.data.quote;
|
|
42
|
+
|
|
43
|
+
const buyAssetAmount = buyAsset.set(
|
|
44
|
+
isOnRamp ? receive.unitCount.toString() : receive.amount.toString(),
|
|
45
|
+
);
|
|
46
|
+
const totalSlippageBps = isOnRamp
|
|
47
|
+
? Math.round((totalFee.amount / receive.amount) * 10_000)
|
|
48
|
+
: Math.round((totalFee.amount / (price.price * baseAmount.amount)) * 10_000);
|
|
49
|
+
|
|
50
|
+
const inbound = Math.ceil(blockTimes[sellAssetChain] * 3);
|
|
51
|
+
const swap = Math.ceil(60);
|
|
52
|
+
const outbound = Math.ceil(blockTimes[buyAssetChain]);
|
|
53
|
+
const routes: QuoteResponseRoute[] = [
|
|
54
|
+
{
|
|
55
|
+
buyAsset: buyAsset.toString(),
|
|
56
|
+
destinationAddress: "{destinationAddress}",
|
|
57
|
+
estimatedTime: { inbound, swap, outbound, total: inbound + swap + outbound },
|
|
58
|
+
expectedBuyAmount: buyAssetAmount.getValue("string"),
|
|
59
|
+
expectedBuyAmountMaxSlippage: buyAssetAmount.getValue("string"),
|
|
60
|
+
providers: [ProviderName.KADO],
|
|
61
|
+
sellAmount: sellAsset.getValue("string"),
|
|
62
|
+
sellAsset: sellAsset.toString(),
|
|
63
|
+
sourceAddress: "{sourceAddress}",
|
|
64
|
+
totalSlippageBps,
|
|
65
|
+
fees: [
|
|
66
|
+
{
|
|
67
|
+
asset: processingFee.currency,
|
|
68
|
+
amount: processingFee.amount.toString(),
|
|
69
|
+
type: FeeTypeEnum.LIQUIDITY,
|
|
70
|
+
protocol: ProviderName.KADO,
|
|
71
|
+
chain: Chain.Fiat,
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
asset: networkFee.currency,
|
|
75
|
+
amount: networkFee.amount.toString(),
|
|
76
|
+
type: FeeTypeEnum.NETWORK,
|
|
77
|
+
protocol: ProviderName.KADO,
|
|
78
|
+
chain: buyAsset.chain,
|
|
79
|
+
},
|
|
80
|
+
],
|
|
81
|
+
legs: [
|
|
82
|
+
{
|
|
83
|
+
provider: ProviderName.KADO,
|
|
84
|
+
sellAsset: sellAsset.toString(),
|
|
85
|
+
sellAmount: sellAsset.getValue("string"),
|
|
86
|
+
buyAsset: buyAsset.toString(),
|
|
87
|
+
buyAmount: receive.unitCount.toString(),
|
|
88
|
+
buyAmountMaxSlippage: receive.unitCount.toString(),
|
|
89
|
+
fees: [
|
|
90
|
+
{
|
|
91
|
+
asset: processingFee.currency,
|
|
92
|
+
amount: processingFee.amount.toString(),
|
|
93
|
+
type: FeeTypeEnum.LIQUIDITY,
|
|
94
|
+
protocol: ProviderName.KADO,
|
|
95
|
+
chain: Chain.Fiat,
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
asset: networkFee.currency,
|
|
99
|
+
amount: networkFee.amount.toString(),
|
|
100
|
+
type: FeeTypeEnum.NETWORK,
|
|
101
|
+
protocol: ProviderName.KADO,
|
|
102
|
+
chain: buyAsset.chain,
|
|
103
|
+
},
|
|
104
|
+
],
|
|
105
|
+
},
|
|
106
|
+
],
|
|
107
|
+
warnings: [],
|
|
108
|
+
meta: { tags: [] },
|
|
109
|
+
},
|
|
110
|
+
];
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
quoteId: crypto.randomUUID(),
|
|
114
|
+
routes,
|
|
115
|
+
error: quote.success ? undefined : quote.message,
|
|
116
|
+
};
|
|
117
|
+
}
|