Skip to content

Instantly share code, notes, and snippets.

@ochaloup
Last active October 24, 2024 05:52
Show Gist options
  • Select an option

  • Save ochaloup/740105bb093b59cbb58661fda2e77b27 to your computer and use it in GitHub Desktop.

Select an option

Save ochaloup/740105bb093b59cbb58661fda2e77b27 to your computer and use it in GitHub Desktop.
Marinade Liquid Stakng Program - Delayed Ticket
# gettting anchor discriminator (8 bytes at start of the Solana anchor generated account)
git clone https://github.com/marinade-finance/liquid-staking-program
cd liquid-staking-program
anchor expand
grep -e 'Discriminator.*TicketAccountData' -A 3 .anchor/expanded-macros/marinade-finance/marinade-finance-*
> impl anchor_lang::Discriminator for TicketAccountData {
> const DISCRIMINATOR: [u8; 8] = [133, 77, 18, 98, 211, 1, 231, 3];
tobase58.py '[133, 77, 18, 98, 211, 1, 231, 3]'
> PJBs1QeXfQN
# epoch 687
fromout.py 687 number
[175,2,0,0,0,0,0,0]
tobase58.py [175,2,0,0,0,0,0,0]
> WGo7Q1FyF8K
# check from particular account with solana cli
solana -um account 6PFLJZt2YgscwojtgHbrb9q18PwfQUXU62sAp1LPJkoH -o /tmp/a.bin
DEC_DATA=$(bintodec.sh /tmp/a.bin)
arraybyindex.sh $DEC_DATA 80 8
[175,2,0,0,0,0,0,0]
# see https://github.com/marinade-finance/liquid-staking-program/blob/anchor-0.27/programs/marinade-finance/src/state/delayed_unstake_ticket.rs
# TicketAccountData anchor discriminator is [133,77,18,98,211,1,231,3], base58: PJBs1QeXfQN
# epoch is at data index 80 (8 - anchor disc, 2*32 - pubkeys, 8 - ticket amount)
# 687: [175,2,0,0,0,0,0,0], base58: WGo7Q1FyF8K
curl $RPC_URL -X POST -H "Content-Type: application/json" -d '{
"jsonrpc": "2.0",
"id": 1,
"method": "getProgramAccounts",
"params": [
"MarBmsSgKXdrN1egZf5sqe1TMai9K1rChYNDJgjq7aD",
{
"encoding": "base64",
"filters": [
{
"memcmp": {
"offset": 0,
"bytes": "PJBs1QeXfQN"
}
},
{
"memcmp": {
"offset": 80,
"bytes": "WGo7Q1FyF8K"
}
}
]
}
]
}' | tee /tmp/accounts.json
#!/bin/bash
# expecting availability some of the tooling from gist here, like https://gist.github.com/ochaloup/58ceee3ed436766ba7c444bf3fbc8545
# alias solsdecimals='N=`xsel -p -o` ; if [ ${#N} -lt 9 ]; then echo ".${N}"; else SOLS="${N::-9}"; echo "${SOLS} .${N:${#SOLS}}"; fi'
solsdecimal() {
DECIMALS=9
N="$@"
[ "x$N" == "x" ] && N=`xsel -p -o`
[ "x$N" == "x" ] && echo "No input" && return 1
if [ ${#N} -lt $DECIMALS ]; then
FILLING_ZEROS=$(printf "%0.s0" $(seq 1 $((9-${#N}))))
echo "0.${FILLING_ZEROS}${N}"
else
SOLS="${N::-$DECIMALS}"
echo "${SOLS:-0}.${N:${#SOLS}}"
fi
}
# Process JSON and decode base64 data
while IFS= read -r line; do
# Check if line contains valid JSON
if echo "$line" | jq . >/dev/null 2>&1; then
# Extract the data field
data=$(echo "$line" | jq -r '.data')
# Decode base64 and store it
encoded=$(frombase64.py $data)
amount_bytes=$(arraybyindex.sh "$encoded" 72 8)
amount=$(toout.py "$amount_bytes" number)
amount_sols=$(solsdecimal "$amount")
# Add encoded field to JSON
JSON=$(echo "$line" | jq --arg encoded "$encoded" '. + {encoded: $encoded}')
JSON=$(echo "$JSON" | jq --arg num "$num" '. + {num: $num}')
JSON=$(echo "$JSON" | jq --arg amount "$amount" '. + {amount: $amount}')
JSON=$(echo "$JSON" | jq --arg amount_sols "$amount_sols" '. + {amount_sols: $amount_sols}')
echo "$JSON"
fi
done
# getting only pubkey and data, one record per line
jq -c '.result[] | {pubkey: .pubkey, data: .account.data[0]}' /tmp/accounts.json > /tmp/accounts.jqed.json
cat /tmp/accounts.jqed.json | data-parser.sh | tee /tmp/results.json
sed -i 's/"amount": "\([^"]\+\)"/"amount": \1/' /tmp/results.json
cat /tmp/results.json | jq -s 'sort_by(.amount) | reverse' > /tmp/results.sorted.json
jq -c '.[] | {pubkey, amount_sols}' /tmp/results.sorted.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment