Skip to content

Commit

Permalink
Transaction Router refactor (#50)
Browse files Browse the repository at this point in the history
* Transaction Router refactor

* lint

* fixes

* for now remove interface

* linter happy

* fix

* fixes

* all tests passing :))

* lint fix

* remove types & artifacts

* update readme

* update CI

* fix

* fix test

* fix

* remove empty file

* add types

---------

Co-authored-by: cute0laf <[email protected]>
  • Loading branch information
Szegoo and cuteolaf authored Jul 27, 2023
1 parent 0a181f7 commit 012df3c
Show file tree
Hide file tree
Showing 38 changed files with 2,165 additions and 3,813 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ package-lock.json

# zombienet
bin/

# toolchain

artifacts/
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ For details regarding the contract deployment go to this [page](https://github.c
1. Install [NodeJs](https://nodejs.org/en/download)
2. `yarn install`
3. `yarn dev`
4. Go to `http://localhost:3000` to interact with the webapp
4. `yarn compile` NOTE: This requires the [dotflow-ink](https://github.com/TheDotflow/dotflow-ink) repository to be cloned next to the `dotflow-ui` repo.
5. Go to `http://localhost:3000` to interact with the webapp

### Running tests

Expand Down
4 changes: 2 additions & 2 deletions __tests__/assetRegistry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,8 @@ describe('AssetRegistry', () => {
asset: {
Token: 'AUSD',
},
name: 'Acala Dollar',
symbol: 'aUSD',
name: 'aUSD SEED',
symbol: 'aSEED',
decimals: 12,
xcmInteriorKey: [
{ network: 'polkadot' },
Expand Down
119 changes: 71 additions & 48 deletions __tests__/transactionRouter.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ApiPromise, Keyring, WsProvider } from "@polkadot/api";
import { KeyringPair } from "@polkadot/keyring/types";

import { Fungible, Receiver, Sender } from "@/utils/transactionRouter/types";

import TransactionRouter from "../src/utils/transactionRouter";
import IdentityContractFactory from "../types/constructors/identity";
import IdentityContract from "../types/contracts/identity";
Expand Down Expand Up @@ -32,42 +34,57 @@ describe("TransactionRouter", () => {
});

it("Can't send tokens to yourself", async () => {
const sender = alice;
const receiver = alice;

// First lets add a network and create an identity.

await addNetwork(identityContract, alice, {
rpcUrl: "ws://127.0.0.1:4242",
accountType: AccountType.accountId32,
});

const sender: Sender = {
keypair: alice,
network: 0
};

const receiver: Receiver = {
addressRaw: alice.addressRaw,
type: AccountType.accountId32,
network: 0,
};

const asset: Fungible = {
multiAsset: {},
amount: 1000
};

await expect(
TransactionRouter.sendTokens(
identityContract,
sender,
0, // origin network
receiver.addressRaw,
AccountType.accountId32,
0, // destination network
{}, // multi asset
1000
receiver,
asset
)
).rejects.toThrow("Cannot send tokens to yourself");
});

it("Sending native asset on the same network works", async () => {
const sender = alice;
const receiver = bob;
const sender: Sender = {
keypair: alice,
network: 0
};

const receiver: Receiver = {
addressRaw: bob.addressRaw,
type: AccountType.accountId32,
network: 0,
};

const westendProvider = new WsProvider("ws://127.0.0.1:4242");
const westendApi = await ApiPromise.create({ provider: westendProvider });

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const { data: balance } = await westendApi.query.system.account(
receiver.address
);
const { data: balance } = (await westendApi.query.system.account(
receiver.addressRaw
)) as any;
const receiverBalance = parseInt(balance.free.toHuman().replace(/,/g, ""));

// First lets add a network.
Expand All @@ -78,36 +95,42 @@ describe("TransactionRouter", () => {

const amount = Math.pow(10, 12);

await TransactionRouter.sendTokens(
identityContract,
sender,
0, // origin network
receiver.addressRaw,
AccountType.accountId32,
0, // destination network
// MultiAsset:
{
const asset: Fungible = {
multiAsset: {
interior: "Here",
parents: 0,
},
amount
);
};

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const { data: balance } = await westendApi.query.system.account(
receiver.address
await TransactionRouter.sendTokens(
identityContract,
sender,
receiver,
asset
);

const { data: newBalance } = (await westendApi.query.system.account(
receiver.addressRaw
)) as any;
const newReceiverBalance = parseInt(
balance.free.toHuman().replace(/,/g, "")
newBalance.free.toHuman().replace(/,/g, "")
);

expect(newReceiverBalance).toBe(receiverBalance + amount);
}, 30000);

it("Sending non-native asset on the same network works", async () => {
const sender = alice;
const receiver = bob;
const sender: Sender = {
keypair: alice,
network: 0
};

const receiver: Receiver = {
addressRaw: bob.addressRaw,
type: AccountType.accountId32,
network: 0,
};

const assetHubProvider = new WsProvider("ws://127.0.0.1:4243");
const assetHubApi = await ApiPromise.create({
Expand All @@ -116,24 +139,24 @@ describe("TransactionRouter", () => {

// First create an asset.
if (!(await getAsset(assetHubApi, 0))) {
await createAsset(assetHubApi, sender, 0);
await createAsset(assetHubApi, sender.keypair, 0);
}

// Mint some assets to the creator.
await mintAsset(assetHubApi, sender, 0, 500);
await mintAsset(assetHubApi, sender.keypair, 0, 500);

const amount = 200;

const senderAccountBefore: any = (await assetHubApi.query.assets.account(
0,
sender.address
sender.keypair.address
)).toHuman();

const senderBalanceBefore = parseInt(senderAccountBefore.balance.replace(/,/g, ""));

const receiverAccountBefore: any = (await assetHubApi.query.assets.account(
0,
receiver.address
bob.address
)).toHuman();

const receiverBalanceBefore = receiverAccountBefore ? parseInt(receiverAccountBefore.balance.replace(/,/g, "")) : 0;
Expand All @@ -144,15 +167,8 @@ describe("TransactionRouter", () => {
accountType: AccountType.accountId32,
});

await TransactionRouter.sendTokens(
identityContract,
sender,
0, // origin network
receiver.addressRaw,
AccountType.accountId32,
0, // destination network
// MultiAsset:
{
const asset: Fungible = {
multiAsset: {
interior: {
X2: [
{ PalletInstance: 50 }, // assets pallet
Expand All @@ -162,18 +178,25 @@ describe("TransactionRouter", () => {
parents: 0,
},
amount
};

await TransactionRouter.sendTokens(
identityContract,
sender,
receiver,
asset
);

const senderAccountAfter: any = (await assetHubApi.query.assets.account(
0,
sender.address
sender.keypair.address
)).toHuman();

const senderBalanceAfter = parseInt(senderAccountAfter.balance.replace(/,/g, ""));

const receiverAccountAfter: any = (await assetHubApi.query.assets.account(
0,
receiver.address
bob.address
)).toHuman();

const receiverBalanceAfter = parseInt(receiverAccountAfter.balance.replace(/,/g, ""));
Expand Down
1 change: 0 additions & 1 deletion artifacts/address_book.contract

This file was deleted.

Loading

0 comments on commit 012df3c

Please sign in to comment.