From cfd9431ecb31156e168c56a76d96ae01d3e3077c Mon Sep 17 00:00:00 2001 From: MarkacRobi Date: Fri, 22 Nov 2024 12:38:07 +0100 Subject: [PATCH] fix: native token allowance, evm chain --- README.md | 8 ++++---- src/constants.ts | 15 +++++++++++++-- src/entities/Providers.ts | 10 ++++++++-- src/services/EvmIntentService.ts | 10 +++++++--- 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index d8f1593..4b68eee 100644 --- a/README.md +++ b/README.md @@ -111,7 +111,7 @@ EVM Provider example: import { EvmProvider } from "@balanced/solver-sdk" // NOTE: user address should be provided by application when user connects wallet -const evmProvider = new EvmProvider("0x3FF796F1968C515f6AC2833545B5Dd2cE765A1a1", (window as any).ethereum) +const evmProvider = new EvmProvider("0x3FF796F1968C515f6AC2833545B5Dd2cE765A1a1", "arb", (window as any).ethereum) ``` SUI Provider example (uses [SUI dApp Kit](https://sdk.mystenlabs.com/dapp-kit/): @@ -142,7 +142,7 @@ import { IntentService, EvmProvider, CreateIntentOrderPayload, IntentStatusCode // create EVM provider because "arb" is of ChainType "evm" (defined in ChainConfig type - see section Load SDK Config) // NOTE: window can only be accessed client side (browser) -const evmProvider = new EvmProvider("0x601020c5797Cdd34f64476b9bf887a353150Cb9a", (window as any).ethereum) +const evmProvider = new EvmProvider("0x601020c5797Cdd34f64476b9bf887a353150Cb9a", "arb", (window as any).ethereum) const intentOrderPayload: CreateIntentOrderPayload = { quote_uuid: "a0dd7652-b360-4123-ab2d-78cfbcd20c6b", @@ -221,7 +221,7 @@ Example cancel order: ```typescript import { IntentService, SwapOrder, EvmProvider } from "@balanced/solver-sdk" -const evmProvider = new EvmProvider("0x601020c5797Cdd34f64476b9bf887a353150Cb9a", (window as any).ethereum) +const evmProvider = new EvmProvider("0x601020c5797Cdd34f64476b9bf887a353150Cb9a", "arb", (window as any).ethereum) const intentOrder: Result = await IntentService.getOrder( "0xabcdefasdasdsafssadasdsadsadasdsadasdsadsa", IntentService.getChainConfig("arb").intentContract, @@ -259,7 +259,7 @@ Example get order: ```typescript import { IntentService, SwapOrder, EvmProvider } from "@balanced/solver-sdk" -const evmProvider = new EvmProvider("0x601020c5797Cdd34f64476b9bf887a353150Cb9a", (window as any).ethereum) +const evmProvider = new EvmProvider("0x601020c5797Cdd34f64476b9bf887a353150Cb9a", "arb", (window as any).ethereum) const intentOrder: Result = await IntentService.getOrder( "0xabcdefasdasdsafssadasdsadsadasdsadasdsadsa", IntentService.getChainConfig("arb").intentContract, diff --git a/src/constants.ts b/src/constants.ts index a88c873..6184cfe 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,9 +1,22 @@ import type { ChainConfig, ChainName, EvmChainConfig, SuiChainConfig } from "./types.js" +import { arbitrum } from "viem/chains" +import type { Chain } from "viem" export const DEFAULT_MAX_RETRY = 3 export const DEFAULT_RETRY_DELAY_MS = 2000 export const SOLVER_API_ENDPOINT = "http://34.224.47.185" // TODO - replace with the production one +export const supportedChains: ChainName[] = ["arb", "sui"] + +export function getEvmViemChain(chainName: ChainName): Chain { + switch (chainName) { + case "arb": + return arbitrum + default: + throw new Error(`Unsupported EVM chain: ${chainName}`) + } +} + export const chainConfig: Record = { ["arb"]: { chain: { @@ -44,5 +57,3 @@ export const chainConfig: Record = { ], } satisfies SuiChainConfig, } as const - -export const supportedChains: ChainName[] = Object.keys(chainConfig) as ChainName[] diff --git a/src/entities/Providers.ts b/src/entities/Providers.ts index 11efd5a..605ff06 100644 --- a/src/entities/Providers.ts +++ b/src/entities/Providers.ts @@ -1,6 +1,7 @@ import { type Account, type Address, + type Chain, createPublicClient, createWalletClient, custom, @@ -11,21 +12,26 @@ import { import { type Wallet, type WalletAccount } from "@mysten/wallet-standard" import { getFullnodeUrl, SuiClient } from "@mysten/sui/client" import type { ChainName, ChainType, SuiNetworkType } from "../types.js" +import { getEvmViemChain } from "../constants.js" export type CustomProvider = { request(...args: any): Promise } export class EvmProvider { - public readonly walletClient: WalletClient + public readonly walletClient: WalletClient public readonly publicClient: PublicClient - constructor(userAddress: Address, provider: CustomProvider) { + constructor(userAddress: Address, chain: ChainName, provider: CustomProvider) { this.walletClient = createWalletClient({ account: userAddress, transport: custom(provider), + chain: getEvmViemChain(chain), }) this.publicClient = createPublicClient({ transport: custom(provider), + chain: getEvmViemChain(chain), }) + + console.log("Evm provider initialised with chain id:", this.walletClient.chain.id) } } diff --git a/src/services/EvmIntentService.ts b/src/services/EvmIntentService.ts index 946eb0b..a90627c 100644 --- a/src/services/EvmIntentService.ts +++ b/src/services/EvmIntentService.ts @@ -23,6 +23,13 @@ export class EvmIntentService { provider: EvmProvider, ): Promise> { try { + if (token == chainConfig.nativeToken) { + return { + ok: true, + value: true, + } + } + const allowedAmount = await provider.publicClient.readContract({ address: token, abi: erc20Abi, @@ -61,7 +68,6 @@ export class EvmIntentService { abi: erc20Abi, functionName: "approve", args: [address, amount], - chain: undefined, }) return { @@ -117,7 +123,6 @@ export class EvmIntentService { abi: intentAbi, functionName: "swap", args: [intent.toObjectData()], - chain: undefined, value: isNative ? intent.amount : undefined, }), } @@ -148,7 +153,6 @@ export class EvmIntentService { abi: intentAbi, functionName: "cancel", args: [orderId], - chain: undefined, }), } } catch (e) {