@lombard.finance/sdk 2.5.2 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +483 -396
- package/dist/ccip.cjs +2 -0
- package/dist/ccip.cjs.map +1 -0
- package/dist/ccip.js +147 -0
- package/dist/ccip.js.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +79 -8269
- package/dist/index.js.map +1 -1
- package/dist/index2.cjs +63 -0
- package/dist/index2.cjs.map +1 -0
- package/dist/index2.js +17989 -0
- package/dist/index2.js.map +1 -0
- package/package.json +10 -10
- package/src/{sdk → api-functions}/generateDepositBtcAddress/generateDepositBtcAddress.stories.tsx +15 -14
- package/src/{sdk → api-functions}/generateDepositBtcAddress/generateDepositBtcAddress.ts +18 -8
- package/src/{sdk → api-functions}/getDepositBtcAddress/getDepositBtcAddress.stories.tsx +23 -14
- package/src/{sdk → api-functions}/getDepositBtcAddress/getDepositBtcAddress.ts +16 -7
- package/src/{sdk → api-functions}/getDepositBtcAddress/getDepositBtcAddresses.stories.tsx +15 -14
- package/src/{sdk → api-functions}/getDepositsByAddress/getDepositsByAddress.stories.tsx +13 -12
- package/src/{sdk → api-functions}/getDepositsByAddress/getDepositsByAddress.ts +9 -9
- package/src/{sdk → api-functions}/getLBTCExchangeRate/getLBTCExchangeRate.stories.tsx +16 -10
- package/src/api-functions/getLBTCExchangeRate/getLBTCExchangeRate.ts +71 -0
- package/src/api-functions/getNetworkFeeSignature/getNetworkFeeSignature.stories.tsx +56 -0
- package/src/{sdk → api-functions}/getNetworkFeeSignature/getNetworkFeeSignature.ts +11 -5
- package/src/{sdk → api-functions}/getPointsByAddress/getPointsByAddress.stories.tsx +14 -13
- package/src/{sdk → api-functions}/getPointsByAddress/getPointsByAddress.ts +7 -4
- package/src/{sdk → api-functions}/getUnstakesByAddress/getUnstakesByAddress.stories.tsx +14 -13
- package/src/{sdk → api-functions}/getUnstakesByAddress/getUnstakesByAddress.ts +15 -10
- package/src/api-functions/getUserStakeAndBakeSignature/getUserStakeAndBakeSignature.stories.tsx +69 -0
- package/src/{sdk → api-functions}/getUserStakeAndBakeSignature/getUserStakeAndBakeSignature.ts +10 -6
- package/src/{sdk → api-functions}/index.ts +0 -3
- package/src/{sdk → api-functions}/setReferral/setReferral.ts +3 -3
- package/src/api-functions/storeNetworkFeeSignature/storeNetworkFeeSignature.stories.tsx +56 -0
- package/src/{sdk → api-functions}/storeNetworkFeeSignature/storeNetworkFeeSignature.ts +9 -7
- package/src/api-functions/storeStakeAndBakeSignature/storeStakeAndBakeSignature.stories.tsx +56 -0
- package/src/{sdk → api-functions}/storeStakeAndBakeSignature/storeStakeAndBakeSignature.ts +8 -6
- package/src/bridge/abi/CCIP_BRIDGE_ADAPTER_ABI.json +704 -0
- package/src/bridge/abi/OFT_BRIDGE_ADAPTER_ABI.json +912 -0
- package/src/bridge/index.ts +11 -0
- package/src/bridge/lib/bridge.stories.tsx +89 -0
- package/src/bridge/lib/bridge.ts +101 -0
- package/src/bridge/lib/ccip-bridge.stories.tsx +90 -0
- package/src/bridge/lib/ccip-bridge.ts +163 -0
- package/src/bridge/lib/config.ts +338 -0
- package/src/bridge/lib/oft-bridge.stories.tsx +89 -0
- package/src/bridge/lib/oft-bridge.ts +212 -0
- package/src/clients/public-client.ts +32 -0
- package/src/clients/rpc-url-config.ts +20 -0
- package/src/clients/wallet-client.ts +33 -0
- package/src/{sdk/apiConfig.ts → common/api-config.ts} +9 -3
- package/src/common/blockchain-identifier.ts +134 -0
- package/src/common/chains.ts +84 -0
- package/src/common/contract-info.ts +8 -0
- package/src/common/parameters.ts +51 -0
- package/src/contract-functions/approveLBTC/approveLBTC.stories.tsx +76 -0
- package/src/contract-functions/approveLBTC/approveLBTC.ts +65 -0
- package/src/{web3Sdk → contract-functions}/claimLBTC/claimLBTC.stories.tsx +22 -30
- package/src/contract-functions/claimLBTC/claimLBTC.ts +90 -0
- package/src/contract-functions/getBasculeDepositStatus/getBasculeDepositStatus.stories.tsx +50 -0
- package/src/contract-functions/getBasculeDepositStatus/getBasculeDepositStatus.ts +127 -0
- package/src/{web3Sdk → contract-functions}/getLBTCMintingFee/getLBTCMintingFee.stories.tsx +15 -11
- package/src/contract-functions/getLBTCMintingFee/getLBTCMintingFee.tsx +37 -0
- package/src/{vaults/lib/get-vault-points.stories.tsx → contract-functions/getLBTCTotalSupply/getLBTCTotalSupply.stories.tsx} +13 -16
- package/src/contract-functions/getLBTCTotalSupply/getLBTCTotalSupply.ts +34 -0
- package/src/{web3Sdk → contract-functions}/getPermitNonce/getPermitNonce.stories.tsx +12 -22
- package/src/contract-functions/getPermitNonce/getPermitNonce.ts +45 -0
- package/src/{web3Sdk → contract-functions}/getShareValue/getShareValue.stories.tsx +12 -7
- package/src/contract-functions/getShareValue/getShareValue.ts +58 -0
- package/src/{web3Sdk → contract-functions}/getSharesByAddress/getSharesByAddress.stories.tsx +14 -9
- package/src/{web3Sdk → contract-functions}/getSharesByAddress/getSharesByAddress.ts +29 -28
- package/src/{web3Sdk → contract-functions}/getStakeAndBakeFee/getStakeAndBakeFee.stories.tsx +29 -16
- package/src/contract-functions/getStakeAndBakeFee/getStakeAndBakeFee.tsx +65 -0
- package/src/{web3Sdk → contract-functions}/index.ts +8 -7
- package/src/contract-functions/signLbtcDestionationAddr/signLbtcDestinationAddr.ts +32 -0
- package/src/contract-functions/signLbtcDestionationAddr/signLbtcDestionationAddr.stories.tsx +63 -0
- package/src/contract-functions/signNetworkFee/signNetworkFee.stories.tsx +91 -0
- package/src/contract-functions/signNetworkFee/signNetworkFee.ts +99 -0
- package/src/contract-functions/signStakeAndBake/index.ts +1 -0
- package/src/{web3Sdk → contract-functions}/signStakeAndBake/signStakeAndBake.stories.tsx +29 -41
- package/src/contract-functions/signStakeAndBake/signStakeAndBake.ts +145 -0
- package/src/contract-functions/unstakeLBTC/unstakeLBTC.stories.tsx +80 -0
- package/src/contract-functions/unstakeLBTC/unstakeLBTC.ts +66 -0
- package/src/index.ts +40 -17
- package/src/metrics/get-lbtc-stats.stories.tsx +51 -0
- package/src/metrics/get-lbtc-stats.ts +38 -0
- package/src/rewards/index.ts +26 -0
- package/src/rewards/lib/claim-reward.stories.tsx +82 -0
- package/src/rewards/lib/claim-reward.ts +84 -0
- package/src/rewards/lib/get-reward-balances.stories.tsx +64 -0
- package/src/rewards/lib/get-reward-balances.ts +81 -0
- package/src/rewards/lib/get-reward-signing-data.stories.tsx +76 -0
- package/src/rewards/lib/get-reward-signing-data.ts +52 -0
- package/src/rewards/lib/get-reward-withdrawal-fee.stories.tsx +61 -0
- package/src/rewards/lib/get-reward-withdrawal-fee.ts +37 -0
- package/src/rewards/lib/get-reward-withdrawals.stories.tsx +60 -0
- package/src/rewards/lib/get-reward-withdrawals.ts +92 -0
- package/src/rewards/lib/reward-tokens.ts +7 -0
- package/src/stories/components/Button/Button.css +10 -0
- package/src/stories/components/Button/Button.tsx +15 -4
- package/src/stories/components/CodeBlock/CodeBlock.tsx +1 -0
- package/src/stories/components/ConnectButton/connect-button.tsx +100 -0
- package/src/stories/components/ConnectButton/index.ts +1 -0
- package/src/stories/components/decorators/function-type.tsx +63 -0
- package/src/stories/components/decorators/index.ts +2 -0
- package/src/stories/components/decorators/wagmi-decorator.tsx +79 -0
- package/src/stories/components/error-block.tsx +21 -0
- package/src/stories/constants.ts +3 -0
- package/src/stories/hooks/useConnection.ts +72 -0
- package/src/stories/hooks/useQuery.ts +2 -2
- package/src/{web3Sdk/abi/LBTC.json → tokens/abi/LBTC_ABI.json} +1 -1
- package/src/tokens/abi/LBTC_ABI.ts +1761 -0
- package/src/tokens/abi/LBTC_BASCULE_ABI.json +850 -0
- package/src/tokens/lbtc-addresses.ts +55 -0
- package/src/tokens/token-addresses.ts +32 -0
- package/src/tokens/tokens.ts +120 -0
- package/src/utils/env.ts +12 -0
- package/src/utils/hex.ts +2 -2
- package/src/utils/numbers.ts +8 -0
- package/src/{common/utils/convertSatoshi.ts → utils/satoshi.ts} +3 -3
- package/src/utils/time.ts +12 -0
- package/src/vaults/abi/VEDA_VAULT_BASE_ASSET_ABI.json +296 -0
- package/src/vaults/abi/VEDA_VAULT_BORING_WITHDRAW_QUEUE_ABI.json +502 -0
- package/src/vaults/abi/VEDA_VAULT_SPENDER_ABI.json +408 -0
- package/src/vaults/abi/VEDA_VAULT_TELLER_ABI.json +700 -0
- package/src/vaults/index.ts +36 -69
- package/src/vaults/lib/config.ts +196 -0
- package/src/vaults/lib/metrics/get-vault-apy.stories.tsx +57 -0
- package/src/vaults/lib/metrics/get-vault-apy.ts +132 -0
- package/src/vaults/lib/metrics/get-vault-points.stories.tsx +59 -0
- package/src/vaults/lib/{get-vault-points.ts → metrics/get-vault-points.ts} +14 -10
- package/src/vaults/lib/metrics/get-vault-tvl.stories.tsx +57 -0
- package/src/vaults/lib/metrics/get-vault-tvl.ts +119 -0
- package/src/vaults/lib/ops/cancel-withdraw.stories.tsx +79 -0
- package/src/vaults/lib/ops/deposit.stories.tsx +81 -0
- package/src/vaults/lib/ops/deposit.ts +151 -0
- package/src/vaults/lib/{get-vault-deposits.stories.tsx → ops/get-vault-deposits.stories.tsx} +20 -14
- package/src/vaults/lib/{get-vault-deposits.ts → ops/get-vault-deposits.ts} +45 -22
- package/src/vaults/lib/{get-vault-withdrawals.stories.tsx → ops/get-vault-withdrawals.stories.tsx} +20 -13
- package/src/vaults/lib/{get-vault-withdrawals.ts → ops/get-vault-withdrawals.ts} +72 -38
- package/src/vaults/lib/ops/withdraw.stories.tsx +81 -0
- package/src/vaults/lib/ops/withdraw.ts +224 -0
- package/src/common/const.ts +0 -5
- package/src/common/types/internalTypes.ts +0 -10
- package/src/common/types/types.ts +0 -57
- package/src/common/utils/isValidChain.ts +0 -5
- package/src/provider/Provider.ts +0 -171
- package/src/provider/ReadProvider.ts +0 -126
- package/src/provider/index.ts +0 -2
- package/src/provider/rpcUrlConfig.ts +0 -19
- package/src/provider/types.ts +0 -58
- package/src/provider/utils/getMaxPriorityFeePerGas.ts +0 -25
- package/src/sdk/getLBTCExchangeRate/getLBTCExchangeRate.ts +0 -65
- package/src/sdk/getNetworkFeeSignature/getNetworkFeeSignature.stories.tsx +0 -85
- package/src/sdk/getUserStakeAndBakeSignature/getUserStakeAndBakeSignature.stories.tsx +0 -86
- package/src/sdk/internalTypes.ts +0 -18
- package/src/sdk/storeNetworkFeeSignature/storeNetworkFeeSignature.stories.tsx +0 -86
- package/src/sdk/storeStakeAndBakeSignature/storeStakeAndBakeSignature.stories.tsx +0 -116
- package/src/sdk/utils/getChainIdByName.ts +0 -48
- package/src/sdk/utils/getChainNameById.ts +0 -41
- package/src/stories/const.ts +0 -1
- package/src/stories/hooks/useConnect.ts +0 -47
- package/src/stories/utils/connectInjectedWallet.ts +0 -12
- package/src/stories/utils/fromCamelCase.ts +0 -16
- package/src/stories/utils/getMetaTitle.ts +0 -7
- package/src/stories/utils/getWalletInfo.ts +0 -31
- package/src/web3Sdk/abi/BASCULE.json +0 -850
- package/src/web3Sdk/abi/IERC20.json +0 -222
- package/src/web3Sdk/abi/STAKE_AND_BAKE.json +0 -15
- package/src/web3Sdk/abi/index.ts +0 -6
- package/src/web3Sdk/approveLBTC/approveLBTC.stories.tsx +0 -77
- package/src/web3Sdk/approveLBTC/approveLBTC.ts +0 -48
- package/src/web3Sdk/claimLBTC/claimLBTC.ts +0 -86
- package/src/web3Sdk/const.ts +0 -2
- package/src/web3Sdk/getBasculeDepositStatus/getBasculeDepositStatus.stories.tsx +0 -89
- package/src/web3Sdk/getBasculeDepositStatus/getBasculeDepositStatus.ts +0 -75
- package/src/web3Sdk/getBasculeDepositStatus/utils/const.ts +0 -8
- package/src/web3Sdk/getBasculeDepositStatus/utils/throwBasculeDepositStatusError.ts +0 -21
- package/src/web3Sdk/getLBTCMintingFee/getLBTCMintingFee.tsx +0 -41
- package/src/web3Sdk/getLBTCTotalSupply/getLBTCTotalSupply.stories.tsx +0 -60
- package/src/web3Sdk/getLBTCTotalSupply/getLBTCTotalSupply.ts +0 -26
- package/src/web3Sdk/getPermitNonce/getPermitNonce.ts +0 -47
- package/src/web3Sdk/getShareValue/getShareValue.ts +0 -58
- package/src/web3Sdk/getStakeAndBakeFee/getStakeAndBakeFee.tsx +0 -48
- package/src/web3Sdk/lbtcAddressConfig.ts +0 -96
- package/src/web3Sdk/signLbtcDestionationAddr/signLbtcDestinationAddr.ts +0 -23
- package/src/web3Sdk/signLbtcDestionationAddr/signLbtcDestionationAddr.stories.tsx +0 -97
- package/src/web3Sdk/signNetworkFee/signNetworkFee.stories.tsx +0 -91
- package/src/web3Sdk/signNetworkFee/signNetworkFee.ts +0 -92
- package/src/web3Sdk/signStakeAndBake/contracts.ts +0 -80
- package/src/web3Sdk/signStakeAndBake/getTypedData.ts +0 -78
- package/src/web3Sdk/signStakeAndBake/index.ts +0 -3
- package/src/web3Sdk/signStakeAndBake/signStakeAndBake.ts +0 -105
- package/src/web3Sdk/signStakeAndBake/utils.ts +0 -23
- package/src/web3Sdk/types.ts +0 -16
- package/src/web3Sdk/unstakeLBTC/unstakeLBTC.stories.tsx +0 -77
- package/src/web3Sdk/unstakeLBTC/unstakeLBTC.ts +0 -51
- package/src/web3Sdk/utils/chainIdToEnv.ts +0 -12
- package/src/web3Sdk/utils/getBasculeTokenContract.ts +0 -21
- package/src/web3Sdk/utils/getGasMultiplier.ts +0 -20
- package/src/web3Sdk/utils/getLbtcTokenContract.ts +0 -36
- package/src/web3Sdk/utils/getRpcUrlConfigFromChain.ts +0 -34
- package/src/web3Sdk/utils/getTokenABI.ts +0 -12
- /package/src/{sdk → api-functions}/generateDepositBtcAddress/index.ts +0 -0
- /package/src/{sdk → api-functions}/getDepositBtcAddress/index.ts +0 -0
- /package/src/{sdk → api-functions}/getDepositsByAddress/index.ts +0 -0
- /package/src/{sdk → api-functions}/getLBTCExchangeRate/index.ts +0 -0
- /package/src/{sdk → api-functions}/getNetworkFeeSignature/index.ts +0 -0
- /package/src/{sdk → api-functions}/getPointsByAddress/index.ts +0 -0
- /package/src/{sdk → api-functions}/getUnstakesByAddress/index.ts +0 -0
- /package/src/{sdk → api-functions}/getUserStakeAndBakeSignature/index.ts +0 -0
- /package/src/{sdk → api-functions}/setReferral/index.ts +0 -0
- /package/src/{sdk → api-functions}/storeNetworkFeeSignature/index.ts +0 -0
- /package/src/{sdk → api-functions}/storeStakeAndBakeSignature/index.ts +0 -0
- /package/src/{sdk/const.ts → common/constants.ts} +0 -0
- /package/src/{web3Sdk → contract-functions}/approveLBTC/index.ts +0 -0
- /package/src/{web3Sdk → contract-functions}/claimLBTC/index.ts +0 -0
- /package/src/{web3Sdk → contract-functions}/getBasculeDepositStatus/index.ts +0 -0
- /package/src/{web3Sdk → contract-functions}/getLBTCMintingFee/index.ts +0 -0
- /package/src/{web3Sdk → contract-functions}/getLBTCTotalSupply/index.ts +0 -0
- /package/src/{web3Sdk → contract-functions}/getPermitNonce/index.ts +0 -0
- /package/src/{web3Sdk → contract-functions}/getShareValue/index.ts +0 -0
- /package/src/{web3Sdk → contract-functions}/getSharesByAddress/index.ts +0 -0
- /package/src/{web3Sdk → contract-functions}/getStakeAndBakeFee/index.ts +0 -0
- /package/src/{web3Sdk → contract-functions}/signLbtcDestionationAddr/index.ts +0 -0
- /package/src/{web3Sdk → contract-functions}/signNetworkFee/getTypedData.ts +0 -0
- /package/src/{web3Sdk → contract-functions}/signNetworkFee/index.ts +0 -0
- /package/src/{web3Sdk → contract-functions}/unstakeLBTC/index.ts +0 -0
- /package/src/{common/utils/getErrorMessage.ts → utils/err.ts} +0 -0
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** Generic bridge func */
|
|
2
|
+
export { bridge, type BridgeParameters } from './lib/bridge';
|
|
3
|
+
|
|
4
|
+
/** CCIP bridge func */
|
|
5
|
+
export { bridgeCCIP, type BridgeCCIPParameters } from './lib/ccip-bridge';
|
|
6
|
+
|
|
7
|
+
/** OFT bridge func */
|
|
8
|
+
export { bridgeOFT, type BridgeOFTParameters } from './lib/oft-bridge';
|
|
9
|
+
|
|
10
|
+
/** Utils */
|
|
11
|
+
export { getBridgeInfo } from './lib/config';
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from '@storybook/react';
|
|
2
|
+
import { Button } from '../../stories/components/Button';
|
|
3
|
+
import { CodeBlock } from '../../stories/components/CodeBlock';
|
|
4
|
+
import {
|
|
5
|
+
canPerformAction,
|
|
6
|
+
useConnection,
|
|
7
|
+
} from '../../stories/hooks/useConnection';
|
|
8
|
+
import useQuery from '../../stories/hooks/useQuery';
|
|
9
|
+
import { ConnectButton } from '../../stories/components/ConnectButton';
|
|
10
|
+
import {
|
|
11
|
+
functionType,
|
|
12
|
+
wagmiDecorator,
|
|
13
|
+
} from '../../stories/components/decorators';
|
|
14
|
+
import { ErrorBlock } from '../../stories/components/error-block';
|
|
15
|
+
import { ChainId } from '../../common/chains';
|
|
16
|
+
import { BRIDGE_CHAINS, OFT_BRIDGE_CHAINS } from './config';
|
|
17
|
+
import { bridge, BridgeParameters } from './bridge';
|
|
18
|
+
|
|
19
|
+
const meta = {
|
|
20
|
+
title: 'bridge/bridge',
|
|
21
|
+
component: StoryView,
|
|
22
|
+
tags: ['autodocs'],
|
|
23
|
+
decorators: [wagmiDecorator, functionType('write')],
|
|
24
|
+
} satisfies Meta<typeof StoryView>;
|
|
25
|
+
|
|
26
|
+
export default meta;
|
|
27
|
+
|
|
28
|
+
type Story = StoryObj<typeof meta>;
|
|
29
|
+
|
|
30
|
+
export const WithParams: Story = {
|
|
31
|
+
args: {
|
|
32
|
+
to: ChainId.sonic,
|
|
33
|
+
amount: '0.0001',
|
|
34
|
+
approve: true,
|
|
35
|
+
env: 'prod',
|
|
36
|
+
},
|
|
37
|
+
argTypes: {
|
|
38
|
+
to: {
|
|
39
|
+
mapping: ChainId,
|
|
40
|
+
options: BRIDGE_CHAINS.map(
|
|
41
|
+
ch => Object.entries(ChainId).find(([k, v]) => v === ch)?.[0],
|
|
42
|
+
),
|
|
43
|
+
control: { type: 'select' },
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
type Props = Omit<BridgeParameters, 'account' | 'chainId' | 'provider'>;
|
|
49
|
+
|
|
50
|
+
export function StoryView(props: Props) {
|
|
51
|
+
const connection = useConnection();
|
|
52
|
+
|
|
53
|
+
const request = async () => {
|
|
54
|
+
if (!canPerformAction(connection)) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return bridge({
|
|
59
|
+
...props,
|
|
60
|
+
|
|
61
|
+
account: connection.account.address,
|
|
62
|
+
chainId: connection.account.chainId,
|
|
63
|
+
provider: connection.provider,
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const { data, error, isLoading, refetch } = useQuery(request, [], false);
|
|
68
|
+
|
|
69
|
+
return (
|
|
70
|
+
<>
|
|
71
|
+
<p>This method bridges funds between chains.</p>
|
|
72
|
+
|
|
73
|
+
<div className="mb-4">
|
|
74
|
+
<ConnectButton />
|
|
75
|
+
</div>
|
|
76
|
+
|
|
77
|
+
<Button
|
|
78
|
+
onClick={refetch}
|
|
79
|
+
disabled={isLoading || !canPerformAction(connection)}
|
|
80
|
+
isLoading={isLoading}
|
|
81
|
+
actionName={bridge.name}
|
|
82
|
+
/>
|
|
83
|
+
|
|
84
|
+
<ErrorBlock>{error}</ErrorBlock>
|
|
85
|
+
|
|
86
|
+
<CodeBlock text={data} />
|
|
87
|
+
</>
|
|
88
|
+
);
|
|
89
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import BigNumber from 'bignumber.js';
|
|
2
|
+
import { bridgeCCIP } from './ccip-bridge';
|
|
3
|
+
import {
|
|
4
|
+
BRIDGE_CHAINS,
|
|
5
|
+
BRIDGE_EXPLORER_URL_MAP,
|
|
6
|
+
BridgeChain,
|
|
7
|
+
BridgeType,
|
|
8
|
+
CCIPBridgeChain,
|
|
9
|
+
getBridgeInfo,
|
|
10
|
+
OFTBridgeChain,
|
|
11
|
+
} from './config';
|
|
12
|
+
import { bridgeOFT } from './oft-bridge';
|
|
13
|
+
import { Address } from 'viem';
|
|
14
|
+
import { CommonWriteParameters } from '../../common/parameters';
|
|
15
|
+
|
|
16
|
+
export type BridgeParameters = {
|
|
17
|
+
/** The destination chain id. */
|
|
18
|
+
to: BridgeChain;
|
|
19
|
+
/** The LBTC amount. */
|
|
20
|
+
amount: BigNumber.Value;
|
|
21
|
+
/**
|
|
22
|
+
* A flag determining whether the amount should be approved within
|
|
23
|
+
* the execution of this function. If set to `false` it will
|
|
24
|
+
* throw an error when the deposit amount exceeds allowance.
|
|
25
|
+
*/
|
|
26
|
+
approve?: boolean;
|
|
27
|
+
/** The destination address. If omitted the same as the account address. */
|
|
28
|
+
recipient?: Address;
|
|
29
|
+
} & CommonWriteParameters;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Bridges funds (`amount`) from the connected chain (`chainId`) to the provided
|
|
33
|
+
* destination chain (`to`)
|
|
34
|
+
*/
|
|
35
|
+
export async function bridge({
|
|
36
|
+
to,
|
|
37
|
+
amount,
|
|
38
|
+
approve,
|
|
39
|
+
account,
|
|
40
|
+
chainId,
|
|
41
|
+
provider,
|
|
42
|
+
recipient,
|
|
43
|
+
rpcUrl,
|
|
44
|
+
env,
|
|
45
|
+
}: BridgeParameters) {
|
|
46
|
+
const bridgeInfo = getBridgeInfo(chainId as BridgeChain, to);
|
|
47
|
+
|
|
48
|
+
if (!bridgeInfo) {
|
|
49
|
+
throw new Error(
|
|
50
|
+
`Unsupported bridge from ${chainId} to ${to}. Please switch to the supported chains: ${BRIDGE_CHAINS.join(', ')}`,
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
switch (bridgeInfo.type) {
|
|
55
|
+
case BridgeType.CCIP: {
|
|
56
|
+
const txHash = await bridgeCCIP({
|
|
57
|
+
to: to as CCIPBridgeChain,
|
|
58
|
+
amount,
|
|
59
|
+
approve,
|
|
60
|
+
recipient,
|
|
61
|
+
account,
|
|
62
|
+
chainId,
|
|
63
|
+
provider,
|
|
64
|
+
env,
|
|
65
|
+
rpcUrl,
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
return {
|
|
69
|
+
txHash,
|
|
70
|
+
explorerUrl: BRIDGE_EXPLORER_URL_MAP[BridgeType.CCIP].replace(
|
|
71
|
+
'{txHash}',
|
|
72
|
+
txHash,
|
|
73
|
+
),
|
|
74
|
+
type: BridgeType.CCIP,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
case BridgeType.OFT: {
|
|
79
|
+
const txHash = await bridgeOFT({
|
|
80
|
+
to: to as OFTBridgeChain,
|
|
81
|
+
amount,
|
|
82
|
+
approve,
|
|
83
|
+
recipient,
|
|
84
|
+
account,
|
|
85
|
+
chainId,
|
|
86
|
+
provider,
|
|
87
|
+
env,
|
|
88
|
+
rpcUrl,
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
return {
|
|
92
|
+
txHash,
|
|
93
|
+
explorerUrl: BRIDGE_EXPLORER_URL_MAP[BridgeType.OFT].replace(
|
|
94
|
+
'{txHash}',
|
|
95
|
+
txHash,
|
|
96
|
+
),
|
|
97
|
+
type: BridgeType.OFT,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from '@storybook/react';
|
|
2
|
+
import { Button } from '../../stories/components/Button';
|
|
3
|
+
import { CodeBlock } from '../../stories/components/CodeBlock';
|
|
4
|
+
import {
|
|
5
|
+
canPerformAction,
|
|
6
|
+
useConnection,
|
|
7
|
+
} from '../../stories/hooks/useConnection';
|
|
8
|
+
import useQuery from '../../stories/hooks/useQuery';
|
|
9
|
+
import { ConnectButton } from '../../stories/components/ConnectButton';
|
|
10
|
+
import {
|
|
11
|
+
functionType,
|
|
12
|
+
wagmiDecorator,
|
|
13
|
+
} from '../../stories/components/decorators';
|
|
14
|
+
import { ErrorBlock } from '../../stories/components/error-block';
|
|
15
|
+
import { bridgeCCIP, BridgeCCIPParameters } from './ccip-bridge';
|
|
16
|
+
import { ChainId } from '../../common/chains';
|
|
17
|
+
import { CCIP_BRIDGE_CHAINS } from './config';
|
|
18
|
+
|
|
19
|
+
const meta = {
|
|
20
|
+
title: 'bridge/bridgeCCIP',
|
|
21
|
+
component: StoryView,
|
|
22
|
+
tags: ['autodocs'],
|
|
23
|
+
decorators: [wagmiDecorator, functionType('write')],
|
|
24
|
+
} satisfies Meta<typeof StoryView>;
|
|
25
|
+
|
|
26
|
+
export default meta;
|
|
27
|
+
|
|
28
|
+
type Story = StoryObj<typeof meta>;
|
|
29
|
+
|
|
30
|
+
export const WithParams: Story = {
|
|
31
|
+
args: {
|
|
32
|
+
to: ChainId.holesky,
|
|
33
|
+
amount: '0.0001',
|
|
34
|
+
approve: true,
|
|
35
|
+
env: 'stage',
|
|
36
|
+
},
|
|
37
|
+
argTypes: {
|
|
38
|
+
to: {
|
|
39
|
+
mapping: ChainId,
|
|
40
|
+
options: CCIP_BRIDGE_CHAINS.map(
|
|
41
|
+
ch => Object.entries(ChainId).find(([k, v]) => v === ch)?.[0],
|
|
42
|
+
),
|
|
43
|
+
defaultValue: 'holesky',
|
|
44
|
+
control: { type: 'select' },
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
type Props = Omit<BridgeCCIPParameters, 'account' | 'chainId' | 'provider'>;
|
|
50
|
+
|
|
51
|
+
export function StoryView(props: Props) {
|
|
52
|
+
const connection = useConnection();
|
|
53
|
+
|
|
54
|
+
const request = async () => {
|
|
55
|
+
if (!canPerformAction(connection)) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return bridgeCCIP({
|
|
60
|
+
...props,
|
|
61
|
+
|
|
62
|
+
account: connection.account.address,
|
|
63
|
+
chainId: connection.account.chainId,
|
|
64
|
+
provider: connection.provider,
|
|
65
|
+
});
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const { data, error, isLoading, refetch } = useQuery(request, [], false);
|
|
69
|
+
|
|
70
|
+
return (
|
|
71
|
+
<>
|
|
72
|
+
<p>This method bridges funds between chains.</p>
|
|
73
|
+
|
|
74
|
+
<div className="mb-4">
|
|
75
|
+
<ConnectButton />
|
|
76
|
+
</div>
|
|
77
|
+
|
|
78
|
+
<Button
|
|
79
|
+
onClick={refetch}
|
|
80
|
+
disabled={isLoading || !canPerformAction(connection)}
|
|
81
|
+
isLoading={isLoading}
|
|
82
|
+
actionName={bridgeCCIP.name}
|
|
83
|
+
/>
|
|
84
|
+
|
|
85
|
+
<ErrorBlock>{error}</ErrorBlock>
|
|
86
|
+
|
|
87
|
+
<CodeBlock text={data} />
|
|
88
|
+
</>
|
|
89
|
+
);
|
|
90
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import BigNumber from 'bignumber.js';
|
|
2
|
+
import { CommonWriteParameters } from '../../common/parameters';
|
|
3
|
+
import {
|
|
4
|
+
BridgeType,
|
|
5
|
+
CCIP_BRIDGE_CHAINS,
|
|
6
|
+
CCIPBridgeChain,
|
|
7
|
+
getBridgeInfo,
|
|
8
|
+
MIN_BRIDGE_AMOUNT,
|
|
9
|
+
} from './config';
|
|
10
|
+
import { makePublicClient } from '../../clients/public-client';
|
|
11
|
+
import { makeWalletClient } from '../../clients/wallet-client';
|
|
12
|
+
import {
|
|
13
|
+
fromBaseDenomination,
|
|
14
|
+
getTokenInfo,
|
|
15
|
+
toBaseDenomination,
|
|
16
|
+
} from '../../tokens/tokens';
|
|
17
|
+
import { getErrorMessage } from '../../utils/err';
|
|
18
|
+
import toBigInt from '../../utils/numbers';
|
|
19
|
+
import { Address, pad, parseEther, toBytes, toHex } from 'viem';
|
|
20
|
+
import { approveLBTC } from '../../contract-functions';
|
|
21
|
+
import { Token } from '../../tokens/token-addresses';
|
|
22
|
+
|
|
23
|
+
export type BridgeCCIPParameters = {
|
|
24
|
+
/** The destination chain id. */
|
|
25
|
+
to: CCIPBridgeChain;
|
|
26
|
+
/** The LBTC amount. */
|
|
27
|
+
amount: BigNumber.Value;
|
|
28
|
+
/**
|
|
29
|
+
* A flag determining whether the amount should be approved within
|
|
30
|
+
* the execution of this function. If set to `false` it will
|
|
31
|
+
* throw an error when the deposit amount exceeds allowance.
|
|
32
|
+
*/
|
|
33
|
+
approve?: boolean;
|
|
34
|
+
/** The destination address. If omitted the same as the account address. */
|
|
35
|
+
recipient?: Address;
|
|
36
|
+
} & CommonWriteParameters;
|
|
37
|
+
export async function bridgeCCIP({
|
|
38
|
+
to,
|
|
39
|
+
amount: amountRaw,
|
|
40
|
+
approve,
|
|
41
|
+
recipient: optionalRecipient,
|
|
42
|
+
account,
|
|
43
|
+
chainId: from,
|
|
44
|
+
provider,
|
|
45
|
+
env,
|
|
46
|
+
rpcUrl,
|
|
47
|
+
}: BridgeCCIPParameters) {
|
|
48
|
+
const amount = BigNumber(amountRaw);
|
|
49
|
+
const recipient = optionalRecipient || account;
|
|
50
|
+
|
|
51
|
+
const bridgeInfo = getBridgeInfo(from as CCIPBridgeChain, to);
|
|
52
|
+
if (!bridgeInfo || bridgeInfo.type !== BridgeType.CCIP) {
|
|
53
|
+
throw new Error(
|
|
54
|
+
`Unsupported bridge from ${from} to ${to}. Please switch to the supported chains: ${CCIP_BRIDGE_CHAINS.join(', ')}`,
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const bridgeContract = bridgeInfo.contract;
|
|
59
|
+
|
|
60
|
+
const lbtcContract = await getTokenInfo(Token.LBTC, from, env, rpcUrl);
|
|
61
|
+
if (!lbtcContract) {
|
|
62
|
+
throw new Error('Could not retrieve LBTC contract info.');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const publicClient = makePublicClient({ chainId: from, rpcUrl });
|
|
66
|
+
const walletClient = makeWalletClient({ provider, chainId: from });
|
|
67
|
+
|
|
68
|
+
const amountBase = toBigInt(
|
|
69
|
+
toBaseDenomination(amount, lbtcContract.decimals),
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
if (amount.isLessThan(MIN_BRIDGE_AMOUNT)) {
|
|
73
|
+
throw new Error(
|
|
74
|
+
`The amount is smaller than the minimum amount allowed: ${MIN_BRIDGE_AMOUNT.toFixed()}`,
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const balanceRaw = await publicClient.readContract({
|
|
79
|
+
address: lbtcContract.address,
|
|
80
|
+
abi: lbtcContract.abi,
|
|
81
|
+
functionName: 'balanceOf',
|
|
82
|
+
args: [account],
|
|
83
|
+
});
|
|
84
|
+
const balance = fromBaseDenomination(
|
|
85
|
+
String(balanceRaw),
|
|
86
|
+
lbtcContract.decimals,
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
// check if amount exceeds balance
|
|
90
|
+
if (amount.isGreaterThan(balance)) {
|
|
91
|
+
throw new Error(
|
|
92
|
+
`The amount exceeds the account's balance. \nAmount: ${amount.toFixed()} \nBalance: ${balance.toFixed()}`,
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const allowanceRaw = await publicClient.readContract({
|
|
97
|
+
address: lbtcContract.address,
|
|
98
|
+
abi: lbtcContract.abi,
|
|
99
|
+
functionName: 'allowance',
|
|
100
|
+
args: [account, bridgeContract.address],
|
|
101
|
+
});
|
|
102
|
+
const allowance = fromBaseDenomination(
|
|
103
|
+
String(allowanceRaw),
|
|
104
|
+
lbtcContract.decimals,
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
// check if amount exceeds allowance
|
|
108
|
+
if (amount.isGreaterThan(allowance)) {
|
|
109
|
+
const exceededMessage = `The amount exceeds allowance. \nAmount: ${amount.toFixed()} \nAllowance: ${allowance.toFixed()}`;
|
|
110
|
+
if (!approve) {
|
|
111
|
+
throw new Error(exceededMessage);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// try to approve new amount
|
|
115
|
+
console.info(exceededMessage);
|
|
116
|
+
try {
|
|
117
|
+
const txHash = await approveLBTC({
|
|
118
|
+
account,
|
|
119
|
+
spender: bridgeContract.address,
|
|
120
|
+
amount,
|
|
121
|
+
chainId: from,
|
|
122
|
+
provider,
|
|
123
|
+
rpcUrl,
|
|
124
|
+
env,
|
|
125
|
+
});
|
|
126
|
+
console.info(`Approve tx hash: ${txHash}`);
|
|
127
|
+
console.info(`Approved ${amountBase} for ${bridgeContract.address}`);
|
|
128
|
+
} catch (err) {
|
|
129
|
+
const msg = getErrorMessage(err);
|
|
130
|
+
throw new Error(
|
|
131
|
+
`Could not approve ${amountBase} for ${bridgeContract.address}. \nReason: ${msg}`,
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const bridgeArgs = [
|
|
137
|
+
/** toChain - bytes32 */
|
|
138
|
+
toHex(to, { size: 32 }),
|
|
139
|
+
/** toAddress - bytes32 */
|
|
140
|
+
pad(recipient),
|
|
141
|
+
/** amount - uint64 */
|
|
142
|
+
amountBase,
|
|
143
|
+
];
|
|
144
|
+
|
|
145
|
+
const adapterFee = (await publicClient.readContract({
|
|
146
|
+
abi: bridgeContract.abi,
|
|
147
|
+
address: bridgeContract.address,
|
|
148
|
+
functionName: 'getAdapterFee',
|
|
149
|
+
args: bridgeArgs,
|
|
150
|
+
})) as bigint;
|
|
151
|
+
|
|
152
|
+
const { request } = await publicClient.simulateContract({
|
|
153
|
+
abi: bridgeContract.abi,
|
|
154
|
+
address: bridgeContract.address,
|
|
155
|
+
account,
|
|
156
|
+
functionName: 'deposit',
|
|
157
|
+
args: bridgeArgs,
|
|
158
|
+
value: adapterFee,
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
const txHash = await walletClient.writeContract(request);
|
|
162
|
+
return txHash;
|
|
163
|
+
}
|