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

New XC Asset Registration Process #1013

Merged
merged 65 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from 44 commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
0c08cb2
rev
themacexpert Sep 30, 2024
1b4b1af
Merge branch 'master' into themacexpert/ups
themacexpert Oct 23, 2024
6e8beb0
rev
themacexpert Oct 23, 2024
3e0c659
rev
themacexpert Oct 23, 2024
446028d
rev
themacexpert Oct 23, 2024
e24eac7
rev
themacexpert Oct 23, 2024
8e60218
rev
themacexpert Oct 23, 2024
563698e
add
themacexpert Oct 23, 2024
5499060
run grammarly
themacexpert Oct 23, 2024
54470e9
run prettier
themacexpert Oct 23, 2024
0a7cca0
rev
themacexpert Oct 23, 2024
e1f2dd1
revision
themacexpert Dec 13, 2024
faa5e73
revisions
themacexpert Dec 13, 2024
0561cd7
add
themacexpert Jan 15, 2025
33c1390
rev
themacexpert Jan 23, 2025
e01b316
clarify asset ID creation process
themacexpert Jan 23, 2025
a1aff3e
update relative price
themacexpert Jan 24, 2025
ca00d3a
clarify relative price etc
themacexpert Jan 24, 2025
e58fc11
rev
themacexpert Jan 24, 2025
ac80547
rev
themacexpert Jan 24, 2025
41df3b7
Update builders/interoperability/xcm/xc-registration/assets.md
themacexpert Jan 29, 2025
8086a12
Update builders/interoperability/xcm/xc-registration/assets.md
themacexpert Jan 29, 2025
cfdea79
update relative price script
themacexpert Jan 29, 2025
b463579
clarify relative price calculation with steps etc
themacexpert Jan 30, 2025
2eb4e22
progress
themacexpert Jan 30, 2025
a747f91
add
themacexpert Jan 31, 2025
020cb72
run grammarly
themacexpert Jan 31, 2025
2eabe59
additional grammar etc
themacexpert Jan 31, 2025
4af094d
Update .snippets/code/builders/interoperability/xcm/xc-registration/a…
themacexpert Feb 7, 2025
da2a5f3
Update builders/interoperability/xcm/xc-registration/assets.md
themacexpert Feb 7, 2025
234d455
Update builders/interoperability/xcm/xc-registration/assets.md
themacexpert Feb 7, 2025
be3d061
Update builders/interoperability/xcm/xc-registration/assets.md
themacexpert Feb 7, 2025
5a396d6
Update builders/interoperability/xcm/xc-registration/assets.md
themacexpert Feb 7, 2025
68e2c6b
Update builders/interoperability/xcm/xc-registration/assets.md
themacexpert Feb 7, 2025
4b54698
Update builders/interoperability/xcm/xc-registration/assets.md
themacexpert Feb 7, 2025
4966320
remove how relative price is calculated
themacexpert Feb 7, 2025
ae66ffc
Update builders/interoperability/xcm/xc-registration/assets.md
themacexpert Feb 7, 2025
b07a1ca
Update builders/interoperability/xcm/xc-registration/assets.md
themacexpert Feb 7, 2025
231dd39
Update builders/interoperability/xcm/xc-registration/assets.md
themacexpert Feb 7, 2025
aacef73
clarify commands
themacexpert Feb 7, 2025
53591ef
add generic call propose info
themacexpert Feb 7, 2025
16a1be4
revise relative price examples
themacexpert Feb 7, 2025
f877a22
revise image diagram
themacexpert Feb 7, 2025
25b908e
rev
themacexpert Feb 7, 2025
2345876
Update .snippets/code/builders/interoperability/xcm/xc-registration/a…
themacexpert Feb 11, 2025
5f4137e
Update .snippets/code/builders/interoperability/xcm/xc-registration/a…
themacexpert Feb 11, 2025
35737ad
Update .snippets/code/builders/interoperability/xcm/xc-registration/a…
themacexpert Feb 11, 2025
69b0fa1
Update .snippets/code/builders/interoperability/xcm/xc-registration/i…
themacexpert Feb 11, 2025
9c87ad7
Update .snippets/code/builders/interoperability/xcm/xc-registration/i…
themacexpert Feb 11, 2025
ea67718
Update builders/interoperability/xcm/xc-registration/assets.md
themacexpert Feb 11, 2025
d61d611
Update builders/interoperability/xcm/xc-registration/assets.md
themacexpert Feb 11, 2025
ae90244
Update builders/interoperability/xcm/xc-registration/assets.md
themacexpert Feb 11, 2025
27cba57
Update builders/interoperability/xcm/xc-registration/assets.md
themacexpert Feb 11, 2025
39d8b3a
Update builders/interoperability/xcm/xc-registration/assets.md
themacexpert Feb 11, 2025
07dbbc4
Update builders/interoperability/xcm/xc-registration/assets.md
themacexpert Feb 11, 2025
64a4bd2
Update builders/interoperability/xcm/xc-registration/assets.md
themacexpert Feb 11, 2025
07bbaf3
Update builders/interoperability/xcm/xc-registration/assets.md
themacexpert Feb 11, 2025
bdaa52b
Update builders/interoperability/xcm/xc-registration/assets.md
themacexpert Feb 11, 2025
a451f9f
Update builders/interoperability/xcm/xc-registration/assets.md
themacexpert Feb 11, 2025
2611503
Update builders/interoperability/xcm/xc-registration/assets.md
themacexpert Feb 11, 2025
7a620de
Update builders/interoperability/xcm/xc-registration/assets.md
themacexpert Feb 11, 2025
654c3c1
make bold lists
themacexpert Feb 11, 2025
5cba02d
rev
themacexpert Feb 11, 2025
868c61f
prettier
themacexpert Feb 11, 2025
1a29184
rev
themacexpert Feb 11, 2025
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import axios from "axios";

