Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove esbuild + remove some any #61

Merged
merged 16 commits into from
Jan 19, 2023
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ contracts/artifacts/
.vscode

write-file-atomic*

junit.xml
6 changes: 6 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
presets: [
['@babel/preset-env', { targets: { esmodules: true } }],
'@babel/preset-typescript',
],
};
13 changes: 2 additions & 11 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const ch = require('child_process');
const { defaults } = require('jest-config');

const config = {
reporters: ['default', 'jest-junit'],
cacheDirectory: '.jest/cache',
coverageDirectory: '.jest/coverage',
bail: true,
Expand All @@ -17,17 +18,7 @@ const config = {
},
setupFilesAfterEnv: ['jest-extended/all'],
transform: {
'^.+\\.tsx?$': [
'esbuild-jest',
{
target: 'node14',
format: 'cjs',
sourcemap: true,
loaders: {
'.test.ts': 'tsx',
},
},
],
'^.+\\.[t|j]sx?$': 'babel-jest',
},
};

Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@
"@types/lodash": "^4.14.182",
"@types/long": "^4.0.2",
"axios": "^0.27.2",
"babel-jest": "^29.3.1",
"date-fns": "^2.16.1",
"esbuild": "^0.14.49",
"esbuild-jest": "^0.5.0",
"express": "^4.17.1",
"jest": "^27.5.1",
"jest-junit": "^15.0.0",
"lodash": "^4.17.21",
"long": "^5.2.0"
},
"devDependencies": {
"@babel/preset-env": "^7.20.2",
"@babel/preset-typescript": "^7.18.6",
"@types/express": "^4.17.9",
"@types/jest": "^28.1",
"@typescript-eslint/eslint-plugin": "^5.19.0",
Expand Down
43 changes: 25 additions & 18 deletions src/helpers/cosmos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const TREASURY_CONTRACT_ADDRESS =
'neutron1vguuxez2h5ekltfj9gjd62fs5k4rl2zy5hfrncasykzw08rezpfsd2rhm7';
const CONTRACTS_PATH = process.env.CONTRACTS_PATH || './contracts/artifacts';

type ChannelsList = {
export type ChannelsList = {
channels: {
state: string;
ordering: string;
Expand Down Expand Up @@ -100,13 +100,15 @@ type SingleChoiceProposal = {
/// The threshold at which this proposal will pass.
/// proposal's creation.
readonly total_power: string;
readonly status:
| 'open'
| 'rejected'
| 'passed'
| 'executed'
| 'closed'
| 'execution_failed';
readonly proposal: {
status:
| 'open'
| 'rejected'
| 'passed'
| 'executed'
| 'closed'
| 'execution_failed';
};
};

type TotalPowerAtHeightResponse = {
Expand Down Expand Up @@ -841,7 +843,7 @@ export class CosmosWrapper {

async checkPassedProposal(propose_contract: string, proposalId: number) {
await getWithAttempts(
this,
this.blockWaiter,
async () => await this.queryProposal(propose_contract, proposalId),
async (response) => response.proposal.status === 'passed',
20,
Expand All @@ -853,7 +855,7 @@ export class CosmosWrapper {
proposalId: number,
) {
await getWithAttempts(
this,
this.blockWaiter,
async () =>
await this.queryMultiChoiceProposal(propose_contract, proposalId),
async (response) => response.proposal.status === 'passed',
Expand All @@ -866,7 +868,7 @@ export class CosmosWrapper {
proposalId: number,
) {
await getWithAttempts(
this,
this.blockWaiter,
async () =>
await this.queryMultiChoiceProposal(propose_contract, proposalId),
async (response) => response.proposal.status === 'executed',
Expand All @@ -880,7 +882,7 @@ export class CosmosWrapper {
) {
await this.executeProposal(propose_contract, proposalId);
await getWithAttempts(
this,
this.blockWaiter,
async () => await this.queryProposal(propose_contract, proposalId),
async (response) => response.proposal.status === 'executed',
20,
Expand All @@ -893,7 +895,7 @@ export class CosmosWrapper {
) {
await this.executeMultiChoiceProposal(proposalContract, proposalId);
await getWithAttempts(
this,
this.blockWaiter,
async () =>
await this.queryMultiChoiceProposal(proposalContract, proposalId),
async (response) => response.proposal.status === 'executed',
Expand Down Expand Up @@ -931,21 +933,26 @@ export class CosmosWrapper {
async queryProposal(
propose_contract: string,
proposalId: number,
): Promise<any> {
): Promise<SingleChoiceProposal> {
return await this.queryContract<SingleChoiceProposal>(propose_contract, {
proposal: {
proposal_id: proposalId,
},
});
}

async queryTotalVotingPower(core_contract: string): Promise<any> {
async queryTotalVotingPower(
core_contract: string,
NeverHappened marked this conversation as resolved.
Show resolved Hide resolved
): Promise<TotalPowerAtHeightResponse> {
return await this.queryContract<TotalPowerAtHeightResponse>(core_contract, {
total_power_at_height: {},
});
}

async queryVotingPower(core_contract: string, addr: string): Promise<any> {
async queryVotingPower(
core_contract: string,
addr: string,
): Promise<VotingPowerAtHeightResponse> {
return await this.queryContract<VotingPowerAtHeightResponse>(
core_contract,
{
Expand Down Expand Up @@ -1081,10 +1088,10 @@ export class CosmosWrapper {
}

async listIBCChannels(): Promise<ChannelsList> {
const req = await axios.get<ChannelsList>(
const res = await axios.get<ChannelsList>(
`${this.sdk.url}/ibc/core/channel/v1/channels`,
);
return req.data;
return res.data;
}

async queryTotalBurnedNeutronsAmount(): Promise<TotalBurnedNeutronsAmountResponse> {
Expand Down
13 changes: 8 additions & 5 deletions src/helpers/env.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from 'axios';
import { execSync } from 'child_process';
import { wait } from './wait';
import { getContractsHashes } from './cosmos';
import { ChannelsList, getContractsHashes } from './cosmos';

const BLOCKS_COUNT_BEFORE_START = process.env.BLOCKS_COUNT_BEFORE_START
? parseInt(process.env.BLOCKS_COUNT_BEFORE_START, 10)
Expand Down Expand Up @@ -70,13 +70,16 @@ export const waitForChannel = async (

while (Date.now() < start + timeout) {
try {
const r = await axios.get(`${host}/ibc/core/channel/v1/channels`, {
timeout: 1000,
});
const r = await axios.get<ChannelsList>(
`${host}/ibc/core/channel/v1/channels`,
{
timeout: 1000,
},
);
if (
r.data.channels.length > 0 &&
r.data.channels.every(
(channel: any) => channel.counterparty.channel_id !== '',
(channel) => channel.counterparty.channel_id !== '',
)
) {
await wait(20);
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/ica.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const getIca = (
numAttempts = 20,
) =>
getWithAttempts(
cm,
cm.blockWaiter,
() =>
cm.queryContract<{
interchain_account_address: string;
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/icq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const waitForICQResultWithRemoteHeight = (
numAttempts = 20,
) =>
getWithAttempts(
cm,
cm.blockWaiter,
() => getRegisteredQuery(cm, contractAddress, queryId),
async (query) =>
query.registered_query.last_submitted_result_remote_height >=
Expand Down Expand Up @@ -80,7 +80,7 @@ export const waitForTransfersAmount = (
numAttempts = 50,
) =>
getWithAttempts(
cm,
cm.blockWaiter,
async () =>
(await queryTransfersNumber(cm, contractAddress)).transfers_number,
async (amount) => amount == expectedTransfersAmount,
Expand Down
21 changes: 8 additions & 13 deletions src/helpers/wait.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
import { rest, websocket } from '@cosmos-client/core';
import { CosmosSDK } from '@cosmos-client/core/cjs/sdk';

(global as any).WebSocket = require('ws');
global.WebSocket = require('ws');

export const wait = async (seconds: number) =>
new Promise((r) => {
setTimeout(() => r(true), 1000 * seconds);
});

/*
* Following functions accepts `sdk` as `any` instead of `CosmosSDK`.
* That's because otherwise the script wouldn't even run due to some
* weird babel issues.
*/

export const getRemoteHeight = async (sdk: any) => {
export const getRemoteHeight = async (sdk: CosmosSDK) => {
const block = await rest.tendermint.getLatestBlock(sdk);
return +block.data.block.header.height;
};

export class BlockWaiter {
url;
url: string;

constructor(url: string) {
this.url = url;
Expand All @@ -41,8 +36,8 @@ export class BlockWaiter {
method: 'subscribe',
params: ["tm.event='NewBlock'"],
});
ws.subscribe((res) => {
if (Object.entries((res as any).result).length !== 0) {
ws.subscribe((res: websocket.ResponseSchema) => {
if (Object.entries(res.result).length !== 0) {
n--;
if (n == 0) {
ws.unsubscribe();
Expand All @@ -60,7 +55,7 @@ export class BlockWaiter {
* and only then returns result of getFunc()
*/
export const getWithAttempts = async <T>(
cm: any,
blockWaiter: BlockWaiter,
getFunc: () => Promise<T>,
readyFunc: (t: T) => Promise<boolean>,
numAttempts = 20,
Expand All @@ -76,7 +71,7 @@ export const getWithAttempts = async <T>(
} catch (e) {
error = e;
}
await cm.blockWaiter.waitBlocks(1);
await blockWaiter.waitBlocks(1);
}
throw error != null ? error : new Error('getWithAttempts: no attempts left');
};
22 changes: 11 additions & 11 deletions src/testcases/governance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ describe('Neutron / Governance', () => {
cm.wallet.address.toString(),
);
await getWithAttempts(
cm,
cm.blockWaiter,
async () =>
await cm.queryVotingPower(
CORE_CONTRACT_ADDRESS,
cm.wallet.address.toString(),
),
async (response) => response.power == '1000',
async (response) => response.power == 1000,
20,
);
});
Expand All @@ -64,13 +64,13 @@ describe('Neutron / Governance', () => {
cm2.wallet.address.toString(),
);
await getWithAttempts(
cm2,
cm2.blockWaiter,
async () =>
await cm2.queryVotingPower(
CORE_CONTRACT_ADDRESS,
cm2.wallet.address.toString(),
),
async (response) => response.power == '1000',
async (response) => response.power == 1000,
20,
);
});
Expand All @@ -81,21 +81,21 @@ describe('Neutron / Governance', () => {
cm3.wallet.address.toString(),
);
await getWithAttempts(
cm3,
cm3.blockWaiter,
async () =>
await cm3.queryVotingPower(
CORE_CONTRACT_ADDRESS,
cm3.wallet.address.toString(),
),
async (response) => response.power == '1000',
async (response) => response.power == 1000,
20,
);
});
test('check voting power', async () => {
await getWithAttempts(
cm,
cm.blockWaiter,
async () => await cm.queryTotalVotingPower(CORE_CONTRACT_ADDRESS),
async (response) => response.power == '3000',
async (response) => response.power == 3000,
20,
);
});
Expand All @@ -105,7 +105,7 @@ describe('Neutron / Governance', () => {
test('send funds from wallet 1', async () => {
await cm.msgSend(CORE_CONTRACT_ADDRESS, '1000');
await getWithAttempts(
cm,
cm.blockWaiter,
async () => await cm.queryBalances(CORE_CONTRACT_ADDRESS),
async (response) => response.balances[0].amount == '1000',
20,
Expand Down Expand Up @@ -294,7 +294,7 @@ describe('Neutron / Governance', () => {
}
expect(rawLog.includes("proposal is not in 'passed' state"));
await getWithAttempts(
cm,
cm.blockWaiter,
async () =>
await cm.queryProposal(PROPOSE_CONTRACT_ADDRESS, proposalId),
async (response) => response.proposal.status === 'rejected',
Expand Down Expand Up @@ -404,7 +404,7 @@ describe('Neutron / Governance', () => {
}
expect(rawLog.includes("proposal is not in 'passed' state"));
await getWithAttempts(
cm.sdk,
cm.blockWaiter,
async () =>
await cm.queryMultiChoiceProposal(
PROPOSE_MULTIPLE_CONTRACT_ADDRESS,
Expand Down
Loading