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

add encointer #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Binary file added .papi/metadata/ksmEnc.scale
Binary file not shown.
5 changes: 5 additions & 0 deletions .papi/polkadot-api.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
"pasAh": {
"chain": "paseo_asset_hub",
"metadata": ".papi/metadata/pasAh.scale"
},
"ksmEnc": {
"chain": "ksmcc3_encointer",
"wsUrl": "wss://kusama.api.encointer.org",
"metadata": ".papi/metadata/ksmEnc.scale"
}
}
}
1 change: 1 addition & 0 deletions src/api/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const CHAIN_NAMES: Record<ChainId, string> = {
dotAh: "Polkadot AssetHub",
ksm: "Kusama RelayChain",
ksmAh: "Kusama AssetHub",
ksmEnc: "Kusama Encointer",
wnd: "Westend RelayChain",
wndAh: "Westend AssetHub",
roc: "Rococo RelayChain",
Expand Down
1 change: 1 addition & 0 deletions src/api/clients/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export { dotAhClient } from "./dotAh"

export { ksmClient } from "./ksm"
export { ksmAhClient } from "./ksmAh"
export { ksmEncClient } from "./ksmEnc"

export { wndClient } from "./wnd"
export { wndAhClient } from "./wndAh"
Expand Down
13 changes: 13 additions & 0 deletions src/api/clients/ksmEnc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { createClient } from "polkadot-api"
import { getSmProvider } from "polkadot-api/sm-provider"
import { smoldot } from "./smoldot"
import { ksmRelayChain } from "./ksm"

const smoldotParaChain = Promise.all([
ksmRelayChain,
import("polkadot-api/chains/ksmcc3_encointer"),
]).then(([relayChain, { chainSpec }]) =>
smoldot.addChain({ chainSpec, potentialRelayChains: [relayChain] }),
)

export const ksmEncClient = createClient(getSmProvider(smoldotParaChain))
56 changes: 56 additions & 0 deletions src/api/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ export const fromRelayToAssetHub = (
weight_limit: XcmV3WeightLimit.Unlimited(),
})

export const fromRelayToEncointer = (
from: PolkadotSigner,
amount: bigint,
to?: SS58String,
) => ({
dest: XcmVersionedLocation.V3({
parents: 0,
interior: XcmV3Junctions.X1(XcmV3Junction.Parachain(1001)),
}),
beneficiary: getBeneficiary(to ?? from.publicKey),
assets: getNativeAsset(0, amount),
fee_asset_item: 0,
weight_limit: XcmV3WeightLimit.Unlimited(),
})

export const fromAssetHubToRelay = (
from: PolkadotSigner,
amount: bigint,
Expand All @@ -81,6 +96,27 @@ export const fromAssetHubToRelay = (
weight_limit: XcmV3WeightLimit.Unlimited(),
})

export const fromEncointerToRelay = (
from: PolkadotSigner,
amount: bigint,
to?: SS58String,
) => ({
dest: {
type: "V3" as const,
value: {
parents: 1,
interior: {
type: "Here" as const,
value: undefined,
},
},
},
beneficiary: getBeneficiary(to ?? from.publicKey),
assets: getNativeAsset(1, amount),
fee_asset_item: 0,
weight_limit: XcmV3WeightLimit.Unlimited(),
})

export const fromAssetHubToForeign = (
network: XcmV3JunctionNetworkId,
parachainId: number,
Expand All @@ -101,6 +137,26 @@ export const fromAssetHubToForeign = (
weight_limit: XcmV3WeightLimit.Unlimited(),
})

export const fromEncointerToForeign = (
network: XcmV3JunctionNetworkId,
parachainId: number,
assets: XcmVersionedAssets,
from: PolkadotSigner,
to?: SS58String,
) => ({
dest: XcmVersionedLocation.V3({
parents: 2,
interior: XcmV3Junctions.X2([
XcmV3Junction.GlobalConsensus(network),
XcmV3Junction.Parachain(parachainId),
]),
}),
beneficiary: getBeneficiary(to ?? from.publicKey),
assets,
fee_asset_item: 0,
weight_limit: XcmV3WeightLimit.Unlimited(),
})

/*
export const toAssetHub = (
from: PolkadotSigner,
Expand Down
36 changes: 36 additions & 0 deletions src/api/ksm/enc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {
XcmV3JunctionNetworkId,
ksmEnc,
} from "@polkadot-api/descriptors"
import { ksmEncClient } from "@/api/clients"
import { AssetInChain } from "../types"
import {
fromEncointerToForeign,
fromEncointerToRelay,
getNativeAsset,
watchAccoutFreeBalance,
} from "../common"

const api = ksmEncClient.getTypedApi(ksmEnc)

const ksm: AssetInChain = {
chain: "ksmEnc",
symbol: "KSM",
watchFreeBalance: watchAccoutFreeBalance(api),
teleport: {
ksm: (...args) =>
api.tx.PolkadotXcm.limited_teleport_assets(fromEncointerToRelay(...args)),
dotAh: (from, amount, to) =>
api.tx.PolkadotXcm.limited_reserve_transfer_assets(
fromEncointerToForeign(
XcmV3JunctionNetworkId.Polkadot(),
1000,
getNativeAsset(1, amount),
from,
to,
),
),
},
}

export default [ksm]
3 changes: 2 additions & 1 deletion src/api/ksm/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import relay from "./relay"
import ah from "./ah"
import enc from "./enc"

export default [...relay, ...ah]
export default [...relay, ...ah, ...enc]
4 changes: 3 additions & 1 deletion src/api/ksm/relay.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ksm as descriptors } from "@polkadot-api/descriptors"
import { ksmClient } from "@/api/clients"
import { AssetInChain } from "../types"
import { fromRelayToAssetHub, watchAccoutFreeBalance } from "../common"
import { fromRelayToAssetHub, fromRelayToEncointer, watchAccoutFreeBalance } from "../common"

const api = ksmClient.getTypedApi(descriptors)

Expand All @@ -12,6 +12,8 @@ const ksm: AssetInChain = {
teleport: {
ksmAh: (...args) =>
api.tx.XcmPallet.limited_teleport_assets(fromRelayToAssetHub(...args)),
ksmEnc: (...args) =>
api.tx.XcmPallet.limited_teleport_assets(fromRelayToEncointer(...args)),
},
}

Expand Down
1 change: 1 addition & 0 deletions src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type ChainId =
| "wndAh"
| "ksm"
| "ksmAh"
| "ksmEnc"
| "roc"
| "rocAh"
| "pas"
Expand Down