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

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
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,64 @@
import { ApiPromise, WsProvider } from '@polkadot/api';
import { encodeAddress } from '@polkadot/util-crypto';
import { u8aToHex } from '@polkadot/util';

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

try {
// Parameters for createForeignAsset
const assetId = '340282366920938463463374607431768211455';
const xcmLocation = {
parents: 1,
interior: {
X1: [{ Parachain: 888 }],
},
};
const assetName = Array.from(Buffer.from('My Foreign Asset'));
const assetSymbol = Array.from(Buffer.from('xcMFA'));
const decimals = 18;

// Parameters for xcmWeightTrader.addAsset
const relativePrice = '5000';

// Create individual calls
const createAssetCall = api.tx.evmForeignAssets.createForeignAsset(
assetId,
xcmLocation,
decimals,
assetSymbol,
assetName
);

const addAssetCall = api.tx.xcmWeightTrader.addAsset(
xcmLocation,
relativePrice
);

// Combine calls into a batch
const batchCall = api.tx.utility.batch([createAssetCall, addAssetCall]);

// Get the encoded call data for the batch
const encodedBatchData = batchCall.method.toHex();

console.log('Individual Calls:');
console.log('Create Asset Call:', createAssetCall.method.toHex());
console.log('Add Asset Call:', addAssetCall.method.toHex());
console.log('\nBatch Call:');
console.log('Encoded Batch Call Data:', encodedBatchData);

await api.disconnect();
return encodedBatchData;
} catch (error) {
console.error('Error details:', error);
await api.disconnect();
throw error;
}
}

// Execute the function
getEncodedBatchCallData()
.catch(console.error)
.finally(() => process.exit());
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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 });

// Example parameters with correct types
const assetId = '340282366920938463463374607431768211455';

// XCM V4 Multilocation structured according to Moonbeam's format
const xcmLocation = {
parents: 1,
interior: {
X1: [
{ Parachain: 888 }, // Replace with actual parachain ID
],
},
};

// Convert strings to Uint8Array for Bytes type
const assetName = Array.from(Buffer.from('My Foreign Asset'));
const assetSymbol = Array.from(Buffer.from('xcMFA'));

// Decimals as a number (not string)
const decimals = 18;

