From a7c96788e47416ac8d99d02cb21d51caa27b58f4 Mon Sep 17 00:00:00 2001 From: Kevin Peters Date: Fri, 15 Nov 2024 19:00:17 -0600 Subject: [PATCH] portico: pass portico bridge instance instead of destination address makes it harder to misuse the interface and pass a wrong address --- connect/src/routes/portico/automatic.ts | 4 +--- .../src/protocols/portico/portico.ts | 4 +++- platforms/evm/protocols/portico/src/bridge.ts | 19 ++++++++++++++++++- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/connect/src/routes/portico/automatic.ts b/connect/src/routes/portico/automatic.ts index 5803e66fa..72714e04e 100644 --- a/connect/src/routes/portico/automatic.ts +++ b/connect/src/routes/portico/automatic.ts @@ -272,9 +272,7 @@ export class AutomaticPorticoRoute const destToken = request.destination!.id; const fromPorticoBridge = await request.fromChain.getPorticoBridge(); - const tokenGroup = fromPorticoBridge.getTokenGroup(sourceToken.toString()); const toPorticoBridge = await request.toChain.getPorticoBridge(); - const destPorticoAddress = toPorticoBridge.getPorticoAddress(tokenGroup); const xfer = fromPorticoBridge.transfer( Wormhole.parseAddress(sender.chain(), sender.address()), @@ -282,7 +280,7 @@ export class AutomaticPorticoRoute sourceToken, amount.units(params.normalizedParams.amount), destToken!, - destPorticoAddress, + toPorticoBridge, details!, ); diff --git a/core/definitions/src/protocols/portico/portico.ts b/core/definitions/src/protocols/portico/portico.ts index 7da0bdb94..27efbc6e8 100644 --- a/core/definitions/src/protocols/portico/portico.ts +++ b/core/definitions/src/protocols/portico/portico.ts @@ -81,7 +81,7 @@ export interface PorticoBridge, amount: bigint, destToken: TokenId, - destPorticoAddress: string, + toPorticoBridge: PorticoBridge, quote: PorticoBridge.Quote, ): AsyncGenerator>; @@ -113,4 +113,6 @@ export interface PorticoBridge, amount: bigint, destToken: TokenId, - destinationPorticoAddress: string, + toPorticoBridge: PorticoBridge, quote: PorticoBridge.Quote, ) { + const toChain = toPorticoBridge.getChain(); + if (this.chain === toChain) { + throw new Error('Cannot transfer to the same chain'); + } + + if (receiver.chain !== toChain) { + throw new Error('Invalid destination chain'); + } + + const tokenGroup = this.getTokenGroup(token.toString()); + const destinationPorticoAddress = + toPorticoBridge.getPorticoAddress(tokenGroup); + const { minAmountStart, minAmountFinish } = quote.swapAmounts; if (minAmountStart === 0n) throw new Error('Invalid min swap amount'); if (minAmountFinish === 0n) throw new Error('Invalid min swap amount'); @@ -296,6 +309,10 @@ export class EvmPorticoBridge< return token.group; } + getChain() { + return this.chain; + } + private async *approve( token: string, senderAddr: string,