Skip to content

Commit

Permalink
refactor: Refactor sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
wenty22 committed Nov 15, 2024
1 parent f85b474 commit d05066d
Show file tree
Hide file tree
Showing 24 changed files with 639 additions and 726 deletions.
87 changes: 50 additions & 37 deletions common/config/rush/pnpm-lock.yaml

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions packages/canonical-bridge-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
},
"peerDependencies": {
"axios": "^0",
"viem": "^2"
"viem": "^2",
"@solana/web3.js": "^1"
},
"devDependencies": {
"@solana/web3.js": "~1.95.4",
"@types/react": "^18",
"@types/react-dom": "^18",
"@vitejs/plugin-react": "^4.2.0",
Expand All @@ -43,6 +45,7 @@
"typescript": "^5",
"viem": "~2.21.14",
"vite": "^4.5.0",
"vite-plugin-dts": "^3.6.3"
"vite-plugin-dts": "^3.6.3",
"@types/node": "~22.9.0"
}
}
137 changes: 135 additions & 2 deletions packages/canonical-bridge-sdk/src/adapters/cBridge/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getContract, Hash, isAddress } from 'viem';
import { getContract, Hash, isAddress, parseUnits } from 'viem';
import { BaseAdapter } from '@/adapters/base';
import { IInitialOptions, ITransferTokenPair } from '@/adapters/base/types';
import {
Expand All @@ -18,7 +18,7 @@ import {
IGetCBridgeTransferParamsInput,
ISendCBridgeToken,
} from '@/adapters/cBridge/types';
import { BridgeType } from '@/aggregator/types';
import { BridgeType, IBridgeChain, IBridgeToken } from '@/aggregator/types';
import axios, { AxiosInstance } from 'axios';
import { CLIENT_TIME_OUT, env } from '@/constants';
import {
Expand All @@ -28,6 +28,7 @@ import {
PEGGED_TOKEN_BRIDGE,
PEGGED_TOKEN_BRIDGE_V2,
} from '@/adapters/cBridge/exports';
import { isNativeToken } from '@/shared/address';

export class CBridgeAdapter extends BaseAdapter<
ICBridgeTransferConfig,
Expand Down Expand Up @@ -392,6 +393,138 @@ export class CBridgeAdapter extends BaseAdapter<
};
}

public getTransactionParams({
userAddress,
fromChain,
toChain,
fromToken,
sendValue,
slippage,
}: {
userAddress: string;
fromChain: IBridgeChain;
toChain: IBridgeChain;
fromToken: IBridgeToken;
sendValue: string;
slippage: number;
}) {
const isPegged = !!fromToken.isPegged;

const bridgeAddress = (() => {
try {
if (
!fromChain ||
(isPegged && !fromToken?.cBridge?.peggedConfig) ||
(!isPegged && !fromChain?.cBridge?.raw)
) {
return null;
}
return this.getTransferAddress({
fromChainId: fromChain?.id as number,
isPegged,
peggedConfig: fromToken?.cBridge?.peggedConfig,
chainConfig: fromChain?.cBridge?.raw,
});
} catch (e: any) {
// eslint-disable-next-line no-console
console.log(e);
}
})();

// Mint/deposit or burn/withdraw
const transferType = (() => {
if (fromToken?.cBridge?.peggedConfig?.org_chain_id === fromChain?.id) {
return 'deposit';
}
if (fromToken?.cBridge?.peggedConfig?.pegged_chain_id === fromChain?.id) {
return 'withdraw';
}
return '';
})();

const argument = (() => {
if (
!sendValue ||
sendValue === '0' ||
!toChain ||
!fromToken ||
!userAddress
) {
return null;
}
const nonce = new Date().getTime();

let amount = 0n;
try {
if (fromToken.isPegged === false) {
amount = parseUnits(
sendValue,
fromToken?.cBridge?.raw?.token.decimal as number
); // Convert to big number
} else if (transferType === 'deposit') {
amount = parseUnits(
sendValue,
fromToken?.cBridge?.peggedConfig?.org_token.token.decimal as number
);
} else if (transferType === 'withdraw') {
amount = parseUnits(
sendValue,
fromToken?.cBridge?.peggedConfig?.pegged_token.token
.decimal as number
);
}
} catch (e: any) {
// eslint-disable-next-line no-console
console.log(e);
}

return this.getTransferParams({
amount,
isPegged,
toChainId: toChain.id,
tokenAddress: fromToken?.address as `0x${string}`,
address: userAddress as `0x${string}`,
maxSlippage: slippage,
transferType: transferType ? transferType : undefined,
peggedConfig: fromToken.cBridge?.peggedConfig,
isNativeToken: isNativeToken(fromToken.address),
nonce,
});
})();

// Arguments for bridge smart contract
const args = (() => {
const peggedConfig = fromToken?.cBridge?.peggedConfig;
if (
!argument ||
(isPegged && !transferType) ||
!userAddress ||
!bridgeAddress ||
!fromToken
) {
return null;
}
const abi = this.getABI({
isPegged,
transferType: transferType || undefined,
peggedConfig,
});
const functionName = this.getTransferFunction({
isPegged,
isNativeToken: isNativeToken(fromToken?.address),
transferType: transferType || undefined,
});
return {
address: bridgeAddress as `0x${string}`,
abi: abi,
functionName: functionName,
account: userAddress as `0x${string}`,
args: argument,
};
})();
return { args, bridgeAddress };
}

