Skip to content

Commit

Permalink
getSendToReserveChainInstructions fully tested
Browse files Browse the repository at this point in the history
  • Loading branch information
Szegoo committed Aug 18, 2023
1 parent 65deb92 commit 06fbdd1
Show file tree
Hide file tree
Showing 2 changed files with 362 additions and 3 deletions.
356 changes: 356 additions & 0 deletions src/utils/transactionRouter/reserveTransfer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,362 @@ describe("TransactionRouter unit tests", () => {
)
});

describe("getSendToReserveChainInstructions works", () => {
it("Works from parachain to parachain", () => {
const bob = ecdsaKering.addFromUri("//Bob");

const destParaId = 2002;
const beneficiary: Receiver = {
addressRaw: bob.addressRaw,
network: 1,
type: AccountType.accountId32
};

const asset: Fungible = {
multiAsset: {
interior: {
X3: [
{ Parachain: destParaId },
{ PalletInstance: 42 },
{ GeneralIndex: 69 }
]
},
parents: 0,
},
amount: 200
};

// @ts-ignore
expect(ReserveTransfer.getSendToReserveChainInstructions(
asset,
destParaId,
beneficiary,
true
)).toStrictEqual({
V2: [
{
WithdrawAsset: [
{
fun: {
Fungible: 200,
},
id: {
Concrete: {
interior: {
X3: [
{
Parachain: destParaId,
},
{
PalletInstance: 42,
},
{
GeneralIndex: 69,
},
],
},
parents: 1,
},
},
},
]
},
{
InitiateReserveWithdraw: {
assets: {
Wild: "All",
},
reserve: {
interior: {
X1: {
Parachain: destParaId,
},
},
parents: 1,
},
xcm: [
{
BuyExecution: {
fees: {
fun: {
Fungible: 450000000000,
},
id: {
Concrete: {
interior: {
X2: [
{
PalletInstance: 42,
},
{
GeneralIndex: 69,
},
],
},
parents: 0,
},
},
},
weightLimit: "Unlimited",
},
},
{
DepositAsset: {
assets: {
Wild: "All",
},
beneficiary: {
interior: {
X1: {
AccountId32: {
id: bob.addressRaw,
network: "Any"
}
}
},
parents: 0
},
maxAssets: 1
}
}
]
}
}
]
});
});

it("Works from parachain to relaychain", () => {
const bob = ecdsaKering.addFromUri("//Bob");

const destParaId = -1;
const beneficiary: Receiver = {
addressRaw: bob.addressRaw,
network: 1,
type: AccountType.accountId32
};

const asset: Fungible = {
multiAsset: {
interior: {
X2: [
{ PalletInstance: 42 },
{ GeneralIndex: 69 }
]
},
parents: 0,
},
amount: 200
};

// @ts-ignore
expect(ReserveTransfer.getSendToReserveChainInstructions(
asset,
destParaId,
beneficiary,
true
)).toStrictEqual({
V2: [
{
WithdrawAsset: [
{
fun: {
Fungible: 200,
},
id: {
Concrete: {
interior: {
X2: [
{
PalletInstance: 42,
},
{
GeneralIndex: 69,
},
],
},
parents: 1,
},
},
},
]
},
{
InitiateReserveWithdraw: {
assets: {
Wild: "All",
},
reserve: {
interior: "Here",
parents: 1,
},
xcm: [
{
BuyExecution: {
fees: {
fun: {
Fungible: 450000000000,
},
id: {
Concrete: {
interior: {
X2: [
{
PalletInstance: 42,
},
{
GeneralIndex: 69,
},
],
},
parents: 0,
},
},
},
weightLimit: "Unlimited",
},
},
{
DepositAsset: {
assets: {
Wild: "All",
},
beneficiary: {
interior: {
X1: {
AccountId32: {
id: bob.addressRaw,
network: "Any"
}
}
},
parents: 0
},
maxAssets: 1
}
}
]
}
}
]
});
});

it("Works from relaychain to parachain", () => {
const bob = ecdsaKering.addFromUri("//Bob");

const destParaId = 1000;
const beneficiary: Receiver = {
addressRaw: bob.addressRaw,
network: 1,
type: AccountType.accountId32
};

const asset: Fungible = {
multiAsset: {
interior: {
X2: [
{ Parachain: destParaId },
{ PalletInstance: 42 },
{ GeneralIndex: 69 }
]
},
parents: 0,
},
amount: 200
};

// @ts-ignore
expect(ReserveTransfer.getSendToReserveChainInstructions(
asset,
destParaId,
beneficiary,
false
)).toStrictEqual({
V2: [
{
WithdrawAsset: [
{
fun: {
Fungible: 200,
},
id: {
Concrete: {
interior: {
X3: [
{ Parachain: destParaId },
{ PalletInstance: 42 },
{ GeneralIndex: 69 },
],
},
parents: 0,
},
},
},
]
},
{
InitiateReserveWithdraw: {
assets: {
Wild: "All",
},
reserve: {
interior: { X1: { Parachain: destParaId } },
parents: 0,
},
xcm: [
{
BuyExecution: {
fees: {
fun: {
Fungible: 450000000000,
},
id: {
Concrete: {
interior: {
X2: [
{
PalletInstance: 42,
},
{
GeneralIndex: 69,
},
],
},
parents: 0,
},
},
},
weightLimit: "Unlimited",
},
},
{
DepositAsset: {
assets: {
Wild: "All",
},
beneficiary: {
interior: {
X1: {
AccountId32: {
id: bob.addressRaw,
network: "Any"
}
}
},
parents: 0
},
maxAssets: 1
}
}
]
}
}
]
});
});
});

describe("getTwoHopTransferInstructions works", () => {
it("Works with parachain reserve", () => {
const bob = ecdsaKering.addFromUri("//Bob");
Expand Down
9 changes: 6 additions & 3 deletions src/utils/transactionRouter/reserveTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class ReserveTransfer {
let assetFromReservePerspective = JSON.parse(JSON.stringify(asset.multiAsset));
if (reserveParaId > 0) {
// The location of the asset will always start with the parachain if the reserve is a parachain.
this.removeParachainFromLocation(assetFromReservePerspective);
this.assetFromReservePerspective(assetFromReservePerspective);
} else {
// The reserve is the relay chain.
assetFromReservePerspective.parents = 0;
Expand Down Expand Up @@ -236,7 +236,10 @@ class ReserveTransfer {
let assetFromReservePerspective = JSON.parse(JSON.stringify(asset.multiAsset));
if (destParaId >= 0) {
// The location of the asset will always start with the parachain if the reserve is a parachain.
this.removeParachainFromLocation(assetFromReservePerspective);
this.assetFromReservePerspective(assetFromReservePerspective);
} else {
// The reserve is the relay chain.
assetFromReservePerspective.parents = 0;
}

return {
Expand Down Expand Up @@ -446,7 +449,7 @@ class ReserveTransfer {
}

// Helper function to remove a specific key from an object.
private static removeParachainFromLocation(location: any) {
private static assetFromReservePerspective(location: any) {
const junctions = this.extractJunctions(location);
junctions.splice(0, 1);
delete location.interior[`X${junctions.length + 1}`];
Expand Down

0 comments on commit 06fbdd1

Please sign in to comment.