Skip to content
Open
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
67 changes: 28 additions & 39 deletions sdk/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gobob/bob-sdk",
"version": "4.3.8",
"version": "4.3.9",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
Expand Down Expand Up @@ -51,6 +51,7 @@
"bitcoin-address-validation": "^3.0.0",
"bitcoinjs-lib": "^6.1.7",
"global": "^4.4.0",
"viem": "^2.33.2"
}
"viem": "^2.38.3"
},
"packageManager": "[email protected]+sha512.d615db246fe70f25dcfea6d8d73dee782ce23e2245e3c4f6f888249fb568149318637dca73c2c5c8ef2a4ca0d5657fb9567188bfab47f566d1ee6ce987815c39"
}
16 changes: 13 additions & 3 deletions sdk/src/gateway/layerzero.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
import { bob, bobSepolia, mainnet } from 'viem/chains';
import { layerZeroOftAbi, quoterV2Abi } from './abi';
import { AllWalletClientParams, GatewayApiClient } from './client';
import { getTokenAddress, getTokenSlots } from './tokens';
import { getTokenAddress } from './tokens';
import {
CrossChainOrder,
ExecuteQuoteParams,
Expand Down Expand Up @@ -605,7 +605,7 @@ export class LayerZeroGatewayClient extends GatewayApiClient {

if (chain.id === mainnet.id) {
// WBTC mainnet
const wbtcMainnetSlots = getTokenSlots(wbtcMainnetAddress, 'ethereum');
const wbtcMainnetSlots = this.getTokenSlots('ethereum');
const allowanceSlot = computeAllowanceSlot(
user as Address,
wbtcOftAddress as Address,
Expand All @@ -628,7 +628,7 @@ export class LayerZeroGatewayClient extends GatewayApiClient {
});
} else {
// WBTC OFT
const wbtcOftSlots = getTokenSlots(wbtcOftAddress as Address, fromChain);
const wbtcOftSlots = this.getTokenSlots(fromChain);

const oftBalanceSlot = computeBalanceSlot(user as Address, wbtcOftSlots.balanceSlot);

Expand Down Expand Up @@ -716,6 +716,16 @@ export class LayerZeroGatewayClient extends GatewayApiClient {
});
}

private getTokenSlots(originChain: string) {
// slots for WBTC ERC20 token on ethereum
if (originChain === 'ethereum') return { allowanceSlot: 2n, balanceSlot: 0n };

return {
allowanceSlot: 6n,
balanceSlot: 5n,
};
}

/**
* Retrieves all orders (onramp, offramp, and crosschain swaps) for a specific user address.
*
Expand Down
55 changes: 42 additions & 13 deletions sdk/src/gateway/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as bitcoin from 'bitcoinjs-lib';
import {
Address,
createPublicClient,
defineChain,
encodeAbiParameters,
formatUnits,
Hex,
Expand Down Expand Up @@ -138,31 +139,59 @@ export function formatBtc(btc: bigint) {
return formatUnits(btc, 8);
}

const supportedChains = [
const supportedChainsMapping = {
bob,
mainnet,
sonic,
ethereum: defineChain({
...mainnet,
rpcUrls: {
default: {
http: ['https://ethereum-rpc.publicnode.com'],
},
},
}),
sonic: defineChain({
...sonic,
rpcUrls: {
default: {
http: ['https://sonic.drpc.org'],
},
},
}),
bsc,
unichain,
berachain,
sei,
bera: berachain,
sei: defineChain({
...sei,
rpcUrls: {
default: {
http: ['https://sei.drpc.org'],
},
},
}),
avalanche,
base,
soneium,
optimism,
] as const;
optimism: defineChain({
...optimism,
rpcUrls: {
default: {
http: ['https://optimism.drpc.org'],
},
},
}),
} as const;

const chainIdToChainConfigMapping = supportedChains.reduce(
const chainIdToChainConfigMapping = Object.values(supportedChainsMapping).reduce(
(acc, chain) => {
acc[chain.id] = chain;
return acc;
},
{} as Record<ViemChain['id'], ViemChain>
);

const chainNameToChainIdMapping = supportedChains.reduce(
(acc, chain) => {
acc[chain.name.toLowerCase()] = chain.id;
const chainNameToChainIdMapping = Object.entries(supportedChainsMapping).reduce(
(acc, [name, chain]) => {
acc[name.toLowerCase()] = chain.id;
return acc;
},
{} as Record<ViemChain['name'], ViemChain['id']>
Comment on lines +192 to 197
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The keys of supportedChainsMapping are already in lowercase, so the call to .toLowerCase() on the name is redundant. You can simplify this by using name directly. Additionally, the type assertion can be made more accurate to reflect that the keys are strings, not necessarily ViemChain['name'].

Suggested change
const chainNameToChainIdMapping = Object.entries(supportedChainsMapping).reduce(
(acc, [name, chain]) => {
acc[name.toLowerCase()] = chain.id;
return acc;
},
{} as Record<ViemChain['name'], ViemChain['id']>
const chainNameToChainIdMapping = Object.entries(supportedChainsMapping).reduce(
(acc, [name, chain]) => {
acc[name] = chain.id;
return acc;
},
{} as Record<string, ViemChain['id']>
);

Expand All @@ -172,7 +201,7 @@ function getChainIdByName(chainName: string) {
const chainId = chainNameToChainIdMapping[chainName.toLowerCase()];
if (!chainId) {
throw new Error(
`Chain id for "${chainName}" not found. Allowed values ${supportedChains.map((chain) => chain.name)}`
`Chain id for "${chainName}" not found. Allowed values ${Object.keys(supportedChainsMapping).map((chainName) => chainName.toLocaleLowerCase())}`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The keys of supportedChainsMapping are already in lowercase, so the call to .toLocaleLowerCase() is redundant here. You can simplify the error message generation by removing it.

Suggested change
`Chain id for "${chainName}" not found. Allowed values ${Object.keys(supportedChainsMapping).map((chainName) => chainName.toLocaleLowerCase())}`
`Chain id for "${chainName}" not found. Allowed values ${Object.keys(supportedChainsMapping)}`

);
}
return chainId;
Expand All @@ -182,7 +211,7 @@ function getChainConfigById(chainId: number) {
const config = chainIdToChainConfigMapping[chainId];
if (!config) {
throw new Error(
`Chain id for "${chainId}" not found. Allowed values ${supportedChains.map((chain) => chain.id)}`
`Chain id for "${chainId}" not found. Allowed values ${Object.values(supportedChainsMapping).map((chain) => chain.id)}`
);
}

Expand Down
Loading