Skip to content

Commit

Permalink
chore: add tests for stargate
Browse files Browse the repository at this point in the history
  • Loading branch information
bangjelkoski committed Sep 9, 2024
1 parent e995ed8 commit eb50873
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 7 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe('ChainGrpcPermissionsApi', () => {
try {
const response = await chainGrpcPermissionsApi.fetchNamespaceByDenom({
denom: INJ_DENOM,
include_roles: true,
includeRoles: true,
})

expect(response).toBeDefined()
Expand Down
93 changes: 93 additions & 0 deletions packages/sdk-ts/src/core/stargate/SigninStargateClient.spec.ts
Original file line number Diff line number Diff line change
@@ -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()
})
})
12 changes: 6 additions & 6 deletions packages/sdk-ts/src/core/stargate/SigningStargateClient.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down

0 comments on commit eb50873

Please sign in to comment.