@pancakeswap/pools 4.0.3

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 (101) hide show
  1. package/.turbo/turbo-build.log +18 -0
  2. package/CHANGELOG.md +91 -0
  3. package/LICENSE +674 -0
  4. package/dist/abis/ICake.d.ts +92 -0
  5. package/dist/abis/ICake.d.ts.map +1 -0
  6. package/dist/abis/ICakeFlexibleSideVaultV2.d.ts +378 -0
  7. package/dist/abis/ICakeFlexibleSideVaultV2.d.ts.map +1 -0
  8. package/dist/abis/ICakeVaultV2.d.ts +750 -0
  9. package/dist/abis/ICakeVaultV2.d.ts.map +1 -0
  10. package/dist/abis/ISmartChef.d.ts +523 -0
  11. package/dist/abis/ISmartChef.d.ts.map +1 -0
  12. package/dist/abis/ISousChef.d.ts +212 -0
  13. package/dist/abis/ISousChef.d.ts.map +1 -0
  14. package/dist/abis/ISousChefBNB.d.ts +279 -0
  15. package/dist/abis/ISousChefBNB.d.ts.map +1 -0
  16. package/dist/abis/ISousChefV2.d.ts +264 -0
  17. package/dist/abis/ISousChefV2.d.ts.map +1 -0
  18. package/dist/abis/ISousChefV3.d.ts +523 -0
  19. package/dist/abis/ISousChefV3.d.ts.map +1 -0
  20. package/dist/constants/contracts.d.ts +22 -0
  21. package/dist/constants/contracts.d.ts.map +1 -0
  22. package/dist/constants/index.d.ts +8 -0
  23. package/dist/constants/index.d.ts.map +1 -0
  24. package/dist/constants/pools/1.d.ts +5 -0
  25. package/dist/constants/pools/1.d.ts.map +1 -0
  26. package/dist/constants/pools/56.d.ts +4 -0
  27. package/dist/constants/pools/56.d.ts.map +1 -0
  28. package/dist/constants/pools/97.d.ts +5 -0
  29. package/dist/constants/pools/97.d.ts.map +1 -0
  30. package/dist/constants/pools/index.d.ts +15 -0
  31. package/dist/constants/pools/index.d.ts.map +1 -0
  32. package/dist/constants/supportedChains.d.ts +4 -0
  33. package/dist/constants/supportedChains.d.ts.map +1 -0
  34. package/dist/index.d.ts +13 -0
  35. package/dist/index.d.ts.map +1 -0
  36. package/dist/index.js +6288 -0
  37. package/dist/index.js.map +1 -0
  38. package/dist/index.mjs +6224 -0
  39. package/dist/index.mjs.map +1 -0
  40. package/dist/queries/fetchPools.d.ts +37 -0
  41. package/dist/queries/fetchPools.d.ts.map +1 -0
  42. package/dist/queries/fetchUserIfo.d.ts +16 -0
  43. package/dist/queries/fetchUserIfo.d.ts.map +1 -0
  44. package/dist/queries/fetchUserPoolsData.d.ts +16 -0
  45. package/dist/queries/fetchUserPoolsData.d.ts.map +1 -0
  46. package/dist/queries/fetchVaultPublic.d.ts +39 -0
  47. package/dist/queries/fetchVaultPublic.d.ts.map +1 -0
  48. package/dist/queries/fetchVaultUser.d.ts +12 -0
  49. package/dist/queries/fetchVaultUser.d.ts.map +1 -0
  50. package/dist/queries/getAddresses.d.ts +4 -0
  51. package/dist/queries/getAddresses.d.ts.map +1 -0
  52. package/dist/queries/index.d.ts +7 -0
  53. package/dist/queries/index.d.ts.map +1 -0
  54. package/dist/types.d.ts +176 -0
  55. package/dist/types.d.ts.map +1 -0
  56. package/dist/utils/apr.d.ts +11 -0
  57. package/dist/utils/apr.d.ts.map +1 -0
  58. package/dist/utils/getContractAddress.d.ts +4 -0
  59. package/dist/utils/getContractAddress.d.ts.map +1 -0
  60. package/dist/utils/getPoolContractBySousId.d.ts +35 -0
  61. package/dist/utils/getPoolContractBySousId.d.ts.map +1 -0
  62. package/dist/utils/index.d.ts +6 -0
  63. package/dist/utils/index.d.ts.map +1 -0
  64. package/dist/utils/isLegacyPool.d.ts +4 -0
  65. package/dist/utils/isLegacyPool.d.ts.map +1 -0
  66. package/dist/utils/isPoolsSupported.d.ts +3 -0
  67. package/dist/utils/isPoolsSupported.d.ts.map +1 -0
  68. package/package.json +40 -0
  69. package/src/abis/ICake.ts +59 -0
  70. package/src/abis/ICakeFlexibleSideVaultV2.ts +487 -0
  71. package/src/abis/ICakeVaultV2.ts +487 -0
  72. package/src/abis/ISmartChef.ts +326 -0
  73. package/src/abis/ISousChef.ts +122 -0
  74. package/src/abis/ISousChefBNB.ts +166 -0
  75. package/src/abis/ISousChefV2.ts +341 -0
  76. package/src/abis/ISousChefV3.ts +326 -0
  77. package/src/constants/contracts.ts +26 -0
  78. package/src/constants/index.ts +10 -0
  79. package/src/constants/pools/1.ts +80 -0
  80. package/src/constants/pools/56.ts +2933 -0
  81. package/src/constants/pools/97.ts +74 -0
  82. package/src/constants/pools/index.ts +44 -0
  83. package/src/constants/supportedChains.ts +5 -0
  84. package/src/index.ts +14 -0
  85. package/src/queries/fetchPools.ts +350 -0
  86. package/src/queries/fetchUserIfo.ts +53 -0
  87. package/src/queries/fetchUserPoolsData.ts +143 -0
  88. package/src/queries/fetchVaultPublic.ts +175 -0
  89. package/src/queries/fetchVaultUser.ts +106 -0
  90. package/src/queries/getAddresses.ts +12 -0
  91. package/src/queries/index.ts +6 -0
  92. package/src/types.ts +207 -0
  93. package/src/utils/apr.ts +35 -0
  94. package/src/utils/getContractAddress.ts +11 -0
  95. package/src/utils/getPoolContractBySousId.ts +111 -0
  96. package/src/utils/index.ts +5 -0
  97. package/src/utils/isLegacyPool.ts +15 -0
  98. package/src/utils/isPoolsSupported.ts +5 -0
  99. package/tsconfig.json +14 -0
  100. package/tsup.config.ts +26 -0
  101. package/vitest.config.ts +9 -0