public init(initialOptions?: IInitialOptions) {
this.initOptions(initialOptions);

Expand Down
20 changes: 10 additions & 10 deletions packages/canonical-bridge-sdk/src/adapters/cBridge/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,6 @@ export interface ICBridgeTransactionResponse {
error: null | unknown;
}

export interface ICBridgeEstimateAmountRequest {
src_chain_id: number;
dst_chain_id: number;
token_symbol: string;
amt: string;
user_addr?: string;
slippage_tolerance: number;
is_pegged?: boolean;
}

export interface ICBridgeEstimateAmountResponse {
err: object;
eq_value_token_amt: string;
Expand Down Expand Up @@ -222,3 +212,13 @@ export interface ICBridgeMaxMinSendAmt {
max: string;
min: string;
}

export interface ICBridgeEstimateAmountRequest {
src_chain_id: number;
dst_chain_id: number;
token_symbol: string;
amt: string;
user_addr?: string;
slippage_tolerance: number;
is_pegged?: boolean;
}
30 changes: 22 additions & 8 deletions packages/canonical-bridge-sdk/src/adapters/layerZero/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,22 @@ export class LayerZeroAdapter extends BaseAdapter<
walletClient,
gasAmount = 200000n,
version = 1,
airDropGas = 0n,
dstAddress = '0x',
}: ISendCakeTokenInput): Promise<Hash> {
try {
const address32Bytes = pad(userAddress, { size: 32 });
const adapterParams = encodePacked(
['uint16', 'uint256'],
[version, gasAmount]
);
/* version 1 - send token
* version 2 - send token and air drop native gas on destination chain
* https://docs.layerzero.network/v1/developers/evm/evm-guides/advanced/relayer-adapter-parameters#airdrop
*/
const adapterParams =
version === 1
? encodePacked(['uint16', 'uint256'], [version, gasAmount])
: encodePacked(
['uint16', 'uint', 'uint', 'address'],
[2, gasAmount, airDropGas, dstAddress]
);
const fees = await publicClient.readContract({
address: bridgeAddress,
abi: CAKE_PROXY_OFT_ABI,
Expand Down Expand Up @@ -117,13 +126,18 @@ export class LayerZeroAdapter extends BaseAdapter<
publicClient,
gasAmount = 200000n,
version = 1,
airDropGas = 0n,
dstAddress = '0x',
}: IGetEstimateFeeInput) {
try {
const address32Bytes = pad(userAddress, { size: 32 });
const adapterParams = encodePacked(
['uint16', 'uint256'],
[version, gasAmount]
);
const adapterParams =
version === 1
? encodePacked(['uint16', 'uint256'], [version, gasAmount])
: encodePacked(
['uint16', 'uint', 'uint', 'address'],
[2, gasAmount, airDropGas, dstAddress]
);
const fees = await publicClient.readContract({
address: bridgeAddress,
abi: CAKE_PROXY_OFT_ABI,
Expand Down
Loading

0 comments on commit d05066d

Please sign in to comment.