@exponent-labs/adrena-idl 0.1.5

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/src/index.ts ADDED
@@ -0,0 +1,95 @@
1
+ import { BorshCoder, Idl, IdlAccounts, IdlTypes, web3 } from "@coral-xyz/anchor"
2
+ import BN from "bn.js"
3
+
4
+ import { Adrena } from "./adrena" // <-- generated types
5
+ import adrena_idl from "./adrena.json" // <-- the raw IDL
6
+
7
+ const ADRENA_POOL_IDL: Idl = adrena_idl as Idl
8
+
9
+ // Use type aliases that avoid recursive inference
10
+ // @ts-ignore
11
+ type AdrenaAccounts = IdlAccounts<Adrena>
12
+ type AdrenaTypes = IdlTypes<Adrena>
13
+ // @ts-ignore
14
+ export type PoolState = AdrenaAccounts["pool"]
15
+ // @ts-ignore
16
+ export type CustodyState = AdrenaAccounts["custody"]
17
+ export type FeesStats = AdrenaTypes["FeesStats"]
18
+
19
+ export const decodePoolAccount = async (
20
+ connection: web3.Connection,
21
+ accountPubkey: web3.PublicKey,
22
+ // @ts-ignore
23
+ ): Promise<PoolState> => {
24
+ const account = await connection.getAccountInfo(accountPubkey)
25
+
26
+ if (!account) {
27
+ throw new Error(`Could not fetch Adrena Pool ${accountPubkey.toString()}`)
28
+ }
29
+
30
+ const coder = new BorshCoder(ADRENA_POOL_IDL)
31
+
32
+ const decoded = coder.accounts.decode<PoolState>("Pool", account.data)
33
+ return decoded
34
+ }
35
+
36
+ export const encodePoolAccount = async (accountBuffer: PoolState) => {
37
+ const coder = new BorshCoder(ADRENA_POOL_IDL as unknown as Idl)
38
+
39
+ return coder.accounts.encode("Pool", accountBuffer)
40
+ }
41
+
42
+ export const decodeCustodyAccount = async (
43
+ connection: web3.Connection,
44
+ accountPubkey: web3.PublicKey,
45
+ ): Promise<CustodyState> => {
46
+ const account = await connection.getAccountInfo(accountPubkey)
47
+
48
+ if (!account) {
49
+ throw new Error(`Could not fetch Adrena Custody ${accountPubkey.toString()}`)
50
+ }
51
+
52
+ const coder = new BorshCoder(ADRENA_POOL_IDL)
53
+
54
+ const decoded = coder.accounts.decode<CustodyState>("Custody", account.data)
55
+ return decoded
56
+ }
57
+
58
+ export const encodeCustodyAccount = async (custody: CustodyState) => {
59
+ const coder = new BorshCoder(ADRENA_POOL_IDL as unknown as Idl)
60
+
61
+ return coder.accounts.encode("Custody", custody)
62
+ }
63
+
64
+ // Decode pool and custody accounts from raw account data (without additional RPC calls)
65
+ export const decodePoolAndCustodyAccounts = (accountInfos: { data: Buffer }[]) => {
66
+ const coder = new BorshCoder(ADRENA_POOL_IDL as unknown as Idl)
67
+
68
+ // @ts-ignore
69
+ const poolAccount = coder.accounts.decode("Pool", accountInfos[0].data) as PoolState
70
+ const custodyAccounts = [
71
+ coder.accounts.decode("Custody", accountInfos[1].data) as CustodyState,
72
+ coder.accounts.decode("Custody", accountInfos[2].data) as CustodyState,
73
+ coder.accounts.decode("Custody", accountInfos[3].data) as CustodyState,
74
+ coder.accounts.decode("Custody", accountInfos[4].data) as CustodyState,
75
+ ]
76
+
77
+ return { poolAccount, custodyAccounts }
78
+ }
79
+
80
+ // Calculate total fees from custody accounts
81
+ export const calculateTotalFeesFromCustodies = (custodyAccounts: CustodyState[]): BN => {
82
+ return custodyAccounts.reduce((acc: BN, c: CustodyState) => {
83
+ const fees = c.collectedFees as FeesStats
84
+
85
+ // @ts-ignore
86
+ const totalCustodyFees = fees.swapUsd
87
+ .add(fees.addLiquidityUsd)
88
+ .add(fees.removeLiquidityUsd)
89
+ .add(fees.closePositionUsd)
90
+ .add(fees.liquidationUsd)
91
+ .add(fees.borrowUsd)
92
+
93
+ return acc.add(totalCustodyFees)
94
+ }, new BN(0))
95
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "files": ["src/index.ts", "src/adrena.json", "src/adrena.ts"],
3
+ "compilerOptions": {
4
+ "rootDir": "src",
5
+ "sourceMap": true,
6
+ "incremental": true /* Save .tsbuildinfo files to allow for incremental compilation of projects. */,
7
+ "composite": true /* Enable constraints that allow a TypeScript project to be used with project references. */,
8
+ "target": "ESNext" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
9
+ "module": "CommonJS" /* Specify what module code is generated. */,
10
+ "declaration": true /* Generate .d.ts files from TypeScript and JavaScript files in your project. */,
11
+ "outDir": "./build" /* Specify an output folder for all emitted files. */,
12
+ "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
13
+ "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
14
+ "skipLibCheck": true /* Skip type checking all .d.ts files. */,
15
+ "resolveJsonModule": true
16
+ }
17
+ }