Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Api for moonbeam integration #1367

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions web/packages/integrations/moonbeam/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PRIVATE_KEY=
5 changes: 5 additions & 0 deletions web/packages/integrations/moonbeam/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
dist
tsconfig.tsbuildinfo
build
.env
23 changes: 23 additions & 0 deletions web/packages/integrations/moonbeam/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"overrides": [
{
"files": "*.sol",
"options": {
"printWidth": 100,
"tabWidth": 4,
"singleQuote": false,
"bracketSpacing": true,
"explicitTypes": "always"
}
},
{
"files": "*.ts",
"options": {
"semi": false,
"printWidth": 100,
"tabWidth": 4,
"singleQuote": false
}
}
]
}
39 changes: 39 additions & 0 deletions web/packages/integrations/moonbeam/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "@snowbridge/moonbeam",
"version": "0.1.27",
"description": "Snowbridge moonbeam integration",
"license": "Apache-2.0",
"repository": {
"type": "git",
"url": "/~https://github.com/Snowfork/snowbridge.git",
"directory": "web/packages/integration/moonbeam"
},
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"rebuild": "pnpm solc && pnpm typechain && tsc",
"build": "tsc",
"solc": "solcjs --abi precompiles/*.sol -o build",
"typechain": "typechain --target ethers-v6 './build/*.abi' --out-dir src/bindings",
"testTransfer": "npx ts-node src/testTransferWeth.ts start"
},
"devDependencies": {
"@typechain/ethers-v6": "^0.5.1",
"ts-node": "^10.9.2",
"tsconfig-paths": "^4.2.0",
"typechain": "^8.3.2",
"typescript": "^5.4.5",
"solc": "^0.8.28",
"prettier": "^2.8.8"
},
"dependencies": {
"@polkadot/api": "^14.3.1",
"@polkadot/keyring": "^13.2.3",
"@polkadot/types": "^14.3.1",
"@polkadot/util": "^13.2.3",
"@polkadot/util-crypto": "^13.2.3",
"@typechain/ethers-v6": "^0.5.1",
"ethers": "^6.13.5",
"dotenv": "^16.4.5"
}
}
167 changes: 167 additions & 0 deletions web/packages/integrations/moonbeam/precompiles/XcmInterface.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity >=0.8.3;

