Skip to content

Commit

Permalink
add rococo <-> westend
Browse files Browse the repository at this point in the history
  • Loading branch information
josepot committed Apr 15, 2024
1 parent 212785c commit ab17f94
Show file tree
Hide file tree
Showing 14 changed files with 308 additions and 18 deletions.
14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
"format": "prettier --write README.md \"src/**/*.{js,jsx,ts,tsx,json,md}\"",
"preview": "vite preview"
},
"pnpm": {
"overrides": {
"smoldot": "2.0.23"
}
},
"dependencies": {
"@polkadot-api/descriptors": "^0.0.1",
"@radix-ui/react-dialog": "^1.0.5",
Expand All @@ -25,6 +30,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"rxjs": "^7.8.1",
"smoldot": "2.0.23",
"tailwind-merge": "^2.2.2",
"tailwindcss-animate": "^1.0.7"
},
Expand Down Expand Up @@ -69,6 +75,14 @@
"ksmAh": {
"wsUrl": "wss://kusama-asset-hub-rpc.polkadot.io",
"metadata": "ksmAh.scale"
},
"roc": {
"chain": "rococo_v2_2",
"metadata": "roc.scale"
},
"rocAh": {
"wsUrl": "wss://rococo-asset-hub-rpc.dwellir.com",
"metadata": "rocAh.scale"
}
}
}
19 changes: 8 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added roc.scale
Binary file not shown.
Binary file added rocAh.scale
Binary file not shown.
6 changes: 5 additions & 1 deletion src/api/chains.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import dot from "./dot"
import ksm from "./ksm"
import wnd from "./wnd"
import roc from "./roc"
import { AssetId, AssetInChain, ChainId } from "./types"

const assetsInChains = [...dot, ...ksm, ...wnd]
const assetsInChains = [...dot, ...ksm, ...wnd, ...roc]

