Skip to content

Commit

Permalink
feat: add auto convertion for query and rpc results
Browse files Browse the repository at this point in the history
  • Loading branch information
Grigoriy Simonov committed Oct 2, 2023
1 parent 9844cf7 commit 625bc84
Show file tree
Hide file tree
Showing 49 changed files with 12,379 additions and 1,533 deletions.
11,013 changes: 11,013 additions & 0 deletions tests/package-lock.json

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,13 @@
"@openzeppelin/contracts": "^4.9.2",
"@polkadot/api": "10.9.1",
"@polkadot/rpc-core": "^10.9.1",
"@polkadot/util": "12.3.2",
"@polkadot/util-crypto": "12.3.2",
"@polkadot/util": "12.5.1",
"@polkadot/util-crypto": "12.5.1",
"@polkadot/wasm-crypto-asmjs": "^7.2.1",
"@polkadot/wasm-crypto-wasm": "^7.2.1",
"@rmrk-team/evm-contracts": "^1.2.1",
"@typechain/web3-v1": "^6.0.3",
"@unique-nft/opal-testnet-types": "^1.0.0",
"chai-as-promised": "^7.1.1",
"chai-like": "^1.1.1",
"csv-writer": "^1.6.0",
Expand All @@ -154,6 +155,9 @@
"resolutions": {
"decode-uri-component": "^0.2.1"
},
"workspaces": [
"src/interfaces"
],
"type": "module",
"packageManager": "[email protected]"
}
}
2 changes: 1 addition & 1 deletion tests/src/adminTransferAndBurn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('Integration Test: ownerCanTransfer allows admins to use only transferF

await helper.nft.transferTokenFrom(alice, collectionId, tokenId, {Substrate: bob.address}, {Substrate: charlie.address});
const newTokenOwner = await helper.nft.getTokenOwner(collectionId, tokenId);
expect(newTokenOwner.Substrate).to.be.equal(charlie.address);
expect(newTokenOwner?.Substrate).to.be.equal(charlie.address);
});

