From eb508735ae21da1c22f61fc7d232682545b946b3 Mon Sep 17 00:00:00 2001 From: Bojan Angjelkoski Date: Mon, 9 Sep 2024 20:00:25 +0300 Subject: [PATCH] chore: add tests for stargate --- package.json | 1 + .../grpc/ChainGrpcPermissionsApi.spec.ts | 2 +- .../stargate/SigninStargateClient.spec.ts | 93 +++++++++++++++++++ .../core/stargate/SigningStargateClient.ts | 12 +-- 4 files changed, 101 insertions(+), 7 deletions(-) create mode 100644 packages/sdk-ts/src/core/stargate/SigninStargateClient.spec.ts diff --git a/package.json b/package.json index 050cbef8e..412d65dd9 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "clean-up": "lerna run clean && shx rm -rf .build-cache *.log coverage junit.xml", "bootstrap": "yarn clean-up && lerna bootstrap", "test": "jest", + "test:sdk-ts:core:stargate": "jest ./packages/sdk-ts/src/core/stargate", "test:sdk-ts:client": "jest ./packages/sdk-ts/src/client", "test:sdk-ts:core": "jest ./packages/sdk-ts/src/core", "test:bridge-ts": "jest ./packages/bridge-ts/src", diff --git a/packages/sdk-ts/src/client/chain/grpc/ChainGrpcPermissionsApi.spec.ts b/packages/sdk-ts/src/client/chain/grpc/ChainGrpcPermissionsApi.spec.ts index ef68ff807..eebcce6d4 100644 --- a/packages/sdk-ts/src/client/chain/grpc/ChainGrpcPermissionsApi.spec.ts +++ b/packages/sdk-ts/src/client/chain/grpc/ChainGrpcPermissionsApi.spec.ts @@ -96,7 +96,7 @@ describe('ChainGrpcPermissionsApi', () => { try { const response = await chainGrpcPermissionsApi.fetchNamespaceByDenom({ denom: INJ_DENOM, - include_roles: true, + includeRoles: true, }) expect(response).toBeDefined() diff --git a/packages/sdk-ts/src/core/stargate/SigninStargateClient.spec.ts b/packages/sdk-ts/src/core/stargate/SigninStargateClient.spec.ts new file mode 100644 index 000000000..275a558a6 --- /dev/null +++ b/packages/sdk-ts/src/core/stargate/SigninStargateClient.spec.ts @@ -0,0 +1,93 @@ +import { coins } from '@cosmjs/amino' +import { PrivateKey } from '../accounts' +import { InjectiveDirectEthSecp256k1Wallet } from '../accounts/signers' +import { InjectiveOfflineSigner } from '../accounts/signers/types' +import { SigningStargateClient } from './SigningStargateClient' +import { getNetworkEndpoints, Network } from '@injectivelabs/networks' +import { OfflineSigner } from '@cosmjs/proto-signing' + +describe('SigningStargateClient', () => { + it('can sign and broadcast a transaction', async () => { + const privateKey = PrivateKey.fromHex( + process.env.TEST_PRIVATE_KEY as string, + ) + const network = Network.Devnet + const endpoints = getNetworkEndpoints(network) + const privateKeyHex = privateKey.toPrivateKeyHex() + const privateKeyHexWithoutPrefix = privateKeyHex.startsWith('0x') + ? privateKeyHex.slice(2) + : privateKeyHex + + const privateKeyBuffer = Buffer.from(privateKeyHexWithoutPrefix, 'hex') + + const wallet = (await InjectiveDirectEthSecp256k1Wallet.fromKey( + privateKeyBuffer, + )) as InjectiveOfflineSigner + + const client = await SigningStargateClient.connectWithSigner( + endpoints.rpc as string, + wallet as OfflineSigner, + ) + + const msgSend = { + typeUrl: '/cosmos.bank.v1beta1.MsgSend', + value: { + fromAddress: privateKey.toAddress().toBech32(), + toAddress: privateKey.toAddress().toBech32(), + amount: coins(1, 'inj'), + }, + } + + const fee = { + amount: [{ denom: 'inj', amount: '2000' }], + gas: '200000', + } + + const response = await client.signAndBroadcast( + privateKey.toBech32(), + [msgSend], + fee, + ) + + expect(response.code).toEqual(0) + expect(response.transactionHash).toBeDefined() + }) + + it('can send coins', async () => { + const privateKey = PrivateKey.fromHex( + process.env.TEST_PRIVATE_KEY as string, + ) + const network = Network.Devnet + const endpoints = getNetworkEndpoints(network) + const privateKeyHex = privateKey.toPrivateKeyHex() + const privateKeyHexWithoutPrefix = privateKeyHex.startsWith('0x') + ? privateKeyHex.slice(2) + : privateKeyHex + + const privateKeyBuffer = Buffer.from(privateKeyHexWithoutPrefix, 'hex') + + const wallet = (await InjectiveDirectEthSecp256k1Wallet.fromKey( + privateKeyBuffer, + )) as InjectiveOfflineSigner + + const client = await SigningStargateClient.connectWithSigner( + endpoints.rpc as string, + wallet as OfflineSigner, + ) + + const fee = { + amount: [{ denom: 'inj', amount: '2000' }], + gas: '200000', + } + + const response = await client.sendTokens( + privateKey.toBech32(), //sender + privateKey.toBech32(), // recipient, + coins(1, 'inj'), + fee, + ) + + expect(response.code).toEqual(0) + expect(response.transactionHash).toBeDefined() + }) +}) diff --git a/packages/sdk-ts/src/core/stargate/SigningStargateClient.ts b/packages/sdk-ts/src/core/stargate/SigningStargateClient.ts index 97d09a1c2..3d2404803 100644 --- a/packages/sdk-ts/src/core/stargate/SigningStargateClient.ts +++ b/packages/sdk-ts/src/core/stargate/SigningStargateClient.ts @@ -1,23 +1,23 @@ import { + StdFee, encodeSecp256k1Pubkey, makeSignDoc as makeSignDocAmino, - StdFee, } from '@cosmjs/amino' import { Int53, Uint53 } from '@cosmjs/math' import { - EncodeObject, + Registry, + makeSignDoc, encodePubkey, + EncodeObject, + OfflineSigner, isOfflineDirectSigner, makeAuthInfoBytes, - makeSignDoc, - OfflineSigner, - Registry, TxBodyEncodeObject, } from '@cosmjs/proto-signing' import { + CometClient, HttpEndpoint, Tendermint37Client, - CometClient, } from '@cosmjs/tendermint-rpc' import { assert, assertDefined } from '@cosmjs/utils' import { Coin } from 'cosmjs-types/cosmos/base/v1beta1/coin'