Skip to content

Commit

Permalink
chore: add option to use signAndBroadcast instead of sign
Browse files Browse the repository at this point in the history
  • Loading branch information
genaroibc committed Mar 29, 2024
1 parent ec00c62 commit 8662cf0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
37 changes: 32 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { toUtf8 } from "@cosmjs/encoding";
import {
AminoTypes,
Coin,
DeliverTxResponse,
GasPrice,
SigningStargateClient,
calculateFee,
Expand Down Expand Up @@ -313,8 +314,11 @@ export class Squid {
signerAddress,
route,
executionSettings,
overrides
}: ExecuteRoute): Promise<ethers.providers.TransactionResponse | TxRaw> {
overrides,
useBroadcast = false
}: ExecuteRoute): Promise<
ethers.providers.TransactionResponse | TxRaw | DeliverTxResponse
> {
this.validateInit();

if (!route.transactionRequest) {
Expand All @@ -333,7 +337,8 @@ export class Squid {
return await this.executeRouteCosmos(
signer as SigningStargateClient,
signerAddress!,
route
route,
useBroadcast
);
}

Expand Down Expand Up @@ -460,8 +465,9 @@ export class Squid {
private async executeRouteCosmos(
signer: SigningStargateClient | SigningCosmWasmClient,
signerAddress: string,
route: RouteData
): Promise<TxRaw> {
route: RouteData,
useBroadcast = false
): Promise<TxRaw | DeliverTxResponse> {
const cosmosMsg: CosmosMsg = JSON.parse(route.transactionRequest!.data);
const msgs = [];
switch (cosmosMsg.msgTypeUrl) {
Expand Down Expand Up @@ -530,6 +536,27 @@ export class Squid {
);
const gasMultiplier = Number(route.transactionRequest!.maxFeePerGas) || 1.3;

if (useBroadcast) {
const fee = calculateFee(
Math.trunc(estimatedGas * gasMultiplier),
GasPrice.fromString(route.transactionRequest!.gasPrice)
);

console.log({
signerAddress,
fromAminoMsg,
fee,
estimatedGas,
gasMultiplier
});

return (signer as SigningCosmWasmClient).signAndBroadcast(
signerAddress,
[fromAminoMsg],
fee,
""
);
}
return (signer as SigningCosmWasmClient).sign(
signerAddress,
[fromAminoMsg],
Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ export type ExecuteRoute = {
setGasPrice?: boolean;
};
overrides?: OverrideParams;
useBroadcast?: boolean;
};

export type Allowance = {
Expand Down

0 comments on commit 8662cf0

Please sign in to comment.