Skip to content

Commit

Permalink
Add Cosmos IBC transfer plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
peachbits committed Feb 27, 2024
1 parent 77f18d9 commit 1eda314
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- added: Cosmos IBC transfer plugin

## 2.1.1 (2023-02-27)

- added: (Swapuz) Above limit response handling
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { makeGodexPlugin } from './swap/central/godex'
import { makeLetsExchangePlugin } from './swap/central/letsexchange'
import { makeSideshiftPlugin } from './swap/central/sideshift'
import { makeSwapuzPlugin } from './swap/central/swapuz'
import { makeCosmosIbcPlugin } from './swap/defi/cosmosIbc'
import { makeLifiPlugin } from './swap/defi/lifi'
import { makeThorchainPlugin } from './swap/defi/thorchain'
import { makeThorchainDaPlugin } from './swap/defi/thorchainDa'
Expand All @@ -22,6 +23,7 @@ const plugins = {
// Swap plugins:
changehero: makeChangeHeroPlugin,
changenow: makeChangeNowPlugin,
cosmosibc: makeCosmosIbcPlugin,
exolix: makeExolixPlugin,
godex: makeGodexPlugin,
letsexchange: makeLetsExchangePlugin,
Expand Down
104 changes: 104 additions & 0 deletions src/swap/defi/cosmosIbc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import {
EdgeCorePluginOptions,
EdgeSpendInfo,
EdgeSwapInfo,
EdgeSwapPlugin,
EdgeSwapQuote,
EdgeSwapRequest,
SwapCurrencyError
} from 'edge-core-js/types'

import {
getMaxSwappable,
makeSwapPluginQuote,
SwapOrder
} from '../../util/swapHelpers'
import { convertRequest } from '../../util/utils'
import { EdgeSwapRequestPlugin } from '../types'

const swapInfo: EdgeSwapInfo = {
pluginId: 'cosmosibc',
displayName: 'Cosmos IBC',
orderUri: undefined,
supportEmail: '[email protected]'
}

export function makeCosmosIbcPlugin(
opts: EdgeCorePluginOptions
): EdgeSwapPlugin {
const fetchSwapQuoteInner = async (
request: EdgeSwapRequestPlugin
): Promise<SwapOrder> => {
const {
publicAddress: toAddress
} = await request.toWallet.getReceiveAddress({ tokenId: request.toTokenId })

const spendInfo: EdgeSpendInfo = {
tokenId: request.fromTokenId,
spendTargets: [
{
nativeAmount: request.nativeAmount,
publicAddress: toAddress
}
],
assetAction: {
assetActionType: 'transfer'
},
swapData: {
isEstimate: false,
payoutAddress: toAddress,
plugin: { ...swapInfo },
payoutCurrencyCode: request.toCurrencyCode,
payoutNativeAmount: request.nativeAmount,
payoutWalletId: request.toWallet.id
},
savedAction: {
actionType: 'swap',
swapInfo,
isEstimate: false,
canBePartial: false,
fromAsset: {
pluginId: request.fromWallet.currencyInfo.pluginId,
tokenId: request.fromTokenId,
nativeAmount: request.nativeAmount
},
toAsset: {
pluginId: request.toWallet.currencyInfo.pluginId,
tokenId: request.toTokenId,
nativeAmount: request.nativeAmount
},
payoutAddress: toAddress,
payoutWalletId: request.toWallet.id
}
}

return {
request,
spendInfo,
swapInfo,
fromNativeAmount: request.nativeAmount
}
}

const out: EdgeSwapPlugin = {
swapInfo,

async fetchSwapQuote(req: EdgeSwapRequest): Promise<EdgeSwapQuote> {
const request = convertRequest(req)

if (
request.fromWallet.currencyInfo.pluginId ===
request.toWallet.currencyInfo.pluginId ||
request.fromCurrencyCode !== request.toCurrencyCode
) {
throw new SwapCurrencyError(swapInfo, request)
}

const newRequest = await getMaxSwappable(fetchSwapQuoteInner, request)
const swapOrder = await fetchSwapQuoteInner(newRequest)
return await makeSwapPluginQuote(swapOrder)
}
}

return out
}

0 comments on commit 1eda314

Please sign in to comment.