Trireme commited on
Commit
e5a819a
·
verified ·
1 Parent(s): 0d887ad

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. app.py +45 -23
  2. meteora/index.js +23 -14
app.py CHANGED
@@ -11,27 +11,50 @@ import datetime
11
  import os
12
  import subprocess, json
13
 
14
- result = subprocess.run(
15
- ["node", "meteora/index.js"],
16
- capture_output=True,
17
- text=True
18
- )
19
-
20
- print("STDOUT:", result.stdout)
21
- print("STDERR:", result.stderr)
22
-
23
- #if result.returncode != 0:
24
- # raise RuntimeError(f"Node.js error: {result.stderr}")
25
-
26
- positions = json.loads(result.stdout)
27
- print(positions)
28
- lp_df_real = pd.json_normalize(positions)
29
- print(lp_df_real)
30
- lp_df_real.drop(columns=['positionAccountAddress'],inplace=True)
31
- y_df_real = lp_df_real[['tokenYAddress','tokenYAmount']].copy()
32
- lp_df_real.rename(columns={'tokenXAddress':'symbol','tokenXAmount':'balance'},inplace=True)
33
- y_df_real.rename(columns={'tokenYAddress':'symbol','tokenYAmount':'balance'},inplace=True)
34
- final_lp_df = pd.concat([lp_df_real,y_df_real]).dropna(axis=1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
  wallets = [
37
  "41KL7hbeA2dDuX6gCUFhVaozACnx2hLvGBuY4tgrpJ31",
@@ -163,8 +186,7 @@ async def get_all_dfs():
163
  sol_price = float(final_df.loc[final_df['symbol']=='SOL','value'] / final_df.loc[final_df['symbol']=='SOL','balance'])
164
  symbolMap = {'So11111111111111111111111111111111111111112':'SOL','Aq8Gocyvyyi8xk5EYxd6viUfVmVvs9T9R6mZFzZFpump':'HYPER'}
165
  priceMap = {'SOL':sol_price,'HYPER':hyper_price}
166
- lp_df0 = final_lp_df
167
- print(lp_df0)
168
  lp_df0['symbol'] = lp_df0['symbol'].map(symbolMap)
169
  lp_df0['value'] = lp_df0['balance'] * lp_df0['symbol'].map(priceMap)
170
  lp_df0 = lp_df0.groupby('symbol').agg({'balance':'sum','value':'sum'}).reset_index()
 
11
  import os
12
  import subprocess, json
13
 
14
+ def get_lp_positions(node_path="meteora/index.js"):
15
+ """
16
+ Runs the Node.js script, parses the output, and returns a combined DataFrame
17
+ with tokenX and tokenY positions.
18
+ """
19
+ try:
20
+ # Run Node.js script
21
+ result = subprocess.run(
22
+ ["node", node_path],
23
+ capture_output=True,
24
+ text=True
25
+ )
26
+
27
+ # Optional: print warnings
28
+ if result.stderr.strip():
29
+ print("Node STDERR:", result.stderr)
30
+
31
+ # Handle Node.js errors
32
+ if result.returncode != 0:
33
+ print("Node.js exited with an error")
34
+ return pd.DataFrame() # return empty DataFrame on failure
35
+
36
+ # Parse JSON output
37
+ positions = json.loads(result.stdout) if result.stdout.strip() else []
38
+ if not positions:
39
+ return pd.DataFrame()
40
+
41
+ # Normalize JSON to DataFrame
42
+ lp_df_real = pd.json_normalize(positions)
43
+ lp_df_real.drop(columns=['positionAccountAddress'], inplace=True, errors='ignore')
44
+
45
+ # Split tokenY
46
+ y_df_real = lp_df_real[['tokenYAddress','tokenYAmount']].copy()
47
+ lp_df_real.rename(columns={'tokenXAddress':'symbol','tokenXAmount':'balance'}, inplace=True)
48
+ y_df_real.rename(columns={'tokenYAddress':'symbol','tokenYAmount':'balance'}, inplace=True)
49
+
50
+ # Combine tokenX and tokenY DataFrames
51
+ final_lp_df = pd.concat([lp_df_real, y_df_real], ignore_index=True).dropna(axis=1)
52
+
53
+ return final_lp_df
54
+
55
+ except Exception as e:
56
+ print(f"Error in get_lp_positions: {e}")
57
+ return pd.DataFrame() # fallback empty DataFrame
58
 
59
  wallets = [
60
  "41KL7hbeA2dDuX6gCUFhVaozACnx2hLvGBuY4tgrpJ31",
 
186
  sol_price = float(final_df.loc[final_df['symbol']=='SOL','value'] / final_df.loc[final_df['symbol']=='SOL','balance'])
187
  symbolMap = {'So11111111111111111111111111111111111111112':'SOL','Aq8Gocyvyyi8xk5EYxd6viUfVmVvs9T9R6mZFzZFpump':'HYPER'}
188
  priceMap = {'SOL':sol_price,'HYPER':hyper_price}
189
+ lp_df0 = get_lp_positions()
 
190
  lp_df0['symbol'] = lp_df0['symbol'].map(symbolMap)
191
  lp_df0['value'] = lp_df0['balance'] * lp_df0['symbol'].map(priceMap)
192
  lp_df0 = lp_df0.groupby('symbol').agg({'balance':'sum','value':'sum'}).reset_index()
meteora/index.js CHANGED
@@ -1,26 +1,32 @@
1
  const DLMM = require('@meteora-ag/dlmm').default;
2
- const {Connection, PublicKey} = require('@solana/web3.js');
3
 
4
  const POOL_ADDRESS = "GZ65Nw5MCJUaEEDFdpoV5CNgnyh3JDZJB6JFtAXEAjeH";
5
  const USER_ADDRESS = "5Xihd2FdAXZnER7HWjT8GiZm14hyPiUpGfqLLL86yaa9";
6
 
7
  (async () => {
8
  try {
9
- const connection = new Connection('https://mainnet.helius-rpc.com/?api-key=4754acdc-8330-4d7d-98bd-b9b35af1b178');
 
 
 
10
  const dlmmPool = await DLMM.create(connection, new PublicKey(POOL_ADDRESS));
11
- const {tokenX,tokenY}=dlmmPool
12
- const {address:tokenXAddress,decimals:tokenXDecimals}=tokenX.mint;
13
- const {address:tokenYAddress,decimals:tokenYDecimals}=tokenY.mint;
14
 
15
- const {userPositions} = await dlmmPool.getPositionsByUserAndLbPair(new PublicKey(USER_ADDRESS));
 
 
 
16
 
17
  const results = [];
18
-
19
- userPositions.forEach((position) => {
20
  const { positionData } = position;
21
  const { totalXAmount, totalYAmount } = positionData;
22
- const formattedXAmount = totalXAmount / (10 ** tokenXDecimals);
23
- const formattedYAmount = totalYAmount / (10 ** tokenYDecimals);
24
 
25
  results.push({
26
  positionAccountAddress: position.publicKey.toBase58(),
@@ -29,11 +35,14 @@ const USER_ADDRESS = "5Xihd2FdAXZnER7HWjT8GiZm14hyPiUpGfqLLL86yaa9";
29
  tokenYAddress: tokenYAddress.toBase58(),
30
  tokenYAmount: formattedYAmount
31
  });
32
- });
33
 
34
- console.log(JSON.stringify(results, null, 2));
 
 
35
  } catch (error) {
36
- console.log(error);
 
37
  process.exit(1);
38
  }
39
- })();
 
1
  const DLMM = require('@meteora-ag/dlmm').default;
2
+ const { Connection, PublicKey } = require('@solana/web3.js');
3
 
4
  const POOL_ADDRESS = "GZ65Nw5MCJUaEEDFdpoV5CNgnyh3JDZJB6JFtAXEAjeH";
5
  const USER_ADDRESS = "5Xihd2FdAXZnER7HWjT8GiZm14hyPiUpGfqLLL86yaa9";
6
 
7
  (async () => {
8
  try {
9
+ const connection = new Connection(
10
+ 'https://mainnet.helius-rpc.com/?api-key=4754acdc-8330-4d7d-98bd-b9b35af1b178'
11
+ );
12
+
13
  const dlmmPool = await DLMM.create(connection, new PublicKey(POOL_ADDRESS));
14
+ const { tokenX, tokenY } = dlmmPool;
15
+ const { address: tokenXAddress, decimals: tokenXDecimals } = tokenX.mint;
16
+ const { address: tokenYAddress, decimals: tokenYDecimals } = tokenY.mint;
17
 
18
+ // Ensure userPositions is always an array
19
+ const { userPositions = [] } = await dlmmPool.getPositionsByUserAndLbPair(
20
+ new PublicKey(USER_ADDRESS)
21
+ );
22
 
23
  const results = [];
24
+
25
+ for (const position of userPositions) {
26
  const { positionData } = position;
27
  const { totalXAmount, totalYAmount } = positionData;
28
+ const formattedXAmount = totalXAmount / 10 ** tokenXDecimals;
29
+ const formattedYAmount = totalYAmount / 10 ** tokenYDecimals;
30
 
31
  results.push({
32
  positionAccountAddress: position.publicKey.toBase58(),
 
35
  tokenYAddress: tokenYAddress.toBase58(),
36
  tokenYAmount: formattedYAmount
37
  });
38
+ }
39
 
40
+ // Always output a JSON array
41
+ console.log(JSON.stringify(results));
42
+ process.stdout.write("\n"); // ensure stdout flushes
43
  } catch (error) {
44
+ // Output error as JSON to keep Python parsing safe
45
+ console.error(JSON.stringify({ error: error.message || error }));
46
  process.exit(1);
47
  }
48
+ })();