Skip to content

Commit

Permalink
calculating fee
Browse files Browse the repository at this point in the history
  • Loading branch information
Vishwas1 committed Oct 7, 2024
1 parent 680a696 commit 0141b52
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
13 changes: 8 additions & 5 deletions src/components/deploy-onchain-kyc-popup/deploy-kyc.vue
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
import ConnectWalletButton from "../element/authButtons/ConnectWalletButton.vue";
import { mapGetters, mapMutations, mapActions, mapState } from "vuex";
import { getCosmosBlockchainLabel, getCosmosChainConfig, getCosmosCoinLogo } from '@hypersign-protocol/hypersign-kyc-chains-metadata/cosmos/wallet/cosmos-wallet-utils'
import { createNonSigningClient } from '../../utils/cosmos-client'
import { createNonSigningClient, calculateFee } from '../../utils/cosmos-client'
import { getSupportedChains } from '@hypersign-protocol/hypersign-kyc-chains-metadata/blockchain'
import { smartContractExecuteRPC } from '@hypersign-protocol/hypersign-kyc-chains-metadata/cosmos/contract/execute'
import { smartContractQueryRPC } from '@hypersign-protocol/hypersign-kyc-chains-metadata/cosmos/contract/query'
Expand Down Expand Up @@ -182,6 +182,7 @@ export default {
this.reset();
const { interchain } = this.allSupportedChains
console.log(interchain)
const interchainOptions = []
interchain.forEach(chain => {
const chainLabel = getCosmosBlockchainLabel(chain)
Expand Down Expand Up @@ -414,17 +415,19 @@ export default {
...proof
});
console.log(smartContractMsg)
const chainConfig = this.chainConfig
const chainCoinDenom = chainConfig["feeCurrencies"][0]["coinMinimalDenom"]
const gasPriceAvg = chainConfig["gasPriceStep"]["average"]
const fee = calculateFee(500_000, (gasPriceAvg + chainCoinDenom).toString())
const result = await smartContractExecuteRPC(
this.getCosmosConnection.signingClient,
chainCoinDenom,
this.getBlockchainUser.walletAddress,
HYPERSIGN_KYC_FACTORY_CONTRACT_ADDRESS,
smartContractMsg);
smartContractMsg,
fee
);
if (result) {
console.log(result)
Expand Down
7 changes: 5 additions & 2 deletions src/components/deploy-onchain-kyc-popup/deploy-sbt.vue
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ import { smartContractExecuteRPC } from '@hypersign-protocol/hypersign-kyc-chain
import { smartContractQueryRPC } from '@hypersign-protocol/hypersign-kyc-chains-metadata/cosmos/contract/query'
import { constructInitSbtMsg, constructGetRegistredSBTContractAddressMsg } from '@hypersign-protocol/hypersign-kyc-chains-metadata/cosmos/contract/msg';
import { getCosmosChainConfig } from '@hypersign-protocol/hypersign-kyc-chains-metadata/cosmos/wallet/cosmos-wallet-utils'
import { createNonSigningClient } from '../../utils/cosmos-client'
import { createNonSigningClient, calculateFee } from '../../utils/cosmos-client'
import UtilsMixin from '../../mixins/utils'
import ConnectWalletButton from "../element/authButtons/ConnectWalletButton.vue";
Expand Down Expand Up @@ -193,12 +193,15 @@ export default {
this.chainConfig = getCosmosChainConfig(this.selectedChainId)
const chainCoinDenom = this.chainConfig["feeCurrencies"][0]["coinMinimalDenom"]
const gasPriceAvg = this.chainConfig["gasPriceStep"]["average"]
const fee = calculateFee(500_000, (gasPriceAvg + chainCoinDenom).toString())
const result = await smartContractExecuteRPC(
this.getCosmosConnection.signingClient,
chainCoinDenom,
this.getBlockchainUser.walletAddress,
this.onChainIssuer.issuer.kyc_contract_address,
smartContractMsg);
smartContractMsg, fee);
if (result) {
console.log(result)
Expand Down
19 changes: 19 additions & 0 deletions src/utils/cosmos-client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { SigningCosmWasmClient, CosmWasmClient } from '@cosmjs/cosmwasm-stargate'
import { Uint53 } from "@cosmjs/math";
import {
GasPrice, coins
} from "@cosmjs/stargate";
export async function createClient(rpcUrl, offlineSigner) {
const client = SigningCosmWasmClient.connectWithSigner(
rpcUrl,
Expand All @@ -12,3 +16,18 @@ export async function createNonSigningClient(rpcUrl) {
return client
}


export function calculateFee(gasLimit, gasPrice) {
const processedGasPrice = typeof gasPrice === "string" ? GasPrice.fromString(gasPrice) : gasPrice;
const { denom, amount: gasPriceAmount } = processedGasPrice;
// Note: Amount can exceed the safe integer range (https://github.com/cosmos/cosmjs/issues/1134),
// which we handle by converting from Decimal to string without going through number.
const t = gasPriceAmount.multiply(new Uint53(gasLimit))
const amount = t.toString();
return {
amount: coins(amount, denom),
gas: gasLimit.toString(),
};
}


0 comments on commit 0141b52

Please sign in to comment.