// CoinGecko IDs for the networks
const NETWORK_IDS = {
GLMR: "moonbeam",
MOVR: "moonriver",
};

async function calculateRelativePrice(
assetPrice: number,
assetDecimals: number,
network: "GLMR" | "MOVR"
): Promise<bigint> {
try {
// Fetch the native token price from CoinGecko
const response = await axios.get(
`https://api.coingecko.com/api/v3/simple/price?ids=${NETWORK_IDS[network]}&vs_currencies=usd`
);

const nativeTokenPrice = response.data[NETWORK_IDS[network]].usd;

// Calculate relative price with 18 decimal places
// Formula: (assetPrice / nativeTokenPrice) * 10^18
// This gives us how many units of the asset we need to equal 1 unit of native token
const relativePrice = BigInt(
0.175 * Math.pow(10, 18 - assetDecimals) * (assetPrice / nativeTokenPrice) * Math.pow(10, 18)
);

// Return as string to preserve precision
return relativePrice;
} catch (error) {
if (error instanceof Error) {
throw new Error(`Failed to calculate relative price: ${error.message}`);
}
throw error;
}
}

function validateInput(
price: string,
decimals: string,
network: string
): { assetPrice: number; assetDecimals: number; network: "GLMR" | "MOVR" } {
// Validate price
const assetPrice = parseFloat(price);
if (isNaN(assetPrice) || assetPrice <= 0) {
throw new Error("Price must be a positive number");
}

// Validate decimals
const assetDecimals = parseFloat(decimals);
if (isNaN(assetDecimals) || assetDecimals <= 0) {
throw new Error("Decimals must be a positive number");
}

// Validate network
const upperNetwork = network.toUpperCase() as "GLMR" | "MOVR";
if (!["GLMR", "MOVR"].includes(upperNetwork)) {
throw new Error("Network must be either GLMR or MOVR");
}

return { assetPrice, assetDecimals, network: upperNetwork };
}

function printUsage() {
console.log("\nUsage:");
console.log("npx ts-node calculate-relative-price.ts <price> <decimals> <network>");
console.log("\nExample:");
console.log("npx ts-node calculate-relative-price.ts 0.25 12 GLMR");
console.log("\nParameters:");
console.log("price - The price of your asset in USD");
console.log("decimals - The decimals of your asset");
console.log("network - Either GLMR or MOVR");
}