export const chains = new Map<ChainId, Map<AssetId, AssetInChain>>()
assetsInChains.forEach((assetinChain) => {
Expand All @@ -17,6 +18,7 @@ export const ASSET_DECIMALS: Record<AssetId, number> = {
DOT: 10,
KSM: 12,
WND: 12,
ROC: 12,
}

export const CHAIN_NAMES: Record<ChainId, string> = {
Expand All @@ -26,4 +28,6 @@ export const CHAIN_NAMES: Record<ChainId, string> = {
ksmAh: "Kusama AssetHub",
wnd: "Westend RelayChain",
wndAh: "Westend AssetHub",
roc: "Rococo RelayChain",
rocAh: "Rococo AssetHub",
}
3 changes: 3 additions & 0 deletions src/api/clients/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ export { ksmAhClient } from "./ksmAh"

export { wndClient } from "./wnd"
export { wndAhClient } from "./wndAh"

export { rocClient } from "./roc"
export { rocAhClient } from "./rocAh"
9 changes: 9 additions & 0 deletions src/api/clients/roc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { createClient } from "polkadot-api"
import { getSmProvider } from "polkadot-api/sm-provider"
import { smoldot } from "./smoldot"

export const rocRelayChain = import("polkadot-api/chains/rococo_v2_2").then(
({ chainSpec }) => smoldot.addChain({ chainSpec }),
)

export const rocClient = createClient(getSmProvider(rocRelayChain))
32 changes: 32 additions & 0 deletions src/api/clients/rocAh.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { createClient } from "polkadot-api"
import { getSmProvider } from "polkadot-api/sm-provider"
import { smoldot } from "./smoldot"
import { rocRelayChain } from "./roc"

const chainSpec = `{
"name": "Rococo Asset Hub",
"id": "asset-hub-rococo",
"chainType": "Live",
"bootNodes": [
"/dns/rococo-asset-hub-bootnode-0.polkadot.io/tcp/30333/p2p/12D3KooWRrZMndHAopzao34uGsN7srjS3gh9nAjTGKLSyJeU31Lg",
"/dns/rococo-asset-hub-bootnode-1.polkadot.io/tcp/30333/p2p/12D3KooWAewimoNJqMaiiV5pYiowA5hLuh5JS5QiRJCCyWVrrSTS"
],
"telemetryEndpoints": null,
"protocolId": null,
"properties": {
"tokenDecimals": 12,
"tokenSymbol": "ROC"
},
"relay_chain": "rococo_v2_2",
"para_id": 1000,
"codeSubstitutes": {},
"genesis": {
"stateRootHash": "0xad0224198565f04f9f7a6120231d310f9280669ec3395b898dd2bb69e1deb87d"
}
}`

const smoldotParaChain = rocRelayChain.then((relayChain) =>
smoldot.addChain({ chainSpec, potentialRelayChains: [relayChain] }),
)

export const rocAhClient = createClient(getSmProvider(smoldotParaChain))
76 changes: 75 additions & 1 deletion src/api/clients/smoldot.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,78 @@
import SmWorker from "polkadot-api/smoldot/worker?worker"
import { startFromWorker } from "polkadot-api/smoldot/from-worker"

export const smoldot = startFromWorker(new SmWorker())
/*
interface QueueNode<T> {
value: T
next?: QueueNode<T>
}
export default class Queue<T> {
private first?: QueueNode<T>
private last?: QueueNode<T>
constructor(...vals: T[]) {
if (vals.length === 0) return
vals.forEach((val) => this.push(val))
}
push(value: T) {
const nextLast: QueueNode<T> = { value }
if (this.last === undefined) {
this.last = nextLast
this.first = this.last
} else {
this.last.next = nextLast
this.last = nextLast
}
}
pop() {
const result = this.first?.value
if (this.first) {
this.first = this.first.next
if (!this.first) {
this.last = undefined
}
}
return result
}
peek() {
return this.first?.value
}
}
const messages = new Queue<string>()
let tickDate = ""
const setTickDate = () => {
tickDate = new Date().toISOString()
setTimeout(setTickDate, 0)
}
setTickDate()
const getTickDate = () => tickDate
*/

export const smoldot = startFromWorker(
new SmWorker() /*, {
maxLogLevel: 9,
logCallback: (level: number, target: string, message: string) => {
messages.push(`${getTickDate()} (${level})${target}\n${message}\n\n`)
},
}*/,
)
/*;(window as any).getLogs = () => {
console.log("touch the window now!")
setTimeout(() => {
console.log("putting the logs in place")
let data = ``
while (messages.peek() !== undefined) {
data += messages.pop()!
}
navigator.clipboard.writeText(data)
console.log("copied!")
}, 3_000)
}*/
73 changes: 73 additions & 0 deletions src/api/roc/ah.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import {
DotXcmV3Junctions,
DotXcmV3MultiassetAssetId,
XcmV3Junction,
XcmV3JunctionNetworkId,
XcmV3MultiassetFungibility,
XcmVersionedMultiAssets,
rocAh as descriptors,
} from "@polkadot-api/descriptors"
import { rocAhClient } from "@/api/clients"
import { AssetInChain } from "../types"
import {
fromAssetHubToForeign,
fromAssetHubToRelay,
getNativeAsset,
watchAccoutFreeBalance,
watchForeingAssetAccoutFreeBalance,
} from "../common"

const api = rocAhClient.getTypedApi(descriptors)

const chain = "rocAh"
const roc: AssetInChain = {
chain,
symbol: "ROC",
watchFreeBalance: watchAccoutFreeBalance(api),
teleport: {
roc: (...args) =>
api.tx.PolkadotXcm.limited_teleport_assets(fromAssetHubToRelay(...args)),
wndAh: (from, amount, to) =>
api.tx.PolkadotXcm.limited_reserve_transfer_assets(
fromAssetHubToForeign(
XcmV3JunctionNetworkId.Westend(),
1000,
getNativeAsset(1, amount),
from,
to,
),
),
},
}

const wndInRocAh: Parameters<typeof DotXcmV3MultiassetAssetId.Concrete>[0] = {
parents: 2,
interior: DotXcmV3Junctions.X1(
XcmV3Junction.GlobalConsensus(XcmV3JunctionNetworkId.Westend()),
),
}

const wnd: AssetInChain = {
chain,
symbol: "WND",
watchFreeBalance: watchForeingAssetAccoutFreeBalance(api, wndInRocAh),
teleport: {
wndAh: (from, amount, to) =>
api.tx.PolkadotXcm.limited_reserve_transfer_assets(
fromAssetHubToForeign(
XcmV3JunctionNetworkId.Westend(),
1000,
XcmVersionedMultiAssets.V3([
{
id: DotXcmV3MultiassetAssetId.Concrete(wndInRocAh),
fun: XcmV3MultiassetFungibility.Fungible(amount),
},
]),
from,
to,
),
),
},
}

export default [wnd, roc]
4 changes: 4 additions & 0 deletions src/api/roc/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import relay from "./relay"
import ah from "./ah"

export default [...relay, ...ah]
18 changes: 18 additions & 0 deletions src/api/roc/relay.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { roc as descriptors } from "@polkadot-api/descriptors"
import { rocClient } from "@/api/clients"
import { AssetInChain } from "../types"
import { fromRelayToAssetHub, watchAccoutFreeBalance } from "../common"

const api = rocClient.getTypedApi(descriptors)

const roc: AssetInChain = {
chain: "roc",
symbol: "ROC",
watchFreeBalance: watchAccoutFreeBalance(api),
teleport: {
rocAh: (...args) =>
api.tx.XcmPallet.limited_teleport_assets(fromRelayToAssetHub(...args)),
},
}

export default [roc]
12 changes: 10 additions & 2 deletions src/api/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import type { Observable } from "rxjs"
import type { PolkadotSigner, SS58String, Transaction } from "polkadot-api"

export type ChainId = "dot" | "dotAh" | "wnd" | "wndAh" | "ksm" | "ksmAh"
export type AssetId = "DOT" | "KSM" | "WND"
export type ChainId =
| "dot"
| "dotAh"
| "wnd"
| "wndAh"
| "ksm"
| "ksmAh"
| "roc"
| "rocAh"
export type AssetId = "DOT" | "KSM" | "WND" | "ROC"

export type TeleportAsset = (
from: PolkadotSigner,
Expand Down
Loading

0 comments on commit ab17f94

Please sign in to comment.