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

Update lib for cross transfers #45

Merged
merged 4 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,16 @@
"nodemon": "^2.0.20",
"typescript": "^4.4.x"
},
"resolutions": {
"@polkadot/types": "^10.9.1",
"@polkadot/api": "^10.9.1"
},
"dependencies": {
"@babel/runtime": "^7.15.3",
"@equilab/api": "^1.12.23",
"@isaacs/ttlcache": "^1.0.3",
"@kodadot1/uniquery": "^0.3.0-rc.0",
"@polkawallet/bridge": "^0.0.4",
"@polkawallet/bridge": "0.1.3",
"@subsocial/api": "0.8.14",
"axios": "^0.26.0",
"connect-timeout": "^1.9.0",
Expand Down
33 changes: 9 additions & 24 deletions src/connections/networks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ export const standalones: Networks = {
isMixedConnection: true,
isTransferable: true
},
sora: {
name: 'SORA',
icon: 'sora-substrate.svg',
...resolveOnfinalityUrl('sora'),
nativeToken: 'XOR',
isTransferable: true
},
edgeware: {
name: 'Edgeware',
icon: 'edgeware-circle.svg',
Expand Down Expand Up @@ -114,13 +107,6 @@ export const kusamaParachains: Networks = {
isTransferable: true,
tokenTransferMethod: 'assets.transfer(id,recipient,amount)'
},
kilt: {
name: 'Kilt Spiritnet',
...resolveOnfinalityUrl('spiritnet'),
icon: 'kilt.png',
paraId: 2086,
isTransferable: true
},
polkasmith: {
name: 'PolkaSmith',
node: '',
Expand Down Expand Up @@ -227,6 +213,13 @@ export const kusamaParachains: Networks = {
isTransferable: true,
tokenTransferMethod: 'assets.transfer(id,recipient,amount)'
},
kilt: {
name: 'Kilt Spiritnet',
...resolveOnfinalityUrl('spiritnet'),
icon: 'kilt.png',
paraId: 2086,
isTransferable: true
},
picasso: {
name: 'Picasso',
node: 'wss://rpc.composablenodes.tech',
Expand Down Expand Up @@ -254,20 +247,12 @@ export const kusamaParachains: Networks = {
},
robonomics: {
name: 'Robonomics',
...resolveOnfinalityUrl('robonomics'),
node: 'wss://kusama.rpc.robonomics.network/',
icon: 'robonomics.svg',
paraId: 2048,
vestingMethod: 'vesting.vest',
isTransferable: true
},
quartz: {
name: 'QUARTZ by UNIQUE',
...resolveOnfinalityUrl('quartz'),
icon: 'quartz.png',
paraId: 2095,
vestingMethod: 'vesting.claim',
isTransferable: true
},
zeitgeist: {
name: 'Zeitgeist',
...resolveOnfinalityUrl('zeitgeist'),
Expand Down Expand Up @@ -482,7 +467,7 @@ export const polkadotParachains: Networks = {
icon: 'subdao.png',
paraId: 2018
},
'parallel': {
parallel: {
name: 'Parallel',
...resolveOnfinalityUrl('parallel'),
icon: 'parallel.svg',
Expand Down
4 changes: 2 additions & 2 deletions src/constant/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ export const validatorsStakingNetworks = [
]

export const VALIDATOR_STAKING_REWARDS_API_KEY = 'f00511ab0c5e422796b8f5d9c7029fbe'
export const ONFINALITY_API_KEY = 'e75486ad-bbb5-4b93-8699-3123f8dbabc5'
// export const ONFINALITY_API_KEY = 'e75486ad-bbb5-4b93-8699-3123f8dbabc5'

// export const ONFINALITY_API_KEY = '28209e0e-fc76-481e-a80f-745ffdf477d7' // test api key for localhost
export const ONFINALITY_API_KEY = '28209e0e-fc76-481e-a80f-745ffdf477d7' // test api key for localhost


export const SUBSOCIAL_GRAPHQL_CLIENT = 'https://squid.subsquid.io/subsocial/graphql'
Expand Down
36 changes: 18 additions & 18 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import express from 'express'
import cors from 'cors'
import timeout from 'connect-timeout'
import { reqTimeoutSecs, allowedOrigins, port } from './constant/env'
import { reqTimeoutSecs/* , allowedOrigins */, port } from './constant/env'
import { newLogger } from '@subsocial/utils'

import { createRoutes } from './routes'
Expand All @@ -21,28 +21,28 @@ export const startHttpServer = (apis: Connections) => {

app.use(express.static('public'))

app.use(
cors((req, callback) => {
const corsOptions = { origin: true }
const origin = req.header('Origin')
const isAllowedOrigin = allowedOrigins.some((allowedOrigin) =>
origin?.includes(allowedOrigin)
)
if (!isAllowedOrigin) {
corsOptions.origin = false
}
callback(null, corsOptions)
})
)

// For localhost testing
// app.use(
// cors((req, callback) => {
// const origin = req.method === 'GET' ? '*' : '*'
// callback(null, { origin })
// const corsOptions = { origin: true }
// const origin = req.header('Origin')
// const isAllowedOrigin = allowedOrigins.some((allowedOrigin) =>
// origin?.includes(allowedOrigin)
// )
// if (!isAllowedOrigin) {
// corsOptions.origin = false
// }
// callback(null, corsOptions)
// })
// )

// For localhost testing
app.use(
cors((req, callback) => {
const origin = req.method === 'GET' ? '*' : '*'
callback(null, { origin })
})
)

function haltOnTimedout(req: express.Request, _res: express.Response, next) {
if (!req.timedout) next()
}
Expand Down
4 changes: 2 additions & 2 deletions src/services/fees/transfer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChainName, FN } from '@polkawallet/bridge'
import { ChainId, FN } from '@polkawallet/bridge'
import { ApiPromise, Apis } from '../../connections/networks/types'
import { getCrossChainAdapter } from './utils'
import { firstValueFrom } from 'rxjs'
Expand Down Expand Up @@ -74,7 +74,7 @@ async function getCrossChainTransferFee ({ from, token, to }: GetTransferFeePara
const adapter = await getCrossChainAdapter(from, node)
if (!adapter) return amount
amount = await firstValueFrom(adapter.estimateTxFee({
to: to as ChainName,
to: to as ChainId,
address: dummyAccount,
amount: FN.ZERO,
signer: dummyAccount,
Expand Down
94 changes: 52 additions & 42 deletions src/services/fees/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ApiProvider, Bridge, ChainName } from '@polkawallet/bridge'
import { ApiProvider, Bridge, ChainId } from '@polkawallet/bridge'
import { AcalaAdapter, KaruraAdapter } from '@polkawallet/bridge/adapters/acala'
import { AstarAdapter, ShidenAdapter } from '@polkawallet/bridge/adapters/astar'
import { BifrostAdapter } from '@polkawallet/bridge/adapters/bifrost'
import { AltairAdapter } from '@polkawallet/bridge/adapters/centrifuge'
import { ShadowAdapter } from '@polkawallet/bridge/adapters/crust'
import { CrabAdapter } from '@polkawallet/bridge/adapters/darwinia'
import { BasiliskAdapter } from '@polkawallet/bridge/adapters/hydradx'
import { BasiliskAdapter, HydraAdapter } from '@polkawallet/bridge/adapters/hydradx'
import { IntegriteeAdapter } from '@polkawallet/bridge/adapters/integritee'
import { InterlayAdapter, KintsugiAdapter } from '@polkawallet/bridge/adapters/interlay'
import { KicoAdapter } from '@polkawallet/bridge/adapters/kico'
Expand All @@ -17,110 +17,120 @@ import { HeikoAdapter, ParallelAdapter } from '@polkawallet/bridge/adapters/para
import { KhalaAdapter } from '@polkawallet/bridge/adapters/phala'
import { PolkadotAdapter, KusamaAdapter } from '@polkawallet/bridge/adapters/polkadot'
import { StatemineAdapter } from '@polkawallet/bridge/adapters/statemint'
import { QuartzAdapter } from '@polkawallet/bridge/adapters/unique'
import { ZeitgeistAdapter } from '@polkawallet/bridge/adapters/zeitgeist'
import { BaseCrossChainAdapter } from '@polkawallet/bridge/base-chain-adapter'
import { firstValueFrom } from 'rxjs'

const transferAdapters: Record<string, { adapter: BaseCrossChainAdapter; chainName?: ChainName }> = {
const transferAdapters: Record<string, { adapter: BaseCrossChainAdapter; chainName?: ChainId }> = {
polkadot: {
adapter: new PolkadotAdapter(),
adapter: new PolkadotAdapter()
},
kusama: {
adapter: new KusamaAdapter(),
adapter: new KusamaAdapter()
},
karura: {
adapter: new KaruraAdapter(),
},
bifrost: {
adapter: new BifrostAdapter(),
adapter: new KaruraAdapter()
},
astar: {
adapter: new AstarAdapter(),
adapter: new AstarAdapter()
},
shiden: {
adapter: new ShidenAdapter(),
adapter: new ShidenAdapter()
},
acala: {
adapter: new AcalaAdapter(),
adapter: new AcalaAdapter()
},
statemine: {
adapter: new StatemineAdapter(),
adapter: new StatemineAdapter()
},
statemint: {
adapter: new StatemineAdapter(),
adapter: new StatemineAdapter()
},
altair: {
adapter: new AltairAdapter(),
adapter: new AltairAdapter()
},
shadow: {
adapter: new ShadowAdapter(),
adapter: new ShadowAdapter()
},
'darwinia-crab-parachain': {
adapter: new CrabAdapter(),
chainName: 'crab'
},
basilisk: {
adapter: new BasiliskAdapter(),
},
integritee: {
adapter: new IntegriteeAdapter(),
adapter: new BasiliskAdapter()
},
kintsugi: {
adapter: new KintsugiAdapter(),
adapter: new KintsugiAdapter()
},
interlay: {
adapter: new InterlayAdapter(),
adapter: new InterlayAdapter()
},
kico: {
adapter: new KicoAdapter(),
adapter: new KicoAdapter()
},
pichiu: {
adapter: new PichiuAdapter(),
adapter: new PichiuAdapter()
},
calamari: {
adapter: new CalamariAdapter(),
adapter: new CalamariAdapter()
},
moonbeam: {
adapter: new MoonbeamAdapter(),
adapter: new MoonbeamAdapter()
},
moonriver: {
adapter: new MoonriverAdapter(),
adapter: new MoonriverAdapter()
},
khala: {
adapter: new KhalaAdapter()
},
bifrostKusama: {
adapter: new BifrostAdapter(),
chainName: 'bifrost'
},
integritee: {
adapter: new IntegriteeAdapter()
},
turing: {
adapter: new TuringAdapter(),
adapter: new TuringAdapter()
},
parallel: {
adapter: new ParallelAdapter(),
adapter: new ParallelAdapter()
},
parallelHeiko: {
heiko: {
adapter: new HeikoAdapter(),
chainName: 'heiko'
},
khala: {
adapter: new KhalaAdapter(),
},
quartz: {
adapter: new QuartzAdapter(),
hydra: {
adapter: new HydraAdapter(),
chainName: 'hydradx'
},
zeitgeist: {
adapter: new ZeitgeistAdapter()
}
}
function getPolkawalletChainName (chain: string) {
function getPolkawalletChainName(chain: string) {
const chainData = transferAdapters[chain]
if (!chainData) return undefined
return chainData.chainName || chain as ChainName
return chainData.chainName || (chain as ChainId)
}

const provider = new ApiProvider()

const bridge = new Bridge({ adapters: Object.values(transferAdapters).map(({ adapter }) => adapter) })
const bridge = new Bridge({
adapters: Object.values(transferAdapters).map(({ adapter }) => adapter)
})

export async function getCrossChainAdapter (chain: string, connectNode?: string): Promise<BaseCrossChainAdapter | undefined> {
export async function getCrossChainAdapter(
chain: string,
connectNode?: string
): Promise<BaseCrossChainAdapter | undefined> {
const chainName = getPolkawalletChainName(chain)
if (!chainName) return undefined

const adapter = bridge.findAdapter(chainName)
if (connectNode) {
await firstValueFrom(provider.connectFromChain([ chainName ], { [chainName]: [ connectNode ] }))
await adapter.setApi(provider.getApi(chainName))
await firstValueFrom(provider.connectFromChain([chainName], { [chainName]: [connectNode] }))
await adapter.init(provider.getApi(chainName))
}
return adapter
}
Loading