@epoch-protocol/inventory-sdk 1.0.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 (53) hide show
  1. package/.github/workflows/main.yml +91 -0
  2. package/dist/chainlink-ops/chainlink-abi.d.ts +40 -0
  3. package/dist/chainlink-ops/chainlink-abi.d.ts.map +1 -0
  4. package/dist/chainlink-ops/chainlink-abi.js +287 -0
  5. package/dist/chainlink-ops/chainlink-abi.js.map +1 -0
  6. package/dist/chainlink-ops/index.d.ts +8 -0
  7. package/dist/chainlink-ops/index.d.ts.map +1 -0
  8. package/dist/chainlink-ops/index.js +35 -0
  9. package/dist/chainlink-ops/index.js.map +1 -0
  10. package/dist/chainlink-ops/models.d.ts +2 -0
  11. package/dist/chainlink-ops/models.d.ts.map +1 -0
  12. package/dist/chainlink-ops/models.js +3 -0
  13. package/dist/chainlink-ops/models.js.map +1 -0
  14. package/dist/index.d.ts +8 -0
  15. package/dist/index.d.ts.map +1 -0
  16. package/dist/index.js +24 -0
  17. package/dist/index.js.map +1 -0
  18. package/dist/safe-ops/index.d.ts +20 -0
  19. package/dist/safe-ops/index.d.ts.map +1 -0
  20. package/dist/safe-ops/index.js +99 -0
  21. package/dist/safe-ops/index.js.map +1 -0
  22. package/dist/safe-ops/models.d.ts +22 -0
  23. package/dist/safe-ops/models.d.ts.map +1 -0
  24. package/dist/safe-ops/models.js +6 -0
  25. package/dist/safe-ops/models.js.map +1 -0
  26. package/dist/safe-ops/safe-abi.d.ts +43 -0
  27. package/dist/safe-ops/safe-abi.d.ts.map +1 -0
  28. package/dist/safe-ops/safe-abi.js +565 -0
  29. package/dist/safe-ops/safe-abi.js.map +1 -0
  30. package/dist/token/erc20-abi.d.ts +39 -0
  31. package/dist/token/erc20-abi.d.ts.map +1 -0
  32. package/dist/token/erc20-abi.js +226 -0
  33. package/dist/token/erc20-abi.js.map +1 -0
  34. package/dist/token/index.d.ts +9 -0
  35. package/dist/token/index.d.ts.map +1 -0
  36. package/dist/token/index.js +109 -0
  37. package/dist/token/index.js.map +1 -0
  38. package/dist/token/models.d.ts +27 -0
  39. package/dist/token/models.d.ts.map +1 -0
  40. package/dist/token/models.js +3 -0
  41. package/dist/token/models.js.map +1 -0
  42. package/package.json +22 -0
  43. package/src/chainlink-ops/chainlink-abi.ts +283 -0
  44. package/src/chainlink-ops/index.ts +53 -0
  45. package/src/chainlink-ops/models.ts +0 -0
  46. package/src/index.ts +7 -0
  47. package/src/safe-ops/index.ts +98 -0
  48. package/src/safe-ops/models.ts +23 -0
  49. package/src/safe-ops/safe-abi.ts +561 -0
  50. package/src/token/erc20-abi.ts +222 -0
  51. package/src/token/index.ts +126 -0
  52. package/src/token/models.ts +32 -0
  53. package/tsconfig.json +43 -0
