Skip to content

Commit

Permalink
test: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasmpw committed Dec 11, 2024
1 parent 8c65c05 commit 9ae2455
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 67 deletions.
1 change: 0 additions & 1 deletion packages/arch3-core/src/archwayclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { CosmWasmClient } from '@cosmjs/cosmwasm-stargate';
import { EncodeObject } from '@cosmjs/proto-signing';
import {
HttpEndpoint,
RpcClient,
HttpBatchClient,
HttpBatchClientOptions,
CometClient,
Expand Down
81 changes: 81 additions & 0 deletions packages/arch3-core/src/modules/authz/tx.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { GenericAuthorization } from 'cosmjs-types/cosmos/authz/v1beta1/authz';
import { MsgGrant, MsgRevoke } from 'cosmjs-types/cosmos/authz/v1beta1/tx';
import { SendAuthorization } from 'cosmjs-types/cosmos/bank/v1beta1/authz';
import { MsgWithdrawDelegatorReward } from 'cosmjs-types/cosmos/distribution/v1beta1/tx';

import { createAuthzAminoConverters } from '.';

const aminoConverters = createAuthzAminoConverters();

const archwayd = {
chainId: 'local-1',
endpoint: 'http://localhost:26657',
prefix: 'archway',
denom: process.env.DENOM || 'aarch',
};

const granterAddress = 'archway1ecak50zhujddqd639xw4ejghnyrrc6jlwnlgwt';
const granteeAddress = 'archway1vrzhff4gnnye4qt37stn9v6xker333cdew6xdt';

describe('Amino converter for /cosmos.authz.v1beta1.MsgGrant', () => {
describe('Grants generic authorization with /cosmos.authz.v1beta1.GenericAuthorization', () => {
it('encodes and decodes back to the same value', () => {
const message = {
granter: granterAddress,
grantee: granteeAddress,
grant: {
authorization: {
typeUrl: GenericAuthorization.typeUrl,
value: GenericAuthorization.encode(
GenericAuthorization.fromPartial({
msg: MsgWithdrawDelegatorReward.typeUrl,
}),
).finish(),
},
expiration: undefined,
},
};
const toAmino = aminoConverters[MsgGrant.typeUrl].toAmino(message);
const result = aminoConverters[MsgGrant.typeUrl].fromAmino(toAmino);

expect(message).toStrictEqual(result);
});
});
describe('Grants generic authorization with /cosmos.authz.v1beta1.SendAuthorization', () => {
it('encodes and decodes back to the same value', () => {
const message = {
granter: granterAddress,
grantee: granteeAddress,
grant: {
authorization: {
typeUrl: SendAuthorization.typeUrl,
value: SendAuthorization.encode(
SendAuthorization.fromPartial({
spendLimit: [{ denom: archwayd.denom, amount: '1' }],
}),
).finish(),
},
expiration: undefined,
},
};
const toAmino = aminoConverters[MsgGrant.typeUrl].toAmino(message);
const result = aminoConverters[MsgGrant.typeUrl].fromAmino(toAmino);

expect(message).toStrictEqual(result);
});
});
});

describe('Amino converter for /cosmos.authz.v1beta1.MsgRevoke', () => {
it('encodes and decodes back to the same value', () => {
const message = {
granter: granterAddress,
grantee: granteeAddress,
msgTypeUrl: MsgWithdrawDelegatorReward.typeUrl,
};
const toAmino = aminoConverters[MsgRevoke.typeUrl].toAmino(message);
const result = aminoConverters[MsgRevoke.typeUrl].fromAmino(toAmino);

expect(message).toStrictEqual(result);
});
});
135 changes: 70 additions & 65 deletions packages/arch3-core/src/modules/authz/tx.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,48 @@
import { AminoConverters } from "@cosmjs/stargate";
import { GenericAuthorization } from "cosmjs-types/cosmos/authz/v1beta1/authz";
import { MsgGrant, MsgRevoke } from "cosmjs-types/cosmos/authz/v1beta1/tx";
import { SendAuthorization } from "cosmjs-types/cosmos/bank/v1beta1/authz";
import { Timestamp } from "cosmjs-types/google/protobuf/timestamp";
import { fromJsonTimestamp, fromTimestamp } from "cosmjs-types/helpers";
import { AminoConverters } from '@cosmjs/stargate';
import { GenericAuthorization } from 'cosmjs-types/cosmos/authz/v1beta1/authz';
import { MsgGrant, MsgRevoke } from 'cosmjs-types/cosmos/authz/v1beta1/tx';
import { SendAuthorization } from 'cosmjs-types/cosmos/bank/v1beta1/authz';
import { Timestamp } from 'cosmjs-types/google/protobuf/timestamp';
import { fromJsonTimestamp, fromTimestamp } from 'cosmjs-types/helpers';

export function createAuthzAminoConverters(): AminoConverters {
/**
* Creates the Amino converters for the Authz tx messages
*
* @returns to be used in a client
*/
export const createAuthzAminoConverters = (): AminoConverters => {
return {
[MsgGrant.typeUrl]: {
aminoType: "cosmos-sdk/MsgGrant",
aminoType: 'cosmos-sdk/MsgGrant',
toAmino: ({ granter, grantee, grant }) => {
if (!grant || !grant.authorization) {

Check failure

Code scanning / ESLint

Disallow member access on a value with type `any` Error

Unsafe member access .authorization on an any value.
throw new Error(`Unsupported grant type: '${grant?.authorization?.typeUrl}'`);

Check failure

Code scanning / ESLint

Disallow member access on a value with type `any` Error

Unsafe member access .authorization on an any value.
}
let authorizationValue;
switch (grant?.authorization?.typeUrl) {

Check failure

Code scanning / ESLint

Disallow member access on a value with type `any` Error

Unsafe member access .authorization on an any value.
case GenericAuthorization.typeUrl: {
const generic = GenericAuthorization.decode(grant.authorization.value);
authorizationValue = {
type: "cosmos-sdk/GenericAuthorization",
value: {
msg: generic.msg,
},
};
break;
}
case SendAuthorization.typeUrl: {
const spend = SendAuthorization.decode(grant.authorization.value);
authorizationValue = {
type: "cosmos-sdk/SendAuthorization",
value: {
spend_limit: spend.spendLimit,
},
};
break;
}
default:
throw new Error(`Unsupported grant type: '${grant.authorization.typeUrl}'`);
case GenericAuthorization.typeUrl: {
const generic = GenericAuthorization.decode(grant.authorization.value);

Check failure

Code scanning / ESLint

Disallow calling a function with a value with type `any` Error

Unsafe argument of type any assigned to a parameter of type Uint8Array | BinaryReader.

Check failure

Code scanning / ESLint

Disallow member access on a value with type `any` Error

Unsafe member access .authorization on an any value.
authorizationValue = {
type: 'cosmos-sdk/GenericAuthorization',
value: {
msg: generic.msg,
},
};
break;
}
case SendAuthorization.typeUrl: {
const spend = SendAuthorization.decode(grant.authorization.value);

Check failure

Code scanning / ESLint

Disallow calling a function with a value with type `any` Error

Unsafe argument of type any assigned to a parameter of type Uint8Array | BinaryReader.

Check failure

Code scanning / ESLint

Disallow member access on a value with type `any` Error

Unsafe member access .authorization on an any value.
authorizationValue = {
type: 'cosmos-sdk/SendAuthorization',
value: {
spend_limit: spend.spendLimit,

Check failure

Code scanning / ESLint

Enforce naming conventions for everything across a codebase Error

Object Literal Property name spend\_limit must match one of the following formats: strictCamelCase

Check failure

Code scanning / ESLint

Enforce camelcase naming convention Error

Identifier 'spend_limit' is not in camel case.
},
};
break;
}
default:
throw new Error(`Unsupported grant type: '${grant.authorization.typeUrl}'`);

Check failure

Code scanning / ESLint

Disallow member access on a value with type `any` Error

Unsafe member access .authorization on an any value.
}
const expiration = grant.expiration?.seconds;

return {
granter,

Check failure

Code scanning / ESLint

Disallow assigning a value with type `any` to variables and properties Error

Unsafe assignment of an any value.
Expand All @@ -47,8 +51,8 @@ export function createAuthzAminoConverters(): AminoConverters {
authorization: authorizationValue,

Check failure

Code scanning / ESLint

Disallow assigning a value with type `any` to variables and properties Error

Unsafe assignment of an any value.
expiration: grant.expiration

Check failure

Code scanning / ESLint

Disallow member access on a value with type `any` Error

Unsafe member access .expiration on an any value.
? fromTimestamp(grant.expiration)

Check failure

Code scanning / ESLint

Disallow calling a function with a value with type `any` Error

Unsafe argument of type any assigned to a parameter of type Timestamp.

Check failure

Code scanning / ESLint

Disallow member access on a value with type `any` Error

Unsafe member access .expiration on an any value.
.toISOString()
.replace(/\.\d{3}Z$/, "Z")
.toISOString()
.replace(/\.\d{3}Z$/, 'Z')
: undefined,
},
};
Expand All @@ -57,28 +61,28 @@ export function createAuthzAminoConverters(): AminoConverters {
const authorizationType = grant?.authorization?.type;

Check failure

Code scanning / ESLint

Disallow assigning a value with type `any` to variables and properties Error

Unsafe assignment of an any value.

Check failure

Code scanning / ESLint

Disallow member access on a value with type `any` Error

Unsafe member access .authorization on an any value.
let authorizationValue;
switch (authorizationType) {
case "cosmos-sdk/GenericAuthorization": {
authorizationValue = {
typeUrl: GenericAuthorization.typeUrl,
value: GenericAuthorization.encode({
msg: grant.authorization.value.msg,
}).finish(),
};
break;
}
case "cosmos-sdk/SendAuthorization": {
authorizationValue = {
typeUrl: SendAuthorization.typeUrl,
value: SendAuthorization.encode(
SendAuthorization.fromPartial({
spendLimit: grant.authorization.value.spend_limit,
}),
).finish(),
};
break;
}
default:
throw new Error(`Unsupported grant type: '${grant?.authorization?.type}'`);
case 'cosmos-sdk/GenericAuthorization': {
authorizationValue = {
typeUrl: GenericAuthorization.typeUrl,
value: GenericAuthorization.encode({
msg: grant.authorization.value.msg,

Check failure

Code scanning / ESLint

Disallow assigning a value with type `any` to variables and properties Error

Unsafe assignment of an any value.

Check failure

Code scanning / ESLint

Disallow member access on a value with type `any` Error

Unsafe member access .authorization on an any value.
}).finish(),
};
break;
}
case 'cosmos-sdk/SendAuthorization': {
authorizationValue = {
typeUrl: SendAuthorization.typeUrl,
value: SendAuthorization.encode(
SendAuthorization.fromPartial({
spendLimit: grant.authorization.value.spend_limit,

Check failure

Code scanning / ESLint

Disallow assigning a value with type `any` to variables and properties Error

Unsafe assignment of an any value.

Check failure

Code scanning / ESLint

Disallow member access on a value with type `any` Error

Unsafe member access .authorization on an any value.
}),
).finish(),
};
break;
}
default:
throw new Error(`Unsupported grant type: '${grant?.authorization?.type}'`);

Check failure

Code scanning / ESLint

Disallow member access on a value with type `any` Error

Unsafe member access .authorization on an any value.
}
return MsgGrant.fromPartial({
granter,

Check failure

Code scanning / ESLint

Disallow assigning a value with type `any` to variables and properties Error

Unsafe assignment of an any value.
Expand All @@ -91,18 +95,19 @@ export function createAuthzAminoConverters(): AminoConverters {
},
},
[MsgRevoke.typeUrl]: {
aminoType: "cosmos-sdk/MsgRevoke",
toAmino: ({ granter, grantee, msgTypeUrl }) => ({
aminoType: 'cosmos-sdk/MsgRevoke',
toAmino: ({ granter, grantee, msgTypeUrl }) => {
return {
granter,

Check failure

Code scanning / ESLint

Disallow assigning a value with type `any` to variables and properties Error

Unsafe assignment of an any value.
grantee,

Check failure

Code scanning / ESLint

Disallow assigning a value with type `any` to variables and properties Error

Unsafe assignment of an any value.
msg_type_url: msgTypeUrl,

Check failure

Code scanning / ESLint

Disallow assigning a value with type `any` to variables and properties Error

Unsafe assignment of an any value.

Check failure

Code scanning / ESLint

Enforce naming conventions for everything across a codebase Error

Object Literal Property name msg\_type\_url must match one of the following formats: strictCamelCase

Check failure

Code scanning / ESLint

Enforce camelcase naming convention Error

Identifier 'msg_type_url' is not in camel case.
};
},
fromAmino: ({ granter, grantee, msg_type_url }) => MsgRevoke.fromPartial({

Check failure

Code scanning / ESLint

Enforce naming conventions for everything across a codebase Error

Parameter name msg\_type\_url must match one of the following formats: strictCamelCase

Check failure

Code scanning / ESLint

Enforce camelcase naming convention Error

Identifier 'msg_type_url' is not in camel case.
granter,

Check failure

Code scanning / ESLint

Disallow assigning a value with type `any` to variables and properties Error

Unsafe assignment of an any value.
grantee,

Check failure

Code scanning / ESLint

Disallow assigning a value with type `any` to variables and properties Error

Unsafe assignment of an any value.
msg_type_url: msgTypeUrl,
msgTypeUrl: msg_type_url,

Check failure

Code scanning / ESLint

Disallow assigning a value with type `any` to variables and properties Error

Unsafe assignment of an any value.

Check failure

Code scanning / ESLint

Enforce camelcase naming convention Error

Identifier 'msg_type_url' is not in camel case.
}),
fromAmino: ({ granter, grantee, msg_type_url }) =>
MsgRevoke.fromPartial({
granter,
grantee,
msgTypeUrl: msg_type_url,
}),
},
};
}
};
2 changes: 1 addition & 1 deletion packages/arch3-core/src/signingarchwayclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
logs,
StdFee,
} from '@cosmjs/stargate';
import { CometClient, HttpBatchClient, HttpBatchClientOptions, RpcClient, connectComet } from '@cosmjs/tendermint-rpc';
import { CometClient, HttpBatchClient, HttpBatchClientOptions, connectComet } from '@cosmjs/tendermint-rpc';

Check failure

Code scanning / ESLint

Disallow unused variables Error

'HttpBatchClient' is defined but never used. Allowed unused vars must match /^_/u.
import { assertDefined } from '@cosmjs/utils';
import { SimulateResponse } from 'cosmjs-types/cosmos/tx/v1beta1/service';
import _ from 'lodash';
Expand Down

0 comments on commit 9ae2455

Please sign in to comment.