async function main() {
try {
// Get command line arguments
const [, , price, decimals, network] = process.argv;

// Check if help flag is passed
if (price === "--help" || price === "-h") {
printUsage();
return;
}

// Check if required arguments are provided
if (!price || !decimals || !network) {
console.error("Error: Missing required arguments");
printUsage();
process.exit(1);
}

// Validate inputs
const {
assetPrice,
assetDecimals,
network: validNetwork,
} = validateInput(price, decimals, network);

console.log(
`\nCalculating relative price for asset worth $${assetPrice} against ${validNetwork}...`
);
const relativePrice = await calculateRelativePrice(assetPrice, assetDecimals, validNetwork);
const nativeTokenPrice = (
await axios.get(
`https://api.coingecko.com/api/v3/simple/price?ids=${NETWORK_IDS[validNetwork]}&vs_currencies=usd`
)
).data[NETWORK_IDS[validNetwork]].usd;

const decimalRatio = nativeTokenPrice / assetPrice;

console.log(`\nResults:`);
console.log(`Asset Price: $${assetPrice}`);
console.log(`Network: ${validNetwork}`);
console.log(`Native Token Price (from CoinGecko): $${nativeTokenPrice}`);
console.log(`\nRelative Price Analysis:`);
console.log(
`1 ${validNetwork} is equal to approximately ${decimalRatio.toFixed(
3
)} of your specified token.`
);
console.log(
`With 18 decimals, 1 ${validNetwork} or in WEI, 1000000000000000000 is equal to a relative price of ${relativePrice} units of your token`
);
console.log(`\nRelative Price: ${relativePrice}`);
console.log(
`\nThe relative price you should specify in asset registration steps is ${relativePrice}\n`
);
} catch (error) {
console.error("\nError:", error instanceof Error ? error.message : error);
process.exit(1);
}
}

main();
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<div id="termynal" data-termynal>
<span data-ty="input">yarn calculate-relative-price 0.04901 12 GLMR</span>
<span data-ty></span>
<span data-ty>Calculating relative price for asset worth $0.04901 against GLMR...</span>
<span data-ty></span>
<span data-ty>Results:</span>
<span data-ty>Asset Price: $0.04901</span>
<span data-ty>Network: GLMR</span>
<span data-ty>Native Token Price (from CoinGecko): $0.126009</span>
<span data-ty></span>
<span data-ty>Relative Price Analysis:</span>
<span data-ty>1 GLMR is equal to approximately 2.571 of your specified token.</span>
<span data-ty>With 18 decimals, 1 GLMR (in WEI: 1000000000000000000) is equal to 68064582688538114392064 units of your token.</span>
<span data-ty></span>
<span data-ty>Relative Price: 68064582688538114392064</span>
<span data-ty></span>
<span data-ty>The relative price you should specify in asset registration steps is 68064582688538114392064</span>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<div id="termynal" data-termynal>
<span data-ty>scripts % yarn register-asset --w wss://wss.api.moonbeam.network \</span>
<span data-ty>--asset '{ "parents": 1, "interior": {"X1": [{ "Parachain": 3370}]}}' \</span>
<span data-ty>--symbol "xcLAOS" \</span>
<span data-ty>--decimals 18 \</span>
<span data-ty>--name "LAOS" \</span>
<span data-ty>--relative-price 68064582688538114392064</span>
<span data-ty>yarn run v1.22.22</span>
<span data-ty>warning ../../../package.json: No license field</span>
<span data-ty>$ ts-node 'scripts/xcm-asset-registrator.ts' --w wss://wss.api.moonbeam.network --asset '{ "parents": 1, "interior": {"X1": [{ "Parachain": 3370}]}}' --symbol xcLAOS --decimals 18 --name LAOS --relative-price 68064582688538114392064</span>
<span data-ty>XCM Version is V4</span>
<span data-ty>Encoded Call Data for registerAsset is 0x72008835e1b9f588de47ec5e4a828e4e70dd010100a934121878634c414f53104c414f53</span>
<span data-ty>Encoded Call Data for Set Relative Price is 0x7300010100a9340000805c9cf5d5c9690e000000000000</span>
<span data-ty>Encoded Call Data for batched is 0x1e000872008835e1b9f588de47ec5e4a828e4e70dd010100a934121878634c414f53104c414f537300010100a9340000805c9cf5d5c9690e000000000000</span>
<span data-ty>✨ Done in 5.20s.</span>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { ApiPromise, WsProvider } from '@polkadot/api';
import { encodeAddress } from '@polkadot/util-crypto';
import { u8aToHex } from '@polkadot/util';

