Skip to content

Commit

Permalink
Merge pull request #32 from cosmology-tech/preferred-sign-mode
Browse files Browse the repository at this point in the history
add preferred signing mode and test
  • Loading branch information
Zetazzz authored Aug 24, 2024
2 parents e3d5624 + 942feb4 commit 5b54a2c
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 15 deletions.
16 changes: 16 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@
"cwd": "${workspaceFolder}/libs/interchainjs",
"internalConsoleOptions": "neverOpen"
},
{
"type": "node",
"request": "launch",
"name": "Debug Jest Tests in Interchainjs staking",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": [
"--config",
"./jest.starship.config.js",
"--verbose",
"--bail",
"starship/__tests__/staking.test.ts"
],
"console": "integratedTerminal",
"cwd": "${workspaceFolder}/libs/interchainjs",
"internalConsoleOptions": "neverOpen"
},
{
"type": "node",
"request": "launch",
Expand Down
14 changes: 4 additions & 10 deletions libs/interchainjs/src/signing-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import { toEncoder } from '@interchainjs/cosmos/utils';
import { TxBody, TxRaw } from '@interchainjs/cosmos-types/cosmos/tx/v1beta1/tx';
import { TxRpc } from '@interchainjs/cosmos-types/types';
import { BroadcastOptions, HttpEndpoint, IKey, StdFee } from '@interchainjs/types';
import { BroadcastOptions, HttpEndpoint, StdFee } from '@interchainjs/types';
import { fromBase64 } from '@interchainjs/utils';

import {
Expand Down Expand Up @@ -87,7 +87,6 @@ export class SigningClient {
}

async connect() {
let firstPubkey: IKey;
if (isOfflineAminoSigner(this.offlineSigner)) {
const aminoSigners = await AminoSigner.fromWalletToSigners(
this.offlineSigner,
Expand All @@ -100,9 +99,6 @@ export class SigningClient {
);

for (const signer of aminoSigners) {
if (!firstPubkey) {
firstPubkey = await signer.publicKey;
}
this.aminoSigners[await signer.getAddress()] = signer;
}
}
Expand All @@ -118,9 +114,6 @@ export class SigningClient {
);

for (const signer of directSigners) {
if (!firstPubkey) {
firstPubkey = await signer.publicKey;
}
this.directSigners[await signer.getAddress()] = signer;
}
}
Expand All @@ -143,8 +136,9 @@ export class SigningClient {
}

getSinger(signerAddress: string) {
const signer =
this.aminoSigners[signerAddress] || this.directSigners[signerAddress];
const signer = this.options.preferredSigningMethod ?
this.options.preferredSigningMethod === 'amino' ? this.aminoSigners[signerAddress] : this.directSigners[signerAddress]
: this.aminoSigners[signerAddress] || this.directSigners[signerAddress];

if (!signer) {
throw new Error(`No signer found for address ${signerAddress}`);
Expand Down
1 change: 1 addition & 0 deletions libs/interchainjs/src/types/signing-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface SignerOptions {
gasPrice?: Price | string;
prefix?: string;
broadcast?: BroadcastOptions;
preferredSigningMethod?: 'amino' | 'direct';
}

export interface SignerData {
Expand Down
108 changes: 103 additions & 5 deletions libs/interchainjs/starship/__tests__/staking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { useChain } from 'starshipjs';
const cosmosHdPath = "m/44'/118'/0'/0/0";

describe('Staking tokens testing', () => {
let protoSigner: OfflineDirectSigner, denom: string, address: string;
let wallet: Secp256k1HDWallet, protoSigner: OfflineDirectSigner, denom: string, address: string;
let commonPrefix: string,
chainInfo: ChainInfo,
getCoin: () => Promise<Asset>,
Expand All @@ -30,6 +30,7 @@ describe('Staking tokens testing', () => {
let queryClient: RpcQuery;
let validatorAddress: string;
let delegationAmount: string;
let totalDelegationAmount: bigint;

beforeAll(async () => {
({ chainInfo, getCoin, getRpcEndpoint, creditFromFaucet } =
Expand All @@ -40,7 +41,7 @@ describe('Staking tokens testing', () => {

const mnemonic = generateMnemonic();
// Initialize wallet
const wallet = Secp256k1HDWallet.fromMnemonic(mnemonic, [
wallet = Secp256k1HDWallet.fromMnemonic(mnemonic, [
{
prefix: commonPrefix,
hdPath: cosmosHdPath,
Expand Down Expand Up @@ -82,7 +83,7 @@ describe('Staking tokens testing', () => {
validatorAddress = allValidators[0].operatorAddress;
});

it('stake tokens to genesis validator', async () => {
it('stake tokens to genesis validator default signing mode', async () => {
const signingClient = await StargateSigningClient.connectWithSigner(
await getRpcEndpoint(),
protoSigner,
Expand All @@ -101,7 +102,104 @@ describe('Staking tokens testing', () => {

// Stake half of the tokens
// eslint-disable-next-line no-undef
delegationAmount = (BigInt(balance!.amount) / BigInt(2)).toString();
delegationAmount = (BigInt(balance!.amount) / BigInt(10)).toString();
totalDelegationAmount = BigInt(delegationAmount);
const msg = {
typeUrl: MsgDelegate.typeUrl,
value: MsgDelegate.fromPartial({
delegatorAddress: address,
validatorAddress: validatorAddress,
amount: {
amount: delegationAmount,
denom: balance!.denom,
},
}),
};

const fee = {
amount: [
{
denom,
amount: '100000',
},
],
gas: '550000',
};

const result = await signingClient.signAndBroadcast(address, [msg], fee);
assertIsDeliverTxSuccess(result);
});

it('stake tokens to genesis validator direct signing mode', async () => {
const signingClient = await StargateSigningClient.connectWithSigner(
await getRpcEndpoint(),
wallet,
{
preferredSigningMethod: 'direct',
broadcast: {
checkTx: true,
deliverTx: true,
},
}
);

const { balance } = await queryClient.balance({
address,
denom,
});

// Stake half of the tokens
// eslint-disable-next-line no-undef
delegationAmount = (BigInt(balance!.amount) / BigInt(10)).toString();
totalDelegationAmount = totalDelegationAmount + BigInt(delegationAmount);
const msg = {
typeUrl: MsgDelegate.typeUrl,
value: MsgDelegate.fromPartial({
delegatorAddress: address,
validatorAddress: validatorAddress,
amount: {
amount: delegationAmount,
denom: balance!.denom,
},
}),
};

const fee = {
amount: [
{
denom,
amount: '100000',
},
],
gas: '550000',
};

const result = await signingClient.signAndBroadcast(address, [msg], fee);
assertIsDeliverTxSuccess(result);
});

it('stake tokens to genesis validator amino signing mode', async () => {
const signingClient = await StargateSigningClient.connectWithSigner(
await getRpcEndpoint(),
wallet,
{
preferredSigningMethod: 'amino',
broadcast: {
checkTx: true,
deliverTx: true,
},
}
);

const { balance } = await queryClient.balance({
address,
denom,
});

// Stake half of the tokens
// eslint-disable-next-line no-undef
delegationAmount = (BigInt(balance!.amount) / BigInt(10)).toString();
totalDelegationAmount = totalDelegationAmount + BigInt(delegationAmount);
const msg = {
typeUrl: MsgDelegate.typeUrl,
value: MsgDelegate.fromPartial({
Expand Down Expand Up @@ -140,7 +238,7 @@ describe('Staking tokens testing', () => {
// eslint-disable-next-line no-undef
BigInt(0)
);
expect(delegationResponse!.balance.amount).toEqual(delegationAmount);
expect(delegationResponse!.balance.amount).toEqual(totalDelegationAmount.toString());
expect(delegationResponse!.balance.denom).toEqual(denom);
});
});

0 comments on commit 5b54a2c

Please sign in to comment.