@archwayhq/arch3-core
Version:
Core library to interact with Archway Network
253 lines (252 loc) • 15.3 kB
TypeScript
import { Coin, Pubkey } from '@cosmjs/amino';
import { HttpEndpoint, SigningCosmWasmClient, SigningCosmWasmClientOptions } from '@cosmjs/cosmwasm-stargate';
import { EncodeObject, OfflineSigner } from '@cosmjs/proto-signing';
import { DeliverTxResponse, Event, GasPrice, logs, StdFee } from '@cosmjs/stargate';
import { CometClient, HttpBatchClientOptions } from '@cosmjs/tendermint-rpc';
import { SimulateResponse } from 'cosmjs-types/cosmos/tx/v1beta1/service';
import { IArchwayQueryClient } from './queryclient';
import { BlockTracking, ContractMetadata, ContractPremium, EstimateTxFees, OutstandingRewards, RewardsPool, RewardsRecord } from './types';
export interface SigningArchwayClientOptions extends SigningCosmWasmClientOptions {
/**
* @deprecated
* The Archway protocol has built-in mechanisms for calculating the optimal gas price based on network
* usage and contract premiums. Specifying a gasPrice can cause unintended behaviour when calculating tx fees.
*/
readonly gasPrice?: GasPrice;
/**
* Default adjustment factor to be multiplied against the estimate returned by the tx simulation.
* If the gas limit is set manually in the transaction, this option is ignored.
*
* @default 1.5
*/
readonly gasAdjustment?: number;
}
/**
* A base interface for all transaction results.
*/
export interface TxResult {
/** Block height in which the transaction is included. */
readonly height: number;
/** Transaction hash (might be used as transaction ID). Guaranteed to be non-empty upper-case hex. */
readonly transactionHash: string;
/** Transaction logs. */
readonly logs: readonly logs.Log[];
/** Transaction events. */
readonly events: readonly Event[];
/** Amount of gas sent with the transaction. */
readonly gasWanted: bigint;
/** Amount of gas consumed by the transaction. */
readonly gasUsed: bigint;
}
/**
* The result of a {@link SigningArchwayClient.setContractMetadata} transaction.
*/
export interface SetContractMetadataResult extends TxResult {
/** Contract rewards distribution options for a particular contract. */
readonly metadata: ContractMetadata;
}
/**
* The result of a {@link SigningArchwayClient.setContractPremium} transaction.
*/
export interface SetContractPremiumResult extends TxResult {
/** Contract premium fee for a particular contract. */
readonly premium: ContractPremium;
}
/**
* The result of a {@link SigningArchwayClient.withdrawContractRewards} transaction.
*/
export interface WithdrawContractRewardsResult extends TxResult {
/** Address receiving the rewards. */
rewardsAddress: string;
/** Total rewards withdrawn. */
rewards: Coin[];
}
/**
* Extension to the {@link SigningCosmWasmClient} for transacting with Archway's modules.
*/
export declare class SigningArchwayClient extends SigningCosmWasmClient implements IArchwayQueryClient {
private readonly archwaySigner;
private readonly archwayQueryClient;
private readonly gasAdjustment;
protected constructor(cometClient: CometClient | undefined, signer: OfflineSigner, options: SigningArchwayClientOptions);
/**
* Creates an instance by connecting to the given Tendermint RPC endpoint.
*
* @param endpoint - String URL of the RPC endpoint to connect or an {@link HttpEndpoint} object.
* @param signer - The transaction signer configuration.
* @param options - Options for the signing client.
* @returns A {@link SigningArchwayClient} connected to the endpoint.
*/
static connectWithSigner(endpoint: string | HttpEndpoint, signer: OfflineSigner, options?: SigningArchwayClientOptions): Promise<SigningArchwayClient>;
/**
* Creates an instance by connecting to the given Tendermint RPC endpoint using an HttpBatchClient to batch
* multiple requests and reduce queries to the server.
*
* @param endpoint - String URL of the RPC endpoint to connect or an {@link HttpEndpoint} object.
* @param signer - The transaction signer configuration.
* @param options - Options for the signing client.
* @param batchClientOptions - Optional configuration to control how the HttpBatchClient will batch requests.
* @returns A {@link SigningArchwayClient} connected to the endpoint.
*
* @remarks This factory method doesn't support WebSocket endpoints.
*/
static connectWithSignerAndBatchClient(endpoint: string | HttpEndpoint, signer: OfflineSigner, options?: SigningArchwayClientOptions, batchClientOptions?: Partial<HttpBatchClientOptions>): Promise<SigningArchwayClient>;
/**
* Creates an instance from a manually created CometBFT client.
*
* @param cometClient - A CometBFT client for a given endpoint.
* @param signer - The transaction signer configuration.
* @param options - Options for the signing client.
* @returns A {@link SigningArchwayClient} connected to the endpoint.
*/
static createWithSigner(cometClient: CometClient, signer: OfflineSigner, options?: SigningArchwayClientOptions): Promise<SigningArchwayClient>;
/**
* Creates a client in offline mode.
*
* @remarks
* This should only be used in niche cases where you know exactly what you're doing,
* e.g. when building an offline signing application.
*
* When you try to use online functionality with such a signer, an
* exception will be raised.
*
* @param signer - the transaction signer configuration.
* @param options - options for signing and broadcasting transactions.
* @returns An offline {@link SigningArchwayClient}.
*/
static offline(signer: OfflineSigner, options?: SigningArchwayClientOptions): Promise<SigningArchwayClient>;
/**
* Updates the rewards metadata of a contract.
*
* @param senderAddress - Address of the message sender.
* @param metadata - The rewards metadata.
* @param fee - Fee to pay for the transaction. Use 'auto' or a number to calculate the fees automatically.
* When a number is set, it will be used as a gas adjustment multiplier for the estimated fees.
* @param memo - Optional memo to add to the transaction.
* @returns A {@link SetContractMetadataResult} with the contract's metadata.
* @throws Error if the transaction fails.
*
* @see {@link SigningArchwayClient.withdrawContractRewards} for details on how to withdraw rewards.
* @see Check the [Archway Bindings](https://github.com/archway-network/archway-bindings) repository
* for more information on how to withdraw rewards from a contract.
*/
setContractMetadata(senderAddress: string, metadata: ContractMetadata, fee: StdFee | 'auto' | number, memo?: string): Promise<SetContractMetadataResult>;
/**
* Updates the contract's premium fee. Only the owner of the contract metadata can update the fee.
*
* @param senderAddress - Address of the message sender.
* @param contractAddress - Contract address to set the premium fee.
* @param flatFee - The contract premium fee. To disable the fee, set its `amount` to `0`.
* @param fee - Fee to pay for the transaction. Use 'auto' or a number to calculate the fees automatically.
* When a number is set, it will be used as a gas adjustment multiplier for the estimated fees.
* @param memo - Optional memo to add to the transaction.
* @returns A {@link SetContractPremiumResult} with the contract's premium fee.
* @throws Error if the transaction fails.
*
* @see {@link SigningArchwayClient.withdrawContractRewards} for details on how to withdraw rewards.
* @see Check the [Archway Bindings](https://github.com/archway-network/archway-bindings) repository
* for more information on how to withdraw rewards from a contract.
*/
setContractPremium(senderAddress: string, contractAddress: string, flatFee: Coin, fee: StdFee | 'auto' | number, memo?: string): Promise<SetContractPremiumResult>;
/**
* Withdraws rewards for the `senderAddress` up to the given `limit` of records to process.
* If the limit is set to `0`, it will use the default limit from the protocol.
* The default limit is a parameter on the rewards module and it can be updated via governance.
*
* @remarks
* This method is useful when the contract has a large number of rewards to withdraw,
* so they can be processed in batches.
*
* @param senderAddress - Address of the message sender and rewards destination.
* @param limit - Maximum number of rewards to withdraw.
* @param fee - Fee to pay for the transaction. Use 'auto' or a number to calculate the fees automatically.
* When a number is set, it will be used as a gas adjustment multiplier for the estimated fees.
* @param memo - Optional memo to add to the transaction.
* @returns A {@link WithdrawContractRewardsResult} with information about the rewards withdrawn.
* @throws Error if the transaction fails.
*
* @see Check the [Archway Bindings](https://github.com/archway-network/archway-bindings) repository
* for more information on how to withdraw rewards from a contract.
*/
withdrawContractRewards(senderAddress: string, limit: number, fee: StdFee | 'auto' | number, memo?: string): Promise<WithdrawContractRewardsResult>;
/**
* Creates a transaction with the given messages, fee and memo. Then signs and broadcasts the transaction.
*
* When setting the fee to 'auto' or a number, the fee will be calculated automatically based on the messages,
* the minimum price of gas (mPoG) and the minimum consensus fee. If the messages include a contract execution
* or migration, the contract premium fee will be added to the transaction fee.
*
* @param signerAddress - The address that will sign transactions using this instance.
* The signer must be able to sign with this address.
* @param messages - The messages to include in the transaction. The messages types should be registered in the
* {@link SigningArchwayClient.registry} when the client is instantiated.
* @param fee - Fee to pay for the transaction. Use 'auto' or a number to calculate the fees automatically.
* When a number is set, it will be used as a gas adjustment multiplier for the estimated fees.
* @param memo - Optional memo to add to the transaction.
* @returns A {@link DeliverTxResponse} after successfully broadcasting the transaction.
*
* @see {@link SigningArchwayClient.calculateFee} for calculating the fees before broadcasting.
*/
signAndBroadcast(signerAddress: string, messages: readonly EncodeObject[], fee: number | StdFee | 'auto', memo?: string): Promise<DeliverTxResponse>;
/**
* Calculates tx fees by simulating the execution of a transaction with the given messages.
* The fee will be calculated based on the minimum price of gas (mPoG) and the minimum consensus
* fee of the network. If the messages include a contract execution or migration, the contract
* premium fee will be added to the calculation.
*
* @param signerAddress - Address used in the gas simulation that will sign transactions.
* The signer must be able to sign with this address.
* @param messages - The messages to include in the transaction for simulating the gas wanted.
* The messages types should be registered in the {@link SigningArchwayClient.registry}
* when the client is instantiated.
* @param memo - Optional memo to add to the transaction.
* @param gasAdjustment - Adjustment factor to be multiplied against the gas estimate.
* @param granter - The granter address that is used for paying with feegrants.
* @param payer - The fee payer address. The payer must have signed the transaction.
* @returns A {@link StdFee} with the estimated fee for the transaction.
*
* @see {@link SigningArchwayClient.simulate} for simulating the execution of a transaction.
* @see {@link SigningArchwayClient.getEstimateTxFees} for getting the minimum price of gas (mPoG) and the minimum
* consensus fee of the network.
*/
calculateFee(signerAddress: string, messages: readonly EncodeObject[], memo?: string, gasAdjustment?: number, granter?: string, payer?: string): Promise<StdFee>;
private includeFlatFees;
private assertSignAndBroadcast;
/**
* Withdraws staking rewards.
*
* @param delegatorAddress - Address of the delegator withdrawing the staking rewards.
* @param validatorAddress - Address of the validator in the format `archwayval` + hex encoded public key.
* @param fee - Fee to pay for the transaction. Use 'auto' to calculate the fee automatically.
* @param memo - Optional memo to add to the transaction.
* @returns A {@link DeliverTxResponse} with information about the the withdraw tx.
*/
withdrawRewards(delegatorAddress: string, validatorAddress: string, fee: number | StdFee | 'auto', memo?: string): Promise<DeliverTxResponse>;
/**
* Simulates the transaction and estimates the gas it uses.
* Unlike {@link SigningCosmWasmClient.simulate}, it uses `payer` and `granter`
* arguments during the simulation, to support Archway-specific `cw-fees` mechanics
* and include gas the `SudoMsg::CwGrant` message consumes into the estimation.
*
* @param signerAddress - Address used in the gas simulation that will sign transactions.
* The signer must be able to sign with this address.
* @param messages - The messages to include in the transaction for simulating the gas wanted.
* The messages types should be registered in the {@link SigningArchwayClient.registry}
* when the client is instantiated.
* @param memo - Optional memo to add to the transaction.
* @param granter - The granter address that is used for paying with feegrants.
* @param payer - The fee payer address. The payer must have signed the transaction.
* @returns A number of gas the tx used during the simulation.
*
* @see https://docs.archway.io/developers/guides/cw-fees/introduction
*/
simulate(signerAddress: string, messages: readonly EncodeObject[], memo?: string, granter?: string, payer?: string): Promise<number>;
getBlockRewardsTracking(): Promise<BlockTracking>;
getContractMetadata(contractAddress: string): Promise<ContractMetadata | undefined>;
getContractPremium(contractAddress: string): Promise<ContractPremium>;
getEstimateTxFees(gasLimit?: number, contractAddress?: string): Promise<EstimateTxFees>;
getOutstandingRewards(rewardsAddress: string): Promise<OutstandingRewards>;
getRewardsPool(): Promise<RewardsPool>;
getAllRewardsRecords(rewardsAddress: string): Promise<readonly RewardsRecord[]>;
simulateTx(messages: readonly EncodeObject[], memo: string | undefined, signer: Pubkey, sequence: number, granter?: string, payer?: string): Promise<SimulateResponse>;
}