@@ -0,0 +1,175 @@
1
+ import BigNumber from 'bignumber.js'
2
+ import { BIG_ZERO } from '@pancakeswap/utils/bigNumber'
3
+ import { ChainId } from '@pancakeswap/sdk'
4
+ import { CAKE } from '@pancakeswap/tokens'
5
+ import { Address } from 'viem'
6
+
7
+ import { cakeVaultV2ABI } from '../abis/ICakeVaultV2'
8
+ import { OnChainProvider } from '../types'
9
+ import { getCakeFlexibleSideVaultAddress, getCakeVaultAddress } from './getAddresses'
10
+
11
+ interface Params {
12
+ cakeVaultAddress?: Address
13
+ chainId: ChainId
14
+ provider: OnChainProvider
15
+ }
16
+
17
+ const balanceOfAbi = [
18
+ {
19
+ inputs: [{ internalType: 'address', name: 'account', type: 'address' }],
20
+ name: 'balanceOf',
21
+ outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
22
+ stateMutability: 'view',
23
+ type: 'function',
24
+ },
25
+ ] as const
26
+
27
+ export const fetchPublicVaultData = async ({
28
+ chainId,
29
+ cakeVaultAddress = getCakeVaultAddress(chainId),
30
+ provider,
31
+ }: Params) => {
32
+ try {
33
+ const client = provider({ chainId })
34
+
35
+ const [sharePrice, shares, totalLockedAmount, totalCakeInVault] = await client.multicall({
36
+ contracts: [
37
+ {
38
+ abi: cakeVaultV2ABI,
39
+ address: cakeVaultAddress,
40
+ functionName: 'getPricePerFullShare',
41
+ },
42
+ {
43
+ abi: cakeVaultV2ABI,
44
+ address: cakeVaultAddress,
45
+ functionName: 'totalShares',
46
+ },
47
+ {
48
+ abi: cakeVaultV2ABI,
49
+ address: cakeVaultAddress,
50
+ functionName: 'totalLockedAmount',
51
+ },
52
+ {
53
+ abi: balanceOfAbi,
54
+ address: CAKE[ChainId.BSC].address,
55
+ functionName: 'balanceOf',
56
+ args: [cakeVaultAddress],
57
+ },
58
+ ],
59
+ allowFailure: true,
60
+ })
61
+
62
+ const totalSharesAsBigNumber =
63
+ shares.status === 'success' && shares.result ? new BigNumber(shares.result.toString()) : BIG_ZERO
64
+ const totalLockedAmountAsBigNumber =
65
+ totalLockedAmount.status === 'success' && totalLockedAmount.result
66
+ ? new BigNumber(totalLockedAmount.result.toString())
67
+ : BIG_ZERO
68
+ const sharePriceAsBigNumber =
69
+ sharePrice.status === 'success' && sharePrice.result ? new BigNumber(sharePrice.result.toString()) : BIG_ZERO
70
+
71
+ return {
72
+ totalShares: totalSharesAsBigNumber.toJSON(),
73
+ totalLockedAmount: totalLockedAmountAsBigNumber.toJSON(),
74
+ pricePerFullShare: sharePriceAsBigNumber.toJSON(),
75
+ totalCakeInVault: totalCakeInVault.result ? new BigNumber(totalCakeInVault.result.toString()).toJSON() : '0',
76
+ }
77
+ } catch (error) {
78
+ return {
79
+ totalShares: null,
80
+ totalLockedAmount: null,
81
+ pricePerFullShare: null,
82
+ totalCakeInVault: null,
83
+ }
84
+ }
85
+ }
86
+
87
+ export const fetchPublicFlexibleSideVaultData = async ({
88
+ chainId,
89
+ cakeVaultAddress = getCakeFlexibleSideVaultAddress(chainId),
90
+ provider,
91
+ }: Params) => {
92
+ try {
93
+ const client = provider({ chainId })
94
+
95
+ const [sharePrice, shares, totalCakeInVault] = await client.multicall({
96
+ contracts: [
97
+ {
98
+ abi: cakeVaultV2ABI,
99
+ address: cakeVaultAddress,
100
+ functionName: 'getPricePerFullShare',
101
+ },
102
+ {
103
+ abi: cakeVaultV2ABI,
104
+ address: cakeVaultAddress,
105
+ functionName: 'totalShares',
106
+ },
107
+ {
108
+ abi: balanceOfAbi,
109
+ address: CAKE[ChainId.BSC].address,
110
+ functionName: 'balanceOf',
111
+ args: [cakeVaultAddress],
112
+ },
113
+ ],
114
+ allowFailure: true,
115
+ })
116
+
117
+ const totalSharesAsBigNumber = shares.status === 'success' ? new BigNumber(shares.result.toString()) : BIG_ZERO
118
+ const sharePriceAsBigNumber =
119
+ sharePrice.status === 'success' ? new BigNumber(sharePrice.result.toString()) : BIG_ZERO
120
+ return {
121
+ totalShares: totalSharesAsBigNumber.toJSON(),
122
+ pricePerFullShare: sharePriceAsBigNumber.toJSON(),
123
+ totalCakeInVault: new BigNumber((totalCakeInVault.result || '0').toString()).toJSON(),
124
+ }
125
+ } catch (error) {
126
+ return {
127
+ totalShares: null,
128
+ pricePerFullShare: null,
129
+ totalCakeInVault: null,
130
+ }
131
+ }
132
+ }
133
+
134
+ export const fetchVaultFees = async ({
135
+ chainId,
136
+ cakeVaultAddress = getCakeVaultAddress(chainId),
137
+ provider,
138
+ }: Params) => {
139
+ try {
140
+ const client = provider({ chainId })
141
+
142
+ const [performanceFee, withdrawalFee, withdrawalFeePeriod] = await client.multicall({
143
+ contracts: [
144
+ {
145
+ abi: cakeVaultV2ABI,
146
+ address: cakeVaultAddress,
147
+ functionName: 'performanceFee',
148
+ },
149
+ {
150
+ abi: cakeVaultV2ABI,
151
+ address: cakeVaultAddress,
152
+ functionName: 'withdrawFee',
153
+ },
154
+ {
155
+ abi: cakeVaultV2ABI,
156
+ address: cakeVaultAddress,
157
+ functionName: 'withdrawFeePeriod',
158
+ },
159
+ ],
160
+ allowFailure: false,
161
+ })
162
+
163
+ return {
164
+ performanceFee: Number(performanceFee),
165
+ withdrawalFee: Number(withdrawalFee),
166
+ withdrawalFeePeriod: Number(withdrawalFeePeriod),
167
+ }
168
+ } catch (error) {
169
+ return {
170
+ performanceFee: null,
171
+ withdrawalFee: null,
172
+ withdrawalFeePeriod: null,
173
+ }
174
+ }
175
+ }
@@ -0,0 +1,106 @@
1
+ import BigNumber from 'bignumber.js'
2
+ import { ChainId } from '@pancakeswap/sdk'
3
+ import { Address } from 'viem'
4
+
5
+ import { OnChainProvider, SerializedLockedVaultUser, SerializedVaultUser } from '../types'
6
+ import { cakeVaultV2ABI } from '../abis/ICakeVaultV2'
7
+ import { getCakeFlexibleSideVaultAddress, getCakeVaultAddress } from './getAddresses'
8
+ import { cakeFlexibleSideVaultV2ABI } from '../abis/ICakeFlexibleSideVaultV2'
9
+
10
+ interface Params {
11
+ account: Address
12
+ chainId: ChainId
13
+ provider: OnChainProvider
14
+ }
15
+
16
+ export const fetchVaultUser = async ({ account, chainId, provider }: Params): Promise<SerializedLockedVaultUser> => {
17
+ try {
18
+ const cakeVaultAddress = getCakeVaultAddress(chainId)
19
+
20
+ const client = provider({ chainId })
21
+
22
+ const [userContractResponse, currentPerformanceFee, currentOverdueFee] = await client.multicall({
23
+ contracts: [
24
+ {
25
+ abi: cakeVaultV2ABI,
26
+ address: cakeVaultAddress,
27
+ functionName: 'userInfo',
28
+ args: [account],
29
+ },
30
+ {
31
+ abi: cakeVaultV2ABI,
32
+ address: cakeVaultAddress,
33
+ functionName: 'calculatePerformanceFee',
34
+ args: [account],
35
+ },
36
+ {
37
+ abi: cakeVaultV2ABI,
38
+ address: cakeVaultAddress,
39
+ functionName: 'calculateOverdueFee',
40
+ args: [account],
41
+ },
42
+ ],
43
+ allowFailure: false,
44
+ })
45
+
46
+ return {
47
+ isLoading: false,
48
+ userShares: new BigNumber(userContractResponse[0].toString()).toJSON(),
49
+ lastDepositedTime: userContractResponse[1].toString(),
50
+ lastUserActionTime: userContractResponse[3].toString(),
51
+ cakeAtLastUserAction: new BigNumber(userContractResponse[2].toString()).toJSON(),
52
+ userBoostedShare: new BigNumber(userContractResponse[6].toString()).toJSON(),
53
+ locked: userContractResponse[7],
54
+ lockEndTime: userContractResponse[5].toString(),
55
+ lockStartTime: userContractResponse[4].toString(),
56
+ lockedAmount: new BigNumber(userContractResponse[8].toString()).toJSON(),
57
+ currentPerformanceFee: new BigNumber(currentPerformanceFee.toString()).toJSON(),
58
+ currentOverdueFee: new BigNumber(currentOverdueFee.toString()).toJSON(),
59
+ }
60
+ } catch (error) {
61
+ return {
62
+ isLoading: true,
63
+ userShares: '',
64
+ lastDepositedTime: '',
65
+ lastUserActionTime: '',
66
+ cakeAtLastUserAction: '',
67
+ userBoostedShare: '',
68
+ lockEndTime: '',
69
+ lockStartTime: '',
70
+ locked: false,
71
+ lockedAmount: '',
72
+ currentPerformanceFee: '',
73
+ currentOverdueFee: '',
74
+ }
75
+ }
76
+ }
77
+
78
+ export const fetchFlexibleSideVaultUser = async ({
79
+ account,
80
+ chainId,
81
+ provider,
82
+ }: Params): Promise<SerializedVaultUser> => {
83
+ try {
84
+ const userContractResponse = await await provider({ chainId }).readContract({
85
+ abi: cakeFlexibleSideVaultV2ABI,
86
+ address: getCakeFlexibleSideVaultAddress(chainId),
87
+ functionName: 'userInfo',
88
+ args: [account],
89
+ })
90
+ return {
91
+ isLoading: false,
92
+ userShares: new BigNumber(userContractResponse[0].toString()).toJSON(),
93
+ lastDepositedTime: userContractResponse[1].toString(),
94
+ lastUserActionTime: userContractResponse[3].toString(),
95
+ cakeAtLastUserAction: new BigNumber(userContractResponse[2].toString()).toJSON(),
96
+ }
97
+ } catch (error) {
98
+ return {
99
+ isLoading: true,
100
+ userShares: '',
101
+ lastDepositedTime: '',
102
+ lastUserActionTime: '',
103
+ cakeAtLastUserAction: '',
104
+ }
105
+ }
106
+ }
@@ -0,0 +1,12 @@
1
+ import { ChainId } from '@pancakeswap/sdk'
2
+
3
+ import { CAKE_FLEXIBLE_SIDE_VAULT, CAKE_VAULT } from '../constants/contracts'
4
+ import { getContractAddress } from '../utils'
5
+
6
+ export function getCakeFlexibleSideVaultAddress(chainId: ChainId) {
7
+ return getContractAddress(CAKE_FLEXIBLE_SIDE_VAULT, chainId)
8
+ }
9
+
10
+ export function getCakeVaultAddress(chainId: ChainId) {
11
+ return getContractAddress(CAKE_VAULT, chainId)
12
+ }
@@ -0,0 +1,6 @@
1
+ export * from './fetchPools'
2
+ export * from './fetchUserPoolsData'
3
+ export * from './fetchUserIfo'
4
+ export * from './fetchVaultPublic'
5
+ export * from './getAddresses'
6
+ export * from './fetchVaultUser'
package/src/types.ts ADDED
@@ -0,0 +1,207 @@
1
+ import { ChainId } from '@pancakeswap/sdk'
2
+ import { PublicClient, Address } from 'viem'
3
+ import type { SerializedWrappedToken } from '@pancakeswap/token-lists'
4
+ import BigNumber from 'bignumber.js'
5
+
6
+ export type OnChainProvider = ({ chainId }: { chainId?: ChainId }) => PublicClient
7
+
8
+ export type SerializedBigNumber = string
9
+
10
+ export interface Addresses {
11
+ [chainId: number]: string
12
+ }
13
+
14
+ export enum PoolCategory {
15
+ 'COMMUNITY' = 'Community',
16
+ 'CORE' = 'Core',
17
+ 'BINANCE' = 'Binance', // Pools using native BNB behave differently than pools using a token
18
+ 'AUTO' = 'Auto',
19
+ }
20
+
21
+ // @deprecated
22
+ export interface LegacyPoolConfigBaseProps {
23
+ sousId: number
24
+ contractAddress: Address
25
+ poolCategory: PoolCategory
26
+ tokenPerBlock: string
27
+ isFinished?: boolean
28
+ enableEmergencyWithdraw?: boolean
29
+ version?: number
30
+ }
31
+
32
+ export interface PoolConfigBaseProps {
33
+ sousId: number
34
+ contractAddress: Address
35
+ poolCategory: PoolCategory
36
+ tokenPerSecond: string
37
+ isFinished?: boolean
38
+ enableEmergencyWithdraw?: boolean
39
+ }
40
+
41
+ interface GenericToken {
42
+ decimals: number
43
+ symbol: string
44
+ address: string
45
+ }
46
+
47
+ export interface LegacySerializedPoolConfig<T> extends LegacyPoolConfigBaseProps {
48
+ earningToken: T & GenericToken
49
+ stakingToken: T & GenericToken
50
+ }
51
+
52
+ export interface SerializedPoolConfig<T> extends PoolConfigBaseProps {
53
+ earningToken: T & GenericToken
54
+ stakingToken: T & GenericToken
55
+ }
56
+
57
+ export type LegacySerializedPool = LegacySerializedPoolConfig<SerializedWrappedToken>
58
+
59
+ export type UpgradedSerializedPool = SerializedPoolConfig<SerializedWrappedToken>
60
+
61
+ export type SerializedPool = LegacySerializedPool | UpgradedSerializedPool
62
+
63
+ export type UpgradedSerializedPoolWithInfo = UpgradedSerializedPool & SerializedPoolInfo
64
+
65
+ export type LegacySerializedPoolWithInfo = LegacySerializedPool & SerializedPoolInfo
66
+
67
+ export type SerializedPoolWithInfo = LegacySerializedPoolWithInfo | UpgradedSerializedPoolWithInfo
68
+
69
+ export interface SerializedVaultUser {
70
+ isLoading: boolean
71
+ userShares: SerializedBigNumber
72
+ cakeAtLastUserAction: SerializedBigNumber
73
+ lastDepositedTime: string
74
+ lastUserActionTime: string
75
+ }
76
+
77
+ export interface SerializedLockedVaultUser extends SerializedVaultUser {
78
+ lockStartTime: string
79
+ lockEndTime: string
80
+ userBoostedShare: SerializedBigNumber
81
+ locked: boolean
82
+ lockedAmount: SerializedBigNumber
83
+ currentPerformanceFee: SerializedBigNumber
84
+ currentOverdueFee: SerializedBigNumber
85
+ }
86
+
87
+ export interface LegacyDeserializedPoolConfig<T> extends LegacyPoolConfigBaseProps {
88
+ earningToken: T & GenericToken
89
+ stakingToken: T & GenericToken
90
+ }
91
+
92
+ export interface UpgradedDeserializedPoolConfig<T> extends PoolConfigBaseProps {
93
+ earningToken: T & GenericToken
94
+ stakingToken: T & GenericToken
95
+ }
96
+
97
+ export type DeserializedPoolConfig<T> = LegacyDeserializedPoolConfig<T> | UpgradedDeserializedPoolConfig<T>
98
+
99
+ interface SerializedPoolInfo extends CorePoolProps {
100
+ totalStaked?: string
101
+ stakingLimit?: string
102
+ stakingLimitEndTimestamp?: number
103
+ profileRequirement?: {
104
+ required: boolean
105
+ thresholdPoints: string
106
+ }
107
+ userDataLoaded?: boolean
108
+ userData?: {
109
+ allowance: string
110
+ stakingTokenBalance: string
111
+ stakedBalance: string
112
+ pendingReward: string
113
+ }
114
+ }
115
+
116
+ interface PoolInfo extends CorePoolProps {
117
+ totalStaked?: BigNumber
118
+ stakingLimit?: BigNumber
119
+ stakingLimitEndTimestamp?: number
120
+ profileRequirement?: {
121
+ required: boolean
122
+ thresholdPoints: BigNumber
123
+ }
124
+ userDataLoaded?: boolean
125
+ userData?: {
126
+ allowance: BigNumber
127
+ stakingTokenBalance: BigNumber
128
+ stakedBalance: BigNumber
129
+ pendingReward: BigNumber
130
+ }
131
+ }
132
+
133
+ export type UpgradedDeserializedPool<T> = UpgradedDeserializedPoolConfig<T> & PoolInfo
134
+
135
+ export type LegacyDeserializedPool<T> = LegacyDeserializedPoolConfig<T> & PoolInfo
136
+
137
+ export type DeserializedPool<T> = UpgradedDeserializedPool<T> | LegacyDeserializedPool<T>
138
+
139
+ export type DeserializedPoolVault<T> = DeserializedPool<T> & DeserializedCakeVault
140
+ export type DeserializedPoolLockedVault<T> = DeserializedPool<T> & DeserializedLockedCakeVault
141
+
142
+ export interface DeserializedLockedVaultUser extends DeserializedVaultUser {
143
+ lastDepositedTime: string
144
+ lastUserActionTime: string
145
+ lockStartTime: string
146
+ lockEndTime: string
147
+ burnStartTime: string
148
+ userBoostedShare: BigNumber
149
+ locked: boolean
150
+ lockedAmount: BigNumber
151
+ currentPerformanceFee: BigNumber
152
+ currentOverdueFee: BigNumber
153
+ }
154
+
155
+ export interface DeserializedLockedCakeVault extends Omit<DeserializedCakeVault, 'userData'> {
156
+ totalLockedAmount?: BigNumber
157
+ userData?: DeserializedLockedVaultUser
158
+ }
159
+
160
+ export interface SerializedVaultFees {
161
+ performanceFee: number
162
+ withdrawalFee: number
163
+ withdrawalFeePeriod: number
164
+ }
165
+
166
+ export interface DeserializedVaultFees extends SerializedVaultFees {
167
+ performanceFeeAsDecimal: number
168
+ }
169
+
170
+ export interface DeserializedVaultUser {
171
+ isLoading: boolean
172
+ userShares: BigNumber
173
+ cakeAtLastUserAction: BigNumber
174
+ lastDepositedTime: string
175
+ lastUserActionTime: string
176
+ balance: {
177
+ cakeAsNumberBalance: number
178
+ cakeAsBigNumber: BigNumber
179
+ cakeAsDisplayBalance: string
180
+ }
181
+ }
182
+
183
+ export interface DeserializedCakeVault {
184
+ totalShares?: BigNumber
185
+ totalLockedAmount?: BigNumber
186
+ pricePerFullShare: BigNumber
187
+ totalCakeInVault?: BigNumber
188
+ fees?: DeserializedVaultFees
189
+ userData?: DeserializedVaultUser
190
+ }
191
+
192
+ export enum VaultKey {
193
+ CakeVaultV1 = 'cakeVaultV1',
194
+ CakeVault = 'cakeVault',
195
+ CakeFlexibleSideVault = 'cakeFlexibleSideVault',
196
+ IfoPool = 'ifoPool',
197
+ }
198
+
199
+ interface CorePoolProps {
200
+ startTimestamp?: number
201
+ endTimestamp?: number
202
+ apr?: number
203
+ rawApr?: number
204
+ stakingTokenPrice?: number
205
+ earningTokenPrice?: number
206
+ vaultKey?: VaultKey
207
+ }
@@ -0,0 +1,35 @@
1
+ import BigNumber from 'bignumber.js'
2
+
3
+ import { BLOCKS_PER_YEAR, SECONDS_IN_YEAR } from '../constants'
4
+
5
+ /**
6
+ * Get the APR value in %
7
+ * @param stakingTokenPrice Token price in the same quote currency
8
+ * @param rewardTokenPrice Token price in the same quote currency
9
+ * @param totalStaked Total amount of stakingToken in the pool
10
+ * @param tokenPerBlock Amount of new cake allocated to the pool for each new block
11
+ * @returns Null if the APR is NaN or infinite.
12
+ */
13
+ export const getPoolAprByTokenPerBlock = (
14
+ stakingTokenPrice: number,
15
+ rewardTokenPrice: number,
16
+ totalStaked: number,
17
+ tokenPerBlock: number,
18
+ ): number | null => {
19
+ const totalRewardPricePerYear = new BigNumber(rewardTokenPrice).times(tokenPerBlock).times(BLOCKS_PER_YEAR)
20
+ const totalStakingTokenInPool = new BigNumber(stakingTokenPrice).times(totalStaked)
21
+ const apr = totalRewardPricePerYear.div(totalStakingTokenInPool).times(100)
22
+ return apr.isNaN() || !apr.isFinite() ? null : apr.toNumber()
23
+ }
24
+
25
+ export const getPoolAprByTokenPerSecond = (
26
+ stakingTokenPrice: number,
27
+ rewardTokenPrice: number,
28
+ totalStaked: number,
29
+ tokenPerSecond: number,
30
+ ): number | null => {
31
+ const totalRewardPricePerYear = new BigNumber(rewardTokenPrice).times(tokenPerSecond).times(SECONDS_IN_YEAR)
32
+ const totalStakingTokenInPool = new BigNumber(stakingTokenPrice).times(totalStaked)
33
+ const apr = totalRewardPricePerYear.div(totalStakingTokenInPool).times(100)
34
+ return apr.isNaN() || !apr.isFinite() ? null : apr.toNumber()
35
+ }
@@ -0,0 +1,11 @@
1
+ import { ChainId } from '@pancakeswap/sdk'
2
+
3
+ import { ContractAddresses } from '../constants/contracts'
4
+ import { isPoolsSupported } from './isPoolsSupported'
5
+
6
+ export function getContractAddress(addresses: ContractAddresses, chainId: ChainId) {
7
+ if (!isPoolsSupported(chainId)) {
8
+ throw new Error(`Cannot get contract address. Unsupported chain ${chainId}`)
9
+ }
10
+ return addresses[chainId]
11
+ }
@@ -0,0 +1,111 @@
1
+ import { ChainId } from '@pancakeswap/sdk'
2
+ import { WalletClient, getContract, PublicClient, Address, GetContractReturnType, Account, Chain } from 'viem'
3
+
4
+ import { getPoolsConfig } from '../constants'
5
+ import { isLegacyPool } from './isLegacyPool'
6
+ import { smartChefABI } from '../abis/ISmartChef'
7
+ import { PoolCategory } from '../types'
8
+ import { sousChefV2ABI } from '../abis/ISousChefV2'
9
+ import { sousChefBnbABI } from '../abis/ISousChefBNB'
10
+
11
+ interface Params {
12
+ chainId?: ChainId
13
+ sousId: number
14
+ signer?: any
15
+ publicClient?: any
16
+ }
17
+
18
+ type GetContractReturnType_<TAbi extends readonly unknown[]> = GetContractReturnType<TAbi, any, any> & {
19
+ abi: TAbi
20
+ address: Address
21
+ account?: Account
22
+ chain?: Chain
23
+ }
24
+
25
+ export function getSousChefBNBContract({
26
+ address,
27
+ signer,
28
+ publicClient,
29
+ }: {
30
+ address: Address
31
+ signer?: WalletClient
32
+ publicClient?: PublicClient
33
+ }): GetContractReturnType_<typeof sousChefBnbABI> {
34
+ return {
35
+ ...getContract({
36
+ abi: sousChefBnbABI,
37
+ address,
38
+ walletClient: signer,
39
+ publicClient,
40
+ }),
41
+ abi: sousChefBnbABI,
42
+ address,
43
+ account: signer?.account,
44
+ chain: signer?.chain,
45
+ }
46
+ }
47
+
48
+ export function getSousChefV2Contract({
49
+ address,
50
+ signer,
51
+ publicClient,
52
+ }: {
53
+ address: Address
54
+ signer?: WalletClient
55
+ publicClient?: PublicClient
56
+ }): GetContractReturnType_<typeof sousChefV2ABI> {
57
+ return {
58
+ ...getContract({
59
+ abi: sousChefV2ABI,
60
+ address,
61
+ walletClient: signer,
62
+ publicClient,
63
+ }),
64
+ abi: sousChefV2ABI,
65
+ address,
66
+ account: signer?.account,
67
+ chain: signer?.chain,
68
+ }
69
+ }
70
+
71
+ export function getSmartChefChefV2Contract({
72
+ address,
73
+ signer,
74
+ publicClient,
75
+ }: {
76
+ address: Address
77
+ signer?: WalletClient
78
+ publicClient?: PublicClient
79
+ }): GetContractReturnType_<typeof smartChefABI> {
80
+ return {
81
+ ...getContract({
82
+ abi: smartChefABI,
83
+ address,
84
+ walletClient: signer,
85
+ publicClient,
86
+ }),
87
+ abi: smartChefABI,
88
+ address,
89
+ account: signer?.account,
90
+ chain: signer?.chain,
91
+ }
92
+ }
93
+
94
+ export function getPoolContractBySousId({ chainId, sousId, signer, publicClient }: Params): any | null {
95
+ if (!chainId) {
96
+ return null
97
+ }
98
+ const pools = getPoolsConfig(chainId)
99
+ const pool = pools?.find((p) => p.sousId === Number(sousId))
100
+ if (!pool) {
101
+ return null
102
+ }
103
+ const { contractAddress } = pool
104
+ if (isLegacyPool(pool)) {
105
+ if (pool.poolCategory === PoolCategory.BINANCE) {
106
+ return getSousChefBNBContract({ address: contractAddress, signer, publicClient })
107
+ }
108
+ return getSousChefV2Contract({ address: contractAddress, signer, publicClient })
109
+ }
110
+ return getSmartChefChefV2Contract({ address: contractAddress, signer, publicClient })
111
+ }
@@ -0,0 +1,5 @@
1
+ export * from './getContractAddress'
2
+ export * from './isPoolsSupported'
3
+ export * from './isLegacyPool'
4
+ export * from './apr'
5
+ export * from './getPoolContractBySousId'
@@ -0,0 +1,15 @@
1
+ import { LegacySerializedPool, SerializedPool, UpgradedSerializedPool } from '../types'
2
+
3
+ export function isLegacyPool(pool: SerializedPool): pool is LegacySerializedPool {
4
+ if ((pool as any).tokenPerBlock) {
5
+ return true
6
+ }
7
+ return false
8
+ }
9
+
10
+ export function isUpgradedPool(pool: SerializedPool): pool is UpgradedSerializedPool {
11
+ if ((pool as any).tokenPerSecond) {
12
+ return true
13
+ }
14
+ return false
15
+ }
@@ -0,0 +1,5 @@
1
+ import { SupportedChainId, SUPPORTED_CHAIN_IDS } from '../constants/supportedChains'
2
+
3
+ export function isPoolsSupported(chainId: number): chainId is SupportedChainId {
4
+ return SUPPORTED_CHAIN_IDS.includes(chainId)
5
+ }