Skip to content

Commit

Permalink
extend execute route for requesting deposit address (#346)
Browse files Browse the repository at this point in the history
* extend execute route for requesting deposit address

* fix typo
  • Loading branch information
nnoln authored Nov 28, 2024
1 parent 6ecd5d0 commit 1ae87bc
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 8 additions & 5 deletions src/handlers/cosmos/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 7 additions & 7 deletions src/handlers/evm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import {
Contract,
EvmWallet,
ExecuteRoute,
OnChainExecutionData,
RouteParamsPopulated,
RouteRequest,
SquidData,
Token,
TokenBalance,
TransactionRequest,
Expand Down Expand Up @@ -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,
});

Expand Down Expand Up @@ -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),
});

Expand All @@ -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;

Expand Down Expand Up @@ -224,10 +224,10 @@ export class EvmHandler extends Utils {
route,
overrides,
}: Omit<ExecuteRoute, "signer"> & { 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,
});

Expand Down
4 changes: 2 additions & 2 deletions src/handlers/evm/utils.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -83,7 +83,7 @@ export class Utils {
transactionRequest,
overrides,
}: {
transactionRequest: SquidData & { setGasPrice?: boolean };
transactionRequest: OnChainExecutionData & { setGasPrice?: boolean };
overrides?: OverrideParams;
}): GasData => {
const {
Expand Down
38 changes: 36 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
/* eslint-disable no-case-declarations */
import {
ChainflipDepositAddressData,
ChainType,
CosmosAddress,
CosmosBalance,
DepositAddressResponse,
EvmWallet,
OnChainExecutionData,
RouteRequest,
RouteResponse,
SquidData,
SquidDataType,
StatusResponse,
Token,
TokenBalance,
Expand Down Expand Up @@ -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<TransactionResponses> {
const fromChain = this.getChainData(data.route.params.fromChain);
switch (fromChain.chainType) {
case ChainType.EVM:
Expand All @@ -155,6 +173,22 @@ export class Squid extends TokensChains {
}
}

private async requestDepositAddress(route: ExecuteRoute): Promise<TransactionResponses> {
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,
Expand All @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Token,
RouteRequest,
RouteResponse as _RouteResponse,
DepositAddressResponse,
} from "@0xsquid/squid-types";
import { SigningStargateClient } from "@cosmjs/stargate";

Expand Down Expand Up @@ -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;
Expand Down
15 changes: 4 additions & 11 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
# yarn lockfile v1


"@0xsquid/[email protected].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/[email protected].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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 1ae87bc

Please sign in to comment.