Skip to content

Commit

Permalink
feat: remove unwrap from the migration
Browse files Browse the repository at this point in the history
  • Loading branch information
lrazovic authored and JuaniRios committed Jan 16, 2025
1 parent e07eec3 commit 4bdc0df
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 144 deletions.
46 changes: 0 additions & 46 deletions integration-tests/chopsticks/src/tests/hub.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,50 +61,4 @@ describe('Polkadot Hub -> Polimec Transfer Tests', () => {
// { timeout: 25000 },
// );

// test('sandbox', async () => {
// console.log("hello");
// const weth_1 = {
// parents: 2,
// interior: {
// x2: [
// {
// globalConsensus: {
// ethereum: {
// chainId: 1n
// }
// }
// },
// {
// accountKey20: {
// key: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
// }
// }
// ]
// }
// }
//
// const weth_2 = {
// parents: 2,
// interior: {
// x2: [
// {
// globalConsensus: {
// ethereum: {
// chainId: 1n
// }
// }
// },
// {
// accountKey20: {
// key: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
// }
// }
// ]
// }
// }
//
// const equals = Bun.deepEquals(weth_1, weth_2);
// expect(equals).toEqual(false);
//
// }, { timeout: 10000000 });
});
106 changes: 50 additions & 56 deletions integration-tests/chopsticks/src/tests/polimec.test.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,56 @@
// import { afterAll, beforeAll, beforeEach, describe, test } from 'bun:test';
// import { TRANSFER_AMOUNTS } from '@/constants';
// import { createChainManager } from '@/managers/Factory';
// import { polimec_storage } from '@/polimec';
// import { ChainSetup } from '@/setup';
// import { PolimecToHubTransfer } from '@/transfers/PolimecToHub';
// import { Accounts, Asset, AssetSourceRelation, Chains } from '@/types';
import { afterAll, beforeAll, beforeEach, describe, test } from 'bun:test';
import { TRANSFER_AMOUNTS } from '@/constants';
import { createChainManager } from '@/managers/Factory';
import { polimec_storage } from '@/polimec';
import { ChainSetup } from '@/setup';
import { PolimecToHubTransfer } from '@/transfers/PolimecToHub';
import { Accounts, Asset, AssetSourceRelation, Chains } from '@/types';

