Skip to content

Commit

Permalink
Merge pull request #19 from sunrise-stake/fund-sender-client-scripts
Browse files Browse the repository at this point in the history
Add scripts for using fund-sender client functions
  • Loading branch information
dankelleher committed Oct 25, 2023
2 parents 1f37e74 + 7cec487 commit 8bbf1a7
Show file tree
Hide file tree
Showing 6 changed files with 345 additions and 0 deletions.
28 changes: 28 additions & 0 deletions packages/fund-sender/scripts/getState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { FundSenderClient } from "../client";
import { logBalance } from "./lib/util";
import { PublicKey } from "@solana/web3.js";

// mainnet Sunrise
const defaultSunriseStateAddress =
"43m66crxGfXSJpmx5wXRoFuHubhHA1GCvtHgmHW6cM1P";
const sunriseStateAddress = new PublicKey(
process.env.STATE_ADDRESS ?? defaultSunriseStateAddress
);

// USAGE: yarn ts-node packages/fund-sender/getState.ts destinationName
const destinationName = process.argv[2];

(async () => {
const stateAddress = FundSenderClient.getStateAddressFromSunriseAddress(
sunriseStateAddress,
destinationName
);
const client = await FundSenderClient.fetch(stateAddress);
const log = logBalance(client);

console.log("state address", stateAddress.toBase58());
console.log("state account data", client.config);
console.log("input address", client.getInputAccount().toBase58());

await log("input token", client.getInputAccount());
})().catch(console.error);
27 changes: 27 additions & 0 deletions packages/fund-sender/scripts/lib/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os from "os";
import { PublicKey, Cluster, clusterApiUrl } from "@solana/web3.js";
import { getAccount } from "@solana/spl-token";
import { FundSenderClient } from "../../client";

export const idWallet = os.homedir() + "/.config/solana/id.json";

process.env.ANCHOR_PROVIDER_URL =
process.env.ANCHOR_PROVIDER_URL ??
clusterApiUrl(
(process.env.REACT_APP_SOLANA_NETWORK ?? "devnet" ?? "") as Cluster
);
process.env.ANCHOR_WALLET = process.env.ANCHOR_WALLET ?? idWallet;

export const logBalance =
(client: FundSenderClient) => async (prefix: string, account: PublicKey) => {
const accountInfo = await client.provider.connection.getAccountInfo(
account
);
console.log(`${prefix} balance`, accountInfo?.lamports);
};

export const logSplBalance =
(client: FundSenderClient) => async (prefix: string, account: PublicKey) => {
const accountInfo = await getAccount(client.provider.connection, account);
console.log(`${prefix} balance`, accountInfo?.amount);
};
43 changes: 43 additions & 0 deletions packages/fund-sender/scripts/registerState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* eslint-disable @typescript-eslint/no-var-requires */
import { FundSenderClient } from "../client";
import { Keypair, PublicKey } from "@solana/web3.js";
import BN from "bn.js";

// mainnet Sunrise
const defaultSunriseStateAddress =
"43m66crxGfXSJpmx5wXRoFuHubhHA1GCvtHgmHW6cM1P";
const sunriseStateAddress = new PublicKey(
process.env.STATE_ADDRESS ?? defaultSunriseStateAddress
);

// Sunrise Treasury
const defaultSunriseTreasuryAddress =
"Bup7DZk56XwQUDzuvBz9nzbr8e2iLPVrBpha1KTfEbbJ";
const sunriseTreasuryAddress = new PublicKey(
process.env.TREASURY_ADDRESS ?? defaultSunriseTreasuryAddress
);

// USAGE: yarn ts-node packages/fund-sender/resgisterState.ts destinationName destinationAccount
const destinationName = process.argv[2];
const destinationAccount = new PublicKey(process.argv[3]);

const defaultSpendThreshold = 1000000000;
const spendThreshold = new BN(
process.env.SPEND_THRESHOLD ?? defaultSpendThreshold
);

