@lombard.finance/sdk 2.5.1 → 3.1.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.
Files changed (211) hide show
  1. package/README.md +428 -399
  2. package/dist/ccip.cjs +2 -0
  3. package/dist/ccip.cjs.map +1 -0
  4. package/dist/ccip.js +147 -0
  5. package/dist/ccip.js.map +1 -0
  6. package/dist/index.cjs +1 -1
  7. package/dist/index.cjs.map +1 -1
  8. package/dist/index.js +68 -8269
  9. package/dist/index.js.map +1 -1
  10. package/dist/index2.cjs +53 -0
  11. package/dist/index2.cjs.map +1 -0
  12. package/dist/index2.js +21672 -0
  13. package/dist/index2.js.map +1 -0
  14. package/package.json +5 -5
  15. package/src/{sdk → api-functions}/generateDepositBtcAddress/generateDepositBtcAddress.stories.tsx +15 -14
  16. package/src/{sdk → api-functions}/generateDepositBtcAddress/generateDepositBtcAddress.ts +18 -8
  17. package/src/{sdk → api-functions}/getDepositBtcAddress/getDepositBtcAddress.stories.tsx +15 -14
  18. package/src/{sdk → api-functions}/getDepositBtcAddress/getDepositBtcAddress.ts +16 -7
  19. package/src/{sdk → api-functions}/getDepositBtcAddress/getDepositBtcAddresses.stories.tsx +15 -14
  20. package/src/{sdk → api-functions}/getDepositsByAddress/getDepositsByAddress.stories.tsx +13 -12
  21. package/src/{sdk → api-functions}/getDepositsByAddress/getDepositsByAddress.ts +8 -8
  22. package/src/{sdk → api-functions}/getLBTCExchangeRate/getLBTCExchangeRate.stories.tsx +16 -10
  23. package/src/api-functions/getLBTCExchangeRate/getLBTCExchangeRate.ts +71 -0
  24. package/src/api-functions/getNetworkFeeSignature/getNetworkFeeSignature.stories.tsx +56 -0
  25. package/src/{sdk → api-functions}/getNetworkFeeSignature/getNetworkFeeSignature.ts +11 -5
  26. package/src/{sdk → api-functions}/getPointsByAddress/getPointsByAddress.stories.tsx +14 -13
  27. package/src/{sdk → api-functions}/getPointsByAddress/getPointsByAddress.ts +7 -4
  28. package/src/{sdk → api-functions}/getUnstakesByAddress/getUnstakesByAddress.stories.tsx +14 -13
  29. package/src/{sdk → api-functions}/getUnstakesByAddress/getUnstakesByAddress.ts +14 -9
  30. package/src/api-functions/getUserStakeAndBakeSignature/getUserStakeAndBakeSignature.stories.tsx +61 -0
  31. package/src/{sdk → api-functions}/getUserStakeAndBakeSignature/getUserStakeAndBakeSignature.ts +10 -6
  32. package/src/{sdk → api-functions}/index.ts +0 -3
  33. package/src/{sdk → api-functions}/setReferral/setReferral.ts +3 -3
  34. package/src/api-functions/storeNetworkFeeSignature/storeNetworkFeeSignature.stories.tsx +56 -0
  35. package/src/{sdk → api-functions}/storeNetworkFeeSignature/storeNetworkFeeSignature.ts +9 -7
  36. package/src/api-functions/storeStakeAndBakeSignature/storeStakeAndBakeSignature.stories.tsx +56 -0
  37. package/src/{sdk → api-functions}/storeStakeAndBakeSignature/storeStakeAndBakeSignature.ts +8 -6
  38. package/src/clients/public-client.ts +32 -0
  39. package/src/clients/rpc-url-config.ts +20 -0
  40. package/src/clients/wallet-client.ts +33 -0
  41. package/src/{sdk/apiConfig.ts → common/api-config.ts} +2 -2
  42. package/src/common/blockchain-identifier.ts +107 -0
  43. package/src/common/chains.ts +72 -0
  44. package/src/common/parameters.ts +51 -0
  45. package/src/contract-functions/approveLBTC/approveLBTC.stories.tsx +76 -0
  46. package/src/contract-functions/approveLBTC/approveLBTC.ts +64 -0
  47. package/src/{web3Sdk → contract-functions}/claimLBTC/claimLBTC.stories.tsx +22 -30
  48. package/src/contract-functions/claimLBTC/claimLBTC.ts +89 -0
  49. package/src/contract-functions/getBasculeDepositStatus/getBasculeDepositStatus.stories.tsx +50 -0
  50. package/src/contract-functions/getBasculeDepositStatus/getBasculeDepositStatus.ts +121 -0
  51. package/src/{web3Sdk → contract-functions}/getLBTCMintingFee/getLBTCMintingFee.stories.tsx +15 -11
  52. package/src/contract-functions/getLBTCMintingFee/getLBTCMintingFee.tsx +34 -0
  53. package/src/contract-functions/getLBTCTotalSupply/getLBTCTotalSupply.stories.tsx +49 -0
  54. package/src/contract-functions/getLBTCTotalSupply/getLBTCTotalSupply.ts +30 -0
  55. package/src/{web3Sdk → contract-functions}/getPermitNonce/getPermitNonce.stories.tsx +12 -22
  56. package/src/contract-functions/getPermitNonce/getPermitNonce.ts +39 -0
  57. package/src/{web3Sdk → contract-functions}/getShareValue/getShareValue.stories.tsx +11 -6
  58. package/src/contract-functions/getShareValue/getShareValue.ts +58 -0
  59. package/src/{web3Sdk → contract-functions}/getSharesByAddress/getSharesByAddress.stories.tsx +13 -8
  60. package/src/{web3Sdk → contract-functions}/getSharesByAddress/getSharesByAddress.ts +29 -28
  61. package/src/{web3Sdk → contract-functions}/getStakeAndBakeFee/getStakeAndBakeFee.stories.tsx +16 -16
  62. package/src/contract-functions/getStakeAndBakeFee/getStakeAndBakeFee.tsx +61 -0
  63. package/src/{web3Sdk → contract-functions}/index.ts +8 -7
  64. package/src/contract-functions/signLbtcDestionationAddr/signLbtcDestinationAddr.ts +32 -0
  65. package/src/contract-functions/signLbtcDestionationAddr/signLbtcDestionationAddr.stories.tsx +63 -0
  66. package/src/contract-functions/signNetworkFee/signNetworkFee.stories.tsx +91 -0
  67. package/src/contract-functions/signNetworkFee/signNetworkFee.ts +98 -0
  68. package/src/contract-functions/signStakeAndBake/index.ts +1 -0
  69. package/src/{web3Sdk → contract-functions}/signStakeAndBake/signStakeAndBake.stories.tsx +27 -40
  70. package/src/contract-functions/signStakeAndBake/signStakeAndBake.ts +140 -0
  71. package/src/contract-functions/unstakeLBTC/unstakeLBTC.stories.tsx +80 -0
  72. package/src/contract-functions/unstakeLBTC/unstakeLBTC.ts +65 -0
  73. package/src/index.ts +35 -10
  74. package/src/rewards/index.ts +26 -0
  75. package/src/rewards/lib/claim-reward.stories.tsx +82 -0
  76. package/src/rewards/lib/claim-reward.ts +84 -0
  77. package/src/rewards/lib/get-reward-balances.stories.tsx +64 -0
  78. package/src/rewards/lib/get-reward-balances.ts +81 -0
  79. package/src/rewards/lib/get-reward-signing-data.stories.tsx +76 -0
  80. package/src/rewards/lib/get-reward-signing-data.ts +52 -0
  81. package/src/rewards/lib/get-reward-withdrawal-fee.stories.tsx +61 -0
  82. package/src/rewards/lib/get-reward-withdrawal-fee.ts +37 -0
  83. package/src/rewards/lib/get-reward-withdrawals.stories.tsx +60 -0
  84. package/src/rewards/lib/get-reward-withdrawals.ts +92 -0
  85. package/src/rewards/lib/reward-tokens.ts +7 -0
  86. package/src/stories/components/Button/Button.css +10 -0
  87. package/src/stories/components/Button/Button.tsx +15 -4
  88. package/src/stories/components/CodeBlock/CodeBlock.tsx +1 -0
  89. package/src/stories/components/ConnectButton/connect-button.tsx +100 -0
  90. package/src/stories/components/ConnectButton/index.ts +1 -0
  91. package/src/stories/components/decorators/function-type.tsx +63 -0
  92. package/src/stories/components/decorators/index.ts +2 -0
  93. package/src/stories/components/decorators/wagmi-decorator.tsx +79 -0
  94. package/src/stories/components/error-block.tsx +21 -0
  95. package/src/stories/constants.ts +3 -0
  96. package/src/stories/hooks/useConnection.ts +72 -0
  97. package/src/stories/hooks/useQuery.ts +2 -2
  98. package/src/tokens/abi/LBTC_ABI.json +1761 -0
  99. package/src/tokens/abi/LBTC_BASCULE_ABI.json +850 -0
  100. package/src/tokens/lbtc-addresses.ts +54 -0
  101. package/src/tokens/lbtc-contract.ts +89 -0
  102. package/src/tokens/tokens.ts +111 -0
  103. package/src/utils/env.ts +12 -0
  104. package/src/utils/hex.ts +2 -2
  105. package/src/utils/numbers.ts +5 -0
  106. package/src/{common/utils/convertSatoshi.ts → utils/satoshi.ts} +3 -3
  107. package/src/utils/time.ts +12 -0
  108. package/src/vaults/abi/VEDA_VAULT_BASE_ASSET_ABI.json +296 -0
  109. package/src/vaults/abi/VEDA_VAULT_BORING_WITHDRAW_QUEUE_ABI.json +502 -0
  110. package/src/vaults/abi/VEDA_VAULT_SPENDER_ABI.json +408 -0
  111. package/src/vaults/abi/VEDA_VAULT_TELLER_ABI.json +700 -0
  112. package/src/vaults/index.ts +144 -36
  113. package/src/vaults/lib/cancel-withdraw.stories.tsx +79 -0
  114. package/src/vaults/lib/deposit.stories.tsx +81 -0
  115. package/src/vaults/lib/deposit.ts +151 -0
  116. package/src/vaults/lib/get-vault-deposits.stories.tsx +15 -9
  117. package/src/vaults/lib/get-vault-deposits.ts +45 -22
  118. package/src/vaults/lib/get-vault-points.stories.tsx +13 -6
  119. package/src/vaults/lib/get-vault-points.ts +14 -10
  120. package/src/vaults/lib/get-vault-withdrawals.stories.tsx +15 -8
  121. package/src/vaults/lib/get-vault-withdrawals.ts +72 -38
  122. package/src/vaults/lib/withdraw.stories.tsx +81 -0
  123. package/src/vaults/lib/withdraw.ts +224 -0
  124. package/src/common/const.ts +0 -5
  125. package/src/common/types/internalTypes.ts +0 -10
  126. package/src/common/types/types.ts +0 -57
  127. package/src/common/utils/isValidChain.ts +0 -5
  128. package/src/provider/Provider.ts +0 -171
  129. package/src/provider/ReadProvider.ts +0 -126
  130. package/src/provider/index.ts +0 -2
  131. package/src/provider/rpcUrlConfig.ts +0 -19
  132. package/src/provider/types.ts +0 -58
  133. package/src/provider/utils/getMaxPriorityFeePerGas.ts +0 -25
  134. package/src/sdk/getLBTCExchangeRate/getLBTCExchangeRate.ts +0 -65
  135. package/src/sdk/getNetworkFeeSignature/getNetworkFeeSignature.stories.tsx +0 -85
  136. package/src/sdk/getUserStakeAndBakeSignature/getUserStakeAndBakeSignature.stories.tsx +0 -86
  137. package/src/sdk/internalTypes.ts +0 -18
  138. package/src/sdk/storeNetworkFeeSignature/storeNetworkFeeSignature.stories.tsx +0 -86
  139. package/src/sdk/storeStakeAndBakeSignature/storeStakeAndBakeSignature.stories.tsx +0 -116
  140. package/src/sdk/utils/getChainIdByName.ts +0 -48
  141. package/src/sdk/utils/getChainNameById.ts +0 -38
  142. package/src/stories/const.ts +0 -1
  143. package/src/stories/hooks/useConnect.ts +0 -47
  144. package/src/stories/utils/connectInjectedWallet.ts +0 -12
  145. package/src/stories/utils/fromCamelCase.ts +0 -16
  146. package/src/stories/utils/getMetaTitle.ts +0 -7
  147. package/src/stories/utils/getWalletInfo.ts +0 -31
  148. package/src/web3Sdk/abi/BASCULE.json +0 -850
  149. package/src/web3Sdk/abi/IERC20.json +0 -222
  150. package/src/web3Sdk/abi/LBTC.json +0 -1761
  151. package/src/web3Sdk/abi/STAKE_AND_BAKE.json +0 -15
  152. package/src/web3Sdk/abi/index.ts +0 -6
  153. package/src/web3Sdk/approveLBTC/approveLBTC.stories.tsx +0 -77
  154. package/src/web3Sdk/approveLBTC/approveLBTC.ts +0 -48
  155. package/src/web3Sdk/claimLBTC/claimLBTC.ts +0 -86
  156. package/src/web3Sdk/const.ts +0 -2
  157. package/src/web3Sdk/getBasculeDepositStatus/getBasculeDepositStatus.stories.tsx +0 -89
  158. package/src/web3Sdk/getBasculeDepositStatus/getBasculeDepositStatus.ts +0 -75
  159. package/src/web3Sdk/getBasculeDepositStatus/utils/const.ts +0 -8
  160. package/src/web3Sdk/getBasculeDepositStatus/utils/throwBasculeDepositStatusError.ts +0 -21
  161. package/src/web3Sdk/getLBTCMintingFee/getLBTCMintingFee.tsx +0 -41
  162. package/src/web3Sdk/getLBTCTotalSupply/getLBTCTotalSupply.stories.tsx +0 -60
  163. package/src/web3Sdk/getLBTCTotalSupply/getLBTCTotalSupply.ts +0 -26
  164. package/src/web3Sdk/getPermitNonce/getPermitNonce.ts +0 -47
  165. package/src/web3Sdk/getShareValue/getShareValue.ts +0 -58
  166. package/src/web3Sdk/getStakeAndBakeFee/getStakeAndBakeFee.tsx +0 -48
  167. package/src/web3Sdk/lbtcAddressConfig.ts +0 -96
  168. package/src/web3Sdk/signLbtcDestionationAddr/signLbtcDestinationAddr.ts +0 -23
  169. package/src/web3Sdk/signLbtcDestionationAddr/signLbtcDestionationAddr.stories.tsx +0 -97
  170. package/src/web3Sdk/signNetworkFee/signNetworkFee.stories.tsx +0 -91
  171. package/src/web3Sdk/signNetworkFee/signNetworkFee.ts +0 -92
  172. package/src/web3Sdk/signStakeAndBake/contracts.ts +0 -80
  173. package/src/web3Sdk/signStakeAndBake/getTypedData.ts +0 -78
  174. package/src/web3Sdk/signStakeAndBake/index.ts +0 -3
  175. package/src/web3Sdk/signStakeAndBake/signStakeAndBake.ts +0 -105
  176. package/src/web3Sdk/signStakeAndBake/utils.ts +0 -23
  177. package/src/web3Sdk/types.ts +0 -16
  178. package/src/web3Sdk/unstakeLBTC/unstakeLBTC.stories.tsx +0 -77
  179. package/src/web3Sdk/unstakeLBTC/unstakeLBTC.ts +0 -51
  180. package/src/web3Sdk/utils/chainIdToEnv.ts +0 -12
  181. package/src/web3Sdk/utils/getBasculeTokenContract.ts +0 -21
  182. package/src/web3Sdk/utils/getGasMultiplier.ts +0 -20
  183. package/src/web3Sdk/utils/getLbtcTokenContract.ts +0 -36
  184. package/src/web3Sdk/utils/getRpcUrlConfigFromChain.ts +0 -34
  185. package/src/web3Sdk/utils/getTokenABI.ts +0 -12
  186. /package/src/{sdk → api-functions}/generateDepositBtcAddress/index.ts +0 -0
  187. /package/src/{sdk → api-functions}/getDepositBtcAddress/index.ts +0 -0
  188. /package/src/{sdk → api-functions}/getDepositsByAddress/index.ts +0 -0
  189. /package/src/{sdk → api-functions}/getLBTCExchangeRate/index.ts +0 -0
  190. /package/src/{sdk → api-functions}/getNetworkFeeSignature/index.ts +0 -0
  191. /package/src/{sdk → api-functions}/getPointsByAddress/index.ts +0 -0
  192. /package/src/{sdk → api-functions}/getUnstakesByAddress/index.ts +0 -0
  193. /package/src/{sdk → api-functions}/getUserStakeAndBakeSignature/index.ts +0 -0
  194. /package/src/{sdk → api-functions}/setReferral/index.ts +0 -0
  195. /package/src/{sdk → api-functions}/storeNetworkFeeSignature/index.ts +0 -0
  196. /package/src/{sdk → api-functions}/storeStakeAndBakeSignature/index.ts +0 -0
  197. /package/src/{sdk/const.ts → common/constants.ts} +0 -0
  198. /package/src/{web3Sdk → contract-functions}/approveLBTC/index.ts +0 -0
  199. /package/src/{web3Sdk → contract-functions}/claimLBTC/index.ts +0 -0
  200. /package/src/{web3Sdk → contract-functions}/getBasculeDepositStatus/index.ts +0 -0
  201. /package/src/{web3Sdk → contract-functions}/getLBTCMintingFee/index.ts +0 -0
  202. /package/src/{web3Sdk → contract-functions}/getLBTCTotalSupply/index.ts +0 -0
  203. /package/src/{web3Sdk → contract-functions}/getPermitNonce/index.ts +0 -0
  204. /package/src/{web3Sdk → contract-functions}/getShareValue/index.ts +0 -0
  205. /package/src/{web3Sdk → contract-functions}/getSharesByAddress/index.ts +0 -0
  206. /package/src/{web3Sdk → contract-functions}/getStakeAndBakeFee/index.ts +0 -0
  207. /package/src/{web3Sdk → contract-functions}/signLbtcDestionationAddr/index.ts +0 -0
  208. /package/src/{web3Sdk → contract-functions}/signNetworkFee/getTypedData.ts +0 -0
  209. /package/src/{web3Sdk → contract-functions}/signNetworkFee/index.ts +0 -0
  210. /package/src/{web3Sdk → contract-functions}/unstakeLBTC/index.ts +0 -0
  211. /package/src/{common/utils/getErrorMessage.ts → utils/err.ts} +0 -0
