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 @@ -834,7 +836,7 @@ export class CosmosWrapper {

async checkPassedProposal(propose_contract: string, proposalId: number) {
await getWithAttempts(
this,
this.sdk,
foxpy marked this conversation as resolved.
Show resolved Hide resolved
async () => await this.queryProposal(propose_contract, proposalId),
async (response) => response.proposal.status === 'passed',
20,
Expand All @@ -846,7 +848,7 @@ export class CosmosWrapper {
proposalId: number,
) {
await getWithAttempts(
this,
this.sdk,
async () =>
await this.queryMultiChoiceProposal(propose_contract, proposalId),
async (response) => response.proposal.status === 'passed',
Expand All @@ -859,7 +861,7 @@ export class CosmosWrapper {
proposalId: number,
) {
await getWithAttempts(
this,
this.sdk,
async () =>
await this.queryMultiChoiceProposal(propose_contract, proposalId),
async (response) => response.proposal.status === 'executed',
Expand All @@ -873,7 +875,7 @@ export class CosmosWrapper {
) {
await this.executeProposal(propose_contract, proposalId);
await getWithAttempts(
this,
this.sdk,
async () => await this.queryProposal(propose_contract, proposalId),
async (response) => response.proposal.status === 'executed',
20,
Expand All @@ -886,7 +888,7 @@ export class CosmosWrapper {
) {
await this.executeMultiChoiceProposal(proposalContract, proposalId);
await getWithAttempts(
this,
this.sdk,
async () =>
await this.queryMultiChoiceProposal(proposalContract, proposalId),
async (response) => response.proposal.status === 'executed',
Expand Down Expand Up @@ -924,21 +926,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 @@ -1074,10 +1081,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 @@ -66,13 +66,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
13 changes: 4 additions & 9 deletions src/helpers/wait.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
import { rest } from '@cosmos-client/core';
import { CosmosSDK } from '@cosmos-client/core/cjs/sdk';

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 const waitBlocks = async (sdk: any, n: number) => {
export const waitBlocks = async (sdk: CosmosSDK, n: number) => {
foxpy marked this conversation as resolved.
Show resolved Hide resolved
const targetHeight = (await getRemoteHeight(sdk)) + n;
for (;;) {
await wait(1);
Expand All @@ -32,7 +27,7 @@ export const waitBlocks = async (sdk: any, n: number) => {
* and only then returns result of getFunc()
*/
export const getWithAttempts = async <T>(
sdk: any,
sdk: CosmosSDK,
getFunc: () => Promise<T>,
readyFunc: (t: T) => Promise<boolean>,
numAttempts = 20,
Expand Down
6 changes: 1 addition & 5 deletions src/testcases/interchain_kv_query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -645,12 +645,8 @@ describe('Neutron / Interchain KV Query', () => {

test('should fail to remove icq #2 from non owner address before timeout expiration', async () => {
const queryId = 2;

const result = await removeQueryViaTx(cm[1], queryId);

expect((result as any).raw_log).toMatch(
/authorization failed: unauthorized/i,
);
expect(result.raw_log).toMatch(/authorization failed: unauthorized/i);
});

describe('Remove interchain query', () => {
Expand Down
33 changes: 17 additions & 16 deletions src/testcases/interchaintx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,22 +313,23 @@ describe('Neutron / Interchain TXs', () => {
});
});
test('delegate after the ICA channel was closed', async () => {
let rawLog: any;
let rawLog: string;
albertandrejev marked this conversation as resolved.
Show resolved Hide resolved
try {
rawLog = (
await cm1.executeContract(
contractAddress,
JSON.stringify({
delegate: {
interchain_account_id: icaId1,
validator: testState.wallets.cosmos.val1.address.toString(),
amount: '10',
denom: cm2.denom,
timeout: 1,
},
}),
)
).raw_log;
rawLog =
(
await cm1.executeContract(
contractAddress,
JSON.stringify({
delegate: {
interchain_account_id: icaId1,
validator: testState.wallets.cosmos.val1.address.toString(),
amount: '10',
denom: cm2.denom,
timeout: 1,
},
}),
)
).raw_log || '';
} catch (e) {
rawLog = e.message;
}
Expand Down Expand Up @@ -439,7 +440,7 @@ describe('Neutron / Interchain TXs', () => {
cm1.sdk,
() => cm1.listIBCChannels(),
async (channels) =>
channels.channels.find((c) => c.channel_id == 'channel-3').state ==
channels.channels.find((c) => c.channel_id == 'channel-3')?.state ==
'STATE_OPEN',
);
});
Expand Down
Loading