difftreelog
test make various test helpers accept CrossAccountId
in: master
1 file changed
tests/src/util/helpers.tsdiffbeforeafterboth4//4//556import { ApiPromise, Keyring } from '@polkadot/api';6import { ApiPromise, Keyring } from '@polkadot/api';7import { Enum, Struct } from '@polkadot/types/codec';8import type { AccountId, EventRecord } from '@polkadot/types/interfaces';7import type { AccountId, EventRecord } from '@polkadot/types/interfaces';9import { u128 } from '@polkadot/types/primitive';8import { u128 } from '@polkadot/types/primitive';10import { IKeyringPair } from '@polkadot/types/types';9import { IKeyringPair } from '@polkadot/types/types';10import { evmToAddress } from '@polkadot/util-crypto';11import { BigNumber } from 'bignumber.js';11import { BigNumber } from 'bignumber.js';12import BN from 'bn.js';12import BN from 'bn.js';13import chai from 'chai';13import chai from 'chai';21chai.use(chaiAsPromised);21chai.use(chaiAsPromised);22const expect = chai.expect;22const expect = chai.expect;232324export type CrossAccountId = string | {24export type CrossAccountId = {25 substrate: string,25 substrate: string,26} | {26} | {27 ethereum: string,27 ethereum: string,28};28};29export function normalizeAccountId(input: CrossAccountId): CrossAccountId {29export function normalizeAccountId(input: string | CrossAccountId | IKeyringPair): CrossAccountId {30 if (typeof input === 'string')30 if (typeof input === 'string')31 return { substrate: input };31 return { substrate: input };32 if ('address' in input) {33 return { substrate: input.address };34 }35 if ('ethereum' in input) {36 input.ethereum = input.ethereum.toLowerCase();37 }32 return input;38 return input;33}39}40export function toSubstrateAddress(input: CrossAccountId): string {41 input = normalizeAccountId(input);42 if ('substrate' in input) {43 return input.substrate;44 } else {45 return evmToAddress(input.ethereum);46 }47}344835export const U128_MAX = (1n << 128n) - 1n;49export const U128_MAX = (1n << 128n) - 1n;3650643657644export async function658export async function645 approveExpectSuccess(collectionId: number,659 approveExpectSuccess(collectionId: number,646 tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number | bigint = 1) {660 tokenId: number, owner: IKeyringPair, approved: IKeyringPair | CrossAccountId | string, amount: number | bigint = 1) {647 await usingApi(async (api: ApiPromise) => {661 await usingApi(async (api: ApiPromise) => {662 approved = normalizeAccountId(approved);648 const allowanceBefore =663 const allowanceBefore =649 await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;664 await api.query.nft.allowances(collectionId, [tokenId, owner.address, toSubstrateAddress(approved)]) as unknown as BN;650 const approveNftTx = api.tx.nft.approve(normalizeAccountId(approved.address), collectionId, tokenId, amount);665 const approveNftTx = api.tx.nft.approve(approved, collectionId, tokenId, amount);651 const events = await submitTransactionAsync(owner, approveNftTx);666 const events = await submitTransactionAsync(owner, approveNftTx);652 const result = getCreateItemResult(events);667 const result = getCreateItemResult(events);653 // tslint:disable-next-line:no-unused-expression668 // tslint:disable-next-line:no-unused-expression654 expect(result.success).to.be.true;669 expect(result.success).to.be.true;655 const allowanceAfter =670 const allowanceAfter =656 await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;671 await api.query.nft.allowances(collectionId, [tokenId, owner.address, toSubstrateAddress(approved)]) as unknown as BN;657 expect(allowanceAfter.sub(allowanceBefore).toString()).to.be.equal(amount.toString());672 expect(allowanceAfter.sub(allowanceBefore).toString()).to.be.equal(amount.toString());658 });673 });659}674}663 tokenId: number,678 tokenId: number,664 accountApproved: IKeyringPair,679 accountApproved: IKeyringPair,665 accountFrom: IKeyringPair,680 accountFrom: IKeyringPair,666 accountTo: IKeyringPair,681 accountTo: IKeyringPair | CrossAccountId,667 value: number | bigint = 1,682 value: number | bigint = 1,668 type: string = 'NFT') {683 type: string = 'NFT') {669 await usingApi(async (api: ApiPromise) => {684 await usingApi(async (api: ApiPromise) => {685 const to = normalizeAccountId(accountTo);670 let balanceBefore = new BN(0);686 let balanceBefore = new BN(0);671 if (type === 'Fungible') {687 if (type === 'Fungible') {672 balanceBefore = await api.query.nft.balance(collectionId, accountTo.address) as unknown as BN;688 balanceBefore = await api.query.nft.balance(collectionId, toSubstrateAddress(to)) as unknown as BN;673 }689 }674 const transferFromTx = api.tx.nft.transferFrom(690 const transferFromTx = api.tx.nft.transferFrom(675 normalizeAccountId(accountFrom.address), normalizeAccountId(accountTo.address), collectionId, tokenId, value);691 normalizeAccountId(accountFrom.address), to, collectionId, tokenId, value);676 const events = await submitTransactionAsync(accountApproved, transferFromTx);692 const events = await submitTransactionAsync(accountApproved, transferFromTx);677 const result = getCreateItemResult(events);693 const result = getCreateItemResult(events);678 // tslint:disable-next-line:no-unused-expression694 // tslint:disable-next-line:no-unused-expression679 expect(result.success).to.be.true;695 expect(result.success).to.be.true;680 if (type === 'NFT') {696 if (type === 'NFT') {681 const nftItemData = (await api.query.nft.nftItemList(collectionId, tokenId) as any).toJSON() as ITokenDataType;697 const nftItemData = (await api.query.nft.nftItemList(collectionId, tokenId) as any).toJSON() as ITokenDataType;682 expect(nftItemData.Owner).to.be.deep.equal(normalizeAccountId(accountTo.address));698 expect(nftItemData.Owner).to.be.deep.equal(to);683 }699 }684 if (type === 'Fungible') {700 if (type === 'Fungible') {685 const balanceAfter = (await api.query.nft.fungibleItemList(collectionId, accountTo.address) as any).Value as unknown as BN;701 const balanceAfter = (await api.query.nft.fungibleItemList(collectionId, toSubstrateAddress(to)) as any).Value as unknown as BN;686 expect(balanceAfter.sub(balanceBefore).toString()).to.be.equal(value.toString());702 expect(balanceAfter.sub(balanceBefore).toString()).to.be.equal(value.toString());687 }703 }688 if (type === 'ReFungible') {704 if (type === 'ReFungible') {689 const nftItemData =705 const nftItemData =690 (await api.query.nft.reFungibleItemList(collectionId, tokenId) as any).toJSON() as IReFungibleTokenDataType;706 (await api.query.nft.reFungibleItemList(collectionId, tokenId) as any).toJSON() as IReFungibleTokenDataType;691 expect(nftItemData.Owner[0].Owner).to.be.deep.equal(normalizeAccountId(accountTo.address));707 expect(nftItemData.Owner[0].Owner).to.be.deep.equal(normalizeAccountId(to));692 expect(nftItemData.Owner[0].Fraction).to.be.equal(value);708 expect(nftItemData.Owner[0].Fraction).to.be.equal(value);693 }709 }694 });710 });715 transferExpectSuccess(collectionId: number,731 transferExpectSuccess(collectionId: number,716 tokenId: number,732 tokenId: number,717 sender: IKeyringPair,733 sender: IKeyringPair,718 recipient: IKeyringPair,734 recipient: IKeyringPair | CrossAccountId,719 value: number | bigint = 1,735 value: number | bigint = 1,720 type: string = 'NFT') {736 type: string = 'NFT') {721 await usingApi(async (api: ApiPromise) => {737 await usingApi(async (api: ApiPromise) => {738 const to = normalizeAccountId(recipient);739722 let balanceBefore = new BN(0);740 let balanceBefore = new BN(0);723 if (type === 'Fungible') {741 if (type === 'Fungible') {724 balanceBefore = await api.query.nft.balance(collectionId, recipient.address) as unknown as BN;742 balanceBefore = await api.query.nft.balance(collectionId, toSubstrateAddress(to)) as unknown as BN;725 }743 }726 const transferTx = api.tx.nft.transfer(normalizeAccountId(recipient.address), collectionId, tokenId, value);744 const transferTx = api.tx.nft.transfer(to, collectionId, tokenId, value);727 const events = await submitTransactionAsync(sender, transferTx);745 const events = await submitTransactionAsync(sender, transferTx);728 const result = getTransferResult(events);746 const result = getTransferResult(events);729 // tslint:disable-next-line:no-unused-expression747 // tslint:disable-next-line:no-unused-expression730 expect(result.success).to.be.true;748 expect(result.success).to.be.true;731 expect(result.collectionId).to.be.equal(collectionId);749 expect(result.collectionId).to.be.equal(collectionId);732 expect(result.itemId).to.be.equal(tokenId);750 expect(result.itemId).to.be.equal(tokenId);733 expect(result.sender).to.be.deep.equal(normalizeAccountId(sender.address));751 expect(result.sender).to.be.deep.equal(normalizeAccountId(sender.address));734 expect(result.recipient).to.be.deep.equal(normalizeAccountId(recipient.address));752 expect(result.recipient).to.be.deep.equal(to);735 expect(result.value.toString()).to.be.equal(value.toString());753 expect(result.value.toString()).to.be.equal(value.toString());736 if (type === 'NFT') {754 if (type === 'NFT') {737 const nftItemData = (await api.query.nft.nftItemList(collectionId, tokenId)).toJSON() as unknown as ITokenDataType;755 const nftItemData = (await api.query.nft.nftItemList(collectionId, tokenId)).toJSON() as unknown as ITokenDataType;738 expect(nftItemData.Owner).to.be.deep.equal(normalizeAccountId(recipient.address));756 expect(nftItemData.Owner).to.be.deep.equal(to);739 }757 }740 if (type === 'Fungible') {758 if (type === 'Fungible') {741 const balanceAfter = (await api.query.nft.fungibleItemList(collectionId, recipient.address) as any).Value as unknown as BN;759 const balanceAfter = (await api.query.nft.fungibleItemList(collectionId, toSubstrateAddress(to)) as any).Value as unknown as BN;742 expect(balanceAfter.sub(balanceBefore).toString()).to.be.equal(value.toString());760 expect(balanceAfter.sub(balanceBefore).toString()).to.be.equal(value.toString());743 }761 }744 if (type === 'ReFungible') {762 if (type === 'ReFungible') {745 const nftItemData =763 const nftItemData =746 (await api.query.nft.reFungibleItemList(collectionId, tokenId)).toJSON() as unknown as IReFungibleTokenDataType;764 (await api.query.nft.reFungibleItemList(collectionId, tokenId)).toJSON() as unknown as IReFungibleTokenDataType;747 expect(nftItemData.Owner[0].Owner).to.be.deep.equal(normalizeAccountId(recipient.address));765 expect(nftItemData.Owner[0].Owner).to.be.deep.equal(to);748 expect(nftItemData.Owner[0].Fraction.toString()).to.be.equal(value.toString());766 expect(nftItemData.Owner[0].Fraction.toString()).to.be.equal(value.toString());749 }767 }750 });768 });794 sender: IKeyringPair,812 sender: IKeyringPair,795 collectionId: number,813 collectionId: number,796 data: CreateFungibleData,814 data: CreateFungibleData,797 owner: CrossAccountId = sender.address,815 owner: CrossAccountId | string = sender.address,798) {816) {799 return await usingApi(async (api) => {817 return await usingApi(async (api) => {800 const tx = api.tx.nft.createItem(collectionId, normalizeAccountId(owner), { Fungible: data });818 const tx = api.tx.nft.createItem(collectionId, normalizeAccountId(owner), { Fungible: data });808}826}809827810export async function createItemExpectSuccess(828export async function createItemExpectSuccess(811 sender: IKeyringPair, collectionId: number, createMode: string, owner: string = sender.address) {829 sender: IKeyringPair, collectionId: number, createMode: string, owner: CrossAccountId | string = sender.address) {812 let newItemId: number = 0;830 let newItemId: number = 0;813 await usingApi(async (api) => {831 await usingApi(async (api) => {832 const to = normalizeAccountId(owner);814 const AItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString(), 10);833 const AItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString(), 10);815 const Aitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();834 const Aitem: any = (await api.query.nft.fungibleItemList(collectionId, toSubstrateAddress(to))).toJSON();816 const AItemBalance = new BigNumber(Aitem.Value);835 const AItemBalance = new BigNumber(Aitem.Value);817836818 let tx;837 let tx;819 if (createMode === 'Fungible') {838 if (createMode === 'Fungible') {820 const createData = { fungible: { value: 10 } };839 const createData = { fungible: { value: 10 } };821 tx = api.tx.nft.createItem(collectionId, normalizeAccountId(owner), createData);840 tx = api.tx.nft.createItem(collectionId, to, createData);822 } else if (createMode === 'ReFungible') {841 } else if (createMode === 'ReFungible') {823 const createData = { refungible: { const_data: [], variable_data: [], pieces: 100 } };842 const createData = { refungible: { const_data: [], variable_data: [], pieces: 100 } };824 tx = api.tx.nft.createItem(collectionId, normalizeAccountId(owner), createData);843 tx = api.tx.nft.createItem(collectionId, to, createData);825 } else {844 } else {826 tx = api.tx.nft.createItem(collectionId, normalizeAccountId(owner), createMode);845 tx = api.tx.nft.createItem(collectionId, to, createMode);827 }846 }828847829 const events = await submitTransactionAsync(sender, tx);848 const events = await submitTransactionAsync(sender, tx);830 const result = getCreateItemResult(events);849 const result = getCreateItemResult(events);831850832 const BItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString(), 10);851 const BItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString(), 10);833 const Bitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();852 const Bitem: any = (await api.query.nft.fungibleItemList(collectionId, toSubstrateAddress(to))).toJSON();834 const BItemBalance = new BigNumber(Bitem.Value);853 const BItemBalance = new BigNumber(Bitem.Value);835854836 // What to expect855 // What to expect843 }862 }844 expect(collectionId).to.be.equal(result.collectionId);863 expect(collectionId).to.be.equal(result.collectionId);845 expect(BItemCount.toString()).to.be.equal(result.itemId.toString());864 expect(BItemCount.toString()).to.be.equal(result.itemId.toString());846 expect(normalizeAccountId(owner)).to.be.deep.equal(result.recipient);865 expect(to).to.be.deep.equal(result.recipient);847 newItemId = result.itemId;866 newItemId = result.itemId;848 });867 });849 return newItemId;868 return newItemId;