Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: native token allowance, evm chain #23

Merged
merged 1 commit into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/):
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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<SwapOrder> = await IntentService.getOrder(
"0xabcdefasdasdsafssadasdsadsadasdsadasdsadsa",
IntentService.getChainConfig("arb").intentContract,
Expand Down Expand Up @@ -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<SwapOrder> = await IntentService.getOrder(
"0xabcdefasdasdsafssadasdsadsadasdsadasdsadsa",
IntentService.getChainConfig("arb").intentContract,
Expand Down
15 changes: 13 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -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<ChainName, ChainConfig> = {
["arb"]: {
chain: {
Expand Down Expand Up @@ -44,5 +57,3 @@ export const chainConfig: Record<ChainName, ChainConfig> = {
],
} satisfies SuiChainConfig,
} as const

export const supportedChains: ChainName[] = Object.keys(chainConfig) as ChainName[]
10 changes: 8 additions & 2 deletions src/entities/Providers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
type Account,
type Address,
type Chain,
createPublicClient,
createWalletClient,
custom,
Expand All @@ -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<any> }

export class EvmProvider {
public readonly walletClient: WalletClient<CustomTransport, undefined, Account>
public readonly walletClient: WalletClient<CustomTransport, Chain, Account>
public readonly publicClient: PublicClient<CustomTransport>

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)
}
}

Expand Down
10 changes: 7 additions & 3 deletions src/services/EvmIntentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ export class EvmIntentService {
provider: EvmProvider,
): Promise<Result<boolean>> {
try {
if (token == chainConfig.nativeToken) {
return {
ok: true,
value: true,
}
}

const allowedAmount = await provider.publicClient.readContract({
address: token,
abi: erc20Abi,
Expand Down Expand Up @@ -61,7 +68,6 @@ export class EvmIntentService {
abi: erc20Abi,
functionName: "approve",
args: [address, amount],
chain: undefined,
})

return {
Expand Down Expand Up @@ -117,7 +123,6 @@ export class EvmIntentService {
abi: intentAbi,
functionName: "swap",
args: [intent.toObjectData()],
chain: undefined,
value: isNative ? intent.amount : undefined,
}),
}
Expand Down Expand Up @@ -148,7 +153,6 @@ export class EvmIntentService {
abi: intentAbi,
functionName: "cancel",
args: [orderId],
chain: undefined,
}),
}
} catch (e) {
Expand Down