Skip to content

Commit fdb57ca

Browse files
ntn-x2Ad96el
andauthored
feat: switch functionality (#658)
Fixes KILTprotocol/ticket#3467. Not meant to be merged until all other stacked PRs will be merged into this. Adds the first round of features to the swap pallet, specifically it includes a swap pool that supports only the runtime local currency, swappable 1:1 (not configurable yet) with a configurable remote token. The XCM fees are paid in a local fungible token as injected by the runtime. The pallet also exposes two implementations of the `IsReserve` trait that must be added to the runtime to allow the configured tokens (to swap back to local currency and to pay for XCM fees) to be reserve-transferred to this chain. That code is not yet part of this PR. Additional TODOs for the pallet is both within the code where applicable, and in the pallet `README.md` file. --------- Co-authored-by: Adel Golghalyani <48685760+Ad96el@users.noreply.github.com> Co-authored-by: Adel Golghalyani <ad96el@gmail.com>
1 parent 39b3a6f commit fdb57ca

File tree

117 files changed

+16624
-338
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

117 files changed

+16624
-338
lines changed

.github/workflows/integration-tests.yml

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ jobs:
2929
env:
3030
working-dir: ./integration-tests/chopsticks
3131
CI: "true"
32+
PEREGRINE_WASM_OVERRIDE: ../../target/debug/wbuild/peregrine-runtime/peregrine_runtime.wasm
3233
defaults:
3334
run:
3435
working-directory: ${{ env.working-dir }}
@@ -49,6 +50,9 @@ jobs:
4950

5051
- name: Linting
5152
run: yarn lint
53+
54+
- name: Build Runtime
55+
run: cargo build -p peregrine-runtime
5256

5357
- name: Test Suite
5458
run: yarn test:CI

Cargo.lock

+37
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ attestation = { path = "pallets/attestation", default-features = fals
5757
ctype = { path = "pallets/ctype", default-features = false }
5858
delegation = { path = "pallets/delegation", default-features = false }
5959
did = { path = "pallets/did", default-features = false }
60+
pallet-asset-switch = { path = "pallets/pallet-asset-switch", default-features = false }
6061
pallet-configuration = { path = "pallets/pallet-configuration", default-features = false }
6162
pallet-deposit-storage = { path = "pallets/pallet-deposit-storage", default-features = false }
6263
pallet-did-lookup = { path = "pallets/pallet-did-lookup", default-features = false }
@@ -85,6 +86,7 @@ kilt-runtime-api-did = { path = "runtime-api/did", default-featur
8586
kilt-runtime-api-dip-provider = { path = "runtime-api/dip-provider", default-features = false }
8687
kilt-runtime-api-public-credentials = { path = "runtime-api/public-credentials", default-features = false }
8788
kilt-runtime-api-staking = { path = "runtime-api/staking", default-features = false }
89+
pallet-asset-switch-runtime-api = { path = "runtime-api/asset-switch", default-features = false }
8890

8991
# Internal KILT runtimes (with default disabled)
9092
kestrel-runtime = { path = "runtimes/kestrel", default-features = false }
@@ -125,13 +127,15 @@ rococo-emulated-chain = { git = "https://github.com/paritytech/polka
125127
xcm-emulator = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-crates-io-v1.7.0" }
126128

127129
# Substrate (with default disabled)
130+
128131
frame-benchmarking = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-crates-io-v1.7.0" }
129132
frame-benchmarking-cli = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-crates-io-v1.7.0" }
130133
frame-executive = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-crates-io-v1.7.0" }
131134
frame-support = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-crates-io-v1.7.0" }
132135
frame-system = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-crates-io-v1.7.0" }
133136
frame-system-rpc-runtime-api = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-crates-io-v1.7.0" }
134137
frame-try-runtime = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-crates-io-v1.7.0" }
138+
pallet-assets = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-crates-io-v1.7.0" }
135139
pallet-aura = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-crates-io-v1.7.0" }
136140
pallet-authorship = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-crates-io-v1.7.0" }
137141
pallet-balances = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-crates-io-v1.7.0" }

dip-template/runtimes/dip-consumer/src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,11 @@ impl frame_system::Config for Runtime {
230230
type Version = Version;
231231
}
232232

233-
/// Maximum number of blocks simultaneously accepted by the Runtime, not yet included into the
234-
/// relay chain.
233+
/// Maximum number of blocks simultaneously accepted by the Runtime, not yet
234+
/// included into the relay chain.
235235
const UNINCLUDED_SEGMENT_CAPACITY: u32 = 1;
236-
/// How many parachain blocks are processed by the relay chain per parent. Limits the number of
237-
/// blocks authored per slot.
236+
/// How many parachain blocks are processed by the relay chain per parent.
237+
/// Limits the number of blocks authored per slot.
238238
const BLOCK_PROCESSING_VELOCITY: u32 = 1;
239239
/// Relay chain slot duration, in milliseconds.
240240
const RELAY_CHAIN_SLOT_DURATION_MILLIS: u32 = 6000;

dip-template/runtimes/dip-provider/src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,11 @@ impl frame_system::Config for Runtime {
231231
type Version = Version;
232232
}
233233

234-
/// Maximum number of blocks simultaneously accepted by the Runtime, not yet included into the
235-
/// relay chain.
234+
/// Maximum number of blocks simultaneously accepted by the Runtime, not yet
235+
/// included into the relay chain.
236236
const UNINCLUDED_SEGMENT_CAPACITY: u32 = 1;
237-
/// How many parachain blocks are processed by the relay chain per parent. Limits the number of
238-
/// blocks authored per slot.
237+
/// How many parachain blocks are processed by the relay chain per parent.
238+
/// Limits the number of blocks authored per slot.
239239
const BLOCK_PROCESSING_VELOCITY: u32 = 1;
240240
/// Relay chain slot duration, in milliseconds.
241241
const RELAY_CHAIN_SLOT_DURATION_MILLIS: u32 = 6000;

integration-tests/chopsticks/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,22 @@
1919
"eslint-config-prettier": "^9.1.0",
2020
"eslint-config-standard-with-typescript": "^43.0.1",
2121
"eslint-plugin-import": "^2.25.2",
22+
"eslint-plugin-jsx-a11y": "^6.8.0",
2223
"eslint-plugin-n": "^15.0.0 || ^16.0.0 ",
2324
"eslint-plugin-prettier": "^5.1.3",
2425
"eslint-plugin-promise": "^6.0.0",
2526
"prettier": "^3.2.5",
2627
"ts-node": "^10.9.2",
2728
"tsx": "^4.7.1",
2829
"typescript": "*",
29-
"vitest": "^1.4.0",
30-
"eslint-plugin-jsx-a11y": "^6.8.0"
30+
"vitest": "^1.4.0"
3131
},
3232
"scripts": {
3333
"ts-check": "tsc --noEmit",
3434
"lint": "eslint src && prettier --check src",
3535
"lint:fix": "eslint --fix src && prettier --write src",
3636
"clean": "rm -rf ./db",
3737
"test": "LOG_LEVEL=error vitest",
38-
"test:CI": "vitest --bail 0 --no-file-parallelism"
38+
"test:CI": "vitest --no-file-parallelism --retry 3"
3939
}
4040
}

integration-tests/chopsticks/src/network/assetHub.ts

+70-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,43 @@ export const getSetupOptions = ({
1919
blockNumber,
2020
}) as SetupOption
2121

22+
/// AssetHub has no own coin. Teleported dots are used as the native token.
23+
export function assignDotTokensToAccountsAsStorage(addr: string[], balance: bigint = initialBalanceDOT) {
24+
return {
25+
System: {
26+
Account: addr.map((address) => [[address], { providers: 1, data: { free: balance.toString() } }]),
27+
},
28+
}
29+
}
30+
31+
export function createForeignAsset(manager: string, assetId = eKiltLocation) {
32+
return {
33+
foreignAssets: {
34+
asset: [
35+
[
36+
[assetId],
37+
{
38+
owner: manager,
39+
issuer: manager,
40+
admin: manager,
41+
freezer: manager,
42+
// Just make it big enough
43+
supply: '10000000000000000000000000000',
44+
deposit: 0,
45+
minBalance: 0,
46+
isSufficient: false,
47+
accounts: 0,
48+
sufficients: 0,
49+
approvals: 0,
50+
status: 'Live',
51+
},
52+
],
53+
],
54+
},
55+
}
56+
}
57+
58+
/// Assigns KSM to an account
2259
export function assignKSMtoAccounts(addr: string[], balance: bigint = initialBalanceDOT) {
2360
return {
2461
foreignAssets: {
@@ -35,11 +72,20 @@ export function assignKSMtoAccounts(addr: string[], balance: bigint = initialBal
3572
}
3673
}
3774

38-
/// AssetHub has no own coin. Teleported dots are used as the native token.
39-
export function assignDotTokensToAccounts(addr: string[], balance: bigint = initialBalanceDOT) {
75+
/// Assigns the foreign asset to the accounts.
76+
/// Does not check if supply is matching the sum of the account balances.
77+
export function assignForeignAssetToAccounts(accountInfo: [string, bigint][], assetId = eKiltLocation) {
4078
return {
41-
System: {
42-
Account: addr.map((address) => [[address], { providers: 1, data: { free: balance.toString() } }]),
79+
foreignAssets: {
80+
account: accountInfo.map(([account, balance]) => [
81+
[assetId, account],
82+
{
83+
balance: balance,
84+
status: 'Liquid',
85+
reason: 'Consumer',
86+
extra: null,
87+
},
88+
]),
4389
},
4490
}
4591
}
@@ -59,6 +105,26 @@ export const KSMAssetLocation = {
59105
// Sibling Sovereign Account
60106
export const sovereignAccountOnSiblingChains = '4qXPdpimHh8TR24RSk994yVzxx4TLfvKj5i1qH5puvWmfAqy'
61107

108+
/// Native token in AssetHub
109+
export const nativeTokenLocation = { parents: 1, interior: 'Here' }
110+
111+
export const eKiltLocation = {
112+
parents: 2,
113+
interior: {
114+
X2: [
115+
{
116+
GlobalConsensus: { Ethereum: { chainId: 11155111 } },
117+
},
118+
{
119+
AccountKey20: {
120+
network: null,
121+
key: '0x06012c8cf97bead5deae237070f9587f8e7a266d',
122+
},
123+
},
124+
],
125+
},
126+
}
127+
62128
export async function getContext(): Promise<Config> {
63129
const options = getSetupOptions({})
64130
return setupContext(options)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { setupContext, SetupOption } from '@acala-network/chopsticks-testing'
2+
import type { Config } from './types.js'
3+
import { initialBalanceDOT, initialBalanceHDX, initialBalanceKILT, toNumber } from '../utils.js'
4+
5+
/// Options used to create the HydraDx context
6+
export const options: SetupOption = {
7+
endpoint: process.env.BASILISK_WS || ['wss://basilisk-rococo-rpc.play.hydration.cloud'],
8+
db: './db/basilisk.db.sqlite',
9+
port: toNumber(process.env.HYDRADX_PORT) || 9005,
10+
}
11+
12+
// On Basilisk, there is only KSM. We use that currency and treat it as ROC, since the location is {parents: 1, interior: Here}
13+
export const dotTokenId = 5
14+
15+
export const eKILTTokenId = 90
16+
17+
/// Assigns the native tokens to an accounts
18+
export function assignNativeTokensToAccounts(addr: string[], balance: bigint = initialBalanceHDX) {
19+
return {
20+
System: {
21+
Account: addr.map((address) => [[address], { providers: 1, data: { free: balance } }]),
22+
},
23+
}
24+
}
25+
26+
/// Assigns ROCs tokens to an accounts
27+
export function assignRocTokensToAccounts(addr: string[], balance: bigint = initialBalanceDOT) {
28+
return {
29+
Tokens: {
30+
Accounts: addr.map((address) => [[address, dotTokenId], { free: balance }]),
31+
},
32+
}
33+
}
34+
35+
export function createRemoteAsset(
36+
addr: string[],
37+
assetLocation: Record<string, unknown>,
38+
balance: bigint = initialBalanceKILT
39+
) {
40+
return {
41+
assetRegistry: {
42+
assetIds: [[['eKILT'], 90]],
43+
assetLocations: [[[90], assetLocation]],
44+
assetMetadataMap: [[[90], { symbol: 'eKILT', decimals: 15 }]],
45+
},
46+
47+
tokens: {
48+
accounts: addr.map((acc) => [[acc, 90], { free: balance }]),
49+
},
50+
}
51+
}
52+
53+
/// Basilisk ParaId
54+
export const paraId = 2090
55+
56+
export async function getContext(): Promise<Config> {
57+
return setupContext(options)
58+
}

0 commit comments

Comments
 (0)