Skip to content

Commit

Permalink
add generated types and refactor tests, add eth sign to crowdloan
Browse files Browse the repository at this point in the history
  • Loading branch information
hussein-aitlahcen committed Jan 13, 2022
1 parent 2c1fe04 commit 08d2b3f
Show file tree
Hide file tree
Showing 27 changed files with 22,352 additions and 818 deletions.
7,999 changes: 7,477 additions & 522 deletions integration-tests/runtime-tests/package-lock.json

Large diffs are not rendered by default.

17 changes: 14 additions & 3 deletions integration-tests/runtime-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
"prepare": "npm run compile",
"init": "node src/initializeState.js",
"init_and_test": "npm run init && mocha src/test.js --reporter mochawesome",
"test": "mocha src/test.js --require src/utils/testSetup.js --reporter mochawesome"
"test": "ts-mocha --paths --full-trace --async-stack-traces -p tsconfig.json src/test.ts --require src/utils/testSetup.ts",
"gen": "npm run gen:defs && npm run gen:meta",
"gen:defs": "ts-node --skip-project node_modules/.bin/polkadot-types-from-defs --package @composable/types/interfaces --input ./src/types/interfaces",
"gen:meta": "ts-node --skip-project node_modules/.bin/polkadot-types-from-chain --package @composable/types/interfaces --endpoint ws://localhost:9988 --output src/types/interfaces/"
},
"directories": {
"src": "./src",
Expand All @@ -36,8 +39,15 @@
"node": ">=12.0.0"
},
"dependencies": {
"@polkadot/api": "^7.2.1",
"@polkadot/api": "^7.3.1",
"@polkadot/ts": "latest",
"@polkadot/typegen": "^7.3.1",
"@polkadot/types": "^7.3.1",
"@polkadot/types-augment": "^7.3.1",
"@polkadot/types-codec": "^7.3.1",
"@polkadot/types-create": "^7.3.1",
"@polkadot/types-known": "^7.3.1",
"@polkadot/types-support": "^7.3.1",
"@types/mocha": "^9.0.0",
"@types/node": "^17.0.8",
"@types/ramda": "^0.27.62",
Expand All @@ -47,6 +57,7 @@
"minimist": "^1.2.5",
"mocha": "latest",
"mochawesome": "^7.0.1",
"ramda": "^0.27.1"
"ramda": "^0.27.1",
"web3": "^1.6.1"
}
}

This file was deleted.

19 changes: 19 additions & 0 deletions integration-tests/runtime-tests/src/globals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ApiPromise, Keyring } from '@polkadot/api';
import { KeyringPair } from '@polkadot/keyring/types';
import Web3 from 'web3';

declare global {
var useTestnetWallets: boolean;
var testSudoCommands: boolean;
var endpoint: string;
var api: ApiPromise;
var keyring: Keyring;
var walletAlice: KeyringPair;
var walletBob: KeyringPair;
var walletCharlie: KeyringPair;
var walletDave: KeyringPair;
var walletEve: KeyringPair;
var walletFerdie: KeyringPair;
var web3: Web3;
}

2 changes: 0 additions & 2 deletions integration-tests/runtime-tests/src/initializeState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import { ApiPromise, Keyring, WsProvider } from '@polkadot/api';
import { KeyringPair } from '@polkadot/keyring/types';
import { testTransactionGenerator } from './generators/exampleGenerators/testTransactionGenerator';
import { crowdloanRewardGenerator } from './generators/crowdloanGenerators/crowdloanRewardGenerator';


