From 1ae87bc20d1d9243045e5f643d9721c4d7522405 Mon Sep 17 00:00:00 2001 From: nnoln <48334589+nnoln@users.noreply.github.com> Date: Thu, 28 Nov 2024 14:28:55 -0300 Subject: [PATCH] extend execute route for requesting deposit address (#346) * extend execute route for requesting deposit address * fix typo --- package.json | 2 +- src/handlers/cosmos/index.ts | 13 +++++++----- src/handlers/evm/index.ts | 14 ++++++------- src/handlers/evm/utils.ts | 4 ++-- src/index.ts | 38 ++++++++++++++++++++++++++++++++++-- src/types/index.ts | 3 ++- yarn.lock | 15 ++++---------- 7 files changed, 60 insertions(+), 29 deletions(-) diff --git a/package.json b/package.json index a0d1adf..07668e2 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,7 @@ "semver": "^7.5.4" }, "devDependencies": { - "@0xsquid/squid-types": "0.1.85", + "@0xsquid/squid-types": "0.1.122", "@babel/core": "^7.18.10", "@babel/preset-env": "^7.18.10", "@babel/preset-typescript": "^7.18.6", diff --git a/src/handlers/cosmos/index.ts b/src/handlers/cosmos/index.ts index 3106c7e..736337a 100644 --- a/src/handlers/cosmos/index.ts +++ b/src/handlers/cosmos/index.ts @@ -16,6 +16,7 @@ import { RouteRequest, IBC_TRANSFER_TYPE, WASM_TYPE, + OnChainExecutionData, } from "../../types"; import { TxRaw } from "cosmjs-types/cosmos/tx/v1beta1/tx"; import { MsgExecuteContract } from "cosmjs-types/cosmwasm/wasm/v1/tx"; @@ -64,12 +65,14 @@ export class CosmosHandler { await this.validateBalance({ data, params }); const { route } = data; + const transactionRequest = route.transactionRequest as OnChainExecutionData; + const signerAddress = data.signerAddress as string; const signer = data.signer as CosmosSigner; const msgs = []; - const cosmosMsg: CosmosMsg = JSON.parse(route.transactionRequest?.data as string); + const cosmosMsg: CosmosMsg = JSON.parse(transactionRequest?.data as string); switch (cosmosMsg.typeUrl) { case CCTP_TYPE: { @@ -105,16 +108,16 @@ export class CosmosHandler { } let memo = ""; - if (data.route.transactionRequest?.requestId) { + if (transactionRequest?.requestId) { memo = JSON.stringify({ - squidRequestId: data.route.transactionRequest?.requestId, + squidRequestId: transactionRequest?.requestId, }); } // simulate tx to estimate gas cost const estimatedGas = await signer.simulate(signerAddress, msgs, memo); - const gasMultiplier = Number(route.transactionRequest?.maxFeePerGas) || 1.5; - const gasPrice = route.transactionRequest?.gasPrice as string; + const gasMultiplier = Number(transactionRequest?.maxFeePerGas) || 1.5; + const gasPrice = transactionRequest?.gasPrice as string; return signer.sign( signerAddress, diff --git a/src/handlers/evm/index.ts b/src/handlers/evm/index.ts index 8820592..0486798 100644 --- a/src/handlers/evm/index.ts +++ b/src/handlers/evm/index.ts @@ -5,9 +5,9 @@ import { Contract, EvmWallet, ExecuteRoute, + OnChainExecutionData, RouteParamsPopulated, RouteRequest, - SquidData, Token, TokenBalance, TransactionRequest, @@ -37,11 +37,11 @@ export class EvmHandler extends Utils { route: { transactionRequest }, overrides, } = data; - const { target, value, data: _data } = transactionRequest as SquidData; + const { target, value, data: _data } = transactionRequest as OnChainExecutionData; const signer = data.signer as WalletV6; const gasData = this.getGasData({ - transactionRequest: data.route.transactionRequest as SquidData, + transactionRequest: data.route.transactionRequest as OnChainExecutionData, overrides, }); @@ -126,7 +126,7 @@ export class EvmHandler extends Utils { const hasAllowance = await this.validateAllowance({ fromTokenContract: params.fromTokenContract as Contract, sender: address, - router: (data.route.transactionRequest as SquidData).target, + router: (data.route.transactionRequest as OnChainExecutionData).target, amount: BigInt(params.fromAmount), }); @@ -150,7 +150,7 @@ export class EvmHandler extends Utils { executionSettings, overrides, } = data; - const { target } = transactionRequest as SquidData; + const { target } = transactionRequest as OnChainExecutionData; const { fromIsNative, fromAmount } = params; const fromTokenContract = params.fromTokenContract as Contract; @@ -224,10 +224,10 @@ export class EvmHandler extends Utils { route, overrides, }: Omit & { nonce: number }): string { - const { target, data, value } = route.transactionRequest as SquidData; + const { target, data, value } = route.transactionRequest as OnChainExecutionData; const gasData = this.getGasData({ - transactionRequest: route.transactionRequest as SquidData, + transactionRequest: route.transactionRequest as OnChainExecutionData, overrides, }); diff --git a/src/handlers/evm/utils.ts b/src/handlers/evm/utils.ts index ca6e3c2..371392d 100644 --- a/src/handlers/evm/utils.ts +++ b/src/handlers/evm/utils.ts @@ -1,4 +1,4 @@ -import { ChainData, SquidData, Token } from "@0xsquid/squid-types"; +import { ChainData, OnChainExecutionData, Token } from "@0xsquid/squid-types"; import { OverrideParams, Contract, GasData, RpcProvider, TokenBalance } from "../../types"; import { MulticallWrapper } from "ethers-multicall-provider"; @@ -83,7 +83,7 @@ export class Utils { transactionRequest, overrides, }: { - transactionRequest: SquidData & { setGasPrice?: boolean }; + transactionRequest: OnChainExecutionData & { setGasPrice?: boolean }; overrides?: OverrideParams; }): GasData => { const { diff --git a/src/index.ts b/src/index.ts index a67a257..e3e279f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,12 +1,15 @@ /* eslint-disable no-case-declarations */ import { + ChainflipDepositAddressData, ChainType, CosmosAddress, CosmosBalance, + DepositAddressResponse, EvmWallet, + OnChainExecutionData, RouteRequest, RouteResponse, - SquidData, + SquidDataType, StatusResponse, Token, TokenBalance, @@ -131,6 +134,21 @@ export class Squid extends TokensChains { this.validateInit(); this.validateTransactionRequest(data.route); + switch (data.route.transactionRequest?.type) { + case SquidDataType.OnChainExecution: + return await this.executeOnChainTx(data); + + case SquidDataType.ChainflipDepositAddress: + return await this.requestDepositAddress(data); + + default: + throw new Error( + `Unsupported transaction request type - ${data.route.transactionRequest?.type}`, + ); + } + } + + private async executeOnChainTx(data: ExecuteRoute): Promise { const fromChain = this.getChainData(data.route.params.fromChain); switch (fromChain.chainType) { case ChainType.EVM: @@ -155,6 +173,22 @@ export class Squid extends TokensChains { } } + private async requestDepositAddress(route: ExecuteRoute): Promise { + const depositAddressRequest = route.route.transactionRequest as ChainflipDepositAddressData; + + // request deposit address from api + const { data, status } = await this.httpInstance.post( + "v2/deposit-address", + depositAddressRequest, + ); + + if (status != 200) { + throw new Error(data.error); + } + + return data as DepositAddressResponse; + } + async isRouteApproved({ route, sender, @@ -176,7 +210,7 @@ export class Squid extends TokensChains { return await this.handlers.evm.isRouteApproved({ sender, params, - target: (route.transactionRequest as SquidData).target, + target: (route.transactionRequest as OnChainExecutionData).target, }); default: diff --git a/src/types/index.ts b/src/types/index.ts index 21b6b13..cf975c9 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -3,6 +3,7 @@ import { Token, RouteRequest, RouteResponse as _RouteResponse, + DepositAddressResponse, } from "@0xsquid/squid-types"; import { SigningStargateClient } from "@cosmjs/stargate"; @@ -49,7 +50,7 @@ export type RouteResponse = _RouteResponse & { integratorId?: string; }; -export type TransactionResponses = TransactionResponse | TxRaw; +export type TransactionResponses = TransactionResponse | TxRaw | DepositAddressResponse; export type GetStatus = { transactionId: string; diff --git a/yarn.lock b/yarn.lock index cd9db52..272960b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2,16 +2,14 @@ # yarn lockfile v1 -"@0xsquid/squid-types@0.1.85": - version "0.1.85" - resolved "https://registry.yarnpkg.com/@0xsquid/squid-types/-/squid-types-0.1.85.tgz#586c65b5361fe81eb97e2eb9d15f460e8c59ceef" - integrity sha512-LTn5GM0yPnLqyDBAYOB1kRPYoClPK/gmaPC0Axp4Z1KrTWh3a1yac4+eqmZYsSKr7zZOrBcid7/OchsVUEW2YA== +"@0xsquid/squid-types@0.1.122": + version "0.1.122" + resolved "https://registry.yarnpkg.com/@0xsquid/squid-types/-/squid-types-0.1.122.tgz#78efb021ccf85042389f83116a71757dbf49fa4a" + integrity sha512-rO1/a1l6RwzZrQY3V92spGKYzEk8DLl1KSNRqqytUqBZHPkwUSCP/nU4uahkimCLuvXmiVf/n31Xo9d8ytdjyA== dependencies: "@axelar-network/axelarjs-sdk" "^0.16.1" "@ethersproject/providers" "^5.7.2" - long "^5.2.3" typescript "*" - ws "^8.18.0" "@aashutoshrathi/word-wrap@^1.2.3": version "1.2.6" @@ -7816,11 +7814,6 @@ ws@^8.13.0: resolved "https://registry.yarnpkg.com/ws/-/ws-8.16.0.tgz#d1cd774f36fbc07165066a60e40323eab6446fd4" integrity sha512-HS0c//TP7Ina87TfiPUz1rQzMhHrl/SG2guqRcTOIUYD2q8uhUdNHZYJUaQ8aTGPzCh+c6oawMKW35nFl1dxyQ== -ws@^8.18.0: - version "8.18.0" - resolved "https://registry.yarnpkg.com/ws/-/ws-8.18.0.tgz#0d7505a6eafe2b0e712d232b42279f53bc289bbc" - integrity sha512-8VbfWfHLbbwu3+N6OKsOMpBdT4kXPDDB9cJk2bJ6mh9ucxdlnNvH1e+roYkKmN9Nxw2yjz7VzeO9oOz2zJ04Pw== - ws@~8.11.0: version "8.11.0" resolved "https://registry.yarnpkg.com/ws/-/ws-8.11.0.tgz#6a0d36b8edfd9f96d8b25683db2f8d7de6e8e143"