@@ -0,0 +1,222 @@
1
+ export const ERC20_ABI = [
2
+ {
3
+ constant: true,
4
+ inputs: [],
5
+ name: "name",
6
+ outputs: [
7
+ {
8
+ name: "",
9
+ type: "string",
10
+ },
11
+ ],
12
+ payable: false,
13
+ stateMutability: "view",
14
+ type: "function",
15
+ },
16
+ {
17
+ constant: false,
18
+ inputs: [
19
+ {
20
+ name: "_spender",
21
+ type: "address",
22
+ },
23
+ {
24
+ name: "_value",
25
+ type: "uint256",
26
+ },
27
+ ],
28
+ name: "approve",
29
+ outputs: [
30
+ {
31
+ name: "",
32
+ type: "bool",
33
+ },
34
+ ],
35
+ payable: false,
36
+ stateMutability: "nonpayable",
37
+ type: "function",
38
+ },
39
+ {
40
+ constant: true,
41
+ inputs: [],
42
+ name: "totalSupply",
43
+ outputs: [
44
+ {
45
+ name: "",
46
+ type: "uint256",
47
+ },
48
+ ],
49
+ payable: false,
50
+ stateMutability: "view",
51
+ type: "function",
52
+ },
53
+ {
54
+ constant: false,
55
+ inputs: [
56
+ {
57
+ name: "_from",
58
+ type: "address",
59
+ },
60
+ {
61
+ name: "_to",
62
+ type: "address",
63
+ },
64
+ {
65
+ name: "_value",
66
+ type: "uint256",
67
+ },
68
+ ],
69
+ name: "transferFrom",
70
+ outputs: [
71
+ {
72
+ name: "",
73
+ type: "bool",
74
+ },
75
+ ],
76
+ payable: false,
77
+ stateMutability: "nonpayable",
78
+ type: "function",
79
+ },
80
+ {
81
+ constant: true,
82
+ inputs: [],
83
+ name: "decimals",
84
+ outputs: [
85
+ {
86
+ name: "",
87
+ type: "uint8",
88
+ },
89
+ ],
90
+ payable: false,
91
+ stateMutability: "view",
92
+ type: "function",
93
+ },
94
+ {
95
+ constant: true,
96
+ inputs: [
97
+ {
98
+ name: "_owner",
99
+ type: "address",
100
+ },
101
+ ],
102
+ name: "balanceOf",
103
+ outputs: [
104
+ {
105
+ name: "balance",
106
+ type: "uint256",
107
+ },
108
+ ],
109
+ payable: false,
110
+ stateMutability: "view",
111
+ type: "function",
112
+ },
113
+ {
114
+ constant: true,
115
+ inputs: [],
116
+ name: "symbol",
117
+ outputs: [
118
+ {
119
+ name: "",
120
+ type: "string",
121
+ },
122
+ ],
123
+ payable: false,
124
+ stateMutability: "view",
125
+ type: "function",
126
+ },
127
+ {
128
+ constant: false,
129
+ inputs: [
130
+ {
131
+ name: "_to",
132
+ type: "address",
133
+ },
134
+ {
135
+ name: "_value",
136
+ type: "uint256",
137
+ },
138
+ ],
139
+ name: "transfer",
140
+ outputs: [
141
+ {
142
+ name: "",
143
+ type: "bool",
144
+ },
145
+ ],
146
+ payable: false,
147
+ stateMutability: "nonpayable",
148
+ type: "function",
149
+ },
150
+ {
151
+ constant: true,
152
+ inputs: [
153
+ {
154
+ name: "_owner",
155
+ type: "address",
156
+ },
157
+ {
158
+ name: "_spender",
159
+ type: "address",
160
+ },
161
+ ],
162
+ name: "allowance",
163
+ outputs: [
164
+ {
165
+ name: "",
166
+ type: "uint256",
167
+ },
168
+ ],
169
+ payable: false,
170
+ stateMutability: "view",
171
+ type: "function",
172
+ },
173
+ {
174
+ payable: true,
175
+ stateMutability: "payable",
176
+ type: "fallback",
177
+ },
178
+ {
179
+ anonymous: false,
180
+ inputs: [
181
+ {
182
+ indexed: true,
183
+ name: "owner",
184
+ type: "address",
185
+ },
186
+ {
187
+ indexed: true,
188
+ name: "spender",
189
+ type: "address",
190
+ },
191
+ {
192
+ indexed: false,
193
+ name: "value",
194
+ type: "uint256",
195
+ },
196
+ ],
197
+ name: "Approval",
198
+ type: "event",
199
+ },
200
+ {
201
+ anonymous: false,
202
+ inputs: [
203
+ {
204
+ indexed: true,
205
+ name: "from",
206
+ type: "address",
207
+ },
208
+ {
209
+ indexed: true,
210
+ name: "to",
211
+ type: "address",
212
+ },
213
+ {
214
+ indexed: false,
215
+ name: "value",
216
+ type: "uint256",
217
+ },
218
+ ],
219
+ name: "Transfer",
220
+ type: "event",
221
+ },
222
+ ];
@@ -0,0 +1,126 @@
1
+ import { Contract, JsonRpcProvider, Provider, Wallet } from "ethers";
2
+ import { Token, TokenInfo } from "./models";
3
+ import { ERC20_ABI } from "./erc20-abi";
4
+ import { createSafeTx, getSafeInstance, signSafeTx } from "../safe-ops";
5
+ import { SafeTransaction } from "@safe-global/types-kit";
6
+ import Safe from "@safe-global/protocol-kit";
7
+
8
+ export const getTokenBalance = async (
9
+ address: string,
10
+ token: Token,
11
+ provider: Provider
12
+ ): Promise<TokenInfo> => {
13
+ if (token.address === "0x0000000000000000000000000000000000000000") {
14
+ const balance = await provider.getBalance(address);
15
+ return {
16
+ balance,
17
+ token: token,
18
+ decimals: 18,
19
+ };
20
+ } else {
21
+ console.log("Getting token balance for", token.address);
22
+ console.log("ChainId", await provider.getNetwork());
23
+ const tokenContract = new Contract(token.address, ERC20_ABI, provider);
24
+ console.log("name", await getName(token, provider));
25
+ console.log("symbol", await getSymbol(token, provider));
26
+ console.log("decimals", await tokenContract?.decimals?.());
27
+ console.log("balance function", tokenContract?.balanceOf);
28
+
29
+ console.log("balance", await tokenContract?.balanceOf!(address));
30
+
31
+ const promises = Promise.all([
32
+ getName(token, provider),
33
+ getSymbol(token, provider),
34
+ tokenContract?.decimals?.(),
35
+ tokenContract?.balanceOf?.(address),
36
+ ]);
37
+ const [name, symbol, decimals, balance] = await promises;
38
+ return {
39
+ balance,
40
+ token,
41
+ decimals,
42
+ symbol,
43
+ name,
44
+ };
45
+ }
46
+ };
47
+
48
+ export const getMultipleTokenBalances = async (
49
+ tokens: Token[],
50
+ address: string,
51
+ provider: Provider
52
+ ): Promise<Array<TokenInfo>> => {
53
+ const promises = tokens.map((token) =>
54
+ getTokenBalance(address, token, provider)
55
+ );
56
+
57
+ const results = await Promise.all(promises);
58
+ console.log("results", results);
59
+ return results;
60
+ };
61
+
62
+ export const getSignedTransferCalldataForSafe = async (
63
+ safeInstance: Safe,
64
+ tokenAddress: string,
65
+ recipientAddress: string,
66
+ amount: BigInt
67
+ ): Promise<SafeTransaction> => {
68
+ if (tokenAddress === "0x0000000000000000000000000000000000000000") {
69
+ let safeTx = await createSafeTx(safeInstance, [
70
+ {
71
+ to: recipientAddress,
72
+ value: amount.toString(),
73
+ data: "0x",
74
+ operation: 0,
75
+ },
76
+ ]);
77
+
78
+ safeTx = await signSafeTx(safeInstance, safeTx);
79
+ return safeTx;
80
+ } else {
81
+ const tokenContract = new Contract(tokenAddress, ERC20_ABI);
82
+ const encodedFunctionCall = tokenContract.interface.encodeFunctionData(
83
+ "transfer",
84
+ [recipientAddress, amount]
85
+ );
86
+ console.log("encodedFunctionCall", encodedFunctionCall);
87
+ let safeTx = await createSafeTx(safeInstance, [
88
+ {
89
+ to: tokenAddress,
90
+ value: "0",
91
+ data: encodedFunctionCall,
92
+ operation: 0,
93
+ },
94
+ ]);
95
+
96
+ safeTx = await signSafeTx(safeInstance, safeTx);
97
+ console.log("safeTx signed", safeTx);
98
+ return safeTx;
99
+ }
100
+ };
101
+ export const getDecimals = async (token: Token, provider: Provider) => {
102
+ const erc20 = new Contract(token.address, ERC20_ABI, provider);
103
+ const decimals = await erc20?.decimals?.();
104
+ if (!decimals) {
105
+ throw new Error("Decimals not found");
106
+ }
107
+ return decimals;
108
+ };
109
+ const getName = async (token: Token, provider: Provider): Promise<string> => {
110
+ try {
111
+ const erc20 = new Contract(token.address, ERC20_ABI, provider);
112
+ const name = await erc20?.name?.();
113
+ return name;
114
+ } catch (error) {
115
+ return "";
116
+ }
117
+ };
118
+ const getSymbol = async (token: Token, provider: Provider): Promise<string> => {
119
+ try {
120
+ const erc20 = new Contract(token.address, ERC20_ABI, provider);
121
+ const symbol = await erc20?.symbol?.();
122
+ return symbol;
123
+ } catch (error) {
124
+ return "";
125
+ }
126
+ };
@@ -0,0 +1,32 @@
1
+ import {
2
+ SafeSignature,
3
+ SafeTransaction,
4
+ SafeTransactionData,
5
+ } from "@safe-global/types-kit";
6
+ import { DeconstructedSafeSignature } from "../safe-ops/models";
7
+ export interface TokenInfo {
8
+ token: Token;
9
+ decimals: number;
10
+ balance?: BigInt;
11
+ symbol?: string;
12
+ name?: string;
13
+ //with 10**18
14
+ price?: BigInt;
15
+ usdValue?: BigInt;
16
+ }
17
+
18
+ export interface Token {
19
+ address: string;
20
+ chainId: number;
21
+ priceFeed: {
22
+ priceFeedAddress: string;
23
+ priceFeedDecimals?: number;
24
+ priceFeedChainId: number;
25
+ };
26
+ }
27
+ export interface OptimalTokenWithCalldata {
28
+ token: TokenInfo;
29
+ safeTx: SafeTransactionData;
30
+ safeSignatures: DeconstructedSafeSignature[];
31
+ treasuryAddress: string;
32
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ // Visit https://aka.ms/tsconfig to read more about this file
3
+ "compilerOptions": {
4
+ // File Layout
5
+ // "rootDir": "./src",
6
+ "outDir": "./dist",
7
+
8
+ // Environment Settings
9
+ // See also https://aka.ms/tsconfig/module
10
+ "module": "nodenext",
11
+ "target": "esnext",
12
+ "types": [],
13
+ // For nodejs:
14
+ // "lib": ["esnext"],
15
+ // "types": ["node"],
16
+ // and npm install -D @types/node
17
+
18
+ // Other Outputs
19
+ "sourceMap": true,
20
+ "declaration": true,
21
+ "declarationMap": true,
22
+
23
+ // Stricter Typechecking Options
24
+ "noUncheckedIndexedAccess": true,
25
+ "exactOptionalPropertyTypes": true,
26
+
27
+ // Style Options
28
+ // "noImplicitReturns": true,
29
+ // "noImplicitOverride": true,
30
+ // "noUnusedLocals": true,
31
+ // "noUnusedParameters": true,
32
+ // "noFallthroughCasesInSwitch": true,
33
+ // "noPropertyAccessFromIndexSignature": true,
34
+
35
+ // Recommended Options
36
+ "strict": true,
37
+ "jsx": "react-jsx",
38
+ "isolatedModules": true,
39
+ "noUncheckedSideEffectImports": true,
40
+ "moduleDetection": "force",
41
+ "skipLibCheck": true,
42
+ }
43
+ }