@defisaver/ethena-sdk 0.0.5 → 0.0.6

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 (53) hide show
  1. package/cjs/config/contracts.d.ts +181 -0
  2. package/cjs/config/contracts.js +72 -0
  3. package/cjs/constants/index.d.ts +3 -0
  4. package/cjs/constants/index.js +4 -1
  5. package/cjs/contracts.d.ts +696 -0
  6. package/cjs/contracts.js +80 -0
  7. package/cjs/execution/index.d.ts +2 -0
  8. package/cjs/execution/index.js +15 -0
  9. package/cjs/execution/morpho.d.ts +2 -0
  10. package/cjs/execution/morpho.js +56 -0
  11. package/cjs/index.d.ts +3 -1
  12. package/cjs/index.js +5 -1
  13. package/cjs/safe/index.d.ts +6 -0
  14. package/cjs/safe/index.js +80 -0
  15. package/cjs/services/viem.d.ts +31 -31
  16. package/cjs/types/execution.d.ts +9 -0
  17. package/cjs/types/execution.js +8 -0
  18. package/cjs/types/index.d.ts +2 -0
  19. package/cjs/types/index.js +2 -0
  20. package/cjs/types/safe.d.ts +5 -0
  21. package/cjs/types/safe.js +2 -0
  22. package/esm/config/contracts.d.ts +181 -0
  23. package/esm/config/contracts.js +69 -0
  24. package/esm/constants/index.d.ts +3 -0
  25. package/esm/constants/index.js +3 -0
  26. package/esm/contracts.d.ts +696 -0
  27. package/esm/contracts.js +37 -0
  28. package/esm/execution/index.d.ts +2 -0
  29. package/esm/execution/index.js +11 -0
  30. package/esm/execution/morpho.d.ts +2 -0
  31. package/esm/execution/morpho.js +52 -0
  32. package/esm/index.d.ts +3 -1
  33. package/esm/index.js +3 -1
  34. package/esm/safe/index.d.ts +6 -0
  35. package/esm/safe/index.js +75 -0
  36. package/esm/services/viem.d.ts +31 -31
  37. package/esm/types/execution.d.ts +9 -0
  38. package/esm/types/execution.js +5 -0
  39. package/esm/types/index.d.ts +2 -0
  40. package/esm/types/index.js +2 -0
  41. package/esm/types/safe.d.ts +5 -0
  42. package/esm/types/safe.js +1 -0
  43. package/package.json +2 -1
  44. package/src/config/contracts.ts +72 -0
  45. package/src/constants/index.ts +4 -1
  46. package/src/contracts.ts +57 -0
  47. package/src/execution/index.ts +12 -0
  48. package/src/execution/morpho.ts +47 -0
  49. package/src/index.ts +4 -0
  50. package/src/safe/index.ts +99 -0
  51. package/src/types/execution.ts +11 -0
  52. package/src/types/index.ts +3 -1
  53. package/src/types/safe.ts +5 -0
package/src/index.ts CHANGED
@@ -4,6 +4,8 @@ import * as positionData from './positionData';
4
4
  import * as marketData from './marketData';
5
5
  import * as exchange from './exchange';
6
6
  import * as constants from './constants';
7
+ import * as safe from './safe';
8
+ import * as execution from './execution';
7
9
 
8
10
  export * from './types';
9
11
 
@@ -12,4 +14,6 @@ export {
12
14
  marketData,
13
15
  exchange,
14
16
  constants,
17
+ safe,
18
+ execution,
15
19
  };
