Skip to content

Commit

Permalink
Refactor/fees (#21)
Browse files Browse the repository at this point in the history
* started implementing dynamic gas pricing

* finished refactoring sdk

* refactored sdk config

* added new sdk config structure to core and kysor

* refactored fee metrics

* fixed sdk options

* implemented fees in cosmostation

* fixed linting errors
  • Loading branch information
troykessler authored Feb 9, 2023
1 parent b34f246 commit 67c5d46
Show file tree
Hide file tree
Showing 30 changed files with 441 additions and 494 deletions.
9 changes: 8 additions & 1 deletion common/protocol/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import {
} from "./methods";
import { ICacheProvider, IMetrics, IRuntime } from "./types";
import { standardizeJSON } from "./utils";
import { SupportedChains } from "@kyvejs/sdk/dist/constants";

/**
* Main class of KYVE protocol nodes representing a validator node.
Expand Down Expand Up @@ -85,9 +86,10 @@ export class Validator {
protected staker!: string;
protected valaccount!: string;
protected storagePriv!: string;
protected chainId!: string;
protected chainId!: SupportedChains;
protected rpc!: string[];
protected rest!: string[];
protected gasPrice: number | undefined;
protected cache!: string;
protected debug!: boolean;
protected metrics!: boolean;
Expand Down Expand Up @@ -218,6 +220,10 @@ export class Validator {
"Comma separated list of rest endpoints. If the first fails the next endpoint will be used as fallback.",
parseEndpoints
)
.option(
"--gas-price <number>",
"The gas price the node should use to calculate transaction fees"
)
.option(
"--cache <jsonfile|memory>",
"The cache this node should use",
Expand Down Expand Up @@ -267,6 +273,7 @@ export class Validator {
this.chainId = options.chainId;
this.rpc = options.rpc;
this.rest = options.rest;
this.gasPrice = options.gasPrice;
this.cache = options.cache;
this.debug = options.debug;
this.metrics = options.metrics;
Expand Down
26 changes: 13 additions & 13 deletions common/protocol/src/methods/setups/setupMetrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,33 +360,33 @@ export function setupMetrics(this: Validator): void {
help: "The current index of the first data item in the cache.",
});

// GAS METRICS
// FEE METRICS

this.logger.debug(`Initializing metrics: gas_claim_uploader_role`);
this.logger.debug(`Initializing metrics: fees_claim_uploader_role`);

this.m.gas_claim_uploader_role = new prom_client.Gauge({
name: "gas_claim_uploader_role",
this.m.fees_claim_uploader_role = new prom_client.Counter({
name: "fees_claim_uploader_role",
help: "The current gas costs of tx claim uploader role metrics",
});

this.logger.debug(`Initializing metrics: gas_vote_bundle_proposal`);
this.logger.debug(`Initializing metrics: fees_vote_bundle_proposal`);

this.m.gas_vote_bundle_proposal = new prom_client.Gauge({
name: "gas_vote_bundle_proposal",
this.m.fees_vote_bundle_proposal = new prom_client.Counter({
name: "fees_vote_bundle_proposal",
help: "The current gas costs of tx vote bundle proposal metrics",
});

this.logger.debug(`Initializing metrics: gas_submit_bundle_proposal`);
this.logger.debug(`Initializing metrics: fees_submit_bundle_proposal`);

this.m.gas_submit_bundle_proposal = new prom_client.Gauge({
name: "gas_submit_bundle_proposal",
this.m.fees_submit_bundle_proposal = new prom_client.Counter({
name: "fees_submit_bundle_proposal",
help: "The current gas costs of tx submit bundle proposal metrics",
});

this.logger.debug(`Initializing metrics: gas_skip_uploader_role`);
this.logger.debug(`Initializing metrics: fees_skip_uploader_role`);

this.m.gas_skip_uploader_role = new prom_client.Gauge({
name: "gas_skip_uploader_role",
this.m.fees_skip_uploader_role = new prom_client.Counter({
name: "fees_skip_uploader_role",
help: "The current gas costs of tx skip uploader role metrics",
});

Expand Down
5 changes: 2 additions & 3 deletions common/protocol/src/methods/setups/setupSDK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ export async function setupSDK(this: Validator): Promise<void> {

this.sdk = this.rpc.map(
(_, i) =>
new KyveSDK({
new KyveSDK(this.chainId, {
rpc: this.rpc[i],
rest: this.rest[i],
chainId: this.chainId,
chainName: `KYVE - ${this.chainId}`,
gasPrice: this.gasPrice,
})
);

Expand Down
2 changes: 1 addition & 1 deletion common/protocol/src/methods/txs/claimUploaderRole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export async function claimUploaderRole(this: Validator): Promise<boolean> {
if (receipt.code === 0) {
this.logger.info(`Successfully claimed uploader role`);
this.m.tx_claim_uploader_role_successful.inc();
this.m.gas_claim_uploader_role.set(receipt?.gasUsed ?? 0);
this.m.fees_claim_uploader_role.inc(parseInt(tx.fee.amount[0].amount));

return true;
} else {
Expand Down
2 changes: 1 addition & 1 deletion common/protocol/src/methods/txs/skipUploaderRole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export async function skipUploaderRole(
if (receipt.code === 0) {
this.logger.info(`Successfully skipped uploader role`);
this.m.tx_skip_uploader_role_successful.inc();
this.m.gas_skip_uploader_role.set(receipt?.gasUsed ?? 0);
this.m.fees_skip_uploader_role.inc(parseInt(tx.fee.amount[0].amount));

return true;
} else {
Expand Down
4 changes: 3 additions & 1 deletion common/protocol/src/methods/txs/submitBundleProposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ export async function submitBundleProposal(

if (receipt.code === 0) {
this.m.tx_submit_bundle_proposal_successful.inc();
this.m.gas_submit_bundle_proposal.set(receipt?.gasUsed ?? 0);
this.m.fees_submit_bundle_proposal.inc(
parseInt(tx.fee.amount[0].amount)
);

this.m.bundles_proposed.inc();
this.m.bundles_amount.inc();
Expand Down
2 changes: 1 addition & 1 deletion common/protocol/src/methods/txs/voteBundleProposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export async function voteBundleProposal(
this.logger.info(`Voted ${voteMessage} on bundle "${storageId}"`);

this.m.tx_vote_bundle_proposal_successful.inc();
this.m.gas_vote_bundle_proposal.set(receipt?.gasUsed ?? 0);
this.m.fees_vote_bundle_proposal.inc(parseInt(tx.fee.amount[0].amount));

if (vote === 1) {
this.m.bundles_voted_valid.inc();
Expand Down
10 changes: 5 additions & 5 deletions common/protocol/src/types/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ export interface IMetrics {
cache_index_tail: PromGauge;
cache_index_head: PromGauge;

// GAS METRICS
// FEE METRICS

gas_claim_uploader_role: PromGauge;
gas_vote_bundle_proposal: PromGauge;
gas_submit_bundle_proposal: PromGauge;
gas_skip_uploader_role: PromGauge;
fees_claim_uploader_role: PromCounter;
fees_vote_bundle_proposal: PromCounter;
fees_submit_bundle_proposal: PromCounter;
fees_skip_uploader_role: PromCounter;
}
17 changes: 11 additions & 6 deletions common/sdk/src/clients/full-client.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,48 @@
import { OfflineAminoSigner } from "@cosmjs/amino/build/signer";
import { OfflineSigner, Registry } from "@cosmjs/proto-signing";
import { GasPrice, SigningStargateClient } from "@cosmjs/stargate";
import { SDKConfig } from "../constants";

import { IConfig } from "../constants";
import * as KyveRegistryTx from "../registry/tx.registry";
import KyveClient from "./rpc-client/client";
import KyveWebClient from "./rpc-client/web.client";

export async function getSigningKyveClient(
config: SDKConfig,
config: IConfig,
signer: OfflineSigner,
aminoSigner: OfflineAminoSigner | null,
walletName?: undefined,
defaultTypes?: undefined
): Promise<KyveClient>;

export async function getSigningKyveClient(
config: SDKConfig,
config: IConfig,
signer: OfflineSigner,
aminoSigner: OfflineAminoSigner | null,
walletName?: string,
defaultTypes?: undefined
): Promise<KyveWebClient>;

export async function getSigningKyveClient(
config: SDKConfig,
config: IConfig,
signer: OfflineSigner,
aminoSigner: OfflineAminoSigner | null,
walletName?: string
): Promise<KyveWebClient | KyveClient> {
const registry = new Registry([...KyveRegistryTx.registry]);
const gasPrice = GasPrice.fromString("0tkyve");
const gasPrice = GasPrice.fromString(`${config.gasPrice}${config.coinDenom}`);

const client: SigningStargateClient =
await SigningStargateClient.connectWithSigner(config.rpc, signer, {
registry,
gasPrice,
});

const [account] = await signer.getAccounts();
if (typeof walletName === "string")

if (typeof walletName === "string") {
return new KyveWebClient(client, account, config, aminoSigner, walletName);
}

return new KyveClient(client, account, config, aminoSigner);
}
26 changes: 10 additions & 16 deletions common/sdk/src/clients/rpc-client/client.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { StdSignature } from "@cosmjs/amino";
import { StdFee } from "@cosmjs/amino/build/signdoc";
import { AccountData, OfflineAminoSigner } from "@cosmjs/amino/build/signer";
import { SigningStargateClient } from "@cosmjs/stargate";
import { makeADR36AminoSignDoc } from "@keplr-wallet/cosmos";
import { SDKConfig } from "../../constants";

import { signTx, TxPromise } from "../../utils/helper";
import { IConfig } from "../../constants";
import KyveBaseMethods from "./kyve/base/v1beta1/base";
import KyveBundlesMethods from "./kyve/bundles/v1beta1/bundles";
import KyveDelegationMethods from "./kyve/delegation/v1beta1/delegation";
Expand All @@ -16,7 +14,7 @@ import KyveStakersMethods from "./kyve/stakers/v1beta1/stakers";
export default class KyveClient {
public nativeClient: SigningStargateClient;
public readonly account: AccountData;
public readonly config: SDKConfig;
public readonly config: IConfig;
public kyve: {
base: {
v1beta1: KyveBaseMethods;
Expand All @@ -42,7 +40,7 @@ export default class KyveClient {
constructor(
client: SigningStargateClient,
account: AccountData,
config: SDKConfig,
config: IConfig,
aminoSigner: OfflineAminoSigner | null
) {
this.account = account;
Expand Down Expand Up @@ -82,6 +80,7 @@ export default class KyveClient {
},
};
}

async signString(message: string): Promise<StdSignature> {
if (this.aminoSigner === null)
throw new Error("Wallet doesn't support adr-036");
Expand All @@ -92,17 +91,12 @@ export default class KyveClient {
);
return signature;
}
async txsAll(
txs: TxPromise[],
options?: {
fee?: StdFee | "auto" | number;
memo?: string;
}
) {
const txMessages = txs.map((tx) => tx.tx).flat();
return new TxPromise(
this.nativeClient,
await signTx(this.nativeClient, this.account.address, txMessages, options)

async getKyveBalance(): Promise<string> {
const data = await this.nativeClient.getBalance(
this.account.address,
this.config.coinDenom
);
return data.amount;
}
}
57 changes: 23 additions & 34 deletions common/sdk/src/clients/rpc-client/kyve/base/v1beta1/base.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,10 @@
import { StdFee } from "@cosmjs/amino/build/signdoc";
import { AccountData } from "@cosmjs/amino/build/signer";
import { coins, SigningStargateClient } from "@cosmjs/stargate";
import { coins } from "@cosmjs/stargate";
import BigNumber from "bignumber.js";
import { SDKConfig } from "../../../../../constants";

import { signTx, TxPromise } from "../../../../../utils/helper";

export default class KyveBaseMsg {
private nativeClient: SigningStargateClient;
public readonly account: AccountData;
public readonly config: SDKConfig;

constructor(
client: SigningStargateClient,
account: AccountData,
config: SDKConfig
) {
this.account = account;
this.nativeClient = client;
this.config = config;
}
import { KyveSigning, PendingSignedTx } from "../../../signing";

export default class KyveBaseMsg extends KyveSigning {
async transfer(
recipient: string,
amount: string,
Expand All @@ -38,13 +22,17 @@ export default class KyveBaseMsg {
},
};

return new TxPromise(
this.nativeClient,
await signTx(this.nativeClient, this.account.address, tx, options)
);
return await this.getPendingSignedTx(tx, options);
}

async multiTransfer(recipient: string[], amount: string) {
async multiTransfer(
recipient: string[],
amount: string,
options?: {
fee?: StdFee | "auto" | number;
memo?: string;
}
) {
const allAmount = new BigNumber(amount).multipliedBy(recipient.length);
const tx = {
typeUrl: "/cosmos.bank.v1beta1.MsgMultiSend",
Expand All @@ -62,17 +50,18 @@ export default class KyveBaseMsg {
},
};

return new TxPromise(
this.nativeClient,
await signTx(this.nativeClient, this.account.address, tx)
);
return await this.getPendingSignedTx(tx, options);
}

async getKyveBalance() {
const data = await this.nativeClient.getBalance(
this.account.address,
this.config.coinDenom
);
return data.amount;
async txsAll(
txs: PendingSignedTx[],
options?: {
fee?: StdFee | "auto" | number;
memo?: string;
}
) {
const txMessages = txs.map((tx) => tx.tx).flat();

return await this.getPendingSignedTx(txMessages, options);
}
}
Loading

0 comments on commit 67c5d46

Please sign in to comment.