Skip to content

Commit

Permalink
Merge branch 'Thunnini/ibc-transfer' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Thunnini committed Jul 28, 2023
2 parents 2f9122e + 633dc27 commit 4ea1393
Show file tree
Hide file tree
Showing 59 changed files with 3,705 additions and 548 deletions.
77 changes: 75 additions & 2 deletions packages/background/src/recent-send-history/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ import {
KeplrError,
Message,
} from "@keplr-wallet/router";
import { GetRecentSendHistoriesMsg, SendTxAndRecordMsg } from "./messages";
import {
GetRecentSendHistoriesMsg,
SendTxAndRecordMsg,
SendTxAndRecordWithIBCPacketForwardingMsg,
GetIBCTransferHistories,
RemoveIBCTransferHistory,
ClearAllIBCTransferHistory,
} from "./messages";
import { RecentSendHistoryService } from "./service";

export const getHandler: (service: RecentSendHistoryService) => Handler = (
Expand All @@ -23,6 +30,26 @@ export const getHandler: (service: RecentSendHistoryService) => Handler = (
env,
msg as SendTxAndRecordMsg
);
case SendTxAndRecordWithIBCPacketForwardingMsg:
return handleSendTxAndRecordWithIBCPacketForwardingMsg(service)(
env,
msg as SendTxAndRecordWithIBCPacketForwardingMsg
);
case GetIBCTransferHistories:
return handleGetIBCTransferHistories(service)(
env,
msg as GetIBCTransferHistories
);
case RemoveIBCTransferHistory:
return handleRemoveIBCTransferHistory(service)(
env,
msg as RemoveIBCTransferHistory
);
case ClearAllIBCTransferHistory:
return handleClearAllIBCTransferHistory(service)(
env,
msg as ClearAllIBCTransferHistory
);
default:
throw new KeplrError("tx", 110, "Unknown msg type");
}
Expand Down Expand Up @@ -51,7 +78,53 @@ const handleSendTxAndRecordMsg: (
msg.sender,
msg.recipient,
msg.amount,
msg.memo
msg.memo,
undefined
);
};
};

const handleSendTxAndRecordWithIBCPacketForwardingMsg: (
service: RecentSendHistoryService
) => InternalHandler<SendTxAndRecordWithIBCPacketForwardingMsg> = (service) => {
return async (_env, msg) => {
return await service.sendTxAndRecord(
msg.historyType,
msg.sourceChainId,
msg.destinationChainId,
msg.tx,
msg.mode,
msg.silent,
msg.sender,
msg.recipient,
msg.amount,
msg.memo,
msg.channels
);
};
};

const handleGetIBCTransferHistories: (
service: RecentSendHistoryService
) => InternalHandler<GetIBCTransferHistories> = (service) => {
return (_env, _msg) => {
return service.getRecentIBCTransferHistories();
};
};

const handleRemoveIBCTransferHistory: (
service: RecentSendHistoryService
) => InternalHandler<RemoveIBCTransferHistory> = (service) => {
return (_env, msg) => {
service.removeRecentIBCTransferHistory(msg.id);
return service.getRecentIBCTransferHistories();
};
};

const handleClearAllIBCTransferHistory: (
service: RecentSendHistoryService
) => InternalHandler<ClearAllIBCTransferHistory> = (service) => {
return () => {
service.clearAllRecentIBCTransferHistory();
};
};
13 changes: 12 additions & 1 deletion packages/background/src/recent-send-history/init.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import { Router } from "@keplr-wallet/router";
import { GetRecentSendHistoriesMsg, SendTxAndRecordMsg } from "./messages";
import {
GetRecentSendHistoriesMsg,
SendTxAndRecordMsg,
SendTxAndRecordWithIBCPacketForwardingMsg,
GetIBCTransferHistories,
RemoveIBCTransferHistory,
ClearAllIBCTransferHistory,
} from "./messages";
import { ROUTE } from "./constants";
import { getHandler } from "./handler";
import { RecentSendHistoryService } from "./service";