itSub('admin burns other user\'s token', async ({helper}) => {
Expand Down
8 changes: 4 additions & 4 deletions tests/src/allowLists.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ describe('Integration Test ext. Transfer if included in Allow List', () => {
await helper.nft.addToAllowList(alice, collectionId, {Substrate: charlie.address});
await helper.nft.transferToken(alice, collectionId, tokenId, {Substrate: charlie.address});
const owner = await helper.nft.getTokenOwner(collectionId, tokenId);
expect(owner.Substrate).to.be.equal(charlie.address);
expect(owner?.Substrate).to.be.equal(charlie.address);
});

itSub('If Public Access mode is set to AllowList, tokens can be transferred to a allowlisted address with transferFrom.', async ({helper}) => {
Expand All @@ -190,7 +190,7 @@ describe('Integration Test ext. Transfer if included in Allow List', () => {

await helper.nft.transferTokenFrom(alice, collectionId, tokenId, {Substrate: alice.address}, {Substrate: charlie.address});
const owner = await helper.nft.getTokenOwner(collectionId, tokenId);
expect(owner.Substrate).to.be.equal(charlie.address);
expect(owner?.Substrate).to.be.equal(charlie.address);
});

itSub('If Public Access mode is set to AllowList, tokens can be transferred from a allowlisted address with transfer', async ({helper}) => {
Expand All @@ -202,7 +202,7 @@ describe('Integration Test ext. Transfer if included in Allow List', () => {

await helper.nft.transferToken(alice, collectionId, tokenId, {Substrate: charlie.address});
const owner = await helper.nft.getTokenOwner(collectionId, tokenId);
expect(owner.Substrate).to.be.equal(charlie.address);
expect(owner?.Substrate).to.be.equal(charlie.address);
});

itSub('If Public Access mode is set to AllowList, tokens can be transferred from a allowlisted address with transferFrom', async ({helper}) => {
Expand All @@ -215,7 +215,7 @@ describe('Integration Test ext. Transfer if included in Allow List', () => {

await helper.nft.transferTokenFrom(alice, collectionId, tokenId, {Substrate: alice.address}, {Substrate: charlie.address});
const owner = await helper.nft.getTokenOwner(collectionId, tokenId);
expect(owner.Substrate).to.be.equal(charlie.address);
expect(owner?.Substrate).to.be.equal(charlie.address);
});
});

Expand Down
8 changes: 4 additions & 4 deletions tests/src/approve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ import {CrossAccountId} from './util/playgrounds/unique';
await (helper.nft as any)[testCase.method](bob, collectionId, tokenId, {Substrate: charlie.address});
await helper.nft.transferTokenFrom(charlie, collectionId, tokenId, testCase.account(bob), {Substrate: alice.address});
const owner = await helper.nft.getTokenOwner(collectionId, tokenId);
expect(owner.Substrate).to.be.equal(alice.address);
expect(owner?.Substrate).to.be.equal(alice.address);
});

itSub('Fungible up to an approved amount', async ({helper}) => {
Expand Down Expand Up @@ -201,7 +201,7 @@ import {CrossAccountId} from './util/playgrounds/unique';
await (helper.nft as any)[testCase.method](bob, collectionId, tokenId, {Substrate: charlie.address});
await helper.nft.transferTokenFrom(charlie, collectionId, tokenId, testCase.account(bob), {Substrate: alice.address});
const owner = await helper.nft.getTokenOwner(collectionId, tokenId);
expect(owner.Substrate).to.be.equal(alice.address);
expect(owner?.Substrate).to.be.equal(alice.address);
const transferTokenFromTx = () => helper.nft.transferTokenFrom(charlie, collectionId, tokenId, testCase.account(bob), {Substrate: alice.address});
await expect(transferTokenFromTx()).to.be.rejectedWith('common.ApprovedValueTooLow');
});
Expand Down Expand Up @@ -559,12 +559,12 @@ describe('Administrator and collection owner do not need approval in order to ex

await helper.nft.transferTokenFrom(alice, collectionId, tokenId, {Substrate: charlie.address}, {Substrate: dave.address});
const owner1 = await helper.nft.getTokenOwner(collectionId, tokenId);
expect(owner1.Substrate).to.be.equal(dave.address);
expect(owner1?.Substrate).to.be.equal(dave.address);

await helper.collection.addAdmin(alice, collectionId, {Substrate: bob.address});
await helper.nft.transferTokenFrom(bob, collectionId, tokenId, {Substrate: dave.address}, {Substrate: alice.address});
const owner2 = await helper.nft.getTokenOwner(collectionId, tokenId);
expect(owner2.Substrate).to.be.equal(alice.address);
expect(owner2?.Substrate).to.be.equal(alice.address);
});

itSub('Fungible up to an approved amount', async ({helper}) => {
Expand Down
6 changes: 3 additions & 3 deletions tests/src/collator-selection/collatorSelection.seqtest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ describe('Integration Test: Collator Selection', () => {

await helper.wait.newSessions(2);

const newValidators = await helper.callRpc('api.query.session.validators');
const newValidators = await helper.callQuery('api.query.session.validators');
expect(newValidators).to.contain(gammaNode).and.contain(deltaNode).and.be.length(2);

const lastBlockNumber = await helper.chain.getLatestBlockNumber();
await helper.wait.newBlocks(1);
const lastGammaBlock = (await helper.callRpc('api.query.collatorSelection.lastAuthoredBlock', [gammaNode])).toNumber();
const lastDeltaBlock = (await helper.callRpc('api.query.collatorSelection.lastAuthoredBlock', [deltaNode])).toNumber();
const lastGammaBlock = await helper.callQuery('api.query.collatorSelection.lastAuthoredBlock', [gammaNode]);
const lastDeltaBlock = await helper.callQuery('api.query.collatorSelection.lastAuthoredBlock', [deltaNode]);
expect(lastGammaBlock >= lastBlockNumber || lastDeltaBlock >= lastBlockNumber).to.be.true;
});

Expand Down
2 changes: 1 addition & 1 deletion tests/src/collator-selection/identity.seqtest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ describe('Integration Test: Identities Manipulation', () => {

// oldIdentitiesCount + 9 because one identity is overwritten, not inserted on top
expect((await getIdentityAccounts(helper)).length).to.be.equal(oldIdentitiesCount + 9);
expect((await helper.callRpc('api.query.identity.identityOf', [singleIdentity[0]])).unwrap().info.display)
expect((await helper.callQuery('api.query.identity.identityOf', [singleIdentity[0]]))?.info.display)
.to.be.equal({Raw: 'something special'});
});

Expand Down
3 changes: 2 additions & 1 deletion tests/src/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import {itSub, expect, usingPlaygrounds} from './util';

describe('Connection smoke test', () => {
itSub('Connection can be established', async ({helper}) => {
const health = (await helper.callRpc('api.rpc.system.health')).toJSON();
const health = await helper.callRpc('api.rpc.system.health');
//TODO: probably another check is needed
expect(health).to.be.not.empty;
});

Expand Down
20 changes: 10 additions & 10 deletions tests/src/createItem.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {UniqueHelper} from './util/playgrounds/unique';
async function mintTokenHelper(helper: UniqueHelper, collection: any, signer: IKeyringPair, owner: ICrossAccountId, type: 'nft' | 'fungible' | 'refungible'='nft', properties?: IProperty[]) {
let token;
const itemCountBefore = await helper.collection.getLastTokenId(collection.collectionId);
const itemBalanceBefore = (await helper.callRpc('api.rpc.unique.balance', [collection.collectionId, owner, 0])).toBigInt();
const itemBalanceBefore = await helper.callRpc('api.rpc.unique.balance', [collection.collectionId, owner, 0]);
if(type === 'nft') {
token = await collection.mintToken(signer, owner, properties);
} else if(type === 'fungible') {
Expand All @@ -32,7 +32,7 @@ async function mintTokenHelper(helper: UniqueHelper, collection: any, signer: IK
}

const itemCountAfter = await helper.collection.getLastTokenId(collection.collectionId);
const itemBalanceAfter = (await helper.callRpc('api.rpc.unique.balance', [collection.collectionId, owner, 0])).toBigInt();
const itemBalanceAfter = await helper.callRpc('api.rpc.unique.balance', [collection.collectionId, owner, 0]);

if(type === 'fungible') {
expect(itemBalanceAfter - itemBalanceBefore).to.be.equal(10n);
Expand Down Expand Up @@ -148,12 +148,12 @@ describe('integration test: ext. ():', () => {
const token = await mintTokenHelper(helper, collection, alice, {Substrate: bob.address});
{
const totalPieces = await helper.callRpc('api.rpc.unique.totalPieces', [collection.collectionId, token.tokenId]);
expect(totalPieces?.unwrap().toBigInt()).to.be.equal(amount);
expect(totalPieces).to.be.equal(amount);
}
await token.transfer(bob, {Substrate: alice.address});
{
const totalPieces = await helper.callRpc('api.rpc.unique.totalPieces', [collection.collectionId, token.tokenId]);
expect(totalPieces?.unwrap().toBigInt()).to.be.equal(amount);
expect(totalPieces).to.be.equal(amount);
}
});

Expand Down Expand Up @@ -252,21 +252,21 @@ describe('Negative integration test: ext. createItem():', () => {
itSub('Check total pieces for invalid Fungible token', async ({helper}) => {
const collection = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'}, 0);
const invalidTokenId = 1_000_000;
expect((await helper.callRpc('api.rpc.unique.totalPieces', [collection.collectionId, invalidTokenId]))?.isNone).to.be.true;
expect((await helper.callRpc('api.rpc.unique.tokenData', [collection.collectionId, invalidTokenId]))?.pieces.toBigInt()).to.be.equal(0n);
expect((await helper.callRpc('api.rpc.unique.totalPieces', [collection.collectionId, invalidTokenId]))).to.be.null;
expect((await helper.callRpc('api.rpc.unique.tokenData', [collection.collectionId, invalidTokenId]))?.pieces).to.be.equal(0n);
});

itSub('Check total pieces for invalid NFT token', async ({helper}) => {
const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});
const invalidTokenId = 1_000_000;
expect((await helper.callRpc('api.rpc.unique.totalPieces', [collection.collectionId, invalidTokenId]))?.isNone).to.be.true;
expect((await helper.callRpc('api.rpc.unique.tokenData', [collection.collectionId, invalidTokenId]))?.pieces.toBigInt()).to.be.equal(0n);
expect((await helper.callRpc('api.rpc.unique.totalPieces', [collection.collectionId, invalidTokenId]))).to.be.null;
expect((await helper.callRpc('api.rpc.unique.tokenData', [collection.collectionId, invalidTokenId]))?.pieces).to.be.equal(0n);
});

itSub.ifWithPallets('Check total pieces for invalid Refungible token', [Pallets.ReFungible], async ({helper}) => {
const collection = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});
const invalidTokenId = 1_000_000;
expect((await helper.callRpc('api.rpc.unique.totalPieces', [collection.collectionId, invalidTokenId]))?.isNone).to.be.true;
expect((await helper.callRpc('api.rpc.unique.tokenData', [collection.collectionId, invalidTokenId]))?.pieces.toBigInt()).to.be.equal(0n);
expect((await helper.callRpc('api.rpc.unique.totalPieces', [collection.collectionId, invalidTokenId]))).to.be.null;
expect((await helper.callRpc('api.rpc.unique.tokenData', [collection.collectionId, invalidTokenId]))?.pieces).to.be.equal(0n);
});
});
4 changes: 2 additions & 2 deletions tests/src/creditFeesToTreasury.seqtest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ describe('integration test: Fees must be credited to Treasury:', () => {
await skipInflationBlock(api);
await helper.wait.newBlocks(1);

const totalBefore = (await helper.callRpc('api.query.balances.totalIssuance', [])).toBigInt();
const totalBefore = await helper.callQuery('api.query.balances.totalIssuance', []);

await helper.balance.transferToSubstrate(alice, bob.address, 1n);

const totalAfter = (await helper.callRpc('api.query.balances.totalIssuance', [])).toBigInt();
const totalAfter = await helper.callQuery('api.query.balances.totalIssuance', []);

expect(totalAfter).to.be.equal(totalBefore);
});
Expand Down
2 changes: 1 addition & 1 deletion tests/src/enableDisableTransfer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('Enable/Disable Transfers', () => {
});
const token = await collection.mintToken(alice, {Substrate: alice.address});
await token.transfer(alice, {Substrate: bob.address});
expect(await token.getOwner()).to.be.deep.equal({Substrate: bob.address});
expect((await token.getOwner())?.Substrate).to.be.equal(bob.address);
});

itSub('User can\'n transfer token with disabled transfer flag', async ({helper}) => {
Expand Down
33 changes: 19 additions & 14 deletions tests/src/eth/collectionAdmin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import {IEthCrossAccountId} from '../util/playgrounds/types';
import {usingEthPlaygrounds, itEth} from './util';
import {EthUniqueHelper} from './util/playgrounds/unique.dev';
import {CreateCollectionData} from './util/playgrounds/types';
import {UniqueRpcResult, convert} from '../util/playgrounds/converter';
import {RpcInterface} from '@polkadot/rpc-core/types';
import { CrossAccountId } from '../util/playgrounds/unique';

async function recordEthFee(helper: EthUniqueHelper, userAddress: string, call: () => Promise<any>) {
const before = await helper.balance.getSubstrate(helper.address.ethToSubstrate(userAddress));
Expand Down Expand Up @@ -162,9 +165,12 @@ describe('Add collection admins', () => {
await expect(collectionEvm.methods.addCollectionAdmin(user).call({from: admin}))
.to.be.rejectedWith('NoPermission');

const adminList = await helper.callRpc('api.rpc.unique.adminlist', [collectionId]);
const adminList = convert(await helper.callRpc('api.rpc.unique.adminlist', [collectionId])) as UniqueRpcResult<RpcInterface['unique']['adminlist']>;
expect(adminList.length).to.be.eq(1);
expect(adminList[0].asEthereum.toString().toLocaleLowerCase())
expect(adminList[0]).to.haveOwnProperty('Ethereum');
if(!('Ethereum' in adminList[0]))
throw Error();
expect(adminList[0].Ethereum.toString().toLocaleLowerCase())
.to.be.eq(admin.toLocaleLowerCase());
});

Expand Down Expand Up @@ -200,9 +206,8 @@ describe('Add collection admins', () => {
const adminList = await helper.callRpc('api.rpc.unique.adminlist', [collectionId]);
expect(adminList.length).to.be.eq(1);

const admin0Cross = helper.ethCrossAccount.fromKeyringPair(adminList[0]);
expect(admin0Cross.eth.toLocaleLowerCase())
.to.be.eq(adminCross.eth.toLocaleLowerCase());
const admin0Cross = new CrossAccountId(adminList[0]);
expect(admin0Cross.Ethereum).to.be.undefined;
});

itEth('(!negative tests!) Add [cross] admin by USER is not allowed', async ({helper}) => {
Expand Down Expand Up @@ -242,7 +247,7 @@ describe('Remove collection admins', () => {
{
const adminList = await helper.callRpc('api.rpc.unique.adminlist', [collectionId]);
expect(adminList.length).to.be.eq(1);
expect(adminList[0].asEthereum.toString().toLocaleLowerCase())
expect(new CrossAccountId(adminList[0]).Ethereum.toString().toLocaleLowerCase())
.to.be.eq(newAdmin.toLocaleLowerCase());
}

Expand Down Expand Up @@ -297,9 +302,9 @@ describe('Remove collection admins', () => {
{
const adminList = await helper.callRpc('api.rpc.unique.adminlist', [collectionId]);
expect(adminList.length).to.be.eq(2);
expect(adminList.toString().toLocaleLowerCase())
.to.be.deep.contains(admin0.toLocaleLowerCase())
.to.be.deep.contains(admin1.toLocaleLowerCase());
expect(adminList.map(address => (new CrossAccountId(address)).Ethereum.toLocaleLowerCase()))
.to.include(admin0.toLocaleLowerCase())
.to.include(admin1.toLocaleLowerCase());
}
});

Expand All @@ -318,7 +323,7 @@ describe('Remove collection admins', () => {
.to.be.rejectedWith('NoPermission');
{
const adminList = await helper.callRpc('api.rpc.unique.adminlist', [collectionId]);
expect(adminList[0].asEthereum.toString().toLocaleLowerCase())
expect(new CrossAccountId(adminList[0]).Ethereum.toString().toLocaleLowerCase())
.to.be.eq(admin.toLocaleLowerCase());
expect(adminList.length).to.be.eq(1);
}
Expand All @@ -342,9 +347,9 @@ describe('Remove collection admins', () => {

const adminList = await helper.callRpc('api.rpc.unique.adminlist', [collectionId]);
expect(adminList.length).to.be.eq(2);
expect(adminList.toString().toLocaleLowerCase())
.to.be.deep.contains(admin1.address.toLocaleLowerCase())
.to.be.deep.contains(admin2.address.toLocaleLowerCase());
expect(adminList)
.to.be.deep.contains({Substrate: admin1.address})
.to.be.deep.contains({Substrate: admin2.address});
});

itEth('(!negative tests!) Remove [cross] admin by USER is not allowed', async ({helper}) => {
Expand All @@ -362,7 +367,7 @@ describe('Remove collection admins', () => {

const adminList = await helper.callRpc('api.rpc.unique.adminlist', [collectionId]);
expect(adminList.length).to.be.eq(1);
expect(adminList[0].asSubstrate.toString().toLocaleLowerCase())
expect(new CrossAccountId(adminList[0]).Substrate.toString().toLocaleLowerCase())
.to.be.eq(adminSub.address.toLocaleLowerCase());
});
});
Expand Down
2 changes: 1 addition & 1 deletion tests/src/eth/createCollection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ describe('Create collection from EVM', () => {
expect(data.name).to.be.eq(name);
expect(data.description).to.be.eq(description);
expect(data.raw.tokenPrefix).to.be.eq(prefix);
expect(data.raw.mode).to.be.eq('Nft');
expect(data.raw.mode).to.be.eq('NFT');

const options = await collection.getOptions();

Expand Down
2 changes: 1 addition & 1 deletion tests/src/eth/createNFTCollection.seqtest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('Create NFT collection from EVM', () => {
expect(data.name).to.be.eq(name);
expect(data.description).to.be.eq(description);
expect(data.raw.tokenPrefix).to.be.eq(prefix);
expect(data.raw.mode).to.be.eq('Nft');
expect(data.raw.mode).to.be.eq('NFT');

const options = await collection.getOptions();

Expand Down
2 changes: 1 addition & 1 deletion tests/src/eth/createNFTCollection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe('Create NFT collection from EVM', () => {
expect(data.name).to.be.eq(name);
expect(data.description).to.be.eq(description);
expect(data.raw.tokenPrefix).to.be.eq(prefix);
expect(data.raw.mode).to.be.eq('Nft');
expect(data.raw.mode).to.be.eq('NFT');

expect(await contract.methods.description().call()).to.deep.equal(description);

Expand Down
4 changes: 2 additions & 2 deletions tests/src/eth/getCode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('RPC eth_getCode', () => {
{address: CONTRACT_HELPER},
].map(testCase => {
itEth(`returns value for native contract: ${testCase.address}`, async ({helper}) => {
const contractCodeSub = (await helper.callRpc('api.rpc.eth.getCode', [testCase.address])).toJSON();
const contractCodeSub = await helper.callRpc('api.rpc.eth.getCode', [testCase.address]);
const contractCodeEth = (await helper.getWeb3().eth.getCode(testCase.address));

expect(contractCodeSub).to.has.length.greaterThan(4);
Expand All @@ -44,7 +44,7 @@ describe('RPC eth_getCode', () => {
const signer = await helper.eth.createAccountWithBalance(donor);
const flipper = await helper.eth.deployFlipper(signer);

const contractCodeSub = (await helper.callRpc('api.rpc.eth.getCode', [flipper.options.address])).toJSON();
const contractCodeSub = await helper.callRpc('api.rpc.eth.getCode', [flipper.options.address]);
const contractCodeEth = (await helper.getWeb3().eth.getCode(flipper.options.address));

expect(contractCodeSub).to.has.length.greaterThan(4);
Expand Down
Loading

0 comments on commit 625bc84

Please sign in to comment.