@@ -0,0 +1,140 @@
1
+ import { makeWalletClient } from '../../clients/wallet-client';
2
+ import { CommonWriteParameters } from '../../common/parameters';
3
+ import { isVedaVaultStakeAndBakeChain, Vault, VAULTS } from '../../vaults';
4
+ import { getLBTCContractInfo } from '../../tokens/lbtc-contract';
5
+ import { getPermitNonce } from '../getPermitNonce/getPermitNonce';
6
+ import BigNumber from 'bignumber.js';
7
+ import { DAY, now, toUnix } from '../../utils/time';
8
+
9
+ export interface ISignStakeAndBakeParams extends CommonWriteParameters {
10
+ /**
11
+ * The approved value that will be automatically claimed and deposited
12
+ * to the chosen vault.
13
+ */
14
+ value: BigNumber.Value;
15
+ /**
16
+ * The expiration UNIX time of the signature.
17
+ * Defaults to 24 hours from the time of signing.
18
+ */
19
+ expiry?: number;
20
+ /**
21
+ * The chosen DeFi vault to which the funds will be deposited.
22
+ */
23
+ vaultKey?: Vault;
24
+ }
25
+
26
+ export interface ISignStakeAndBakeResult {
27
+ /**
28
+ * The signature.
29
+ */
30
+ signature: string;
31
+ /**
32
+ * The typed data used to generate the signature.
33
+ */
34
+ typedData: string;
35
+ }
36
+
37
+ /**
38
+ * Signs the "stake and bake" signature that allows Lombard to claim specified
39
+ * amount of BTC (LBTC) and deposit that amount directly to the specified DeFi
40
+ * vault.
41
+ *
42
+ * In order for the "stake and bake" process to work a user has to store the
43
+ * signature to the Lombard's system, see: `storeStakeAndBakeSignature`
44
+ *
45
+ * @param {ISignStakeAndBakeParams} parameters - The parameters.
46
+ * @param {BigNumber.Value} parameters.value - The amount of BTC that's going to be claimed and deposited to the DeFi vault.
47
+ * @param {number} parameters.expiry = The optional expiration UNIX time of the signature.
48
+ * @param {Vault} parameters.vaultKey - The optional DeFi vault identifier.
49
+ * @param {Address} parameters.account - The EVM account address.
50
+ * @param {ChainId} parameters.chainId - The chain id.
51
+ * @param {EIP1193Provider} parameters.provider - The EIP1193 provider.
52
+ * @param {string} parameters.rpcUrl - The optional rpc url.
53
+ *
54
+ * @returns {Promise<ISignStakeAndBakeResult>} - The signature and typed data.
55
+ */
56
+ export async function signStakeAndBake({
57
+ account,
58
+ expiry = toUnix(now() + DAY),
59
+ value,
60
+ vaultKey = Vault.Veda,
61
+ chainId,
62
+ provider,
63
+ rpcUrl,
64
+ env,
65
+ }: ISignStakeAndBakeParams): Promise<ISignStakeAndBakeResult> {
66
+ const vault = VAULTS[vaultKey];
67
+ if (!vault) {
68
+ throw new Error(`Unknown vault key: ${vaultKey}`);
69
+ }
70
+
71
+ if (!isVedaVaultStakeAndBakeChain(chainId)) {
72
+ throw new Error(
73
+ `Unsupported chain id: ${chainId}. Please switch to one of the supported chains: ${vault.stakeAndBakeChains.join(', ')}`,
74
+ );
75
+ }
76
+
77
+ const lbtcContract = getLBTCContractInfo(chainId, env);
78
+ const walletClient = makeWalletClient({ chainId, provider });
79
+ const spenderContract = vault.spenderContracts[chainId];
80
+
81
+ const nonce = await getPermitNonce({
82
+ owner: account,
83
+ chainId,
84
+ rpcUrl,
85
+ });
86
+
87
+ const typedData: Parameters<typeof walletClient.signTypedData>[0] = {
88
+ account,
89
+ domain: {
90
+ name: 'Lombard Staked Bitcoin',
91
+ version: '1',
92
+ chainId: BigInt(chainId),
93
+ verifyingContract: lbtcContract.address,
94
+ },
95
+ types: {
96
+ EIP712Domain: [
97
+ {
98
+ name: 'name',
99
+ type: 'string',
100
+ },
101
+ {
102
+ name: 'version',
103
+ type: 'string',
104
+ },
105
+ {
106
+ name: 'chainId',
107
+ type: 'uint256',
108
+ },
109
+ {
110
+ name: 'verifyingContract',
111
+ type: 'address',
112
+ },
113
+ ],
114
+ Permit: [
115
+ { name: 'owner', type: 'address' },
116
+ { name: 'spender', type: 'address' },
117
+ { name: 'value', type: 'uint256' },
118
+ { name: 'nonce', type: 'uint256' },
119
+ { name: 'deadline', type: 'uint256' },
120
+ ],
121
+ },
122
+ primaryType: 'Permit',
123
+ message: {
124
+ owner: account,
125
+ spender: spenderContract.address,
126
+ value: BigInt(BigNumber(value).toFixed()),
127
+ nonce: BigInt(nonce),
128
+ deadline: BigInt(expiry),
129
+ },
130
+ };
131
+
132
+ const signature = await walletClient.signTypedData(typedData);
133
+
134
+ return {
135
+ signature,
136
+ typedData: JSON.stringify(typedData, (_, v) =>
137
+ typeof v === 'bigint' ? v.toString() : v,
138
+ ),
139
+ };
140
+ }
@@ -0,0 +1,80 @@
1
+ import type { Meta, StoryObj } from '@storybook/react';
2
+ import { DEFAULT_ENV } from '@lombard.finance/sdk-common';
3
+ import { Button } from '../../stories/components/Button';
4
+ import { CodeBlock } from '../../stories/components/CodeBlock';
5
+ import useQuery from '../../stories/hooks/useQuery';
6
+ import { IUnstakeLBTCParams, unstakeLBTC } from './unstakeLBTC';
7
+ import { ConnectButton } from '../../stories/components/ConnectButton';
8
+ import {
9
+ canPerformAction,
10
+ useConnection,
11
+ } from '../../stories/hooks/useConnection';
12
+ import {
13
+ functionType,
14
+ wagmiDecorator,
15
+ } from '../../stories/components/decorators';
16
+
17
+ const meta = {
18
+ title: 'write/unstakeLBTC',
19
+ component: StoryView,
20
+ tags: ['autodocs'],
21
+ decorators: [wagmiDecorator, functionType('write')],
22
+ } satisfies Meta<typeof StoryView>;
23
+
24
+ export default meta;
25
+
26
+ type Story = StoryObj<typeof meta>;
27
+
28
+ export const WithParams: Story = {
29
+ args: {
30
+ amount: 0.00001,
31
+ btcAddress: '',
32
+ env: DEFAULT_ENV,
33
+ },
34
+ };
35
+
36
+ type ClaimLBTCProps = Pick<IUnstakeLBTCParams, 'btcAddress' | 'env' | 'amount'>;
37
+
38
+ export function StoryView(props: ClaimLBTCProps) {
39
+ const connection = useConnection();
40
+
41
+ const request = async () => {
42
+ if (!canPerformAction(connection)) {
43
+ alert('Not connected');
44
+ return;
45
+ }
46
+
47
+ return unstakeLBTC({
48
+ amount: props.amount,
49
+ btcAddress: props.btcAddress,
50
+
51
+ account: connection.account.address,
52
+ chainId: connection.account.chainId,
53
+ provider: connection.provider,
54
+ });
55
+ };
56
+
57
+ const { data, error, isLoading, refetch } = useQuery(request, [], false);
58
+
59
+ return (
60
+ <>
61
+ <p>
62
+ This function unstakes the specified amount of LBTC and transfers the
63
+ equivalent amount of BTC to the provided BTC address.
64
+ </p>
65
+
66
+ <div className="mb-4">
67
+ <ConnectButton />
68
+ </div>
69
+
70
+ <Button
71
+ onClick={refetch}
72
+ disabled={isLoading || !connection.account.address}
73
+ isLoading={isLoading}
74
+ actionName={unstakeLBTC.name}
75
+ />
76
+
77
+ <CodeBlock text={error || data} />
78
+ </>
79
+ );
80
+ }
@@ -0,0 +1,65 @@
1
+ import { getOutputScript } from '@lombard.finance/sdk-common';
2
+ import { CommonWriteParameters } from '../../common/parameters';
3
+ import { toSatoshi } from '../../utils/satoshi';
4
+ import { makeWalletClient } from '../../clients/wallet-client';
5
+ import { makePublicClient } from '../../clients/public-client';
6
+ import { getLBTCContractInfo } from '../../tokens/lbtc-contract';
7
+ import { CHAIN_ID_TO_VIEM_CHAIN_MAP } from '../../common/chains';
8
+ import { Hex } from 'viem';
9
+ import BigNumber from 'bignumber.js';
10
+
11
+ /**
12
+ * The unstake parameters.
13
+ */
14
+ export interface IUnstakeLBTCParams extends CommonWriteParameters {
15
+ /**
16
+ * The BTC address to send the unstaked BTC to.
17
+ */
18
+ btcAddress: string;
19
+ /**
20
+ * The amount of LBTC to unstake.
21
+ */
22
+ amount: BigNumber.Value;
23
+ }
24
+
25
+ /**
26
+ * Unstakes (redeems) specified amount of LBTC and sends the equivalent amount
27
+ * of BTC to the provided BTC address.
28
+ * @param {IUnstakeLBTCParams} parameters - The unstake parameters.
29
+ * @param {string} parameters.btcAddress - The BTC address.
30
+ * @param {BigNumber.Value} parameters.amount - The amount of LBTC to unstake.
31
+ * @param {Address} parameters.account - The EVM address.
32
+ * @param {ChainId} parameters.chainId - The chain id.
33
+ * @param {EIP1193Provider} parameters.provider - The EIP1193 provider.
34
+ * @param {string} parameters.rpcUrl - The optional rpc url.
35
+ */
36
+ export async function unstakeLBTC({
37
+ btcAddress,
38
+ amount,
39
+ account,
40
+ chainId,
41
+ provider,
42
+ rpcUrl,
43
+ env,
44
+ }: IUnstakeLBTCParams): Promise<Hex> {
45
+ const outputScript = getOutputScript(btcAddress, env);
46
+ const amountSat = toSatoshi(amount).toNumber();
47
+
48
+ const lbtcContract = getLBTCContractInfo(chainId, env);
49
+
50
+ const publicClient = makePublicClient({ chainId, rpcUrl });
51
+ const walletClient = makeWalletClient({ provider, chainId });
52
+
53
+ const { request } = await publicClient.simulateContract({
54
+ address: lbtcContract.address,
55
+ account,
56
+ chain: CHAIN_ID_TO_VIEM_CHAIN_MAP[chainId],
57
+ abi: lbtcContract.abi,
58
+ functionName: 'redeem',
59
+ args: [outputScript, BigInt(amountSat)],
60
+ });
61
+
62
+ const txHash = await walletClient.writeContract(request);
63
+
64
+ return txHash;
65
+ }
package/src/index.ts CHANGED
@@ -1,23 +1,48 @@
1
- export * from "./sdk";
2
- export * from "./web3Sdk";
1
+ // API functions:
2
+ export * from './api-functions';
3
3
 