try {
// Create the call
const call = api.tx.evmForeignAssets.createForeignAsset(
assetId,
xcmLocation,
decimals,
assetSymbol,
assetName
);

// 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);
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,99 @@
import { ApiPromise, WsProvider } from '@polkadot/api';
import { Keyring } from '@polkadot/keyring';
import { cryptoWaitReady } from '@polkadot/util-crypto';
import { u8aToHex } from '@polkadot/util';

async function submitPreimage() {
// Wait for the crypto libraries to be ready
await cryptoWaitReady();

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

// Initialize keyring and add account
const keyring = new Keyring({ type: 'ethereum' }); // Use ethereum type for Moonbeam

// IMPORTANT: Replace with your private key
const PRIVATE_KEY = 'INSERT-PRIVATE-KEY'; // e.g., '0x1234...'
const account = keyring.addFromUri(PRIVATE_KEY);

console.log('Account address:', account.address);

// The encoded call data to be submitted as preimage
const encodedCallData =
'0x0100083800ffffffffffffffffffffffffffffffff010100e10d121478634d4641404d7920466f726569676e2041737365743a00010100e10d88130000000000000000000000000000';

try {
// Create the notePreimage call
const preimageCall = api.tx.preimage.notePreimage(encodedCallData);

// Calculate the hash of the preimage
const preimageHash = await api.registry.hash(encodedCallData);

console.log('Submitting preimage for encoded call data:');
console.log('Original Call Data:', encodedCallData);
console.log('Note Preimage Encoded Call:', preimageCall.method.toHex());
console.log('Preimage Hash:', preimageHash.toHex());

// Get the account's current nonce
const nonce = await api.rpc.system.accountNextIndex(account.address);

// Submit and wait for transaction
const txHash = await new Promise((resolve, reject) => {
preimageCall
.signAndSend(
account,
{ nonce },
({ status, dispatchError, events }) => {
if (dispatchError) {
if (dispatchError.isModule) {
// Handle module error
const decoded = api.registry.findMetaError(
dispatchError.asModule
);
const { docs, name, section } = decoded;
reject(new Error(`${section}.${name}: ${docs.join(' ')}`));
} else {
// Handle other errors
reject(new Error(dispatchError.toString()));
}
}

if (status.isInBlock) {
console.log(
`Transaction included in blockHash ${status.asInBlock}`
);
} else if (status.isFinalized) {
console.log(
`Transaction finalized in blockHash ${status.asFinalized}`
);
resolve(status.asFinalized);
}
}
)
.catch(reject);
});

console.log('Transaction successful! Hash:', txHash.toHex());
console.log(
'Preimage hash (save this for reference):',
preimageHash.toHex()
);

await api.disconnect();
return {
transactionHash: txHash.toHex(),
preimageHash: preimageHash.toHex(),
};
} catch (error) {
console.error('Error details:', error);
await api.disconnect();
throw error;
}
}

// Execute the function
submitPreimage()
.catch(console.error)
.finally(() => process.exit());
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { ApiPromise, WsProvider } from '@polkadot/api';
import { Keyring } from '@polkadot/keyring';
import { cryptoWaitReady } from '@polkadot/util-crypto';

async function submitReferendum() {
// Wait for the crypto libraries to be ready
await cryptoWaitReady();

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

// Initialize keyring and add account
const keyring = new Keyring({ type: 'ethereum' });

// IMPORTANT: Replace with your private key
const PRIVATE_KEY = 'INSERT-PRIVATE-KEY';
const account = keyring.addFromUri(PRIVATE_KEY);

console.log('Account address:', account.address);

try {
// Get current block number for enactment calculation
const currentBlock = await api.query.system.number();
const enactmentBlock = currentBlock.toNumber() + 100;

// The preimage data and hash
const preimageHash =
'0x2ac24641cebeff3827ba10eb264d2db2990607c89a3fa2c8b2fef6b1e37e35e3';

// Length of the original batch call data
// This should match the length of your original encoded call data
const preimageLength = 73; // This is the length of your batch call encoded data

// Parameters for the referendum
const origin = { Origins: 'FastGeneralAdmin' };
const proposal = {
Lookup: {
hash: preimageHash,
len: preimageLength,
},
};
const enactment = { After: 100 };

// Create the referendum submission call
const referendumCall = api.tx.referenda.submit(origin, proposal, enactment);

// Get the encoded call data
const encodedCall = referendumCall.method.toHex();
console.log('\nReferendum submission details:');
console.log('Encoded Call:', encodedCall);
console.log('Current Block:', currentBlock.toString());
console.log('Enactment Block:', enactmentBlock);
console.log('Preimage Hash:', preimageHash);
console.log('Preimage Length:', preimageLength);

// Get the account's current nonce
const nonce = await api.rpc.system.accountNextIndex(account.address);

// Submit and wait for transaction
const txHash = await new Promise((resolve, reject) => {
referendumCall
.signAndSend(
account,
{ nonce },
({ status, dispatchError, events }) => {
if (dispatchError) {
if (dispatchError.isModule) {
const decoded = api.registry.findMetaError(
dispatchError.asModule
);
const { docs, name, section } = decoded;
reject(new Error(`${section}.${name}: ${docs.join(' ')}`));
} else {
reject(new Error(dispatchError.toString()));
}
}

if (status.isInBlock) {
console.log(
`Transaction included in blockHash ${status.asInBlock}`
);
// Try to find the referendum index from events
events.forEach(({ event }) => {
if (
event.section === 'referenda' &&
event.method === 'Submitted'
) {
const [referendumIndex] = event.data;
console.log(
`Referendum submitted with index: ${referendumIndex}`
);
}
});
} else if (status.isFinalized) {
console.log(
`Transaction finalized in blockHash ${status.asFinalized}`
);
resolve(status.asFinalized);
}
}
)
.catch(reject);
});

console.log('Transaction successful! Hash:', txHash.toHex());

await api.disconnect();
return {
transactionHash: txHash.toHex(),
};
} catch (error) {
console.error('Error details:', error);
await api.disconnect();
throw error;
}
}

// Execute the function
submitReferendum()
.catch(console.error)
.finally(() => process.exit());
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div id="termynal" data-termynal>
<span data-ty="input"><span class="file-path"></span>node batch-calls.js</span>
<span data-ty>Individual Calls:</span>
<span data-ty>Create Asset Call: 0x3800ffffffffffffffffffffffffffffffff010100e10d121478634d4641404d7920466f726569676e204173736574</span>
<span data-ty>Add Asset Call: 0x3a00010100e10d88130000000000000000000000000000</span>
<span data-ty>Batch Call:</span>
<span data-ty>Encoded Batch Call Data: 0x0100083800ffffffffffffffffffffffffffffffff010100e10d121478634d4641404d7920466f726569676e2041737365743a00010100e10d88130000000000000000000000000000</span>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div id="termynal" data-termynal>
<span data-ty="input"><span class="file-path"></span>node createForeignAsset.js</span>
<span data-ty>Encoded Call Data: 0x3800ffffffffffffffffffffffffffffffff010100e10d121478634d4641404d7920466f726569676e204173736574</span>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div id="termynal" data-termynal>
<span data-ty="input"><span class="file-path"></span>node submit-preimage.js</span>
<span data-ty>Account address: 0x569BE8d8b04538318e1722f6e375FD381D2da865</span>
<span data-ty>Submitting preimage for encoded call data:</span>
<span data-ty>Original Call Data: 0x0100083800ffffffffffffffffffffffffffffffff010100e10d121478634d4641404d7920466f726569676e2041737365743a00010100e10d88130000000000000000000000000000</span>
<span data-ty>Note Preimage Encoded Call: 0x2c0025010100083800ffffffffffffffffffffffffffffffff010100e10d121478634d4641404d7920466f726569676e2041737365743a00010100e10d88130000000000000000000000000000</span>
<span data-ty>Preimage Hash: 0x2ac24641cebeff3827ba10eb264d2db2990607c89a3fa2c8b2fef6b1e37e35e3</span>
<span data-ty>Transaction included in blockHash 0xf84ef0d3238cbc656e482494778acc9d849f14b5173d34c727a720aee48a6e69</span>
<span data-ty>Transaction finalized in blockHash 0xf84ef0d3238cbc656e482494778acc9d849f14b5173d34c727a720aee48a6e69</span>
<span data-ty>Transaction successful! Hash: 0xf84ef0d3238cbc656e482494778acc9d849f14b5173d34c727a720aee48a6e69</span>
<span data-ty>Preimage hash (save this for reference): 0x2ac24641cebeff3827ba10eb264d2db2990607c89a3fa2c8b2fef6b1e37e35e3</span>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<div id="termynal" data-termynal>
<span data-ty="input"><span class="file-path"></span>node submit-referendum.js</span>
<span data-ty>Account address: 0x569BE8d8b04538318e1722f6e375FD381D2da865</span>
<span data-ty>Referendum submission details:</span>
<span data-ty>Encoded Call: 0x2a002b04022ac24641cebeff3827ba10eb264d2db2990607c89a3fa2c8b2fef6b1e37e35e3490000000164000000</span>
<span data-ty>Current Block: 9150304</span>
<span data-ty>Enactment Block: 9150404</span>
<span data-ty>Preimage Hash: 0x2ac24641cebeff3827ba10eb264d2db2990607c89a3fa2c8b2fef6b1e37e35e3</span>
<span data-ty>Preimage Length: 73</span>
<span data-ty>Transaction included in blockHash 0x13d7f729af6fc803c863b6beba9e2e788f1d85114f5165c41f96b80dfdf49844</span>
<span data-ty>Referendum submitted with index: 79</span>
<span data-ty>Transaction finalized in blockHash 0x13d7f729af6fc803c863b6beba9e2e788f1d85114f5165c41f96b80dfdf49844</span>
<span data-ty>Transaction successful! Hash: 0x13d7f729af6fc803c863b6beba9e2e788f1d85114f5165c41f96b80dfdf49844</span>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div id="termynal" data-termynal>
<span data-ty="input"><span class="file-path"></span>node xcmWeightTrader.js</span>
<span data-ty>Encoded Call Data: 0x3a00010100e10d88130000000000000000000000000000</span>
</div>
Loading
Loading