Skip to content

Commit

Permalink
merge main Sovereign Account Balance checking
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaurello committed Oct 14, 2024
2 parents 3f105bc + 69e4516 commit 8f058ac
Show file tree
Hide file tree
Showing 19 changed files with 209 additions and 78 deletions.
9 changes: 9 additions & 0 deletions packages/builder/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# @moonbeam-network/xcm-builder

## 2.5.2

### Patch Changes

- [#339](https://github.com/moonbeam-foundation/xcm-sdk/pull/339) [`6e543ce`](https://github.com/moonbeam-foundation/xcm-sdk/commit/6e543ce1adb1e81d283e1b1811d0eab6bffaad47) Thanks [@mmaurello](https://github.com/mmaurello)! - Add sovereign account balance checking

- Updated dependencies [[`6e543ce`](https://github.com/moonbeam-foundation/xcm-sdk/commit/6e543ce1adb1e81d283e1b1811d0eab6bffaad47)]:
- @moonbeam-network/xcm-types@2.5.0

## 2.5.1

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/builder/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@moonbeam-network/xcm-builder",
"version": "2.5.1",
"version": "2.5.2",
"description": "Moonbeam XCM builder",
"scripts": {
"build": "tsup",
Expand Down
31 changes: 31 additions & 0 deletions packages/builder/src/extrinsic/pallets/polkadotXcm/polkadotXcm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,36 @@ export function polkadotXcm() {
}),
};
},
trasferAssets: () => {
const func = 'transferAssets';

return {
here: (): ExtrinsicConfigBuilder => ({
build: (params) =>
new ExtrinsicConfig({
module: pallet,
func,
getArgs: (extrinsicFunction) =>
getPolkadotXcmExtrinsicArgs({
...params,
func: extrinsicFunction,
asset: [
{
id: {
Concrete: {
parents: 1,
interior: 'Here',
},
},
fun: {
Fungible: params.asset.amount,
},
},
],
}),
}),
}),
};
},
};
}
12 changes: 12 additions & 0 deletions packages/config/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# @moonbeam-network/xcm-config

## 2.7.0

### Minor Changes