// ToDo: Change endpoint to be read from env variables or run parameters.
Expand All @@ -26,7 +25,6 @@ let walletFerdie: KeyringPair;
**/
async function createDefaultData(api: ApiPromise, sudoKey: KeyringPair) {
await testTransactionGenerator.testTransaction(api, walletAlice, walletBob.address);
await crowdloanRewardGenerator.testCrowdloanRewards(api, sudoKey, walletAlice);
// ToDo: Add additional data generator calls here.
// Consider splitting it up into groups of similiar generators, to keep it clean.
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
/* eslint-disable no-trailing-spaces */
import { ApiPromise } from '@polkadot/api';
import { expect } from 'chai';


export class QueryCrowdloanRewardsTests {
/**
*
*
*/
public static runQueryCrowdloanRewardsTests() {
describe('query.crowdloanRewards.account Tests', function () {
describe('query.crowdloanRewards.account Tests', function() {
this.timeout(0);
it('query.crowdloanRewards.claimedRewards Tests', async function() {
await QueryCrowdloanRewardsTests.queryCrowdloanRewardsClaimedRewardsTest();
Expand Down Expand Up @@ -38,8 +36,7 @@ export class QueryCrowdloanRewardsTests {
const expectedClaimedRewards = 500000000000;
const claimedRewards = await global.api.query.crowdloanRewards.claimedRewards();
console.debug("claimedRewards: " + claimedRewards);
expect(claimedRewards).to.satisfy((s)=>{return typeof(s) == typeof(Object)});
expect(parseInt(claimedRewards)).to.equal(expectedClaimedRewards);
expect(claimedRewards.toNumber()).to.equal(expectedClaimedRewards);
}

/**
Expand All @@ -51,35 +48,32 @@ export class QueryCrowdloanRewardsTests {
console.debug('queryCrowdloanRewardsTotalContributorsTest');
const expectedTotalContributors = 100;
const totalContributors = await global.api.query.crowdloanRewards.totalContributors();
expect(totalContributors).to.satisfy((s)=>{return typeof(s) == typeof(Object)});
expect(parseInt(totalContributors)).to.equal(expectedTotalContributors);
expect(totalContributors.toNumber()).to.equal(expectedTotalContributors);
}

/**
* Checks for a successful return of
* query.crowdloanRewards.totalRewards()
*/
private static async queryCrowdloanRewardsTotalRewardsTest() {
private static async queryCrowdloanRewardsTotalRewardsTest() {
// ToDo (D. Roth): Consider removing expected value test and only check for result type.
console.debug('queryCrowdloanRewardsTotalRewardsTest');
const expectedTotalRewards = 5050000000000000;
const totalRewards = await global.api.query.crowdloanRewards.totalRewards();
expect(totalRewards).to.satisfy((s)=>{return typeof(s) == typeof(Object)});
expect(parseInt(totalRewards)).to.equal(expectedTotalRewards);
expect(totalRewards.toNumber()).to.equal(expectedTotalRewards);
}

/**
* Checks for a successful return of
* query.crowdloanRewards.vestingBlockStart()
*/
private static async queryCrowdloanRewardsVestingBlockStartTest() {
private static async queryCrowdloanRewardsVestingBlockStartTest() {
// ToDo (D. Roth): Consider removing expected value test and only check for result type.
console.debug('queryCrowdloanRewardsVestingBlockStartTest');
const vestingBlockStart = await global.api.query.crowdloanRewards.vestingBlockStart();
expect(vestingBlockStart).to.satisfy((s)=>{return typeof(s) == typeof(Object)});
expect(parseInt(vestingBlockStart)).to.be.an('integer');
expect(vestingBlockStart.isSome).to.be.true
}
}

// Uncomment to debug
// QueryCrowdloanRewardsTests.runQueryCrowdloanRewardsTests();
// QueryCrowdloanRewardsTests.runQueryCrowdloanRewardsTests();
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
/* eslint-disable no-trailing-spaces */
import {Promise} from 'bluebird';

Promise.config({
// Enable warnings
warnings: true,
// Enable long stack traces
longStackTraces: true,
// Enable cancellation
cancellation: true,
// Enable monitoring
monitoring: true,
// Enable async hooks
asyncHooks: true,
});
import { sendAndWaitForSuccess } from '@composable/utils/polkadotjs';
import { IKeyringPair } from '@polkadot/types/types';

/**
* Contains all TX tests for the pallet:
Expand All @@ -34,54 +22,24 @@ export class TxBondedFinanceTests {
/**
* Tests tx.bondedFinance.offer successfully. SUDO Check!
*/
private static async txBondedFinanceOfferTest(wallet) {
private static txBondedFinanceOfferTest(wallet: IKeyringPair) {
// ToDo: Find good parameter for reward.asset
const requestParameters = {
beneficiary: wallet.publicKey,
asset: 1,
bondPrice: 1,
nbOfBonds: 10000000000000,
maturity: {Finite:{returnIn:50}},
reward: {asset: [170,141,183,460,469,231,731,687,303,715,884,105,728], amount: 10, maturity: 1}
bondPrice: 10000000000000, // pub const MIN_VESTED_TRANSFER: u32 = 1_000_000;
nbOfBonds: 1,
maturity: { Finite: { returnIn: 10 } },
reward: { asset: 1, amount: 100000000000000, maturity: 1 } // pub MinReward: Balance = 10 * CurrencyId::PICA.unit::<Balance>();
};
try {
return new Promise(function (resolve, reject) {
global.api.tx.bondedFinance.offer(requestParameters)
.signAndSend(wallet, { nonce: -1 }, ({ events=[], status }) => {
console.debug('txBondedFinanceOfferTest: Transaction status: ', status.type);
if (status.isFinalized) {
events
// find/filter for failed events
.filter(({ event }) =>
global.api.events.system.ExtrinsicFailed.is(event)
)
// we know that data for system.ExtrinsicFailed is
// (DispatchError, DispatchInfo)
.forEach(({ event: { data: [error, info] } }) => {
if (error.isModule) {
// for module errors, we have the section indexed, lookup
const decoded = global.api.registry.findMetaError(error.asModule);
const { docs, method, section } = decoded;

console.log(`${section}.${method}: ${docs.join(' ')}`);
throw new Error('txBondedFinanceOfferTest: ExtrinsicFailed!');
} else {
// Other, CannotLookup, BadOrigin, no extra info
console.log(error.toString());
throw new Error('txBondedFinanceOfferTest: ExtrinsicFailed!');
}
});
// If no errors have occured, resolve promise.
// ToDo (D. Roth): Add checks
resolve();
}
});
});
} catch (exc) {
console.error(exc);
}
return sendAndWaitForSuccess(
global.api,
wallet,
global.api.events.bondedFinance.NewOffer.is,
global.api.tx.bondedFinance.offer(requestParameters)
);
}
}

// Uncomment to debug
TxBondedFinanceTests.runTxBondedFinanceTests();
TxBondedFinanceTests.runTxBondedFinanceTests();
Loading

0 comments on commit 08d2b3f

Please sign in to comment.