Skip to content

Commit

Permalink
add decode-logs cli command
Browse files Browse the repository at this point in the history
  • Loading branch information
wphan committed Dec 6, 2023
1 parent 09b7bd9 commit 10a53d4
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 8 deletions.
8 changes: 7 additions & 1 deletion ts/sdk/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
withdraw,
forceWithdraw,
listDepositorsForVault,
managerUpdateMarginTradingEnabled
managerUpdateMarginTradingEnabled,
decodeLogs,
} from "./commands";

import { Command, Option } from 'commander';
Expand Down Expand Up @@ -141,6 +142,11 @@ program
.description("Forces the vault to send out a withdraw after the redeem period has passed")
.addOption(new Option("--vault-depositor-address <vaultDepositorAddress>", "VaultDepositor address").makeOptionMandatory(false))
.action((opts) => forceWithdraw(program, opts));
program
.command("decode-logs")
.description("Decode program logs from a txid")
.addOption(new Option("--tx <tx>", "Transaction hash").makeOptionMandatory(true))
.action((opts) => decodeLogs(program, opts));

program.parseAsync().then(() => {
process.exit(0);
Expand Down
68 changes: 68 additions & 0 deletions ts/sdk/cli/commands/decodeLogs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { PublicKey } from "@solana/web3.js";

Check failure on line 1 in ts/sdk/cli/commands/decodeLogs.ts

View workflow job for this annotation

GitHub Actions / yarn-lint

'PublicKey' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 1 in ts/sdk/cli/commands/decodeLogs.ts

View workflow job for this annotation

GitHub Actions / update-sdk

'PublicKey' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 1 in ts/sdk/cli/commands/decodeLogs.ts

View workflow job for this annotation

GitHub Actions / update-sdk

'PublicKey' is defined but never used. Allowed unused vars must match /^_/u
import {
OptionValues,
Command
} from "commander";
import { getCommandContext } from "../utils";
import { VaultDepositorRecord } from "../../src";
import { BN, TEN, convertToNumber, getVariant } from "@drift-labs/sdk";
import { conversationContext } from "@slack/bolt/dist/conversation-store";

Check failure on line 9 in ts/sdk/cli/commands/decodeLogs.ts

View workflow job for this annotation

GitHub Actions / yarn-lint

'conversationContext' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 9 in ts/sdk/cli/commands/decodeLogs.ts

View workflow job for this annotation

GitHub Actions / update-sdk

'conversationContext' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 9 in ts/sdk/cli/commands/decodeLogs.ts

View workflow job for this annotation

GitHub Actions / update-sdk

'conversationContext' is defined but never used. Allowed unused vars must match /^_/u

export const decodeLogs = async (program: Command, cmdOpts: OptionValues) => {

let txId: string;
try {
txId = cmdOpts.tx as string;
} catch (err) {
console.error("Invalid transaction hash");
process.exit(1);
}
if (!txId) {
console.error("Invalid transaction hash");
process.exit(1);
}

const {
driftVault,
driftClient,
} = await getCommandContext(program, false);

const tx = await driftClient.connection.getParsedTransaction(txId, {
commitment: "confirmed",
maxSupportedTransactionVersion: 0,
});

// @ts-ignore
for (const event of driftVault.program._events._eventParser.parseLogs(
tx!.meta!.logMessages
)) {
switch (event.name) {
case "VaultDepositorRecord":
const data: VaultDepositorRecord = event.data;

Check failure on line 41 in ts/sdk/cli/commands/decodeLogs.ts

View workflow job for this annotation

GitHub Actions / yarn-lint

Unexpected lexical declaration in case block

Check failure on line 41 in ts/sdk/cli/commands/decodeLogs.ts

View workflow job for this annotation

GitHub Actions / update-sdk

Unexpected lexical declaration in case block

Check failure on line 41 in ts/sdk/cli/commands/decodeLogs.ts

View workflow job for this annotation

GitHub Actions / update-sdk

Unexpected lexical declaration in case block

// const vault = await driftVault.getVault(data.vault);
const spotMarket = driftClient.getSpotMarketAccount(data.spotMarketIndex);

Check failure on line 44 in ts/sdk/cli/commands/decodeLogs.ts

View workflow job for this annotation

GitHub Actions / yarn-lint

Unexpected lexical declaration in case block

Check failure on line 44 in ts/sdk/cli/commands/decodeLogs.ts

View workflow job for this annotation

GitHub Actions / update-sdk

Unexpected lexical declaration in case block

Check failure on line 44 in ts/sdk/cli/commands/decodeLogs.ts

View workflow job for this annotation

GitHub Actions / update-sdk

Unexpected lexical declaration in case block
const spotPrecision = TEN.pow(new BN(spotMarket!.decimals));

Check failure on line 45 in ts/sdk/cli/commands/decodeLogs.ts

View workflow job for this annotation

GitHub Actions / yarn-lint

Unexpected lexical declaration in case block

Check failure on line 45 in ts/sdk/cli/commands/decodeLogs.ts

View workflow job for this annotation

GitHub Actions / update-sdk

Unexpected lexical declaration in case block

Check failure on line 45 in ts/sdk/cli/commands/decodeLogs.ts

View workflow job for this annotation

GitHub Actions / update-sdk

Unexpected lexical declaration in case block

console.log(event.name);
const date = new Date(data.ts.toNumber() * 1000);

Check failure on line 48 in ts/sdk/cli/commands/decodeLogs.ts

View workflow job for this annotation

GitHub Actions / yarn-lint

Unexpected lexical declaration in case block

Check failure on line 48 in ts/sdk/cli/commands/decodeLogs.ts

View workflow job for this annotation

GitHub Actions / update-sdk

Unexpected lexical declaration in case block

Check failure on line 48 in ts/sdk/cli/commands/decodeLogs.ts

View workflow job for this annotation

GitHub Actions / update-sdk

Unexpected lexical declaration in case block
console.log(` ts: ${date.toISOString()} (${data.ts.toNumber()})`);
console.log(` vault: ${data.vault.toBase58()}`);
console.log(` depositorAuthority: ${data.depositorAuthority.toBase58()}`);
console.log(` action: ${getVariant(data.action)}`);
console.log(` amount: ${convertToNumber(data.amount, spotPrecision)}`);
console.log(` vaultSharesBefore: ${data.vaultSharesBefore.toNumber()}`);
console.log(` vaultSharesAfter: ${data.vaultSharesAfter.toNumber()} (${data.vaultSharesAfter.toNumber() - data.vaultSharesBefore.toNumber()})`);
console.log(` vaultEquityBefore: ${convertToNumber(data.vaultEquityBefore, spotPrecision)}`);
console.log(` userVaultSharesBefore: ${data.userVaultSharesBefore.toNumber()}`);
console.log(` userVaultSharesAfter: ${data.userVaultSharesAfter.toNumber()} (${data.userVaultSharesAfter.toNumber() - data.userVaultSharesBefore.toNumber()})`);
console.log(` totalVaultSharesBefore: ${data.totalVaultSharesBefore.toNumber()}`);
console.log(` totalVaultSharesAfter: ${data.totalVaultSharesAfter.toNumber()} (${data.totalVaultSharesAfter.toNumber() - data.totalVaultSharesBefore.toNumber()})`);
console.log(` profitShare: ${data.profitShare.toNumber()}`);
console.log(` managementFee: ${data.managementFee.toNumber()}`);
console.log(` managementFeeShares: ${data.managementFeeShares.toNumber()}`);
break;
}
}

};
6 changes: 5 additions & 1 deletion ts/sdk/cli/commands/deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ export const deposit = async (program: Command, cmdOpts: OptionValues) => {
driftVault
} = await getCommandContext(program, true);

const spotMarket = driftClient.getSpotMarketAccount(0); // takes USDC deposits
const vaultDepositorAccount =
await driftVault.program.account.vaultDepositor.fetch(vaultDepositorAddress);
const vaultAddress = vaultDepositorAccount.vault;
const vaultAccount = await driftVault.program.account.vault.fetch(vaultAddress);
const spotMarket = driftClient.getSpotMarketAccount(vaultAccount.spotMarketIndex);
if (!spotMarket) {
throw new Error("No spot market found");
}
Expand Down
3 changes: 2 additions & 1 deletion ts/sdk/cli/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export * from './requestWithdraw';
export * from './forceWithdraw';
export * from './withdraw';
export * from './listDepositorsForVault';
export * from './managerUpdateMarginTradingEnabled';
export * from './managerUpdateMarginTradingEnabled';
export * from './decodeLogs';
6 changes: 3 additions & 3 deletions ts/sdk/cli/commands/initVault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const initVault = async (program: Command, cmdOpts: OptionValues) => {
spotMarketIndex = "0";
}
spotMarketIndex = parseInt(spotMarketIndex);
const spotMarket = driftClient.getSpotMarketAccount(spotMarketIndex); // takes USDC deposits
const spotMarket = driftClient.getSpotMarketAccount(spotMarketIndex);
if (!spotMarket) {
throw new Error("No spot market found");
}
Expand Down Expand Up @@ -77,8 +77,8 @@ export const initVault = async (program: Command, cmdOpts: OptionValues) => {
if (!minDepositAmount) {
minDepositAmount = "0";
}
minDepositAmount = parseInt(minDepositAmount);
const minDepositAmountBN = new BN(minDepositAmount).mul(spotPrecision);
minDepositAmount = parseFloat(minDepositAmount);
const minDepositAmountBN = new BN(spotPrecision.toNumber() * minDepositAmount);

let delegate = cmdOpts.delegate;
if (!delegate) {
Expand Down
3 changes: 2 additions & 1 deletion ts/sdk/cli/commands/managerDeposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export const managerDeposit = async (program: Command, cmdOpts: OptionValues) =>
driftVault
} = await getCommandContext(program, true);

const spotMarket = driftClient.getSpotMarketAccount(0); // takes USDC deposits
const vaultAccount = await driftVault.program.account.vault.fetch(vaultAddress);
const spotMarket = driftClient.getSpotMarketAccount(vaultAccount.spotMarketIndex);
if (!spotMarket) {
throw new Error("No spot market found");
}
Expand Down
4 changes: 3 additions & 1 deletion ts/sdk/cli/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ export async function getCommandContext(program: Command, needToSign: boolean):
}

const wallet = new Wallet(keypair);
console.log(`Signing wallet address (need to sign: ${needToSign}): `, wallet.publicKey.toBase58());
if (needToSign) {
console.log(`Signing wallet address: `, wallet.publicKey.toBase58());
}

const connection = new Connection(opts.url, {
commitment: opts.commitment,
Expand Down

0 comments on commit 10a53d4

Please sign in to comment.