Skip to content

Commit

Permalink
feat: switch functionality (#658)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
3 people authored Aug 14, 2024
1 parent 39b3a6f commit fdb57ca
Show file tree
Hide file tree
Showing 117 changed files with 16,624 additions and 338 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jobs:
env:
working-dir: ./integration-tests/chopsticks
CI: "true"
PEREGRINE_WASM_OVERRIDE: ../../target/debug/wbuild/peregrine-runtime/peregrine_runtime.wasm
defaults:
run:
working-directory: ${{ env.working-dir }}
Expand All @@ -49,6 +50,9 @@ jobs:

- name: Linting
run: yarn lint

- name: Build Runtime
run: cargo build -p peregrine-runtime

- name: Test Suite
run: yarn test:CI
Expand Down
37 changes: 37 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ attestation = { path = "pallets/attestation", default-features = fals
ctype = { path = "pallets/ctype", default-features = false }
delegation = { path = "pallets/delegation", default-features = false }
did = { path = "pallets/did", default-features = false }
pallet-asset-switch = { path = "pallets/pallet-asset-switch", default-features = false }
pallet-configuration = { path = "pallets/pallet-configuration", default-features = false }
pallet-deposit-storage = { path = "pallets/pallet-deposit-storage", default-features = false }
pallet-did-lookup = { path = "pallets/pallet-did-lookup", default-features = false }
Expand Down Expand Up @@ -85,6 +86,7 @@ kilt-runtime-api-did = { path = "runtime-api/did", default-featur
kilt-runtime-api-dip-provider = { path = "runtime-api/dip-provider", default-features = false }
kilt-runtime-api-public-credentials = { path = "runtime-api/public-credentials", default-features = false }
kilt-runtime-api-staking = { path = "runtime-api/staking", default-features = false }
pallet-asset-switch-runtime-api = { path = "runtime-api/asset-switch", default-features = false }

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

# Substrate (with default disabled)

frame-benchmarking = { git = "/~https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-crates-io-v1.7.0" }
frame-benchmarking-cli = { git = "/~https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-crates-io-v1.7.0" }
frame-executive = { git = "/~https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-crates-io-v1.7.0" }
frame-support = { git = "/~https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-crates-io-v1.7.0" }
frame-system = { git = "/~https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-crates-io-v1.7.0" }
frame-system-rpc-runtime-api = { git = "/~https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-crates-io-v1.7.0" }
frame-try-runtime = { git = "/~https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-crates-io-v1.7.0" }
pallet-assets = { git = "/~https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-crates-io-v1.7.0" }
pallet-aura = { git = "/~https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-crates-io-v1.7.0" }
pallet-authorship = { git = "/~https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-crates-io-v1.7.0" }
pallet-balances = { git = "/~https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-crates-io-v1.7.0" }
Expand Down
8 changes: 4 additions & 4 deletions dip-template/runtimes/dip-consumer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,11 @@ impl frame_system::Config for Runtime {
type Version = Version;
}

/// Maximum number of blocks simultaneously accepted by the Runtime, not yet included into the
/// relay chain.
/// Maximum number of blocks simultaneously accepted by the Runtime, not yet
/// included into the relay chain.
const UNINCLUDED_SEGMENT_CAPACITY: u32 = 1;
/// How many parachain blocks are processed by the relay chain per parent. Limits the number of
/// blocks authored per slot.
/// How many parachain blocks are processed by the relay chain per parent.
/// Limits the number of blocks authored per slot.
const BLOCK_PROCESSING_VELOCITY: u32 = 1;
/// Relay chain slot duration, in milliseconds.
const RELAY_CHAIN_SLOT_DURATION_MILLIS: u32 = 6000;
Expand Down
8 changes: 4 additions & 4 deletions dip-template/runtimes/dip-provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,11 @@ impl frame_system::Config for Runtime {
type Version = Version;
}

/// Maximum number of blocks simultaneously accepted by the Runtime, not yet included into the
/// relay chain.
/// Maximum number of blocks simultaneously accepted by the Runtime, not yet
/// included into the relay chain.
const UNINCLUDED_SEGMENT_CAPACITY: u32 = 1;
/// How many parachain blocks are processed by the relay chain per parent. Limits the number of
/// blocks authored per slot.
/// How many parachain blocks are processed by the relay chain per parent.
/// Limits the number of blocks authored per slot.
const BLOCK_PROCESSING_VELOCITY: u32 = 1;
/// Relay chain slot duration, in milliseconds.
const RELAY_CHAIN_SLOT_DURATION_MILLIS: u32 = 6000;
Expand Down
6 changes: 3 additions & 3 deletions integration-tests/chopsticks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@
"eslint-config-prettier": "^9.1.0",
"eslint-config-standard-with-typescript": "^43.0.1",
"eslint-plugin-import": "^2.25.2",
"eslint-plugin-jsx-a11y": "^6.8.0",
"eslint-plugin-n": "^15.0.0 || ^16.0.0 ",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-promise": "^6.0.0",
"prettier": "^3.2.5",
"ts-node": "^10.9.2",
"tsx": "^4.7.1",
"typescript": "*",
"vitest": "^1.4.0",
"eslint-plugin-jsx-a11y": "^6.8.0"
"vitest": "^1.4.0"
},
"scripts": {
"ts-check": "tsc --noEmit",
"lint": "eslint src && prettier --check src",
"lint:fix": "eslint --fix src && prettier --write src",
"clean": "rm -rf ./db",
"test": "LOG_LEVEL=error vitest",
"test:CI": "vitest --bail 0 --no-file-parallelism"
"test:CI": "vitest --no-file-parallelism --retry 3"
}
}
74 changes: 70 additions & 4 deletions integration-tests/chopsticks/src/network/assetHub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,43 @@ export const getSetupOptions = ({
blockNumber,
}) as SetupOption

/// AssetHub has no own coin. Teleported dots are used as the native token.
export function assignDotTokensToAccountsAsStorage(addr: string[], balance: bigint = initialBalanceDOT) {
return {
System: {
Account: addr.map((address) => [[address], { providers: 1, data: { free: balance.toString() } }]),
},
}
}

export function createForeignAsset(manager: string, assetId = eKiltLocation) {
return {
foreignAssets: {
asset: [
[
[assetId],
{
owner: manager,
issuer: manager,
admin: manager,
freezer: manager,
// Just make it big enough
supply: '10000000000000000000000000000',
deposit: 0,
minBalance: 0,
isSufficient: false,
accounts: 0,
sufficients: 0,
approvals: 0,
status: 'Live',
},
],
],
},
}
}

/// Assigns KSM to an account
export function assignKSMtoAccounts(addr: string[], balance: bigint = initialBalanceDOT) {
return {
foreignAssets: {
Expand All @@ -35,11 +72,20 @@ export function assignKSMtoAccounts(addr: string[], balance: bigint = initialBal
}
}

/// AssetHub has no own coin. Teleported dots are used as the native token.
export function assignDotTokensToAccounts(addr: string[], balance: bigint = initialBalanceDOT) {
/// Assigns the foreign asset to the accounts.
/// Does not check if supply is matching the sum of the account balances.
export function assignForeignAssetToAccounts(accountInfo: [string, bigint][], assetId = eKiltLocation) {
return {
System: {
Account: addr.map((address) => [[address], { providers: 1, data: { free: balance.toString() } }]),
foreignAssets: {
account: accountInfo.map(([account, balance]) => [
[assetId, account],
{
balance: balance,
status: 'Liquid',
reason: 'Consumer',
extra: null,
},
]),
},
}
}
Expand All @@ -59,6 +105,26 @@ export const KSMAssetLocation = {
// Sibling Sovereign Account
export const sovereignAccountOnSiblingChains = '4qXPdpimHh8TR24RSk994yVzxx4TLfvKj5i1qH5puvWmfAqy'

/// Native token in AssetHub
export const nativeTokenLocation = { parents: 1, interior: 'Here' }

export const eKiltLocation = {
parents: 2,
interior: {
X2: [
{
GlobalConsensus: { Ethereum: { chainId: 11155111 } },
},
{
AccountKey20: {
network: null,
key: '0x06012c8cf97bead5deae237070f9587f8e7a266d',
},
},
],
},
}

export async function getContext(): Promise<Config> {
const options = getSetupOptions({})
return setupContext(options)
Expand Down
58 changes: 58 additions & 0 deletions integration-tests/chopsticks/src/network/basilisk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { setupContext, SetupOption } from '@acala-network/chopsticks-testing'
import type { Config } from './types.js'
import { initialBalanceDOT, initialBalanceHDX, initialBalanceKILT, toNumber } from '../utils.js'

/// Options used to create the HydraDx context
export const options: SetupOption = {
endpoint: process.env.BASILISK_WS || ['wss://basilisk-rococo-rpc.play.hydration.cloud'],
db: './db/basilisk.db.sqlite',
port: toNumber(process.env.HYDRADX_PORT) || 9005,
}

// On Basilisk, there is only KSM. We use that currency and treat it as ROC, since the location is {parents: 1, interior: Here}
export const dotTokenId = 5

export const eKILTTokenId = 90

/// Assigns the native tokens to an accounts
export function assignNativeTokensToAccounts(addr: string[], balance: bigint = initialBalanceHDX) {
return {
System: {
Account: addr.map((address) => [[address], { providers: 1, data: { free: balance } }]),
},
}
}

/// Assigns ROCs tokens to an accounts
export function assignRocTokensToAccounts(addr: string[], balance: bigint = initialBalanceDOT) {
return {
Tokens: {
Accounts: addr.map((address) => [[address, dotTokenId], { free: balance }]),
},
}
}

export function createRemoteAsset(
addr: string[],
assetLocation: Record<string, unknown>,
balance: bigint = initialBalanceKILT
) {
return {
assetRegistry: {
assetIds: [[['eKILT'], 90]],
assetLocations: [[[90], assetLocation]],
assetMetadataMap: [[[90], { symbol: 'eKILT', decimals: 15 }]],
},

tokens: {
accounts: addr.map((acc) => [[acc, 90], { free: balance }]),
},
}
}

/// Basilisk ParaId
export const paraId = 2090

export async function getContext(): Promise<Config> {
return setupContext(options)
}
Loading

0 comments on commit fdb57ca

Please sign in to comment.