/// @author The Moonbeam Team
/// @title XCM precompile Interface
/// @dev The interface that Solidity contracts use to interact with the substrate pallet-xcm.
interface XCM {
// A location is defined by its number of parents and the encoded junctions (interior)
struct Location {
uint8 parents;
bytes[] interior;
}

// Support for Weights V2
struct Weight {
uint64 refTime;
uint64 proofSize;
}

// A way to represent fungible assets in XCM using Location format
struct AssetLocationInfo {
Location location;
uint256 amount;
}

// A way to represent fungible assets in XCM using address format
struct AssetAddressInfo {
address asset;
uint256 amount;
}

// The values start at `0` and are represented as `uint8`
enum TransferType {
Teleport,
LocalReserve,
DestinationReserve
}

/// @dev Function to send assets via XCM using transfer_assets() pallet-xcm extrinsic.
/// @custom:selector 9ea8ada7
/// @param dest The destination chain.
/// @param beneficiary The actual account that will receive the tokens on dest.
/// @param assets The combination (array) of assets to send in Location format.
/// @param feeAssetItem The index of the asset that will be used to pay for fees.
function transferAssetsLocation(
Location memory dest,
Location memory beneficiary,
AssetLocationInfo[] memory assets,
uint32 feeAssetItem
) external;

/// @dev Function to send assets via XCM to a 20 byte-like parachain
/// using transfer_assets() pallet-xcm extrinsic.
/// @custom:selector a0aeb5fe
/// @param paraId The para-id of the destination chain.
/// @param beneficiary The actual account that will receive the tokens on paraId destination.
/// @param assets The combination (array) of assets to send in Address format.
/// @param feeAssetItem The index of the asset that will be used to pay for fees.
function transferAssetsToPara20(
uint32 paraId,
address beneficiary,
AssetAddressInfo[] memory assets,
uint32 feeAssetItem
) external;

/// @dev Function to send assets via XCM to a 32 byte-like parachain
/// using transfer_assets() pallet-xcm extrinsic.
/// @custom:selector f23032c3
/// @param paraId The para-id of the destination chain.
/// @param beneficiary The actual account that will receive the tokens on paraId destination.
/// @param assets The combination (array) of assets to send in Address format.
/// @param feeAssetItem The index of the asset that will be used to pay for fees.
function transferAssetsToPara32(
uint32 paraId,
bytes32 beneficiary,
AssetAddressInfo[] memory assets,
uint32 feeAssetItem
) external;

/// @dev Function to send assets via XCM to the relay chain
/// using transfer_assets() pallet-xcm extrinsic.
/// @custom:selector 6521cc2c
/// @param beneficiary The actual account that will receive the tokens on the relay chain.
/// @param assets The combination (array) of assets to send in Address format.
/// @param feeAssetItem The index of the asset that will be used to pay for fees.
function transferAssetsToRelay(
bytes32 beneficiary,
AssetAddressInfo[] memory assets,
uint32 feeAssetItem
) external;

/// @dev Function to send assets through transfer_assets_using_type_and_then() pallet-xcm
/// extrinsic.
/// Important: in this selector RemoteReserve type (for either assets or fees) is not allowed.
/// If users want to send assets and fees (in Location format) with a remote reserve,
/// they must use the selector fc19376c.
/// @custom:selector 8425d893
/// @param dest The destination chain.
/// @param assets The combination (array) of assets to send in Location format.
/// @param assetsTransferType The TransferType corresponding to assets being sent.
/// @param remoteFeesIdIndex The index of the asset (inside assets array) to use as fees.
/// @param feesTransferType The TransferType corresponding to the asset used as fees.
/// @param customXcmOnDest The XCM message to execute on destination chain (SCALE encoded).
function transferAssetsUsingTypeAndThenLocation(
Location memory dest,
AssetLocationInfo[] memory assets,
TransferType assetsTransferType,
uint8 remoteFeesIdIndex,
TransferType feesTransferType,
bytes memory customXcmOnDest
) external;

/// @dev Function to send assets through transfer_assets_using_type_and_then() pallet-xcm
/// extrinsic.
/// @custom:selector fc19376c
/// @param dest The destination chain.
/// @param assets The combination (array) of assets to send in Location format.
/// @param remoteFeesIdIndex The index of the asset (inside assets array) to use as fees.
/// @param customXcmOnDest The XCM message to execute on destination chain (SCALE encoded).
/// @param remoteReserve The remote reserve corresponding for assets and fees. They MUST
/// share the same reserve.
function transferAssetsUsingTypeAndThenLocation(
Location memory dest,
AssetLocationInfo[] memory assets,
uint8 remoteFeesIdIndex,
bytes memory customXcmOnDest,
Location memory remoteReserve
) external;

/// @dev Function to send assets through transfer_assets_using_type_and_then() pallet-xcm
/// extrinsic.
/// Important: in this selector RemoteReserve type (for either assets or fees) is not allowed.
/// If users want to send assets and fees (in Address format) with a remote reserve,
/// they must use the selector aaecfc62.
/// @custom:selector 998093ee
/// @param dest The destination chain.
/// @param assets The combination (array) of assets to send in Address format.
/// @param assetsTransferType The TransferType corresponding to assets being sent.
/// @param remoteFeesIdIndex The index of the asset (inside assets array) to use as fees.
/// @param feesTransferType The TransferType corresponding to the asset used as fees.
/// @param customXcmOnDest The XCM message to execute on destination chain (SCALE encoded).
function transferAssetsUsingTypeAndThenAddress(
Location memory dest,
AssetAddressInfo[] memory assets,
TransferType assetsTransferType,
uint8 remoteFeesIdIndex,
TransferType feesTransferType,
bytes memory customXcmOnDest
) external;

/// @dev Function to send assets through transfer_assets_using_type_and_then() pallet-xcm
/// extrinsic.
/// @custom:selector aaecfc62
/// @param dest The destination chain.
/// @param assets The combination (array) of assets to send in Address format.
/// @param remoteFeesIdIndex The index of the asset (inside assets array) to use as fees.
/// @param customXcmOnDest The XCM message to execute on destination chain (SCALE encoded).
/// @param remoteReserve The remote reserve corresponding for assets and fees. They MUST
/// share the same reserve.
function transferAssetsUsingTypeAndThenAddress(
Location memory dest,
AssetAddressInfo[] memory assets,
uint8 remoteFeesIdIndex,
bytes memory customXcmOnDest,
Location memory remoteReserve
) external;
}
58 changes: 58 additions & 0 deletions web/packages/integrations/moonbeam/precompiles/XcmUtil.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity >=0.8.3;

/// @dev The XcmUtils contract's address.
address constant XCM_UTILS_ADDRESS = 0x000000000000000000000000000000000000080C;

/// @dev The XcmUtils contract's instance.
XcmUtils constant XCM_UTILS_CONTRACT = XcmUtils(XCM_UTILS_ADDRESS);

/// @author The Moonbeam Team
/// @title Xcm Utils Interface
/// The interface through which solidity contracts will interact with xcm utils pallet
/// @custom:address 0x000000000000000000000000000000000000080C
interface XcmUtils {
// A multilocation is defined by its number of parents and the encoded junctions (interior)
struct Multilocation {
uint8 parents;
bytes[] interior;
}

/// Get retrieve the account associated to a given MultiLocation
/// @custom:selector 343b3e00
/// @param multilocation The multilocation that we want to know to which account maps to
/// @return account The account the multilocation maps to in this chain
function multilocationToAddress(Multilocation memory multilocation)
external
view
returns (address account);

/// Get the weight that a message will consume in our chain
/// @custom:selector 25d54154
/// @param message scale encoded xcm mversioned xcm message
function weightMessage(bytes memory message)
external
view
returns (uint64 weight);

/// Get units per second charged for a given multilocation
/// @custom:selector 3f0f65db
/// @param multilocation scale encoded xcm mversioned xcm message
function getUnitsPerSecond(Multilocation memory multilocation)
external
view
returns (uint256 unitsPerSecond);

/// Execute custom xcm message
/// @dev This function CANNOT be called from a smart contract
/// @custom:selector 34334a02
/// @param message The versioned message to be executed scale encoded
/// @param maxWeight The maximum weight to be consumed
function xcmExecute(bytes memory message, uint64 maxWeight) external;

/// Send custom xcm message
/// @custom:selector 98600e64
/// @param dest The destination chain to which send this message
/// @param message The versioned message to be sent scale-encoded
function xcmSend(Multilocation memory dest, bytes memory message) external;
}
Loading
Loading