const anchorWallet = Keypair.fromSecretKey(
Buffer.from(require(process.env.ANCHOR_WALLET as string))
);

(async () => {
const state = await FundSenderClient.register(
sunriseStateAddress,
anchorWallet.publicKey,
destinationName,
destinationAccount,
sunriseTreasuryAddress,
spendThreshold
);
console.log("state account data", state.config);
})().catch(console.error);
34 changes: 34 additions & 0 deletions packages/fund-sender/scripts/sendFund.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { FundSenderClient } from "../client";
import { logBalance } from "./lib/util";
import { PublicKey } from "@solana/web3.js";

// mainnet Sunrise
const defaultSunriseStateAddress =
"43m66crxGfXSJpmx5wXRoFuHubhHA1GCvtHgmHW6cM1P";
const sunriseStateAddress = new PublicKey(
process.env.STATE_ADDRESS ?? defaultSunriseStateAddress
);

// USAGE: yarn ts-node packages/fund-sender/sendFund.ts destinationName
const destinationName = process.argv[2];

(async () => {
const stateAddress = FundSenderClient.getStateAddressFromSunriseAddress(
sunriseStateAddress,
destinationName
);
const client = await FundSenderClient.fetch(stateAddress);

const log = logBalance(client);

console.log("state address", stateAddress.toBase58());
console.log("state account data", client.config);
console.log("input address", client.getInputAccount().toBase58());

await log("input token", client.getInputAccount());

console.log("Sending fund...");
await client.sendFunds();

await log("input token", client.getInputAccount());
})().catch(console.error);
79 changes: 79 additions & 0 deletions packages/fund-sender/scripts/storeCertificates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* eslint-disable @typescript-eslint/no-var-requires */
import { AnchorProvider } from "@coral-xyz/anchor";
import { FundSenderClient } from "../client";
import { logSplBalance } from "./lib/util";
import { Keypair, PublicKey } from "@solana/web3.js";
import {
getOrCreateAssociatedTokenAccount,
TOKEN_PROGRAM_ID,
} from "@solana/spl-token";

// mainnet Sunrise
const defaultSunriseStateAddress =
"43m66crxGfXSJpmx5wXRoFuHubhHA1GCvtHgmHW6cM1P";
const sunriseStateAddress = new PublicKey(
process.env.STATE_ADDRESS ?? defaultSunriseStateAddress
);

// USAGE: yarn ts-node packages/fund-sender/storeCertificate.ts destinationName
const destinationName = process.argv[2];

(async () => {
const stateAddress = FundSenderClient.getStateAddressFromSunriseAddress(
sunriseStateAddress,
destinationName
);
const client = await FundSenderClient.fetch(stateAddress);

const log = logSplBalance(client);

console.log("state address", stateAddress.toBase58());
console.log("state account data", client.config);
const provider = AnchorProvider.local();
const connection = provider.connection;
const anchorWallet = Keypair.fromSecretKey(
Buffer.from(require(process.env.ANCHOR_WALLET as string))
);
const allInputTokenAccountsResponse =
await client.provider.connection.getParsedTokenAccountsByOwner(
client.getInputAccount(),
{
programId: TOKEN_PROGRAM_ID,
}
);

const allInputTokenAccounts = allInputTokenAccountsResponse.value;
console.log(
"number of input certificate token addresses",
allInputTokenAccounts.length
);
console.log("Storing certificates...");
for (const inputTokenAccount of allInputTokenAccounts) {
console.log(
"input certificate token account:",
inputTokenAccount.pubkey.toBase58()
);

const mint = inputTokenAccount.account.data.parsed.info.mint;

const certificateVaultAta = await getOrCreateAssociatedTokenAccount(
connection,
anchorWallet,
mint,
client.config.certificateVault,
false
);

await client.storeCertificates(
inputTokenAccount.pubkey,
certificateVaultAta.address,
mint
);

await log(
"remaining input certificate token in account",
inputTokenAccount.pubkey
);
await log("token in certificate vault ATA", certificateVaultAta.address);
}
})().catch(console.error);
134 changes: 134 additions & 0 deletions packages/fund-sender/scripts/updateState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/* eslint-disable @typescript-eslint/no-var-requires */
import { FundSenderClient } from "../client";
import { PublicKey } from "@solana/web3.js";
import BN from "bn.js";
import * as readline from "readline/promises";

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

// mainnet Sunrise
const defaultSunriseStateAddress =
"43m66crxGfXSJpmx5wXRoFuHubhHA1GCvtHgmHW6cM1P";
const sunriseStateAddress = new PublicKey(
process.env.STATE_ADDRESS ?? defaultSunriseStateAddress
);

// USAGE: yarn ts-node packages/fund-sender/updateState.ts destinationName
const destinationName = process.argv[2];

(async () => {
// Get new update authority
let newUpdateAuthority: PublicKey | undefined;

const updateAuthority = await rl.question(
"New Update-Authority Address (Leave empty if you don't want to update it): "
);
if (updateAuthority === "") {
console.log("Don't update Update-Authority");
} else {
try {
newUpdateAuthority = new PublicKey(updateAuthority);
console.log("New Update-Authority:", updateAuthority);
} catch (e) {
console.log(e);
}
}

// Get new certificate vault
let newCertificateVault: PublicKey | undefined;

const certificateVault = await rl.question(
"New certificate vault Address (Leave empty if you don't want to update it): "
);
if (certificateVault === "") {
console.log("Don't update certificate vault");
} else {
try {
newCertificateVault = new PublicKey(certificateVault);
console.log("New certificate vault:", certificateVault);
} catch (e) {
console.log(e);
}
}

// Get new destination account
let newDestinationAccount: PublicKey | undefined;

const destinationAccount = await rl.question(
"New Destination Account (Leave empty if you don't want to update it): "
);
if (destinationAccount === "") {
console.log("Don't update Destination Account");
} else {
try {
newDestinationAccount = new PublicKey(destinationAccount);
console.log("New Destination Account:", destinationAccount);
} catch (e) {
console.log(e);
}
}

// Get new spend threshold
let newSpendThreshold: BN | undefined;

const spendThreshold = await rl.question(
"New Spend Threshold (in lamports; leave empty if you don't want to update it): "
);
if (spendThreshold === "") {
console.log("Don't update spending threshold");
} else {
try {
newSpendThreshold = new BN(spendThreshold);
console.log("New Spend Threshold:", spendThreshold);
} catch (e) {
console.log(e);
}
}

rl.close();

const stateAddress = FundSenderClient.getStateAddressFromSunriseAddress(
sunriseStateAddress,
destinationName
);
const client = await FundSenderClient.fetch(stateAddress);
if (newDestinationAccount === undefined) {
const oldDestinationAccount: PublicKey = client.config.destinationAccount;
newDestinationAccount = oldDestinationAccount;
}

if (newSpendThreshold === undefined) {
const oldSpendThreshold: BN = client.config.spendThreshold;
newSpendThreshold = oldSpendThreshold;
}

// Update destination account and spend threshold
if (destinationAccount !== "" || spendThreshold !== "") {
const state = await client.updateDestinationAccount(
newDestinationAccount,
newSpendThreshold
);
console.log(
"state account data after updating destination account and/or spending threshold",
state.config
);
}

// Update authority
if (newUpdateAuthority !== undefined) {
const state = await client.updateUpdateAuthority(newUpdateAuthority);
console.log("state account data after updating authority", state.config);
}

// Update certificate vault
if (newCertificateVault !== undefined) {
const state = await client.updateCertificateVault(newCertificateVault);
console.log(
"state account data after updating certificate vault",
state.config
);
}
})().catch(console.error);

0 comments on commit 8bbf1a7

Please sign in to comment.