@project-serum/anchor#Program TypeScript Examples

The following examples show how to use @project-serum/anchor#Program. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Cache.ts    From zo-client with Apache License 2.0 6 votes vote down vote up
/**
   * Loads a new Cache object from its public key.
   * @param program
   * @param k The cache account's public key.
   * @param st
   */
  static async load(program: Program<Zo>, k: PublicKey, st: StateSchema) {
    return new this(program, k, await Cache.fetch(program, k, st), st);
  }
Example #2
Source File: get-metadata.ts    From candy-machine-v2 with MIT License 6 votes vote down vote up
getMetadata = async (
  program: Program<Idl>,
  candyMachineId: web3.PublicKey
) => {
  const state: any = await program.account.candyMachine.fetch(candyMachineId);
  const itemsAvailable = state.data.itemsAvailable.toNumber();
  const itemsRedeemed = state.itemsRedeemed.toNumber();
  const itemsRemaining = itemsAvailable - itemsRedeemed;

  return {
    id: candyMachineId,
    program,
    state: {
      itemsAvailable,
      itemsRedeemed,
      itemsRemaining,
      isSoldOut: itemsRemaining === 0,
      isActive:
        state.data.goLiveDate.toNumber() < new Date().getTime() / 1000 &&
        (state.endSettings
          ? state.endSettings.endSettingType.date
            ? state.endSettings.number.toNumber() > new Date().getTime() / 1000
            : itemsRedeemed < state.endSettings.number.toNumber()
          : true),
      goLiveDate: state.data.goLiveDate,
      treasury: state.wallet,
      tokenMint: state.tokenMint,
      gatekeeper: state.data.gatekeeper,
      endSettings: state.data.endSettings,
      whitelistMintSettings: state.data.whitelistMintSettings,
      hiddenSettings: state.data.hiddenSettings,
      price: state.data.price,
    },
  };
}
Example #3
Source File: sunnyUtils.ts    From arrow with GNU Affero General Public License v3.0 6 votes vote down vote up
createSunnyPool = async ({
  provider,
  rewarder,
  quarry,
}: {
  provider: Provider;
  rewarder: PublicKey;
  quarry: PublicKey;
}): Promise<{ sunnyPool: PublicKey; internalMint: PublicKey }> => {
  const sunny = new Program(SunnyPoolQuarryJSON, SUNNY_PROGRAM);
  // set up the sunny pool
  const [pool, bump] = await generateSunnyPoolAddress({
    quarry,
  });
  const sunnyMintKP = Keypair.generate();
  const createInternalMint = await createInitMintInstructions({
    provider,
    mintKP: sunnyMintKP,
    decimals: 6,
    mintAuthority: pool,
    freezeAuthority: pool,
  });
  const newPoolIx = sunny.instruction.newPool(bump, {
    accounts: {
      creator: SUNNY_CREATOR_KEY,
      rewarder,
      quarry,
      pool,
      payer: provider.wallet.publicKey,
      systemProgram: SystemProgram.programId,
      internalMint: sunnyMintKP.publicKey,
    },
  });
  await expectTX(
    createInternalMint.combine(new TransactionEnvelope(provider, [newPoolIx])),
    "create sunny pool"
  ).to.be.fulfilled;
  return { sunnyPool: pool, internalMint: sunnyMintKP.publicKey };
}
Example #4
Source File: test-v1.ts    From protocol with Apache License 2.0 6 votes vote down vote up
async function createDialectAndSubscribeAllMembers(
  program: Program,
  owner: anchor.web3.Keypair,
  member: anchor.web3.Keypair,
  encrypted: boolean,
) {
  const members: Member[] = [
    {
      publicKey: owner.publicKey,
      scopes: [true, true], // owner, read-only
    },
    {
      publicKey: member.publicKey,
      scopes: [false, true], // non-owner, read-write
    },
  ];
  const dialect = await createDialect(program, owner, members, encrypted);
  await subscribeUser(program, dialect, owner.publicKey, owner);
  await subscribeUser(program, dialect, member.publicKey, member);
  return dialect;
}
Example #5
Source File: pollingUserAccountSubscriber.ts    From protocol-v1 with Apache License 2.0 6 votes vote down vote up
public constructor(
		program: Program,
		authority: PublicKey,
		accountLoader: BulkAccountLoader
	) {
		this.isSubscribed = false;
		this.program = program;
		this.authority = authority;
		this.accountLoader = accountLoader;
		this.eventEmitter = new EventEmitter();
	}
Example #6
Source File: auth.ts    From jet-engine with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
   * Creates a connection to the authentication program.
   * @static
   * @param {Provider} provider
   * @returns {Promise<Program<AuthIdl>>}
   * @memberof Auth
   */
  static connect(provider: Provider): Promise<Program<AuthIdl>> {
    return connect(Auth.PROGRAM_ID, provider)
  }
Example #7
Source File: sign.ts    From metaplex with Apache License 2.0 6 votes vote down vote up
async function signWithRetry(
  anchorProgram: Program,
  creatorKeyPair: Keypair,
  metadataAddress: PublicKey,
) {
  await sendTransactionWithRetryWithKeypair(
    anchorProgram.provider.connection,
    creatorKeyPair,
    [
      signMetadataInstruction(
        new PublicKey(metadataAddress),
        creatorKeyPair.publicKey,
      ),
    ],
    [],
    'single',
  );
}
Example #8
Source File: helpers.ts    From psyoptions with Apache License 2.0 6 votes vote down vote up
initOptionMarket = async (
  program: anchor.Program<PsyAmerican>,
  payer: Keypair,
  optionMarket: OptionMarketV2,
  remainingAccounts: AccountMeta[],
  instructions: TransactionInstruction[]
) => {
  await program.rpc.initializeMarket(
    optionMarket.underlyingAmountPerContract,
    optionMarket.quoteAmountPerContract,
    optionMarket.expirationUnixTimestamp,
    optionMarket.bumpSeed,
    {
      accounts: {
        authority: payer.publicKey,
        underlyingAssetMint: optionMarket.underlyingAssetMint,
        quoteAssetMint: optionMarket.quoteAssetMint,
        optionMint: optionMarket.optionMint,
        writerTokenMint: optionMarket.writerTokenMint,
        quoteAssetPool: optionMarket.quoteAssetPool,
        underlyingAssetPool: optionMarket.underlyingAssetPool,
        optionMarket: optionMarket.key,
        feeOwner: FEE_OWNER_KEY,
        tokenProgram: TOKEN_PROGRAM_ID,
        associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
        rent: SYSVAR_RENT_PUBKEY,
        systemProgram: SystemProgram.programId,
        clock: SYSVAR_CLOCK_PUBKEY,
      },
      remainingAccounts,
      signers: [payer],
      instructions,
    }
  );
}
Example #9
Source File: helpers.ts    From sdk with MIT License 6 votes vote down vote up
export async function loadAuctionHouseProgram(
	connection: Connection,
	signer: IWalletSigner
) {
	const provider = new Provider(connection, signer, {
		preflightCommitment: "recent",
	})
	const idl = await Program.fetchIdl(AUCTION_HOUSE_PROGRAM_ID, provider)
	return new Program(idl!, AUCTION_HOUSE_PROGRAM_ID, provider)
}
Example #10
Source File: BaseAccount.ts    From zo-client with Apache License 2.0 5 votes vote down vote up
protected constructor(
    private _program: Program<Zo>,
    public readonly pubkey: PublicKey,
    public data: Readonly<T>,
  ) {}