Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Staker snapshot script #341

Merged
merged 5 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions staking/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ENDPOINT="https://api.mainnet-beta.solana.com"
1 change: 1 addition & 0 deletions staking/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ target
**/*.rs.bk
node_modules
lib
.env
10 changes: 9 additions & 1 deletion staking/app/ProfileConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ export class ProfileConnection {
.accounts({ identityAccount: identityAccountAddress })
.rpc();
}

getIdentityFromProfileAccountData(accountData: Buffer): string {
const decoded = this.program.coder.accounts.decode(
"IdentityAccount",
Buffer.from(accountData)
);
return getIdentityAsString(decoded.identity);
}
}

export function areDifferentProfiles(
Expand All @@ -82,7 +90,7 @@ export function areDifferentProfiles(
return false;
}

function getIdentityAccountAddress(
export function getIdentityAccountAddress(
user: PublicKey,
ecosystem: Ecosystem
): PublicKey {
Expand Down
27 changes: 27 additions & 0 deletions staking/app/StakeConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,33 @@ export class StakeConnection {
);
return receipt !== null;
}

public getStakerAndAmountFromPositionAccountData(
positionAccountData: Buffer
): { owner: PublicKey; stakedAmount: BN } {
const positionAccountJs = new PositionAccountJs(
Buffer.from(positionAccountData),
IDL as Idl
);
const positionAccountWasm = new wasm.WasmPositionData(positionAccountData);

const time = new BN(Date.now() / 1000);
const currentEpoch = time.div(this.config.epochDuration);
const unlockingDuration = this.config.unlockingDuration;
const currentEpochBI = BigInt(currentEpoch.toString());

const lockedBalanceSummary = positionAccountWasm.getLockedBalanceSummary(
currentEpochBI,
unlockingDuration
);

return {
owner: positionAccountJs.owner,
stakedAmount: new BN(lockedBalanceSummary.locked.toString()).add(
new BN(lockedBalanceSummary.preunlocking.toString())
),
};
}
}

export interface BalanceSummary {
Expand Down
153 changes: 153 additions & 0 deletions staking/app/scripts/snapshot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import { Connection, Keypair, PublicKey } from "@solana/web3.js";
import { StakeConnection } from "../StakeConnection";
import { PROFILE_ADDRESS, STAKING_ADDRESS } from "../constants";
import axios from "axios";
import { bs58 } from "@coral-xyz/anchor/dist/cjs/utils/bytes";
import { ZstdInit, ZstdStream } from "@oneidentity/zstd-js";
import NodeWallet from "@coral-xyz/anchor/dist/cjs/nodewallet";
import {
ProfileConnection,
getIdentityAccountAddress,
} from "../ProfileConnection";
import * as fs from "fs";
import Papa from "papaparse";
import dotenv from "dotenv";
import BN from "bn.js";
dotenv.config();

const RPC_URL = process.env.ENDPOINT!;

// The JSON payload is too big when using the @solana/web3.js getProgramAccounts
// We get around this by using the base64+ztsd encoding instead of base64 that @solana/web3.js uses
async function getAllStakeAccounts(
url: string
): Promise<{ publicKey: PublicKey; data: string }[]> {
const response = await axios({
method: "post",
url: url,
data: {
jsonrpc: "2.0",
id: 1,
method: "getProgramAccounts",
params: [
STAKING_ADDRESS,
{
encoding: "base64+zstd",
filters: [
{
memcmp: {
offset: 0,
bytes: bs58.encode(Buffer.from("55c3f14f7cc04f0b", "hex")), // Positions account discriminator
},
},
],
},
],
},
});

return response.data.result.map((x: any) => {
return { publicKey: x.pubkey, data: x.account.data[0] };
});
}

async function getAllProfileAccounts(
url: string
): Promise<Record<string, any>> {
const response = await axios({
method: "post",
url: url,
data: {
jsonrpc: "2.0",
id: 1,
method: "getProgramAccounts",
params: [
PROFILE_ADDRESS,
{
encoding: "base64+zstd",
filters: [
{
memcmp: {
offset: 0,
bytes: bs58.encode(Buffer.from("c25ab5a0b6ce749e", "hex")), // Identity account discriminator
},
},
],
},
],
},
});

const mapping = response.data.result.reduce(
(obj: Record<string, any>, x: any) => {
obj[x.pubkey] = x.account.data[0];
return obj;
},
{} as Record<string, any>
);

return mapping;
}

const connection = new Connection(RPC_URL);
const profileConnection = new ProfileConnection(
connection,
new NodeWallet(new Keypair())
);

async function main() {
await ZstdInit();

const stakeConnection = await StakeConnection.connect(
connection,
new NodeWallet(new Keypair())
);
const stakeAccounts = await getAllStakeAccounts(RPC_URL);
const profileAccounts = await getAllProfileAccounts(RPC_URL);

const stakers: { owner: PublicKey; stakedAmount: BN }[] = stakeAccounts.map(
(x, index) => {
console.log("Processing staker :", index);
const accountData = ZstdStream.decompress(
new Uint8Array(Buffer.from(x.data, "base64"))
);
return stakeConnection.getStakerAndAmountFromPositionAccountData(
Buffer.from(accountData)
);
}
);

const stakersWithProfile = stakers.map(({ owner, stakedAmount }, index) => {
console.log("Processing profile :", index);
const profileAddress = getIdentityAccountAddress(owner, "evm");
let identity = "";
if (profileAccounts[profileAddress.toString()]) {
const accountData = ZstdStream.decompress(
new Uint8Array(
Buffer.from(profileAccounts[profileAddress.toString()], "base64")
)
);
identity = profileConnection.getIdentityFromProfileAccountData(
Buffer.from(accountData)
);
}
return {
solana: owner,
stakedAmount: stakedAmount.toString(),
evm: identity,
};
});

fs.writeFileSync(
"snapshot.json",
JSON.stringify(stakersWithProfile, null, 2),
"utf-8"
);
fs.writeFileSync(
"snapshot.csv",
Papa.unparse(stakersWithProfile, { header: true, skipEmptyLines: true }),
"utf-8"
);
}

main();
4 changes: 4 additions & 0 deletions staking/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@
"@ledgerhq/hw-transport-node-hid": "^6.27.21",
"@metaplex-foundation/js": "^0.17.5",
"@metaplex-foundation/mpl-token-metadata": "^2.5.1",
"@oneidentity/zstd-js": "^1.0.3",
"@types/bn.js": "^5.1.0",
"@types/mocha": "^9.1.1",
"@types/node": "^17.0.34",
"@types/shelljs": "^0.8.11",
"axios": "^1.6.7",
"chai": "^4.3.4",
"dotenv": "^16.4.1",
"mocha": "^9.2.2",
"papaparse": "^5.4.1",
"prettier": "^2.6.2",
"shelljs": "^0.8.5",
"ts-mocha": "^9.0.2",
Expand Down
Loading