forked from jito-foundation/jito-tip-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_switchboard_accounts.sh
executable file
·151 lines (119 loc) · 4.14 KB
/
fetch_switchboard_accounts.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/bin/bash
#------------------------------------------------------------------------------
# Notes
#------------------------------------------------------------------------------
# Switchboard Feed Links:
# (JTO/SOL) https://ondemand.switchboard.xyz/solana/mainnet/feed/5S7ErPSkFmyXuq2aE3rZ6ofwVyZpwzUt6w7m6kqekvMe
# (JITOSOL/SOL) https://ondemand.switchboard.xyz/solana/mainnet/feed/4Z1SLH9g4ikNBV8uP2ZctEouqjYmVqB2Tz5SZxKYBN7z
# Feeds need to be cranked at least once in history to work in tests
#------------------------------------------------------------------------------
# Configuration
#------------------------------------------------------------------------------
# Add your pubkeys here, one per line for readability
PUBKEYS=(
"5S7ErPSkFmyXuq2aE3rZ6ofwVyZpwzUt6w7m6kqekvMe" # JTO/SOL Binance Price
"4Z1SLH9g4ikNBV8uP2ZctEouqjYmVqB2Tz5SZxKYBN7z" # JITOSOL/SOL Redemption Price
)
# Output file path
OUTPUT_FILE="./integration_tests/tests/fixtures/generated_switchboard_accounts.rs"
#------------------------------------------------------------------------------
# Function Definitions
#------------------------------------------------------------------------------
write_file_header() {
cat << EOF > "$OUTPUT_FILE"
use std::str::FromStr;
use solana_sdk::{account::Account, pubkey::Pubkey};
// Auto-generated by format_switchboard_accounts.sh
pub fn get_switchboard_accounts() -> Vec<(Pubkey, Account)> {
let mut accounts = Vec::new();
EOF
}
write_account_header() {
local ACCOUNT="$1"
local OWNER="$2"
local LAMPORTS="$3"
local SPACE="$4"
cat << EOF >> "$OUTPUT_FILE"
{
let address = Pubkey::from_str("$ACCOUNT").unwrap();
let owner = Pubkey::from_str("$OWNER").unwrap();
let lamports = $LAMPORTS;
let space = $SPACE;
let mut account = Account::new(lamports, space, &owner);
let bytes = vec![
EOF
}
write_account_data() {
local ACCOUNT_DATA="$1"
echo "$ACCOUNT_DATA" | awk '
BEGIN {
count = 0
bytes_per_line = 16
}
/^[0-9a-fA-F]{4}:/ {
# Process each byte in the row (columns 2-17 for all 16 bytes)
for(i=2; i<=17; i++) {
if($i ~ /^[0-9a-fA-F]{2}$/) {
if (count > 0) printf ", "
if (count % bytes_per_line == 0 && count > 0) printf "\n "
printf "0x%s", $i
count++
}
}
}
END {
# Ensure we end with a newline
if (count > 0) printf "\n"
}
' >> "$OUTPUT_FILE"
}
write_account_footer() {
cat << EOF >> "$OUTPUT_FILE"
];
account.data = bytes;
accounts.push((address, account));
}
EOF
}
write_file_footer() {
cat << EOF >> "$OUTPUT_FILE"
accounts
}
EOF
}
process_account() {
local ACCOUNT="$1"
echo "Processing account: $ACCOUNT"
# Run solana account command and capture output
local ACCOUNT_DATA=$(solana account "$ACCOUNT" --lamports)
# Check if the command failed
if [ $? -ne 0 ]; then
echo "Error: Failed to fetch data for account $ACCOUNT"
return 1
fi
# Extract key information using grep and awk
local OWNER=$(echo "$ACCOUNT_DATA" | grep "Owner:" | awk '{print $2}')
local LAMPORTS=$(echo "$ACCOUNT_DATA" | grep "Balance:" | awk '{print $2}')
local SPACE=$(echo "$ACCOUNT_DATA" | grep "Length:" | awk '{print $2}')
write_account_header "$ACCOUNT" "$OWNER" "$LAMPORTS" "$SPACE"
write_account_data "$ACCOUNT_DATA"
write_account_footer
}
#------------------------------------------------------------------------------
# Main Script
#------------------------------------------------------------------------------
main() {
# Create the output directory if it doesn't exist
mkdir -p "$(dirname "$OUTPUT_FILE")"
# Write the file header
write_file_header
# Process each account
for ACCOUNT in "${PUBKEYS[@]}"; do
process_account "$ACCOUNT"
done
# Write the file footer
write_file_footer
echo "Generated Rust code has been written to $OUTPUT_FILE"
}
# Execute main function
main