From f363d3377aea9481a0ce35b11d55fd8b6badd879 Mon Sep 17 00:00:00 2001 From: rafaelbelchiorbd Date: Thu, 1 Aug 2024 08:16:01 +0200 Subject: [PATCH 1/2] feat(WIP): do swap Signed-off-by: Rafael Belchior --- src/main/scripts/do-swap.ts | 38 +++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/src/main/scripts/do-swap.ts b/src/main/scripts/do-swap.ts index fad7221..e8eff64 100644 --- a/src/main/scripts/do-swap.ts +++ b/src/main/scripts/do-swap.ts @@ -10,16 +10,21 @@ import { } from "../utils/common"; import { ExchangeApi, + AccountApi, + StatusApi, GetRoutesRequest, Route, RoutesResponse, + GetTokenApprovalRequest, } from "@blockdaemon/blockdaemon-defi-api-typescript-fetch"; import { ethers } from "ethers"; const logger = log.getLogger("do-swap"); async function main() { - const api = new ExchangeApi(apiConfig); + const exchangeAPI = new ExchangeApi(apiConfig); + const accountAPI = new AccountApi(apiConfig); + const statusAPI = new StatusApi(apiConfig); const routeParameters: GetRoutesRequest = { fromChain: "10", // Optimism @@ -33,16 +38,32 @@ async function main() { }; try { - // const routes: RoutesResponse = await api.getRoutes(routeParameters); - // logger.info("Got routes"); + const routes: RoutesResponse = await exchangeAPI.getRoutes(routeParameters); + logger.info("Got routes"); + + const selectedRoute: Route = routes.routes[0]; + logger.info("Selected route:"); + const approvalAddress = selectedRoute.steps[0].estimate.approvalAddress; + + const approvalRequest: GetTokenApprovalRequest = { + chainID: routeParameters.fromChain, + accountAddress: routeParameters.fromAddress, + tokenAddress: routeParameters.toToken, + spenderAddress: approvalAddress, + } + + const approval = await accountAPI.getTokenApproval(approvalRequest); + logger.info("Got approval"); + logger.info(JSON.stringify(approval, null, 2)); + + // set timeout 10 seconds, await the approval to propagate + await new Promise(resolve => setTimeout(resolve, 10000)); + + logger.info("Sending transaction..."); - // const selectedRoute: Route = routes.routes[0]; - // logger.info("Selected route:"); - // const approvalAddress = selectedRoute.steps[0].estimate.approvalAddress; const destination = RECEIVER_ADDRESS; - const txPayload = - "0x0b4cb5d80000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000038d34169c6b532200000000000000000000000000000000000000000000000009497c1a68fe583400000000000000000000000000000000000000000000000000000000669a767900000000000000000000000000000000000000000000000009497c1a68fe583400000000000000000000000000000000000000000000000000000000669a7679000000000000000000000000b3c68a491608952cb1257fc9909a537a0173b63b0000000000000000000000009298dfd8a0384da62643c2e98f437e820029e75e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b017ed3df2acd34e91f651533afaa9e9c4cfcd499a3ca0cf327c00c8e8cfbe90000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000da10009cbd5d07dd0cecc66161fc93d7c9000da1000000000000000000000000f271aafc62634e6dc9a276ac0f6145c4fdbe2ced0000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000000000000000089000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003686f7000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; + const txPayload = selectedRoute.transactionRequest.data; const signedPayload = await signMessage(txPayload); const broadcastResult = await transact(signedPayload, destination); @@ -54,6 +75,7 @@ async function main() { "Check transaction at: https://optimistic.etherscan.io/tx/" + broadcastResult.hash, ); + logger.info("Transaction done with success. Please check your balances."); } else { throw new Error("Failed to broadcast signed message"); } From 946f0afdff734f8ced4b8b5cf29c1f1ef358bc50 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 1 Aug 2024 06:25:01 +0000 Subject: [PATCH 2/2] chore(deps): bump minimist from 1.2.0 to 1.2.8 Bumps [minimist](https://github.com/minimistjs/minimist) from 1.2.0 to 1.2.8. - [Changelog](https://github.com/minimistjs/minimist/blob/main/CHANGELOG.md) - [Commits](https://github.com/minimistjs/minimist/compare/v1.2.0...v1.2.8) --- updated-dependencies: - dependency-name: minimist dependency-type: indirect ... Signed-off-by: dependabot[bot] --- package-lock.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index 7a11eb6..c8541a4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,7 +22,7 @@ "eslint-config-prettier": "9.1.0", "eslint-config-standard": "17.1.0", "npx": "3.0.0", - "prettier": "^3.3.3", + "prettier": "3.3.3", "ts-node": "10.9.2", "tsc": "2.0.4", "typescript": "5.3.3"