From 702f9234ba2f7adfebe53e3a7c283f93ac9a58bd Mon Sep 17 00:00:00 2001 From: Senna46 <29295263+Senna46@users.noreply.github.com> Date: Tue, 17 Oct 2023 23:33:37 +0900 Subject: [PATCH] feat: send-tx logic --- .../cosmos/tx-common.application.service.ts | 11 +++++++++++ .../wallets/keplr/keplr.infrastructure.service.ts | 15 +++++++++++++++ .../src/app/models/wallets/keplr/keplr.service.ts | 5 +++++ 3 files changed, 31 insertions(+) diff --git a/projects/portal/src/app/models/cosmos/tx-common.application.service.ts b/projects/portal/src/app/models/cosmos/tx-common.application.service.ts index e9afd9822..c633a4a98 100644 --- a/projects/portal/src/app/models/cosmos/tx-common.application.service.ts +++ b/projects/portal/src/app/models/cosmos/tx-common.application.service.ts @@ -5,6 +5,7 @@ import { import { ConfigService } from '../config.service'; import { SimulatedTxResultResponse } from '../cosmos/tx-common.model'; import { TxCommonService } from '../cosmos/tx-common.service'; +import { KeplrService } from '../wallets/keplr/keplr.service'; import { CosmosWallet, WalletType } from '../wallets/wallet.model'; import { WalletService } from '../wallets/wallet.service'; import { Dialog } from '@angular/cdk/dialog'; @@ -30,6 +31,7 @@ export class TxCommonApplicationService { private readonly walletService: WalletService, private readonly txCommon: TxCommonService, private readonly config: ConfigService, + private readonly keplr: KeplrService, ) {} async getPrerequisiteData() { @@ -172,6 +174,15 @@ export class TxCommonApplicationService { throw Error('Failed to sign!'); } + if (currentCosmosWallet.type === WalletType.keplr) { + console.log('keplr sendTx'); + const txHash = await this.keplr.sendTx(signedTxBuilder); + if (!txHash) { + throw Error('Failed to broadcast Tx on Keplr!'); + } + return txHash; + } + console.log('NOT keplr sendTx'); txResult = await this.txCommon.announceTx(txBuilder); txHash = txResult?.tx_response?.txhash; if (txHash === undefined) { diff --git a/projects/portal/src/app/models/wallets/keplr/keplr.infrastructure.service.ts b/projects/portal/src/app/models/wallets/keplr/keplr.infrastructure.service.ts index 56df34060..69e39d548 100644 --- a/projects/portal/src/app/models/wallets/keplr/keplr.infrastructure.service.ts +++ b/projects/portal/src/app/models/wallets/keplr/keplr.infrastructure.service.ts @@ -5,6 +5,7 @@ import { StoredWallet, WalletType } from '../wallet.model'; import { IKeplrInfrastructureService } from './keplr.service'; import { Injectable } from '@angular/core'; import { MatSnackBar } from '@angular/material/snack-bar'; +import { BroadcastMode } from '@cosmjs/launchpad'; import cosmosclient from '@cosmos-client/core'; import { ChainInfo, Key } from '@keplr-wallet/types'; import { LoadingDialogService } from 'projects/shared/src/lib/components/loading-dialog'; @@ -270,4 +271,18 @@ export class KeplrInfrastructureService implements IKeplrInfrastructureService { return txBuilder; } + + async sendTx(txBuilder: cosmosclient.TxBuilder): Promise { + if (!window.keplr) { + alert('Please install Keplr extension'); + return; + } + const chainId = this.configService.configs[0].chainID; + await window.keplr.enable(chainId); + const txByteArray = Uint8Array.from(Buffer.from(txBuilder.txBytes(), 'base64')); + const res = await window.keplr.sendTx(chainId, txByteArray, BroadcastMode.Sync); + console.log(res); + const txHash = Buffer.from(res).toString('hex').toUpperCase(); + return txHash; + } } diff --git a/projects/portal/src/app/models/wallets/keplr/keplr.service.ts b/projects/portal/src/app/models/wallets/keplr/keplr.service.ts index d99b262d9..8b25e21b9 100644 --- a/projects/portal/src/app/models/wallets/keplr/keplr.service.ts +++ b/projects/portal/src/app/models/wallets/keplr/keplr.service.ts @@ -12,6 +12,7 @@ export interface IKeplrInfrastructureService { signerBaseAccount: cosmosclient.proto.cosmos.auth.v1beta1.BaseAccount, ) => Promise; checkWallet: () => Promise; + sendTx: (txBuilder: cosmosclient.TxBuilder) => Promise; } @Injectable({ @@ -42,4 +43,8 @@ export class KeplrService { async checkWallet(): Promise { return await this.iKeplrInfrastructureService.checkWallet(); } + + async sendTx(txBuilder: cosmosclient.TxBuilder): Promise { + return await this.iKeplrInfrastructureService.sendTx(txBuilder); + } }