// describe('Polimec -> Hub Transfer Tests', () => {
// const sourceManager = createChainManager(Chains.Polimec);
// const destManager = createChainManager(Chains.PolkadotHub);
// const transferTest = new PolimecToHubTransfer(sourceManager, destManager);
// const chainSetup = new ChainSetup();
describe('Polimec -> Hub Transfer Tests', () => {
const sourceManager = createChainManager(Chains.Polimec);
const destManager = createChainManager(Chains.PolkadotHub);
const transferTest = new PolimecToHubTransfer(sourceManager, destManager);
const chainSetup = new ChainSetup();

// beforeAll(async () => await chainSetup.initialize(polimec_storage));
// beforeEach(() => {
// sourceManager.connect();
// destManager.connect();
// });
// afterAll(async () => await chainSetup.cleanup());
beforeAll(async () => await chainSetup.initialize(polimec_storage));
beforeEach(() => {
sourceManager.connect();
destManager.connect();
});
afterAll(async () => await chainSetup.cleanup());

// async function getBalance(account: Accounts, asset: Asset) {
// return await sourceManager.getAssetBalanceOf(account, asset);
// }
// test('Balance query', () => getBalance(Accounts.BOB, Asset.USDT), { timeout: 250000000 });
async function getBalance(account: Accounts, asset: Asset) {
return await sourceManager.getAssetBalanceOf(account, asset);
}
test('Balance query', () => getBalance(Accounts.BOB, Asset.USDT), { timeout: 250000000 });

// test(
// 'Send USDC to Hub',
// () =>
// transferTest.testTransfer({
// amount: TRANSFER_AMOUNTS.TOKENS,
// account: Accounts.BOB,
// asset: Asset.USDC,
// assetSourceRelation: AssetSourceRelation.Sibling,
// }),
// { timeout: 25000 },
// );
test(
'Send USDC to Hub',
() =>
transferTest.testTransfer({
account: Accounts.BOB,
assets: [[Asset.USDC, TRANSFER_AMOUNTS.TOKENS, AssetSourceRelation.Sibling]]
}),
{ timeout: 25000 },
);

// test(
// 'Send USDT to Hub',
// () =>
// transferTest.testTransfer({
// amount: TRANSFER_AMOUNTS.TOKENS,
// account: Accounts.BOB,
// asset: Asset.USDT,
// assetSourceRelation: AssetSourceRelation.Sibling,
// }),
// { timeout: 25000 },
// );
test(
'Send USDT to Hub',
() =>
transferTest.testTransfer({
account: Accounts.BOB,
assets: [[Asset.USDT, TRANSFER_AMOUNTS.TOKENS, AssetSourceRelation.Sibling]]
}),
{ timeout: 25000 },
);

// test(
// 'Send DOT to Hub',
// () =>
// transferTest.testTransfer({
// amount: TRANSFER_AMOUNTS.NATIVE,
// account: Accounts.BOB,
// asset: Asset.DOT,
// assetSourceRelation: AssetSourceRelation.Parent,
// }),
// { timeout: 25000 },
// );
// });
test(
'Send DOT to Hub',
() =>
transferTest.testTransfer({
account: Accounts.BOB,
assets: [[Asset.DOT, TRANSFER_AMOUNTS.NATIVE, AssetSourceRelation.Parent]]
}),
{ timeout: 25000 },
);
});
100 changes: 58 additions & 42 deletions runtimes/polimec/src/custom_migrations/asset_id_migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@ use frame_support::{
use itertools::Itertools;
use pallet_assets::{Approval, AssetAccount, AssetDetails, AssetMetadata};
use polimec_common::assets::AcceptedFundingAsset;
use sp_api::runtime_decl_for_core::CoreV5;
use sp_runtime::BoundedVec;
use xcm::v4::Location;

// Storage items of pallet-assets are set to private for some reason. So we have to redefine them to get the same storage
// encoding and call the `translate` methods. -_-'
#[cfg(feature = "try-runtime")]
use frame_support::migrations::VersionedPostUpgradeData;

#[cfg(feature = "try-runtime")]
use parity_scale_codec::Encode;

#[cfg(feature = "try-runtime")]
use sp_std::vec::Vec;

// Storage items of pallet-assets are private.
// So we have to redefine them to get the same storage encoding and call the `translate` methods.
pub mod pallet_assets_storage_items {
use super::*;

Expand Down Expand Up @@ -120,74 +128,82 @@ impl OnRuntimeUpgrade for FromOldAssetIdMigration {
log::info!("AssetId Migration can be removed");
return <Runtime as frame_system::Config>::DbWeight::get().reads(1)
}
log::info!("Running AssetId Migration...");
let mut items = 0;
if runtime_version.spec_version == 1_000_000 {
log::info!("Running AssetId Migration...");
let id_map = BTreeMap::from([
(1984, AcceptedFundingAsset::USDT.id()),
(1337, AcceptedFundingAsset::USDC.id()),
(10, AcceptedFundingAsset::DOT.id()),
(1337, AcceptedFundingAsset::USDC.id()),
(1984, AcceptedFundingAsset::USDT.id()),
(3344, Location::here()),
]);

let old_account_iterator = pallet_assets_storage_items::old_types::Account::iter().collect_vec();
for (old_asset_id, account, account_info) in old_account_iterator {
items += 1;
log::info!("old_account item {:?}", items);

pallet_assets_storage_items::new_types::Account::insert(
id_map.get(&old_asset_id).unwrap(),
account.clone(),
account_info,
);
pallet_assets_storage_items::old_types::Account::remove(old_asset_id, account);
if let Some(new_asset_id) = id_map.get(&old_asset_id) {
items += 1;
log::info!("old_account item {:?}", items);

pallet_assets_storage_items::new_types::Account::insert(
new_asset_id,
account.clone(),
account_info,
);
pallet_assets_storage_items::old_types::Account::remove(old_asset_id, account);
}
}

let old_asset_iterator = pallet_assets_storage_items::old_types::Asset::iter().collect_vec();
for (old_asset_id, asset_info) in old_asset_iterator {
items += 1;
log::info!("old_asset item {:?}", items);
pallet_assets_storage_items::new_types::Asset::insert(id_map.get(&old_asset_id).unwrap(), asset_info);
pallet_assets_storage_items::old_types::Asset::remove(old_asset_id);
if let Some(new_asset_id) = id_map.get(&old_asset_id) {
items += 1;
log::info!("old_asset item {:?}", items);
pallet_assets_storage_items::new_types::Asset::insert(new_asset_id, asset_info);
pallet_assets_storage_items::old_types::Asset::remove(old_asset_id);
}
}

let old_approvals_iterator = pallet_assets_storage_items::old_types::Approvals::iter().collect_vec();
for ((old_asset_id, owner, delegate), approval) in old_approvals_iterator {
items += 1;
log::info!("old_approvals item {:?}", items);
pallet_assets_storage_items::new_types::Approvals::insert(
(id_map.get(&old_asset_id).unwrap(), owner.clone(), delegate.clone()),
approval,
);
pallet_assets_storage_items::old_types::Approvals::remove((old_asset_id, owner, delegate));
if let Some(new_asset_id) = id_map.get(&old_asset_id) {
items += 1;
log::info!("old_approvals item {:?}", items);
pallet_assets_storage_items::new_types::Approvals::insert(
(new_asset_id, owner.clone(), delegate.clone()),
approval,
);
pallet_assets_storage_items::old_types::Approvals::remove((old_asset_id, owner, delegate));
}
}

let old_metadata_iterator = pallet_assets_storage_items::old_types::Metadata::iter().collect_vec();
for (old_asset_id, metadata) in old_metadata_iterator {
items += 1;
log::info!("old_metadata item {:?}", items);
pallet_assets_storage_items::new_types::Metadata::insert(id_map.get(&old_asset_id).unwrap(), metadata);
pallet_assets_storage_items::old_types::Metadata::remove(old_asset_id);
if let Some(new_asset_id) = id_map.get(&old_asset_id) {
items += 1;
log::info!("old_metadata item {:?}", items);
pallet_assets_storage_items::new_types::Metadata::insert(new_asset_id, metadata);
pallet_assets_storage_items::old_types::Metadata::remove(old_asset_id);
}
}

let old_oracle_raw_values_iterator = orml_oracle_storage_items::old_types::RawValues::iter().collect_vec();
for (account, old_asset_id, raw_values) in old_oracle_raw_values_iterator {
items += 1;
log::info!("old_oracle_raw_values item {:?}", items);
orml_oracle::RawValues::<Runtime>::insert(
account.clone(),
id_map.get(&old_asset_id).unwrap(),
raw_values,
);
orml_oracle_storage_items::old_types::RawValues::remove(account, old_asset_id);
if let Some(new_asset_id) = id_map.get(&old_asset_id) {
items += 1;
log::info!("old_oracle_raw_values item {:?}", items);
orml_oracle::RawValues::<Runtime>::insert(account.clone(), new_asset_id, raw_values);
orml_oracle_storage_items::old_types::RawValues::remove(account, old_asset_id);
}
}

let old_oracle_values_iterator = orml_oracle_storage_items::old_types::Values::iter().collect_vec();
for (old_asset_id, value) in old_oracle_values_iterator {
items += 1;
log::info!("old_oracle_values item {:?}", items);
orml_oracle::Values::<Runtime>::insert(id_map.get(&old_asset_id).unwrap(), value);
orml_oracle_storage_items::old_types::Values::remove(old_asset_id);
if let Some(new_asset_id) = id_map.get(&old_asset_id) {
items += 1;
log::info!("old_oracle_values item {:?}", items);
orml_oracle::Values::<Runtime>::insert(new_asset_id, value);
orml_oracle_storage_items::old_types::Values::remove(old_asset_id);
}
}
}

Expand Down

0 comments on commit 4bdc0df

Please sign in to comment.