From f4fb457f45ff0981e04a92e3840b38d843e3c1b7 Mon Sep 17 00:00:00 2001 From: KSS Date: Fri, 26 Sep 2025 13:48:20 +0200 Subject: [PATCH] feat: use WSS instead of HTTPS when listening to events --- tee-apps/sealed-bid-auction/.env.template | 12 +- tee-apps/sealed-bid-auction/script/server.ts | 15 +-- .../src/blockchain/BlockchainClient.ts | 115 ++++++++---------- 3 files changed, 59 insertions(+), 83 deletions(-) diff --git a/tee-apps/sealed-bid-auction/.env.template b/tee-apps/sealed-bid-auction/.env.template index 27da695d..959232f9 100644 --- a/tee-apps/sealed-bid-auction/.env.template +++ b/tee-apps/sealed-bid-auction/.env.template @@ -2,22 +2,18 @@ LOG_LEVEL=info SERVER_PORT= WS_URL= -USE_TLS=true -SOLVER_PRICE_TTL_SECONDS= +USE_TLS=false +SOLVER_PRICE_TTL_SECONDS=600 NEXT_NONCE_URL= IS_MAINNET=false #Arbitrum -ARBITRUM_RPC=https://arb1.arbitrum.io/rpc -ARBITRUM_SEPOLIA_RPC=https://arbitrum-sepolia-rpc.publicnode.com -ARBITRUM_SEPOLIA_POLLING_INTERVAL_MS=500 +ARBITRUM_WS= ARBITRUM_T1_ERC7683_CONTRACT_ADDRESS= ARBITRUM_SIGNER_PRIVATE_KEY= #Base -BASE_RPC=https://base-rpc.publicnode.com -BASE_SEPOLIA_RPC=https://base-sepolia-rpc.publicnode.com -BASE_SEPOLIA_POLLING_INTERVAL_MS=2000 +BASE_WS= BASE_T1_ERC7683_CONTRACT_ADDRESS= BASE_SIGNER_PRIVATE_KEY= diff --git a/tee-apps/sealed-bid-auction/script/server.ts b/tee-apps/sealed-bid-auction/script/server.ts index 499d3e2a..33c63d66 100644 --- a/tee-apps/sealed-bid-auction/script/server.ts +++ b/tee-apps/sealed-bid-auction/script/server.ts @@ -1,7 +1,4 @@ import * as dotenv from "dotenv"; - -dotenv.config(); - import {AuctionApiServer} from "../src/api/AuctionApiServer.ts"; import {ViemIntentObserver} from "../src/blockchain/ViemIntentObserver.ts"; import {SolverPriceBook} from "../src/core/SolverPriceBook.ts"; @@ -9,6 +6,8 @@ import {AuctionService} from "../src/core/AuctionService.ts"; import {arbitrum, arbitrumSepolia, base, baseSepolia} from "viem/chains"; import {BlockchainClient} from "../src/blockchain/BlockchainClient.ts"; +dotenv.config(); + const USE_TLS = process.env.USE_TLS as string === "true"; const IS_MAINNET = process.env.IS_MAINNET as string === "true"; const SOLVER_PRICE_TTL_SECONDS = process.env.SOLVER_PRICE_TTL_SECONDS @@ -17,23 +16,21 @@ const TEN_MINUTES_IN_SECONDS = 600; const BASE_T1_ERC_7683_CONTRACT_ADDRESS = process.env.BASE_T1_ERC7683_CONTRACT_ADDRESS as `0x${string}`; const ARBITRUM_T1_ERC_7683_CONTRACT_ADDRESS = process.env.ARBITRUM_T1_ERC7683_CONTRACT_ADDRESS as `0x${string}`; -const ARBITRUM_RPC = (IS_MAINNET ? process.env.ARBITRUM_MAINNET_RPC : process.env.ARBITRUM_SEPOLIA_RPC) as string; -const BASE_RPC = (IS_MAINNET ? process.env.BASE_MAINNET_RPC : process.env.BASE_SEPOLIA_RPC) as string; +const ARBITRUM_WS = (process.env.ARBITRUM_WS) as string; +const BASE_WS = (process.env.BASE_WS) as string; const solverPriceBook = new SolverPriceBook(SOLVER_PRICE_TTL_SECONDS ? Number(SOLVER_PRICE_TTL_SECONDS as string) : TEN_MINUTES_IN_SECONDS); const auctionService = new AuctionService(solverPriceBook); const httpServer = new AuctionApiServer(solverPriceBook, auctionService); const arbitrumClient = new BlockchainClient( - ARBITRUM_RPC, + ARBITRUM_WS, IS_MAINNET ? arbitrum : arbitrumSepolia, - Number(process.env.ARBITRUM_SEPOLIA_POLLING_INTERVAL_MS as string), process.env.ARBITRUM_SIGNER_PRIVATE_KEY as `0x${string}` ); const baseClient = new BlockchainClient( - BASE_RPC, + BASE_WS, IS_MAINNET ? base : baseSepolia, - Number(process.env.BASE_SEPOLIA_POLLING_INTERVAL_MS as string), process.env.BASE_SIGNER_PRIVATE_KEY as `0x${string}` ); const arbitrumSepoliaIntentObserver = new ViemIntentObserver( diff --git a/tee-apps/sealed-bid-auction/src/blockchain/BlockchainClient.ts b/tee-apps/sealed-bid-auction/src/blockchain/BlockchainClient.ts index ae97d68d..253d30fd 100644 --- a/tee-apps/sealed-bid-auction/src/blockchain/BlockchainClient.ts +++ b/tee-apps/sealed-bid-auction/src/blockchain/BlockchainClient.ts @@ -1,78 +1,61 @@ -import { type Chain, createPublicClient, createWalletClient, http } from "viem"; -import { privateKeyToAccount } from "viem/accounts"; +import {type Chain, createPublicClient, webSocket} from "viem"; +import {privateKeyToAccount} from "viem/accounts"; export class BlockchainClient { - private _publicClient; - private _walletClient; - - public constructor( - rpcUrl: string, - chain: Chain, - pollingInterval: number, - private readonly signerKey?: `0x${string}` - ) { - this._publicClient = createPublicClient({ - chain, - transport: http(rpcUrl), - pollingInterval, - }); - - if (signerKey) { - const account = privateKeyToAccount(signerKey); - - this._walletClient = createWalletClient({ - account, - chain: chain, - transport: http(rpcUrl), - }); + private _publicClient; + + public constructor( + wsUrl: string, + chain: Chain, + private readonly signerKey?: `0x${string}`, + ) { + this._publicClient = createPublicClient({ + chain: chain, + transport: webSocket(wsUrl, { + reconnect: true, + keepAlive: true, + retryDelay: 1000, + }), + }); } - } - public get publicClient() { - return this._publicClient; - } - - public get walletClient() { - if (!this._walletClient) { - throw new Error("Wallet client not initialised!"); + public get publicClient() { + return this._publicClient; } - return this._walletClient; - } + public async signMessage(message: string): Promise<`0x${string}`> { + if (!this.signerKey) { + throw new Error("Signer key not initialised!"); + } - public async signMessage(message: string): Promise<`0x${string}`> { - if (!this.signerKey) { - throw new Error("Signer key not initialised!"); - } + const account = privateKeyToAccount(this.signerKey); - const account = privateKeyToAccount(this.signerKey); - - return await account.signMessage({ - message, - }); - } - - public async signTypedData( - domain: Record, - types: Record, - message: Record - ): Promise<`0x${string}`> { - if (!this.signerKey) { - throw new Error("Signer key not initialised!"); + return await account.signMessage({ + message, + }); } - const account = privateKeyToAccount(this.signerKey); - - const primaryType = Object.keys(types)[0]; - if (!primaryType) { - throw new Error("No types provided for EIP-712 signing"); + public async signTypedData( + domain: Record, + types: Record, + message: Record + ): Promise<`0x${string}`> { + if (!this.signerKey) { + throw new Error("Signer key not initialised!"); + } + + const account = privateKeyToAccount(this.signerKey); + + const primaryType = Object.keys(types)[0]; + if (!primaryType) { + throw new Error("No types provided for EIP-712 signing"); + } + + return await account.signTypedData({ + domain, + types, + primaryType, + message, + }); } - - return await account.signTypedData({ - domain, - types, - primaryType, - message, - }); - } }