Skip to content

Commit 0141b52

Browse files
committed
calculating fee
1 parent 680a696 commit 0141b52

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

src/components/deploy-onchain-kyc-popup/deploy-kyc.vue

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
import ConnectWalletButton from "../element/authButtons/ConnectWalletButton.vue";
122122
import { mapGetters, mapMutations, mapActions, mapState } from "vuex";
123123
import { getCosmosBlockchainLabel, getCosmosChainConfig, getCosmosCoinLogo } from '@hypersign-protocol/hypersign-kyc-chains-metadata/cosmos/wallet/cosmos-wallet-utils'
124-
import { createNonSigningClient } from '../../utils/cosmos-client'
124+
import { createNonSigningClient, calculateFee } from '../../utils/cosmos-client'
125125
import { getSupportedChains } from '@hypersign-protocol/hypersign-kyc-chains-metadata/blockchain'
126126
import { smartContractExecuteRPC } from '@hypersign-protocol/hypersign-kyc-chains-metadata/cosmos/contract/execute'
127127
import { smartContractQueryRPC } from '@hypersign-protocol/hypersign-kyc-chains-metadata/cosmos/contract/query'
@@ -182,6 +182,7 @@ export default {
182182
this.reset();
183183
184184
const { interchain } = this.allSupportedChains
185+
console.log(interchain)
185186
const interchainOptions = []
186187
interchain.forEach(chain => {
187188
const chainLabel = getCosmosBlockchainLabel(chain)
@@ -414,17 +415,19 @@ export default {
414415
...proof
415416
});
416417
417-
console.log(smartContractMsg)
418-
419-
420418
const chainConfig = this.chainConfig
421419
const chainCoinDenom = chainConfig["feeCurrencies"][0]["coinMinimalDenom"]
420+
const gasPriceAvg = chainConfig["gasPriceStep"]["average"]
421+
const fee = calculateFee(500_000, (gasPriceAvg + chainCoinDenom).toString())
422+
422423
const result = await smartContractExecuteRPC(
423424
this.getCosmosConnection.signingClient,
424425
chainCoinDenom,
425426
this.getBlockchainUser.walletAddress,
426427
HYPERSIGN_KYC_FACTORY_CONTRACT_ADDRESS,
427-
smartContractMsg);
428+
smartContractMsg,
429+
fee
430+
);
428431
429432
if (result) {
430433
console.log(result)

src/components/deploy-onchain-kyc-popup/deploy-sbt.vue

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ import { smartContractExecuteRPC } from '@hypersign-protocol/hypersign-kyc-chain
7070
import { smartContractQueryRPC } from '@hypersign-protocol/hypersign-kyc-chains-metadata/cosmos/contract/query'
7171
import { constructInitSbtMsg, constructGetRegistredSBTContractAddressMsg } from '@hypersign-protocol/hypersign-kyc-chains-metadata/cosmos/contract/msg';
7272
import { getCosmosChainConfig } from '@hypersign-protocol/hypersign-kyc-chains-metadata/cosmos/wallet/cosmos-wallet-utils'
73-
import { createNonSigningClient } from '../../utils/cosmos-client'
73+
import { createNonSigningClient, calculateFee } from '../../utils/cosmos-client'
7474
import UtilsMixin from '../../mixins/utils'
7575
import ConnectWalletButton from "../element/authButtons/ConnectWalletButton.vue";
7676
@@ -193,12 +193,15 @@ export default {
193193
194194
this.chainConfig = getCosmosChainConfig(this.selectedChainId)
195195
const chainCoinDenom = this.chainConfig["feeCurrencies"][0]["coinMinimalDenom"]
196+
const gasPriceAvg = this.chainConfig["gasPriceStep"]["average"]
197+
const fee = calculateFee(500_000, (gasPriceAvg + chainCoinDenom).toString())
198+
196199
const result = await smartContractExecuteRPC(
197200
this.getCosmosConnection.signingClient,
198201
chainCoinDenom,
199202
this.getBlockchainUser.walletAddress,
200203
this.onChainIssuer.issuer.kyc_contract_address,
201-
smartContractMsg);
204+
smartContractMsg, fee);
202205
203206
if (result) {
204207
console.log(result)

src/utils/cosmos-client.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import { SigningCosmWasmClient, CosmWasmClient } from '@cosmjs/cosmwasm-stargate'
2+
import { Uint53 } from "@cosmjs/math";
3+
import {
4+
GasPrice, coins
5+
} from "@cosmjs/stargate";
26
export async function createClient(rpcUrl, offlineSigner) {
37
const client = SigningCosmWasmClient.connectWithSigner(
48
rpcUrl,
@@ -12,3 +16,18 @@ export async function createNonSigningClient(rpcUrl) {
1216
return client
1317
}
1418

19+
20+
export function calculateFee(gasLimit, gasPrice) {
21+
const processedGasPrice = typeof gasPrice === "string" ? GasPrice.fromString(gasPrice) : gasPrice;
22+
const { denom, amount: gasPriceAmount } = processedGasPrice;
23+
// Note: Amount can exceed the safe integer range (https://github.com/cosmos/cosmjs/issues/1134),
24+
// which we handle by converting from Decimal to string without going through number.
25+
const t = gasPriceAmount.multiply(new Uint53(gasLimit))
26+
const amount = t.toString();
27+
return {
28+
amount: coins(amount, denom),
29+
gas: gasLimit.toString(),
30+
};
31+
}
32+
33+

0 commit comments

Comments
 (0)