- [#339](https://github.com/moonbeam-foundation/xcm-sdk/pull/339) [`6e543ce`](https://github.com/moonbeam-foundation/xcm-sdk/commit/6e543ce1adb1e81d283e1b1811d0eab6bffaad47) Thanks [@mmaurello](https://github.com/mmaurello)! - Add sovereign account balance checking

### Patch Changes

- Updated dependencies [[`6e543ce`](https://github.com/moonbeam-foundation/xcm-sdk/commit/6e543ce1adb1e81d283e1b1811d0eab6bffaad47)]:
- @moonbeam-network/xcm-types@2.5.0
- @moonbeam-network/xcm-builder@2.5.2

## 2.6.4

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/config/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@moonbeam-network/xcm-config",
"version": "2.6.4",
"version": "2.7.0",
"description": "All necessary configuration to transfer assets from Moonbeam, Moonriver, Moonbase to other parachains and back",
"scripts": {
"build": "tsup",
Expand Down
5 changes: 5 additions & 0 deletions packages/config/src/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ export const alphanetRelay = new Parachain({
ecosystem: Ecosystem.AlphanetRelay,
genesisHash:
'0xe1ea3ab1d46ba8f4898b6b4b9c54ffc05282d299f89e84bd0fd08067758c9443',
isRelay: true,
isTestChain: true,
key: 'alphanet-relay',
name: 'Alphanet Relay',
Expand Down Expand Up @@ -749,6 +750,7 @@ export const kusama = new Parachain({
ecosystem: Ecosystem.Kusama,
genesisHash:
'0xb0a8d493285c2df73290dfb7e61f870f17b41801197a149ca93654499ea3dafe',
isRelay: true,
key: 'kusama',
name: 'Kusama',
nativeAsset: ksm,
Expand Down Expand Up @@ -781,6 +783,7 @@ export const kusamaAssetHub = new Parachain({
decimals: 12,
}),
],
checkSovereignAccountBalances: true,
ecosystem: Ecosystem.Kusama,
genesisHash:
'0x48239ef607d7928874027a43a67689209727dfb3d3dc5e5b03a39bdc2eda771a',
Expand Down Expand Up @@ -1963,6 +1966,7 @@ export const polkadot = new Parachain({
ecosystem: Ecosystem.Polkadot,
genesisHash:
'0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3',
isRelay: true,
key: 'polkadot',
name: 'Polkadot',
nativeAsset: dot,
Expand Down Expand Up @@ -2030,6 +2034,7 @@ export const polkadotAssetHub = new Parachain({
},
}),
],
checkSovereignAccountBalances: true,
ecosystem: Ecosystem.Polkadot,
genesisHash:
'0x68d56f15f85d3136970ec16946040bc1752654e906147f7e43e9d539d7c3de2f',
Expand Down
1 change: 1 addition & 0 deletions packages/config/src/xcm-configs/moonbeam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,7 @@ export const moonbeamRoutes = new ChainRoutes({
fee: {
amount: 0.2,
asset: usdt,
balance: BalanceBuilder().substrate().assets().account(),
},
},
contract: ContractBuilder().Xtokens().transferMultiCurrencies(),
Expand Down
13 changes: 13 additions & 0 deletions packages/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# @moonbeam-network/xcm-sdk

## 2.7.0

### Minor Changes

- [#339](https://github.com/moonbeam-foundation/xcm-sdk/pull/339) [`6e543ce`](https://github.com/moonbeam-foundation/xcm-sdk/commit/6e543ce1adb1e81d283e1b1811d0eab6bffaad47) Thanks [@mmaurello](https://github.com/mmaurello)! - Add sovereign account balance checking

### Patch Changes

- Updated dependencies [[`6e543ce`](https://github.com/moonbeam-foundation/xcm-sdk/commit/6e543ce1adb1e81d283e1b1811d0eab6bffaad47)]:
- @moonbeam-network/xcm-config@2.7.0
- @moonbeam-network/xcm-types@2.5.0
- @moonbeam-network/xcm-builder@2.5.2

## 2.6.4

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@moonbeam-network/xcm-sdk",
"version": "2.6.4",
"version": "2.7.0",
"description": "The Moonbeam XCM SDK enables developers to easily deposit and withdraw assets to Moonbeam/Moonriver from the relay chain and other parachains in the Polkadot/Kusama ecosystem",
"scripts": {
"build": "tsup",
Expand Down
57 changes: 56 additions & 1 deletion packages/sdk/src/getTransferData/getDestinationData.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import type { AssetRoute } from '@moonbeam-network/xcm-config';
import type { AssetRoute, DestinationConfig, SourceConfig } from '@moonbeam-network/xcm-config';
import type { DestinationChainTransferData } from '../sdk.interfaces';
import {
getAssetMin,
getBalance,
getDestinationFee,
getExistentialDeposit,
} from './getTransferData.utils';
import {
getSovereignAccountAddresses,
} from '@moonbeam-network/xcm-utils';
import { Parachain } from '@moonbeam-network/xcm-types';

export interface GetDestinationDataParams {
route: AssetRoute;
Expand Down Expand Up @@ -44,5 +48,56 @@ export async function getDestinationData({
existentialDeposit,
fee,
min,
sovereignAccountBalances: await getSovereignAccountBalances({
source: route.source,
destination: route.destination,
}),
};

}

interface GetSovereignAccountBalancesProps {
source: SourceConfig;
destination: DestinationConfig;
}

async function getSovereignAccountBalances( {
destination,
source,
}: GetSovereignAccountBalancesProps) {

if(!Parachain.is(source.chain) || !Parachain.is(destination.chain)) {
return undefined
}

const sovereignAccountAddresses = getSovereignAccountAddresses(
source.chain.parachainId,
);

const destinationFeeAssetBalance =
destination.fee.balance;

const sovereignAccountAddress = destination.chain.isRelay
? sovereignAccountAddresses.relay
: sovereignAccountAddresses.generic;

const sovereignAccountBalance = await getBalance({
address: sovereignAccountAddress,
asset: destination.chain.getChainAsset(destination.asset),
builder: destination.balance,
chain: destination.chain,
});

const sovereignAccountFeeAssetBalance = destinationFeeAssetBalance
? await getBalance({
address: sovereignAccountAddress,
asset: destination.chain.getChainAsset(destination.fee.asset),
builder: destinationFeeAssetBalance,
chain: destination.chain,
})
: undefined;
return {
feeAssetBalance: sovereignAccountFeeAssetBalance?.amount,
transferAssetBalance: sovereignAccountBalance.amount,
};
}
1 change: 1 addition & 0 deletions packages/sdk/src/getTransferData/getSourceData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export async function getSourceData({
return {
balance,
chain: source,
destinationFee,
destinationFeeBalance,
existentialDeposit,
fee,
Expand Down
9 changes: 8 additions & 1 deletion packages/sdk/src/getTransferData/getTransferData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { EvmService } from '../services/evm/EvmService';
import { PolkadotService } from '../services/polkadot';
import { getDestinationData } from './getDestinationData';
import { getSourceData } from './getSourceData';
import { convertToChainDecimals, getMin } from './getTransferData.utils';
import { convertToChainDecimals, getMin, validateSovereignAccountBalances} from './getTransferData.utils';

export interface GetTransferDataParams {
route: AssetRoute;
Expand Down Expand Up @@ -49,6 +49,7 @@ export async function getTransferData({
const bigAmount = Big(
toBigInt(amount, sourceData.balance.decimals).toString(),
);

const result = bigAmount.minus(
sourceData.balance.isSame(destinationFee)
? destinationFee.toBig()
Expand All @@ -66,9 +67,15 @@ export async function getTransferData({
amount,
{ evmSigner, polkadotSigner }: Partial<Signers>,
): Promise<string> {

const source = route.source.chain as AnyParachain;
const destination = route.destination.chain as AnyParachain;
const bigintAmount = toBigInt(amount, sourceData.balance.decimals);
validateSovereignAccountBalances({
amount: bigintAmount,
destination,
source,
});
const asset = AssetAmount.fromChainAsset(
route.source.chain.getChainAsset(route.source.asset),
{ amount: bigintAmount },
Expand Down
41 changes: 39 additions & 2 deletions packages/sdk/src/getTransferData/getTransferData.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,24 @@ import {
type ChainAsset,
EvmChain,
EvmParachain,
Parachain,
} from '@moonbeam-network/xcm-types';
import { convertDecimals, toBigInt } from '@moonbeam-network/xcm-utils';
import Big from 'big.js';
import type { DestinationChainTransferData } from '../sdk.interfaces';
import { EvmService } from '../services/evm/EvmService';
import { PolkadotService } from '../services/polkadot';
import {
DestinationChainTransferData,
SourceChainTransferData,
} from '../sdk.interfaces';


export interface GetBalancesParams {
address: string;
asset: ChainAsset;
builder: BalanceConfigBuilder;
chain: AnyChain;
}

export async function getBalance({
address,
asset,
Expand Down Expand Up @@ -338,3 +342,36 @@ export async function getContractFee({
);
}
}

interface ValidateSovereignAccountBalancesProps {
amount: bigint;
destination: DestinationChainTransferData;
source: SourceChainTransferData;
}

export function validateSovereignAccountBalances({
amount,
source,
destination,
}: ValidateSovereignAccountBalancesProps): void {
if (
!Parachain.is(destination.chain) ||
!destination.chain.checkSovereignAccountBalances ||
!destination.sovereignAccountBalances
) {
return;
}
const { feeAssetBalance, transferAssetBalance } =
destination.sovereignAccountBalances;

if (amount > transferAssetBalance) {
throw new Error(
`${source.chain.name} Sovereign account in ${destination.chain.name} does not have enough balance for this transaction`,
);
}
if (feeAssetBalance && source.destinationFee.amount > feeAssetBalance) {
throw new Error(
`${source.chain.name} Sovereign account in ${destination.chain.name} does not have enough balance to pay for fees for this transaction`,
);
}
}
10 changes: 9 additions & 1 deletion packages/sdk/src/sdk.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,20 @@ export interface TransferData {
}

export interface SourceChainTransferData extends ChainTransferData {
destinationFee: AssetAmount;
destinationFeeBalance: AssetAmount;
feeBalance: AssetAmount;
max: AssetAmount;
}

export interface DestinationChainTransferData extends ChainTransferData {}
export interface SovereignAccountBalance {
feeAssetBalance: bigint | undefined;
transferAssetBalance: bigint;
}

export interface DestinationChainTransferData extends ChainTransferData {
sovereignAccountBalances?: SovereignAccountBalance;
}

export interface ChainTransferData {
balance: AssetAmount;
Expand Down
Loading

0 comments on commit 8f058ac

Please sign in to comment.