Skip to content

Commit 811f6ae

Browse files
mmaurelloElmar Kenigs
and
Elmar Kenigs
authored
Bit.Country Pioneer integration with Moonbase (#39)
* initial pioneer configuration * WIP neer config and balance interface change * adapt neer config to interface * add initial bit configuration * fix and remove unnecessary test * small adjustments and remove obsolete snapshots * add enum constant * add enum constant when missing * add tokens function for each type to avoid clustering configs * fix tests and add new * Update packages/config/src/balance/balance.ts Co-authored-by: Elmar Kenigs <elmar@purestake.com> * improve typing on AssetSymbols * implement the functionality to transfer multiple assets in Withdraw with transferMultiCurrencies from the precompiles * update snapshot * move precompile function call to Contract handler * consideration of min amount of auxiliar that pays for fees * pass origin account to sdk and do balance calculations inside * Update packages/config/src/config/moonbase/assets/bit.ts Co-authored-by: Elmar Kenigs <elmar@purestake.com> * return 2 different mins from withdraw and make origin account optional * return origin balance and not calculations for enough balance * update simple example * change origin account param to have no breaking changes * go back to returning min in withdraw to avoid having breaking changes * remove originAccount param * update example Co-authored-by: Elmar Kenigs <elmar@purestake.com>
1 parent 652c13a commit 811f6ae

36 files changed

+471
-145
lines changed

packages/config/src/balance/__snapshots__/balance.test.ts.snap

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,39 @@ exports[`balance tokens should be correct balance config 1`] = `
7575
}
7676
`;
7777
78+
exports[`balance tokens should be correct balance config foreignAsset 1`] = `
79+
{
80+
"calc": [Function],
81+
"function": "accounts",
82+
"getParams": [Function],
83+
"pallet": "tokens",
84+
"path": [],
85+
}
86+
`;
87+
7888
exports[`balance tokens should be correct params with ForeignAsset 1`] = `
7989
[
8090
"<ACCOUNT>",
8191
{
82-
"ForeignAsset": 5,
92+
"ForeignAsset": 0,
93+
},
94+
]
95+
`;
96+
97+
exports[`balance tokens should be correct params with FungibleToken 1`] = `
98+
[
99+
"<ACCOUNT>",
100+
{
101+
"FungibleToken": 0,
102+
},
103+
]
104+
`;
105+
106+
exports[`balance tokens should be correct params with MiningResource 1`] = `
107+
[
108+
"<ACCOUNT>",
109+
{
110+
"MiningResource": 0,
83111
},
84112
]
85113
`;

packages/config/src/balance/balance.constants.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,12 @@ export enum BalanceFunction {
1313
AssetMetadatas = 'assetMetadatas',
1414
CurrencyMetadatas = 'currencyMetadatas',
1515
}
16+
17+
export enum BalanceCurrencyTypes {
18+
Token = 'Token',
19+
Token2 = 'Token2',
20+
ForeignAsset = 'ForeignAsset',
21+
NativeToken = 'NativeToken',
22+
MiningResource = 'MiningResource',
23+
FungibleToken = 'FungibleToken',
24+
}

packages/config/src/balance/balance.interfaces.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import { u128 } from '@polkadot/types';
22
import { PalletBalancesAccountData } from '@polkadot/types/lookup';
33
import { AssetSymbol } from '../constants';
44
import { AssetId } from '../interfaces';
5-
import { BalanceFunction, BalancePallet } from './balance.constants';
5+
import {
6+
BalanceCurrencyTypes,
7+
BalanceFunction,
8+
BalancePallet,
9+
} from './balance.constants';
610

711
export type BalanceConfig<Symbols extends AssetSymbol = AssetSymbol> =
812
| SystemBalanceConfig
@@ -46,19 +50,20 @@ export interface TokensBalanceConfig<
4650
pallet: BalancePallet.Tokens;
4751
function: BalanceFunction.Accounts;
4852
path: [];
49-
getParams: (account: string) => [
50-
string,
51-
(
52-
| {
53-
Token: Symbols | 'MOVR' | 'KUSD';
54-
}
55-
| { ForeignAsset: AssetId }
56-
| { Token2: AssetId }
57-
),
58-
];
53+
getParams: (account: string) => [string, TokensBalanceParamAsset<Symbols>];
5954
calc: (data: TokensPalletAccountData) => bigint;
6055
}
6156