async function getEncodedCallData() {
// Connect to Moonbase Alpha
const provider = new WsProvider('wss://moonbase-alpha.public.blastapi.io');
const api = await ApiPromise.create({ provider });

// Corrected XCM V4 Multilocation structure
const xcmLocation = {
parents: 1,
interior: {
X1: [{ Parachain: 888 }],
},
};

// Relative price as u128
const relativePrice = 'INSERT-RELATIVE-PRICE'; // Insert value generated
// from the relative price script e.g. 1299476000000000000

try {
// Create the call
const call = api.tx.xcmWeightTrader.addAsset(xcmLocation, relativePrice);

// Get the encoded call data
const encodedData = call.method.toHex();
console.log('Encoded Call Data:', encodedData);

await api.disconnect();
return encodedData;
} catch (error) {
console.error('Error details:', error);
console.error(
'Attempted XCM Location:',
JSON.stringify(xcmLocation, null, 2)
);
await api.disconnect();
throw error;
}
}

// Execute the function
getEncodedCallData()
.catch(console.error)
.finally(() => process.exit());
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<div id="termynal" data-termynal>
<span data-ty>yarn hrmp-manipulator --target-para-id 3370 \</span>
<span data-ty>--parachain-ws-provider wss://moonbeam.public.blastapi.io \</span>
<span data-ty>--relay-ws-provider wss://polkadot-rpc.publicnode.com \</span>
<span data-ty>--hrmp-action accept</span>
<span data-ty>yarn run v1.22.22</span>
<span data-ty>warning ../../../package.json: No license field</span>
<span data-ty>$ ts-node 'scripts/hrmp-channel-manipulator.ts' --target-para-id 3370 --parachain-ws-provider wss://moonbeam.public.blastapi.io --relay-ws-provider wss://polkadot-rpc.publicnode.com --hrmp-action accept</span>
<span data-ty></span>
<span data-ty>Genesis hash is: 0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3</span>
<span data-ty>Polkadot</span>
<span data-ty>FeeAmount is: 10000000000</span>
<span data-ty>XCM Version is V4</span>
<span data-ty>Encoded Call Data for Tx is 0x6b09012a0d0000010401000100e40b5402000000000000000000000002286bee020004000100</span>
<span data-ty>✨ Done in 4.39s.</span>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<div id="termynal" data-termynal>
<span data-ty>yarn hrmp-manipulator --target-para-id 3370 \</span>
<span data-ty>--parachain-ws-provider wss://moonbeam.public.blastapi.io \</span>
<span data-ty>--relay-ws-provider wss://polkadot-rpc.publicnode.com \</span>
<span data-ty>--max-capacity 1000 --max-message-size 102400 \</span>
<span data-ty>--hrmp-action open</span>
<span data-ty>yarn run v1.22.22</span>
<span data-ty>warning ../../../package.json: No license field</span>
<span data-ty>$ ts-node 'scripts/hrmp-channel-manipulator.ts' --target-para-id 3370 --parachain-ws-provider wss://moonbeam.public.blastapi.io --relay-ws-provider wss://polkadot-rpc.publicnode.com --max-capacity 1000 --max-message-size 102400 --hrmp-action open</span>
<span data-ty></span>
<span data-ty>Genesis hash is: 0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3</span>
<span data-ty>Polkadot</span>
<span data-ty>FeeAmount is: 10000000000</span>
<span data-ty>XCM Version is V4</span>
<span data-ty>Encoded Call Data for Tx is 0x6b09002a0d0000e803000000900100010401000100e40b5402000000000000000000000002286bee020004000100</span>
<span data-ty>✨ Done in 4.25s.</span>
</div>
Loading