Skip to content

Commit

Permalink
Merge pull request #9 from nabla-studio/DavideSegullo/reduce-size
Browse files Browse the repository at this point in the history
Davide segullo/reduce size
  • Loading branch information
DavideSegullo authored Nov 6, 2023
2 parents 8a97a12 + 1a6b3ed commit bb9b5fd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
9 changes: 6 additions & 3 deletions packages/core/src/utils/fees.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { EncodeObject } from '@cosmjs/proto-signing';
import { calculateFee, GasPrice } from '@cosmjs/stargate';
import { assertIsDefined } from './asserts';
import type { GasPrice } from '@cosmjs/stargate';
import type { SigningSimulatorClient } from '../types';
import type { Chain } from '@nabla-studio/chain-registry';

Expand All @@ -11,7 +11,8 @@ import type { Chain } from '@nabla-studio/chain-registry';
* @param feeDenom ex. uosmo
* @returns
*/
export const getGasPrice = (chain: Chain, feeDenom?: string) => {
export const getGasPrice = async (chain: Chain, feeDenom?: string) => {
const GasPrice = (await import('@cosmjs/stargate')).GasPrice;
let gasPrice: GasPrice | undefined = undefined;

if (chain.fees && chain.fees.fee_tokens.length > 0) {
Expand All @@ -31,7 +32,7 @@ export const getGasPrice = (chain: Chain, feeDenom?: string) => {
if (averageGasPrice && denom && !denom.startsWith('ibc/')) {
gasPrice = GasPrice.fromString(`${averageGasPrice}${denom}`);
} else {
gasPrice = GasPrice.fromString(`1${denom}`);
GasPrice.fromString(`1${denom}`);
}
}

Expand All @@ -53,5 +54,7 @@ export const estimateFee = async (

const gasEstimation = await client.simulate(sender, messages, memo);

const calculateFee = (await import('@cosmjs/stargate')).calculateFee;

return calculateFee(Math.round(gasEstimation * multiplier), gasPrice);
};
25 changes: 14 additions & 11 deletions packages/store/src/cosmjs/sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,8 @@ import {
getEndpoint,
getGasPrice,
} from '@quirks/core';
import {
SigningStargateClient,
SigningStargateClientOptions,
StargateClient,
} from '@cosmjs/stargate';
import {
SigningCosmWasmClient,
SigningCosmWasmClientOptions,
} from '@cosmjs/cosmwasm-stargate';
import type { SigningStargateClientOptions } from '@cosmjs/stargate';
import type { SigningCosmWasmClientOptions } from '@cosmjs/cosmwasm-stargate';
import { TxRaw } from 'cosmjs-types/cosmos/tx/v1beta1/tx';

/**
Expand Down Expand Up @@ -84,6 +77,8 @@ export const broadcast = async (
clientOptions = stargate(chain);
}

const StargateClient = (await import('@cosmjs/stargate')).StargateClient;

const client = await StargateClient.connect(
endpoint.rpc.address,
clientOptions,
Expand Down Expand Up @@ -120,6 +115,8 @@ export const broadcastSync = async (chainName: string, txRaw: TxRaw) => {
clientOptions = stargate(chain);
}

const StargateClient = (await import('@cosmjs/stargate')).StargateClient;

const client = await StargateClient.connect(
endpoint.rpc.address,
clientOptions,
Expand Down Expand Up @@ -168,6 +165,9 @@ export const sign = async (
clientOptions = signingStargate(chain);
}

const SigningStargateClient = (await import('@cosmjs/stargate'))
.SigningStargateClient;

const client = await SigningStargateClient.connectWithSigner(
endpoint.rpc.address,
offlineSigner,
Expand All @@ -177,7 +177,7 @@ export const sign = async (
if (fee === 'auto') {
const gasPrice = clientOptions?.gasPrice
? clientOptions.gasPrice
: getGasPrice(chain);
: await getGasPrice(chain);

fee = await estimateFee(client, sender, messages, gasPrice, memo);
}
Expand Down Expand Up @@ -225,6 +225,9 @@ export const signCW = async (
clientOptions = signingCosmwasm(chain);
}

const SigningCosmWasmClient = (await import('@cosmjs/cosmwasm-stargate'))
.SigningCosmWasmClient;

const client = await SigningCosmWasmClient.connectWithSigner(
endpoint.rpc.address,
offlineSigner,
Expand All @@ -234,7 +237,7 @@ export const signCW = async (
if (fee === 'auto') {
const gasPrice = clientOptions?.gasPrice
? clientOptions.gasPrice
: getGasPrice(chain);
: await getGasPrice(chain);

fee = await estimateFee(client, sender, messages, gasPrice, memo);
}
Expand Down

0 comments on commit bb9b5fd

Please sign in to comment.