57+
export type TokensBalanceParamAsset<Symbols extends AssetSymbol = AssetSymbol> =
58+
59+
| {
60+
[BalanceCurrencyTypes.Token]: Symbols | AssetSymbol.KUSD;
61+
}
62+
| { [BalanceCurrencyTypes.ForeignAsset]: AssetId }
63+
| { [BalanceCurrencyTypes.MiningResource]: AssetId }
64+
| { [BalanceCurrencyTypes.FungibleToken]: AssetId }
65+
| { [BalanceCurrencyTypes.Token2]: AssetId };
66+
6267
export type MinBalanceConfig =
6368
| MinBalanceAssetsConfig
6469
| MinBalanceAssetRegistryConfig;

packages/config/src/balance/balance.test.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { AssetSymbol } from '../constants';
12
import { createBalanceBuilder } from './balance';
23

34
describe('balance', () => {
@@ -39,7 +40,7 @@ describe('balance', () => {
3940
});
4041

4142
describe('tokens', () => {
42-
const cfg = balance.tokens('MOVR');
43+
const cfg = balance.tokens().token(AssetSymbol.MOVR);
4344

4445
it('should be correct balance config', () => {
4546
expect(cfg).toMatchSnapshot();
@@ -49,16 +50,32 @@ describe('balance', () => {
4950
expect(cfg.getParams(account)).toMatchSnapshot();
5051
});
5152

52-
it('should be correct params with ForeignAsset', () => {
53-
const cfg2 = balance.tokens(5);
53+
const cfg2 = balance.tokens().foreignAsset(0);
54+
55+
it('should be correct balance config foreignAsset', () => {
56+
expect(cfg2).toMatchSnapshot();
57+
});
5458

59+
it('should be correct params with ForeignAsset', () => {
5560
expect(cfg2.getParams(account)).toMatchSnapshot();
5661
});
5762

63+
const cfg3 = balance.tokens().miningResource(0);
64+
65+
it('should be correct params with MiningResource', () => {
66+
expect(cfg3.getParams(account)).toMatchSnapshot();
67+
});
68+
69+
const cfg4 = balance.tokens().fungibleToken(0);
70+
71+
it('should be correct params with FungibleToken', () => {
72+
expect(cfg4.getParams(account)).toMatchSnapshot();
73+
});
74+
5875
it('should be correct params with Token2', () => {
59-
const cfg2 = balance.tokens2(1);
76+
const cfg5 = balance.tokens().token2(1);
6077

61-
expect(cfg2.getParams(account)).toMatchSnapshot();
78+
expect(cfg5.getParams(account)).toMatchSnapshot();
6279
});
6380
});
6481
});

packages/config/src/balance/balance.ts

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@ import { u128 } from '@polkadot/types';
44
import { PalletBalancesAccountData } from '@polkadot/types/lookup';
55
import { AssetSymbol } from '../constants';
66
import { AssetId } from '../interfaces';
7-
import { BalanceFunction, BalancePallet } from './balance.constants';
7+
import {
8+
BalanceCurrencyTypes,
9+
BalanceFunction,
10+
BalancePallet,
11+
} from './balance.constants';
812
import {
913
AssetsBalanceConfig,
1014
MinBalanceAssetRegistryConfig,
1115
MinBalanceAssetsConfig,
1216
OrmlTokensBalanceConfig,
1317
SystemBalanceConfig,
1418
TokensBalanceConfig,
19+
TokensBalanceParamAsset,
1520
TokensPalletAccountData,
1621
} from './balance.interfaces';
1722

@@ -26,9 +31,7 @@ export function createBalanceBuilder<
2631
minCurrencyMetadata,
2732
ormlTokens,
2833
system,
29-
tokens: (asset: number | bigint | Symbols | 'MOVR' | 'KUSD' | 'AUSD') =>
30-
tokens<Symbols>(asset),
31-
tokens2: (asset: AssetId) => tokens2<Symbols>(asset),
34+
tokens: () => tokens<Symbols>(),
3235
};
3336
}
3437

@@ -91,35 +94,30 @@ function system(): SystemBalanceConfig {
9194
};
9295
}
9396

94-
function calcTokensBalance({ free, frozen }: TokensPalletAccountData): bigint {
95-
return BigInt(free.sub(frozen).toString());
96-
}
97-
98-
function tokens<Symbols extends AssetSymbol = AssetSymbol>(
99-
asset: number | bigint | Symbols | 'MOVR' | 'KUSD' | 'AUSD',
100-
): TokensBalanceConfig<Symbols> {
97+
function tokens<Symbols extends AssetSymbol = AssetSymbol>() {
10198
return {
102-
pallet: BalancePallet.Tokens,
103-
function: BalanceFunction.Accounts,
104-
path: [],
105-
getParams: (account: string) => [
106-
account,
107-
Number.isInteger(asset)
108-
? { ForeignAsset: asset as number }
109-
: { Token: asset as Symbols },
110-
],
111-
calc: calcTokensBalance,
99+
foreignAsset: (asset: AssetId | Symbols) =>
100+
tokensBase<Symbols>({ [BalanceCurrencyTypes.ForeignAsset]: asset }),
101+
fungibleToken: (asset: AssetId) =>
102+
tokensBase<Symbols>({ [BalanceCurrencyTypes.FungibleToken]: asset }),
103+
miningResource: (asset: AssetId) =>
104+
tokensBase<Symbols>({ [BalanceCurrencyTypes.MiningResource]: asset }),
105+
token: (asset: Symbols | AssetSymbol.KUSD) =>
106+
tokensBase<Symbols>({ [BalanceCurrencyTypes.Token]: asset }),
107+
token2: (asset: AssetId) =>
108+
tokensBase<Symbols>({ [BalanceCurrencyTypes.Token2]: asset }),
112109
};
113110
}
114111

115-
function tokens2<Symbols extends AssetSymbol = AssetSymbol>(
116-
asset: AssetId,
112+
function tokensBase<Symbols extends AssetSymbol = AssetSymbol>(
113+
asset: TokensBalanceParamAsset<Symbols>,
117114
): TokensBalanceConfig<Symbols> {
118115
return {
119116
pallet: BalancePallet.Tokens,
120117
function: BalanceFunction.Accounts,
121118
path: [],
122-
getParams: (account: string) => [account, { Token2: asset }],
123-
calc: calcTokensBalance,
119+
getParams: (account: string) => [account, asset],
120+
calc: ({ free, frozen }: TokensPalletAccountData) =>
121+
BigInt(free.sub(frozen).toString()),
124122
};
125123
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { AssetSymbol, ChainKey } from '../../../constants';
2+
import {
3+
XTokensExtrinsicCurrencyTypes,
4+
XTokensExtrinsicSuccessEvent,
5+
} from '../../../extrinsic';
6+
import { WithdrawConfig } from '../../../withdraw';
7+
import {
8+
assets,
9+
balance,
10+
chains,
11+
extrinsic,
12+
withdraw,
13+
} from '../moonbase.common';
14+
import { MoonbaseXcmConfig } from '../moonbase.interfaces';
15+
import { NEER } from './neer';
16+
17+
const asset = assets[AssetSymbol.BIT];
18+
const feeAsset = assets[AssetSymbol.NEER];
19+
const origin = chains[ChainKey.BitCountryPioneer];
20+
const neerWithdrawConfig = NEER.withdraw[
21+
origin.key
22+
] as WithdrawConfig<AssetSymbol.NEER>;
23+
24+
export const BIT: MoonbaseXcmConfig = {
25+
asset,
26+
origin,
27+
deposit: {
28+
[origin.key]: {
29+
source: origin,
30+
balance: balance.tokens().miningResource(0),
31+
sourceFeeBalance: balance.system(),
32+
xcmFeeAsset: {
33+
asset: feeAsset,
34+
balance: balance.system(),
35+
},
36+
extrinsic: extrinsic
37+
.xTokens()
38+
.transferMultiCurrencies()
39+
.successEvent(XTokensExtrinsicSuccessEvent.TransferredMultiAssets)
40+
.origin(origin)
41+
.assets(
42+
{
43+
[XTokensExtrinsicCurrencyTypes.MiningResource]: 0,
44+
},
45+
{
46+
[XTokensExtrinsicCurrencyTypes.NativeToken]: 0,
47+
},
48+
),
49+
},
50+
},
51+
withdraw: {
52+
[origin.key]: withdraw.xTokens({
53+
balance: balance.tokens().miningResource(0),
54+
destination: origin,
55+
feePerWeight: neerWithdrawConfig.feePerWeight,
56+
weight: neerWithdrawConfig.weight,
57+
xcmFeeAsset: {
58+
asset: feeAsset,
59+
balance: {
60+
destination: balance.system(),
61+
origin: balance.assets(feeAsset.id),
62+
},
63+
},
64+
}),
65+
},
66+
};

packages/config/src/config/moonbase/assets/dev.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ import { MoonbaseXcmConfig } from '../moonbase.interfaces';
1616

1717
const asset = assets[AssetSymbol.DEV];
1818
const clover = chains[ChainKey.CloverAlphanet];
19+
const pioneer = chains[ChainKey.BitCountryPioneer];
1920

2021
const cloverDevId = getMoonAssetId(clover);
22+
const pioneerDevId = getMoonAssetId(pioneer);
2123

2224
export const DEV: MoonbaseXcmConfig = {
2325
asset,
@@ -36,12 +38,28 @@ export const DEV: MoonbaseXcmConfig = {
3638
[XTokensExtrinsicCurrencyTypes.OtherReserve]: cloverDevId,
3739
}),
3840
},
41+
[pioneer.key]: {
42+
source: pioneer,
43+
balance: balance.tokens().fungibleToken(pioneerDevId),
44+
sourceFeeBalance: balance.system(),
45+
extrinsic: extrinsic
46+
.xTokens()
47+
.transfer()
48+
.successEvent(XTokensExtrinsicSuccessEvent.TransferredMultiAssets)
49+
.origin(pioneer)
50+
.asset({ [XTokensExtrinsicCurrencyTypes.FungibleToken]: pioneerDevId }),
51+
},
3952
},
4053
withdraw: {
4154
[clover.key]: withdraw.xTokens({
4255
balance: balance.assets(cloverDevId),
4356
destination: clover,
4457
feePerWeight: 50_000,
4558
}),
59+
[pioneer.key]: withdraw.xTokens({
60+
balance: balance.tokens().fungibleToken(pioneerDevId),
61+
destination: pioneer,
62+
feePerWeight: 50_000,
63+
}),
4664
},
4765
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { AssetSymbol, ChainKey } from '../../../constants';
2+
import {
3+
XTokensExtrinsicCurrencyTypes,
4+
XTokensExtrinsicSuccessEvent,
5+
} from '../../../extrinsic';
6+
import {
7+
assets,
8+
balance,
9+
chains,
10+
extrinsic,
11+
withdraw,
12+
} from '../moonbase.common';
13+
import { MoonbaseXcmConfig } from '../moonbase.interfaces';
14+
15+
const asset = assets[AssetSymbol.NEER];
16+
const origin = chains[ChainKey.BitCountryPioneer];
17+
18+
export const NEER: MoonbaseXcmConfig = {
19+
asset,
20+
origin,
21+
deposit: {
22+
[origin.key]: {
23+
source: origin,
24+
balance: balance.system(),
25+
extrinsic: extrinsic
26+
.xTokens()
27+
.transfer()
28+
.successEvent(XTokensExtrinsicSuccessEvent.TransferredMultiAssets)
29+
.origin(origin)
30+
.asset({ [XTokensExtrinsicCurrencyTypes.NativeToken]: 0 }),
31+
},
32+
},
33+
withdraw: {
34+
[origin.key]: withdraw.xTokens({
35+
balance: balance.system(),
36+
destination: origin,
37+
feePerWeight: 8_000_000,
38+
}),
39+
},
40+
};

0 commit comments

Comments
 (0)