Global Account

The Global Account is a crucial component of the PumpFundler protocol, storing protocol-wide parameters and configuration. It plays a vital role in managing the overall behavior of the system and ensuring consistency across all tokens created through the protocol.

Structure

The GlobalAccount class has the following properties:

class GlobalAccount {

  public discriminator: bigint;

  public initialized: boolean;

  public authority: PublicKey;

  public feeRecipient: PublicKey;

  public initialVirtualTokenReserves: bigint;

  public initialVirtualSolReserves: bigint;

  public initialRealTokenReserves: bigint;

  public tokenTotalSupply: bigint;

  public feeBasisPoints: bigint;



  // ... constructor and methods ...

}

Let’s break down each property:

  • discriminator: A unique identifier for the account type, ensuring proper deserialization.
  • initialized: A boolean flag indicating if the global account has been initialized.
  • authority: The public key of the account with administrative privileges over the protocol.
  • feeRecipient: The public key that receives protocol fees from all transactions.
  • initialVirtualTokenReserves: The initial virtual token reserves set for new bonding curves.
  • initialVirtualSolReserves: The initial virtual SOL reserves set for new bonding curves.
  • initialRealTokenReserves: The initial real token reserves for new bonding curves.
  • tokenTotalSupply: The total supply of tokens set for new bonding curves.
  • feeBasisPoints: The fee percentage in basis points (1/100th of a percent) applied to transactions.

Key Method: getInitialBuyPrice

The GlobalAccount class includes a crucial method for calculating the initial buy price for tokens:

getInitialBuyPrice(amount: bigint): bigint {

  if (amount <= 0n) {

    return 0n;

  }



  let n = this.initialVirtualSolReserves * this.initialVirtualTokenReserves;

  let i = this.initialVirtualSolReserves + amount;

  let r = n / i + 1n;

  let s = this.initialVirtualTokenReserves - r;

  return s < this.initialRealTokenReserves

    ? s

    : this.initialRealTokenReserves;

}

This method:

  1. Checks if the requested amount is positive.
  2. Calculates the product of initial virtual reserves.
  3. Determines the new virtual SOL reserves after the purchase.
  4. Computes the new virtual token reserves.
  5. Returns the minimum of the calculated tokens and initial real token reserves.

Creating a GlobalAccount

The GlobalAccount can be created from a buffer, typically obtained from on-chain data:

static fromBuffer(buffer: Buffer): GlobalAccount {

  const structure: Layout<GlobalAccount> = struct([

    u64("discriminator"),

    bool("initialized"),

    publicKey("authority"),

    publicKey("feeRecipient"),

    u64("initialVirtualTokenReserves"),

    u64("initialVirtualSolReserves"),

    u64("initialRealTokenReserves"),

    u64("tokenTotalSupply"),

    u64("feeBasisPoints"),

  ]);



  let value = structure.decode(buffer);

  return new GlobalAccount(

    BigInt(value.discriminator),

    value.initialized,

    value.authority,

    value.feeRecipient,

    BigInt(value.initialVirtualTokenReserves),

    BigInt(value.initialVirtualSolReserves),

    BigInt(value.initialRealTokenReserves),

    BigInt(value.tokenTotalSupply),

    BigInt(value.feeBasisPoints),

  );

}

This static method:

  1. Defines the structure of the account data using @coral-xyz/borsh library.
  2. Decodes the buffer into a JavaScript object.
  3. Creates and returns a new GlobalAccount instance with the decoded data.

Usage in the SDK

The GlobalAccount is used in the PumpFundler SDK for:

  1. Retrieving protocol-wide parameters.
  2. Calculating initial prices for new tokens.
  3. Determining fee recipients and amounts.

Here’s an example of how to fetch and use the GlobalAccount in the PumpFundler SDK:

import { PumpFundlerSDK, GlobalAccount } from "pumpfundler-sdk";



// Assume 'sdk' is an initialized PumpFundlerSDK instance



async function analyzeGlobalAccount() {

  const globalAccount = await sdk.getGlobalAccount();

  

  console.log("Fee Recipient:", globalAccount.feeRecipient.toBase58());

  console.log("Fee Basis Points:", globalAccount.feeBasisPoints.toString());

  console.log("Initial Virtual Token Reserves:", globalAccount.initialVirtualTokenReserves.toString());

  console.log("Initial Virtual SOL Reserves:", globalAccount.initialVirtualSolReserves.toString());

  

  const initialBuyAmount = 1000000n; // 1 token with 6 decimals

  const initialBuyPrice = globalAccount.getInitialBuyPrice(initialBuyAmount);

  console.log("Initial price to buy 1 token:", initialBuyPrice.toString());

}



analyzeGlobalAccount();

Importance in the PumpFundler Ecosystem

The GlobalAccount plays a crucial role in the PumpFundler ecosystem:

  1. Protocol Configuration: It stores essential parameters that govern the behavior of all tokens created through the protocol.
  2. Fee Management: The feeBasisPoints and feeRecipient fields determine how fees are calculated and distributed.
  3. Initial Curve Parameters: The initial virtual and real reserves set the starting point for new bonding curves.
  4. Governance: The authority field designates who can make changes to these global parameters.

Interacting with the Global Account

While most users won’t directly modify the GlobalAccount, understanding its contents is crucial for:

  • Developers: To correctly implement trading logic and fee calculations.
  • Token Creators: To understand the initial conditions their tokens will start with.
  • Traders: To comprehend the fee structure and initial pricing mechanics.