Skip to content

Commit

Permalink
add staging cache to builder context
Browse files Browse the repository at this point in the history
  • Loading branch information
Zetazzz committed Nov 13, 2024
1 parent 9ebd73b commit f9a8614
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
8 changes: 5 additions & 3 deletions networks/cosmos/src/base/builder-context.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { AuthInfo } from '@interchainjs/cosmos-types/cosmos/tx/v1beta1/tx';
import { ITxBuilderContext } from '@interchainjs/types';
import { BaseTxBuilderContext, ITxBuilderContext } from '@interchainjs/types';

/**
* Context for the transaction builder.
*/
export class BaseCosmosTxBuilderContext<Signer>
extends BaseTxBuilderContext<Signer>
implements ITxBuilderContext<Signer>
{
constructor(public signer: Signer, public authInfo?: AuthInfo) {}
constructor(public signer: Signer) {
super(signer);
}
}
4 changes: 3 additions & 1 deletion networks/cosmos/src/base/tx-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { calculateFee, toFee } from '../utils';
import { CosmosBaseSigner } from './base-signer';
import { BaseCosmosTxBuilderContext } from './builder-context';

export const STAGING_AUTH_INFO = 'staging_auth_info';

/**
* BaseCosmosSigBuilder is a helper class to build the signature from the document
*/
Expand Down Expand Up @@ -113,7 +115,7 @@ export abstract class BaseCosmosTxBuilder<SignDoc>

const { authInfo, encode: authEncode} = await this.buildAuthInfo([signerInfo], toFee(stdFee));

this.ctx.authInfo = authInfo;
this.ctx.setStagingData(STAGING_AUTH_INFO, authInfo);

return {
bodyBytes: txBodyEncode(),
Expand Down
6 changes: 4 additions & 2 deletions networks/cosmos/src/builder/amino-tx-builder.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { SignMode } from '@interchainjs/cosmos-types/cosmos/tx/signing/v1beta1/signing';
import { AuthInfo } from '@interchainjs/cosmos-types/cosmos/tx/v1beta1/tx';
import { TxRaw } from '@interchainjs/cosmos-types/cosmos/tx/v1beta1/tx';
import { SignDocResponse } from '@interchainjs/types';

import { type AminoSignerBase } from '../signers/amino';
import { BaseCosmosSigBuilder, BaseCosmosTxBuilder } from '../base';
import { BaseCosmosSigBuilder, BaseCosmosTxBuilder, STAGING_AUTH_INFO } from '../base';
import { BaseCosmosTxBuilderContext } from '../base/builder-context';
import { CosmosAminoDoc, CosmosSignArgs } from '../types';
import { encodeStdSignDoc, toAminoMsgs, toFee } from '../utils';
Expand Down Expand Up @@ -61,8 +62,9 @@ export class AminoTxBuilder extends BaseCosmosTxBuilder<CosmosAminoDoc> {

async syncSignedDoc(txRaw: TxRaw, signResp: SignDocResponse<CosmosAminoDoc>): Promise<TxRaw> {
const authFee = toFee(signResp.signDoc.fee);
const authInfo = this.ctx.getStagingData<AuthInfo>(STAGING_AUTH_INFO);

const { encode: authEncode } = await this.buildAuthInfo(this.ctx.authInfo.signerInfos, authFee);
const { encode: authEncode } = await this.buildAuthInfo(authInfo.signerInfos, authFee);
const authInfoBytes = authEncode();

return {
Expand Down
28 changes: 28 additions & 0 deletions packages/types/src/tx-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,32 @@ export interface ITxBuilder<SignArgs = unknown, SignResp = unknown> {
*/
export interface ITxBuilderContext<Signer = unknown> {
signer?: Signer;

/**
* set staging data.
* @param data - staging data
*/
setStagingData(key: string, data: unknown): void;

/**
* get staging data.
*/
getStagingData<TStaging>(key: string): TStaging;
}

/**
* BaseTxBuilderContext is a base class for ITxBuilderContext.
*/
export class BaseTxBuilderContext<Signer> implements ITxBuilderContext<Signer> {
private stagingData: Record<string, unknown> = {};

constructor(public signer?: Signer) {}

setStagingData(key: string, data: unknown): void {
this.stagingData[key] = data;
}

getStagingData<TStaging>(key: string): TStaging {
return this.stagingData[key] as TStaging;
}
}

0 comments on commit f9a8614

Please sign in to comment.