4
- export * from "./common/types/types";
5
- export * from "./common/utils/convertSatoshi";
6
- export * from "./common/utils/isValidChain";
7
- export type { Env as TEnv } from "@lombard.finance/sdk-common";
8
- export { Env as OEnv } from "@lombard.finance/sdk-common";
4
+ // READ and WRITE functions:
5
+ export * from './contract-functions';
6
+
7
+ // Vault:
8
+ export { Vault } from './vaults';
9
+ export {
10
+ deposit,
11
+ type DepositParameters,
12
+ } from './vaults/lib/deposit';
13
+
14
+ export {
15
+ queueWithdraw,
16
+ type QueueWithdrawParameters,
17
+ cancelWithdraw,
18
+ type CancelWithdrawParameters,
19
+ } from './vaults/lib/withdraw';
9
20
 
10
21
  export {
11
22
  getVaultDeposits,
12
23
  type GetVaultDepositsParameters,
13
- } from "./vaults/lib/get-vault-deposits";
24
+ } from './vaults/lib/get-vault-deposits';
14
25
 
15
26
  export {
16
27
  getVaultWithdrawals,
17
28
  type GetVaultWithdrawalsParameters,
18
- } from "./vaults/lib/get-vault-withdrawals";
29
+ } from './vaults/lib/get-vault-withdrawals';
19
30
 
