Skip to content

Commit 5fac645

Browse files
committed
update config
1 parent 649a61f commit 5fac645

File tree

6 files changed

+141
-0
lines changed

6 files changed

+141
-0
lines changed

.env.template

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ RPC_URL_60808=""
3636
RPC_URL_43114=""
3737
# tac
3838
RPC_URL_239=""
39+
# linea
40+
RPC_URL_59144=""
3941

4042
# Provider API keys
4143
LIFI_API_KEY=""

src/common/utils/viemClients.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ export const createClients = (): Record<number, Client<Transport, Chain>> => ({
181181
chain: tac,
182182
transport: http(RPC_URLS[tac.id]),
183183
}),
184+
[chains.linea.id]: createChainConfig(chains.linea),
184185
})
185186

186187
export const viemClients = createClients()

src/swapService/strategies/balmySDK/customSourceList.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
SourceListQuoteResponse,
99
} from "@balmy/sdk"
1010
import { LocalSourceList } from "@balmy/sdk/dist/services/quotes/source-lists/local-source-list"
11+
import { CustomZRXQuoteSource } from "./sources/0xMatchaQuoteSource"
1112
import { CustomEnsoQuoteSource } from "./sources/ensoQuoteSource"
1213
import { CustomKyberswapQuoteSource } from "./sources/kyberswapQuoteSource"
1314
import { CustomLiFiQuoteSource } from "./sources/lifiQuoteSource"
@@ -42,6 +43,7 @@ const customSources = {
4243
enso: new CustomEnsoQuoteSource(),
4344
"okx-dex": new CustomOKXDexQuoteSource(),
4445
paraswap: new CustomParaswapQuoteSource(),
46+
"0x": new CustomZRXQuoteSource(),
4547
oku_bob_icecreamswap: new CustomOkuQuoteSource(
4648
"icecreamswap",
4749
"IceCreamSwap",
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import { Addresses, Chains } from "@balmy/sdk"
2+
import type {
3+
BuildTxParams,
4+
IQuoteSource,
5+
QuoteParams,
6+
QuoteSourceMetadata,
7+
SourceQuoteResponse,
8+
SourceQuoteTransaction,
9+
} from "@balmy/sdk/dist/services/quotes/quote-sources/types"
10+
import {
11+
calculateAllowanceTarget,
12+
failed,
13+
} from "@balmy/sdk/dist/services/quotes/quote-sources/utils"
14+
import qs from "qs"
15+
16+
// Supported Networks: https://0x.org/docs/0x-swap-api/introduction#supported-networks
17+
const SUPPORTED_CHAINS = [
18+
Chains.ETHEREUM,
19+
Chains.ARBITRUM,
20+
Chains.AVALANCHE,
21+
Chains.BASE,
22+
Chains.BLAST,
23+
Chains.BNB_CHAIN,
24+
Chains.LINEA,
25+
Chains.MANTLE,
26+
Chains.MODE,
27+
Chains.OPTIMISM,
28+
Chains.POLYGON,
29+
Chains.SCROLL,
30+
{ chainId: 59144 }, // linea
31+
]
32+
33+
const ZRX_METADATA: QuoteSourceMetadata<ZRXSupport> = {
34+
name: "0x/Matcha",
35+
supports: {
36+
chains: SUPPORTED_CHAINS.map((chain) => chain.chainId),
37+
swapAndTransfer: false,
38+
buyOrders: false,
39+
},
40+
logoURI: "ipfs://QmPQY4siKEJHZGW5F4JDBrUXCBFqfpnKzPA2xDmboeuZzL",
41+
}
42+
type ZRXConfig = { apiKey: string }
43+
type ZRXSupport = { buyOrders: false; swapAndTransfer: false }
44+
type ZRXData = { tx: SourceQuoteTransaction }
45+
export class CustomZRXQuoteSource
46+
implements IQuoteSource<ZRXSupport, ZRXConfig, ZRXData>
47+
{
48+
getMetadata() {
49+
return ZRX_METADATA
50+
}
51+
52+
async quote({
53+
components: { fetchService },
54+
request: {
55+
chainId,
56+
sellToken,
57+
buyToken,
58+
order,
59+
config: { slippagePercentage, timeout },
60+
accounts: { takeFrom },
61+
},
62+
config,
63+
}: QuoteParams<ZRXSupport, ZRXConfig>): Promise<
64+
SourceQuoteResponse<ZRXData>
65+
> {
66+
const queryParams = {
67+
chainId,
68+
sellToken,
69+
buyToken,
70+
taker: takeFrom,
71+
slippageBps: slippagePercentage * 100,
72+
affiliateAddress: config.referrer?.address,
73+
sellAmount: order.sellAmount.toString(),
74+
}
75+
const queryString = qs.stringify(queryParams, {
76+
skipNulls: true,
77+
arrayFormat: "comma",
78+
})
79+
const url = `https://api.0x.org/swap/allowance-holder/quote?${queryString}`
80+
81+
const headers: HeadersInit = {
82+
"0x-api-key": config.apiKey,
83+
"0x-version": "v2",
84+
}
85+
86+
const response = await fetchService.fetch(url, { timeout, headers })
87+
if (!response.ok) {
88+
failed(ZRX_METADATA, chainId, sellToken, buyToken, await response.text())
89+
}
90+
const {
91+
transaction: { data, gas, to, value },
92+
buyAmount,
93+
minBuyAmount,
94+
issues,
95+
} = await response.json()
96+
97+
const allowanceTarget = issues?.allowance?.spender ?? Addresses.ZERO_ADDRESS
98+
99+
return {
100+
sellAmount: order.sellAmount,
101+
maxSellAmount: order.sellAmount,
102+
buyAmount: BigInt(buyAmount),
103+
minBuyAmount: BigInt(minBuyAmount),
104+
estimatedGas: BigInt(gas ?? 0),
105+
allowanceTarget: calculateAllowanceTarget(sellToken, allowanceTarget),
106+
customData: {
107+
tx: {
108+
calldata: data,
109+
to,
110+
value: BigInt(value ?? 0),
111+
},
112+
},
113+
type: order.type,
114+
}
115+
}
116+
117+
async buildTx({
118+
request,
119+
}: BuildTxParams<ZRXConfig, ZRXData>): Promise<SourceQuoteTransaction> {
120+
return request.customData.tx
121+
}
122+
123+
isConfigAndContextValidForQuoting(
124+
config: Partial<ZRXConfig> | undefined,
125+
): config is ZRXConfig {
126+
return !!config?.apiKey
127+
}
128+
129+
isConfigAndContextValidForTxBuilding(
130+
config: Partial<ZRXConfig> | undefined,
131+
): config is ZRXConfig {
132+
return true
133+
}
134+
}

src/swapService/strategies/balmySDK/sources/kyberswapQuoteSource.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const SUPPORTED_CHAINS: Record<ChainId, string> = {
4343
[Chains.SONIC.chainId]: "sonic",
4444
[Chains.ZK_SYNC_ERA.chainId]: "zksync",
4545
[130]: "unichain",
46+
[59144]: "linea",
4647
}
4748

4849
const KYBERSWAP_METADATA: QuoteSourceMetadata<KyberswapSupport> = {

src/swapService/strategies/balmySDK/sources/odosQuoteSource.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const ODOS_METADATA: QuoteSourceMetadata<OdosSupport> = {
4040
Chains.SCROLL.chainId,
4141
Chains.SONIC.chainId,
4242
239, // tac
43+
59144, // linea
4344
],
4445
swapAndTransfer: true,
4546
buyOrders: false,

0 commit comments

Comments
 (0)