export function init(router: Router, service: RecentSendHistoryService): void {
router.registerMessage(GetRecentSendHistoriesMsg);
router.registerMessage(SendTxAndRecordMsg);
router.registerMessage(SendTxAndRecordWithIBCPacketForwardingMsg);
router.registerMessage(GetIBCTransferHistories);
router.registerMessage(RemoveIBCTransferHistory);
router.registerMessage(ClearAllIBCTransferHistory);

router.addHandler(ROUTE, getHandler(service));
}
166 changes: 165 additions & 1 deletion packages/background/src/recent-send-history/messages.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Message } from "@keplr-wallet/router";
import { ROUTE } from "./constants";
import { RecentSendHistory } from "./types";
import { IBCTransferHistory, RecentSendHistory } from "./types";

export class GetRecentSendHistoriesMsg extends Message<RecentSendHistory[]> {
public static type() {
Expand Down Expand Up @@ -96,4 +96,168 @@ export class SendTxAndRecordMsg extends Message<Uint8Array> {
type(): string {
return SendTxAndRecordMsg.type();
}

withIBCPacketForwarding(
channels: {
portId: string;
channelId: string;
counterpartyChainId: string;
}[]
): SendTxAndRecordWithIBCPacketForwardingMsg {
return new SendTxAndRecordWithIBCPacketForwardingMsg(
this.historyType,
this.sourceChainId,
this.destinationChainId,
this.tx,
channels,
this.mode,
this.silent,
this.sender,
this.recipient,
this.amount,
this.memo
);
}
}

export class SendTxAndRecordWithIBCPacketForwardingMsg extends Message<Uint8Array> {
public static type() {
return "send-tx-and-record-with-ibc-packet-forwarding";
}

constructor(
public readonly historyType: string,
public readonly sourceChainId: string,
public readonly destinationChainId: string,
public readonly tx: unknown,
public readonly channels: {
portId: string;
channelId: string;
counterpartyChainId: string;
}[],
public readonly mode: "async" | "sync" | "block",
public readonly silent: boolean,
public readonly sender: string,
public readonly recipient: string,
public readonly amount: {
readonly amount: string;
readonly denom: string;
}[],
public readonly memo: string
) {
super();
}

validateBasic(): void {
if (!this.historyType) {
throw new Error("type is empty");
}

if (!this.sourceChainId) {
throw new Error("chain id is empty");
}

if (!this.destinationChainId) {
throw new Error("chain id is empty");
}

if (!this.tx) {
throw new Error("tx is empty");
}

if (this.channels.length === 0) {
throw new Error("channels is empty");
}

if (
!this.mode ||
(this.mode !== "sync" && this.mode !== "async" && this.mode !== "block")
) {
throw new Error("invalid mode");
}

if (!this.sender) {
throw new Error("sender is empty");
}

if (!this.recipient) {
throw new Error("recipient is empty");
}
}

route(): string {
return ROUTE;
}

type(): string {
return SendTxAndRecordWithIBCPacketForwardingMsg.type();
}
}

export class GetIBCTransferHistories extends Message<IBCTransferHistory[]> {
public static type() {
return "get-ibc-transfer-histories";
}

constructor() {
super();
}

validateBasic(): void {
// noop
}

route(): string {
return ROUTE;
}

type(): string {
return GetIBCTransferHistories.type();
}
}

export class RemoveIBCTransferHistory extends Message<IBCTransferHistory[]> {
public static type() {
return "remove-ibc-transfer-histories";
}

constructor(public readonly id: string) {
super();
}

validateBasic(): void {
if (!this.id) {
throw new Error("id is empty");
}
}

route(): string {
return ROUTE;
}

type(): string {
return RemoveIBCTransferHistory.type();
}
}

export class ClearAllIBCTransferHistory extends Message<void> {
public static type() {
return "clear-all-ibc-transfer-histories";
}

constructor() {
super();
}

validateBasic(): void {
// noop
}

route(): string {
return ROUTE;
}

type(): string {
return ClearAllIBCTransferHistory.type();
}
}
Loading

0 comments on commit 4ea1393

Please sign in to comment.