20
31
  export {
21
32
  getVaultPoints,
22
33
  type GetVaultPointsParameters,
23
- } from "./vaults/lib/get-vault-points";
34
+ } from './vaults/lib/get-vault-points';
35
+
36
+ // Rewards:
37
+ export * from './rewards';
38
+
39
+ // Utils:
40
+ export * from './common/api-config';
41
+ export * from './common/blockchain-identifier';
42
+ export * from './common/chains';
43
+ export * from './utils/satoshi';
44
+ export * from './tokens/lbtc-addresses';
45
+
46
+ // Re-exports:
47
+ export type { Address, EIP1193Provider } from 'viem';
48
+ export { Env } from '@lombard.finance/sdk-common';
@@ -0,0 +1,26 @@
1
+ export { claimReward, type ClaimRewardParameters } from './lib/claim-reward';
2
+
3
+ export {
4
+ getRewardBalances,
5
+ type GetRewardBalancesParameters,
6
+ RewardBlockchainType,
7
+ } from './lib/get-reward-balances';
8
+
9
+ export {
10
+ getRewardSigningData,
11
+ type GetRewardSigningDataParameters,
12
+ } from './lib/get-reward-signing-data';
13
+
14
+ export {
15
+ getRewardWithdrawalFee,
16
+ type GetRewardWithdrawalFeeParameters,
17
+ } from './lib/get-reward-withdrawal-fee';
18
+
19
+ export {
20
+ getRewardWithdrawals,
21
+ type GetRewardsWithdrawalsParameters,
22
+ type RewardWithdrawal,
23
+ RewardWithdrawalStatus,
24
+ } from './lib/get-reward-withdrawals';
25
+
26
+ export { isRewardTokenSupported, RewardToken } from './lib/reward-tokens';
@@ -0,0 +1,82 @@
1
+ import type { Meta, StoryObj } from '@storybook/react';
2
+ import {
3
+ functionType,
4
+ wagmiDecorator,
5
+ } from '../../stories/components/decorators';
6
+ import { Button } from '../../stories/components/Button';
7
+ import { CodeBlock } from '../../stories/components/CodeBlock';
8
+ import { ErrorBlock } from '../../stories/components/error-block';
9
+ import useQuery from '../../stories/hooks/useQuery';
10
+ import { RewardToken } from './reward-tokens';
11
+ import { Env } from '@lombard.finance/sdk-common';
12
+ import { claimReward, ClaimRewardParameters } from './claim-reward';
13
+ import { ConnectButton } from '../../stories/components/ConnectButton';
14
+ import {
15
+ canPerformAction,
16
+ useConnection,
17
+ } from '../../stories/hooks/useConnection';
18
+ import { ChainId } from '../../common/chains';
19
+ import { EXAMPLE_BABYLON_ADDRESS } from '../../stories/constants';
20
+
21
+ const meta = {
22
+ title: 'rewards/claimReward',
23
+ component: StoryView,
24
+ tags: ['autodocs'],
25
+ decorators: [wagmiDecorator, functionType('write')],
26
+ } satisfies Meta<typeof StoryView>;
27
+
28
+ export default meta;
29
+
30
+ type Story = StoryObj<typeof meta>;
31
+
32
+ export const WithParams: Story = {
33
+ args: {
34
+ to: EXAMPLE_BABYLON_ADDRESS,
35
+ amount: '0.001',
36
+ rewardToken: RewardToken.BABY,
37
+ chainId: ChainId.ethereum,
38
+ env: Env.stage,
39
+ },
40
+ };
41
+
42
+ type Props = Omit<ClaimRewardParameters, 'account' | 'provider'>;
43
+
44
+ export function StoryView(props: Props) {
45
+ const connection = useConnection();
46
+
47
+ const request = async () => {
48
+ if (!canPerformAction(connection)) {
49
+ alert('Please connect');
50
+ return;
51
+ }
52
+
53
+ return claimReward({
54
+ account: connection.account.address,
55
+ provider: connection.provider,
56
+ ...props,
57
+ });
58
+ };
59
+
60
+ const { data, error, isLoading, refetch } = useQuery(request, [], false);
61
+
62
+ return (
63
+ <>
64
+ <p>This function claims a reward earned by a user.</p>
65
+
66
+ <div className="mb-4">
67
+ <ConnectButton />
68
+ </div>
69
+
70
+ <Button
71
+ onClick={refetch}
72
+ disabled={isLoading || !canPerformAction(connection)}
73
+ isLoading={isLoading}
74
+ actionName={claimReward.name}
75
+ />
76
+
77
+ <ErrorBlock>{error}</ErrorBlock>
78
+
79
+ <CodeBlock text={data} />
80
+ </>
81
+ );
82
+ }
@@ -0,0 +1,84 @@
1
+ import BigNumber from 'bignumber.js';
2
+ import { CommonWriteParameters } from '../../common/parameters';
3
+ import axios from 'axios';
4
+ import { getApiConfig } from '../../common/api-config';
5
+ import { makeWalletClient } from '../../clients/wallet-client';
6
+ import { getRewardWithdrawalFee } from './get-reward-withdrawal-fee';
7
+ import { getRewardSigningData } from './get-reward-signing-data';
8
+ import {
9
+ mapDataToRewardWithdrawal,
10
+ WithdrawalData,
11
+ } from './get-reward-withdrawals';
12
+ import { isRewardTokenSupported, RewardToken } from './reward-tokens';
13
+
14
+ type Response = {
15
+ withdrawal: WithdrawalData;
16
+ };
17
+
18
+ export type ClaimRewardParameters = {
19
+ /** The reward token. */
20
+ rewardToken: RewardToken;
21
+ /** The amount of reward to be withdrawn (claimed). */
22
+ amount: BigNumber.Value;
23
+ /** The destination address. */
24
+ to: string;
25
+ } & CommonWriteParameters;
26
+
27
+ const REQUEST_WITHDRAW_URL =
28
+ '/api/v1/distribution/account/{from}/withdrawals/{to}';
29
+
30
+ /** Claims a reward. */
31
+ export async function claimReward({
32
+ account: from,
33
+ rewardToken,
34
+ amount,
35
+ to,
36
+ chainId,
37
+ provider,
38
+ env,
39
+ }: ClaimRewardParameters) {
40
+ if (!isRewardTokenSupported(rewardToken)) {
41
+ throw new Error(`Unknown reward token: ${RewardToken}`);
42
+ }
43
+
44
+ const walletClient = makeWalletClient({
45
+ chainId,
46
+ provider,
47
+ });
48
+
49
+ const withdrawalFee = await getRewardWithdrawalFee({
50
+ address: from,
51
+ rewardToken,
52
+ env,
53
+ });
54
+ const signingData = await getRewardSigningData({
55
+ from,
56
+ to,
57
+ amount,
58
+ fee: withdrawalFee,
59
+ rewardToken,
60
+ env,
61
+ });
62
+
63
+ const signature = await walletClient.signMessage({
64
+ account: from,
65
+ message: signingData,
66
+ });
67
+
68
+ const payload = {
69
+ amount: BigNumber(amount).toFixed(),
70
+ fee: BigNumber(withdrawalFee).toFixed(),
71
+ signature,
72
+ };
73
+
74
+ const { baseApiUrl } = getApiConfig(env);
75
+ const { data: withdrawData } = await axios.post<Response>(
76
+ REQUEST_WITHDRAW_URL.replace('{from}', from).replace('{to}', to),
77
+ payload,
78
+ {
79
+ baseURL: baseApiUrl,
80
+ },
81
+ );
82
+
83
+ return mapDataToRewardWithdrawal(withdrawData.withdrawal);
84
+ }
@@ -0,0 +1,64 @@
1
+ import type { Meta, StoryObj } from '@storybook/react';
2
+ import {
3
+ functionType,
4
+ wagmiDecorator,
5
+ } from '../../stories/components/decorators';
6
+ import { Button } from '../../stories/components/Button';
7
+ import { CodeBlock } from '../../stories/components/CodeBlock';
8
+ import { ErrorBlock } from '../../stories/components/error-block';
9
+ import { EXAMPLE_EVM_ADDRESS } from '../../stories/constants';
10
+ import useQuery from '../../stories/hooks/useQuery';
11
+ import {
12
+ getRewardBalances,
13
+ GetRewardBalancesParameters,
14
+ } from './get-reward-balances';
15
+ import { RewardToken } from './reward-tokens';
16
+ import { Env } from '@lombard.finance/sdk-common';
17
+
18
+ const meta = {
19
+ title: 'rewards/getRewardBalances',
20
+ component: StoryView,
21
+ tags: ['autodocs'],
22
+ decorators: [wagmiDecorator, functionType('api-get')],
23
+ } satisfies Meta<typeof StoryView>;
24
+
25
+ export default meta;
26
+
27
+ type Story = StoryObj<typeof meta>;
28
+
29
+ export const WithParams: Story = {
30
+ args: {
31
+ address: EXAMPLE_EVM_ADDRESS,
32
+ rewardToken: RewardToken.BABY,
33
+ env: Env.stage,
34
+ },
35
+ };
36
+
37
+ type Props = GetRewardBalancesParameters;
38
+
39
+ export function StoryView(props: Props) {
40
+ const request = async () => {
41
+ return getRewardBalances({
42
+ ...props,
43
+ });
44
+ };
45
+
46
+ const { data, error, isLoading, refetch } = useQuery(request, [], false);
47
+
48
+ return (
49
+ <>
50
+ <p>This function gets the rewards earned by a user.</p>
51
+
52
+ <Button
53
+ onClick={refetch}
54
+ disabled={isLoading}
55
+ isLoading={isLoading}
56
+ actionName={getRewardBalances.name}
57
+ />
58
+
59
+ <ErrorBlock>{error}</ErrorBlock>
60
+
61
+ <CodeBlock text={data} />
62
+ </>
63
+ );
64
+ }
@@ -0,0 +1,81 @@
1
+ import { Address } from 'viem';
2
+ import { IEnvParam } from '../../common/parameters';
3
+ import { getApiConfig } from '../../common/api-config';
4
+ import axios from 'axios';
5
+ import { isRewardTokenSupported, RewardToken } from './reward-tokens';
6
+ import BigNumber from 'bignumber.js';
7
+
8
+ export enum RewardBlockchainType {
9
+ Undefined = 'BLOCKCHAIN_TYPE_UNDEFINED',
10
+ Evm = 'BLOCKCHAIN_TYPE_EVM',
11
+ Sui = 'BLOCKCHAIN_TYPE_SUI',
12
+ Solana = 'BLOCKCHAIN_TYPE_SOLANA',
13
+ Cosmos = 'BLOCKCHAIN_TYPE_COSMOS',
14
+ }
15
+
16
+ type Response = {
17
+ account: {
18
+ id: string;
19
+ address: string;
20
+ available_balance: string;
21
+ locked_balance: string;
22
+ pending_balance: string;
23
+ blockchain_type: RewardBlockchainType;
24
+ created_at: string;
25
+ };
26
+ };
27
+
28
+ const REWARDS_URL = '/api/v1/distribution/account/{address}';
29
+
30
+ export type GetRewardBalancesParameters = {
31
+ /** The reward earner (claimer). */
32
+ address: Address;
33
+ /** The reward token. */
34
+ rewardToken?: RewardToken;
35
+ } & IEnvParam;
36
+
37
+ export type RewardBalances = {
38
+ /** The address of the reward earner (claimer). */
39
+ address: Address;
40
+ /** The available balance of the reward token (ready to be withdrawn). */
41
+ availableBalance: BigNumber;
42
+ /** The locked balance (in processing). */
43
+ lockedBalance: BigNumber;
44
+ /** The pending balance to be credited. */
45
+ pendingBalance: BigNumber;
46
+ /** The reward token. */
47
+ rewardToken: RewardToken;
48
+ /** The timestamp. */
49
+ timestamp: Date;
50
+ };
51
+
52
+ /**
53
+ * Gets the reward balances earned by the specified address.
54
+ */
55
+ export async function getRewardBalances({
56
+ address,
57
+ rewardToken = RewardToken.BABY,
58
+ env,
59
+ }: GetRewardBalancesParameters) {
60
+ if (!isRewardTokenSupported(rewardToken)) {
61
+ throw new Error(`Unknown reward token: ${RewardToken}`);
62
+ }
63
+
64
+ const { baseApiUrl } = getApiConfig(env);
65
+
66
+ const { data } = await axios.get<Response>(
67
+ REWARDS_URL.replace('{address}', address),
68
+ { baseURL: baseApiUrl },
69
+ );
70
+
71
+ const balances: RewardBalances = {
72
+ address,
73
+ availableBalance: BigNumber(data.account.available_balance),
74
+ lockedBalance: BigNumber(data.account.locked_balance),
75
+ pendingBalance: BigNumber(data.account.pending_balance),
76
+ rewardToken,
77
+ timestamp: new Date(data.account.created_at),
78
+ };
79
+
80
+ return balances;
81
+ }