@@ -0,0 +1,99 @@
1
+ import { NetworkNumber } from '@defisaver/positions-sdk';
2
+ import {
3
+ encodeFunctionData, encodePacked, getCreate2Address, keccak256,
4
+ } from 'viem';
5
+ import { Wallet } from '../types';
6
+ import {
7
+ SAFE_API_URL, SAFE_REFUND_RECEIVER, SALT_PREFIX, ZERO_ADDRESS,
8
+ } from '../constants';
9
+ import {
10
+ getConfigContractAbi,
11
+ getSafeFactoryAddress, getSafeFallbackHandlerAddress, getSafeWalletSingletonAddress, SafeFactoryContract,
12
+ } from '../contracts';
13
+ import { getViemProvider } from '../services/viem';
14
+
15
+ export const getSafeWallets = async (userAddress: string, network: NetworkNumber): Promise<{ success: boolean, wallets: string[] }> => {
16
+ try {
17
+ const res = await fetch(`${SAFE_API_URL}/safe/all-wallets?network=${network}&account=${userAddress}`);
18
+ const wallets = await res.json();
19
+
20
+ const oneOneWallets = wallets
21
+ .filter((wallet: Wallet) => wallet.type === 'Safe' && (wallet.owners || []).length === 1)
22
+ .map((wallet: Wallet) => wallet.address);
23
+ return { success: true, wallets: oneOneWallets };
24
+ } catch (e) {
25
+ return { success: false, wallets: [] };
26
+ }
27
+ };
28
+
29
+ const getSafeSetupParams = (owners: string[], threshold: number, network: NetworkNumber) => [
30
+ owners as `0x${string}`[],
31
+ BigInt(threshold),
32
+ ZERO_ADDRESS as `0x${string}`,
33
+ '0x',
34
+ getSafeFallbackHandlerAddress(network) as `0x${string}`,
35
+ ZERO_ADDRESS as `0x${string}`,
36
+ BigInt(0),
37
+ SAFE_REFUND_RECEIVER as `0x${string}`,
38
+ ];
39
+
40
+ const _predictSafeAddress = async (
41
+ rpcUrl: string,
42
+ network: NetworkNumber,
43
+ setupArgs: string,
44
+ saltNonce: string,
45
+ ) => {
46
+ const provider = getViemProvider(rpcUrl, network);
47
+ const safeProxyFactoryAddress = getSafeFactoryAddress(network);
48
+ const safeProxyFactoryContract = SafeFactoryContract(provider, network);
49
+ const masterCopyAddress = getSafeWalletSingletonAddress(network);
50
+
51
+ const proxyCreationCode = await safeProxyFactoryContract.read.proxyCreationCode();
52
+
53
+ // @ts-ignore
54
+ const initCodeHash = keccak256(
55
+ encodePacked(['bytes', 'bytes'], [proxyCreationCode, masterCopyAddress.slice(2).padStart(64, '0') as `0x${string}`]), 'bytes',
56
+ ) as string;
57
+
58
+ const salt = keccak256(
59
+ encodePacked(
60
+ ['bytes', 'uint256'],
61
+ [keccak256(setupArgs as `0x${string}`), BigInt(saltNonce)],
62
+ ),
63
+ );
64
+
65
+ return getCreate2Address(
66
+ {
67
+ bytecodeHash: initCodeHash as `0x${string}`,
68
+ from: safeProxyFactoryAddress,
69
+ salt,
70
+ },
71
+ );
72
+ };
73
+
74
+ export const predictSafeAddress = async (owner: string, rpcUrl: string, network: NetworkNumber): Promise<string> => {
75
+ const provider = getViemProvider(rpcUrl, network);
76
+
77
+ const threshold = 1;
78
+ const owners = [owner];
79
+ const setupParams = getSafeSetupParams(owners, threshold, network);
80
+ const setupParamsEncoded = encodeFunctionData({
81
+ abi: getConfigContractAbi('Safe130'),
82
+ functionName: 'setup',
83
+ // @ts-ignore
84
+ args: setupParams,
85
+ });
86
+ const oneOfOneWalletsCount = (await getSafeWallets(owner, network)).wallets.length;
87
+ const failAfter = 10;
88
+ for (let nonce = oneOfOneWalletsCount + 1; nonce < oneOfOneWalletsCount + failAfter + 1; nonce += 1) {
89
+ const salt = `${SALT_PREFIX}${nonce}`;
90
+ const predictedAddr = await _predictSafeAddress(rpcUrl, network, setupParamsEncoded, salt);
91
+ const bytecode = await provider.getCode({ address: predictedAddr });
92
+ if (!bytecode) {
93
+ // safe does not exist
94
+ return predictedAddr;
95
+ }
96
+ }
97
+
98
+ return '';
99
+ };
@@ -0,0 +1,11 @@
1
+ import { NetworkNumber } from '@defisaver/positions-sdk';
2
+
3
+ export enum RequestType {
4
+ Signature = 'Signature',
5
+ EthCall = 'EthCall',
6
+ }
7
+
8
+ export interface Request {
9
+ type: RequestType;
10
+ getParams: (rpcUrl: string, network: NetworkNumber, userAddress: string) => Promise<any>;
11
+ }
@@ -1,3 +1,5 @@
1
1
  export * from './common';
2
2
  export * from './markets';
3
- export * from './exchange';
3
+ export * from './exchange';
4
+ export * from './safe';
5
+ export * from './execution';
@@ -0,0 +1,5 @@
1
+ export type Wallet = {
2
+ address: string;
3
+ type: string;
4
+ owners?: string[];
5
+ };