npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dbricks/dbricks-ts

v0.0.67

Published

## Introduction

Downloads

111

Readme

dbricks-ts

Introduction

dbricks-ts is a Typescript SDK for dbricks.xyz.

dbricks.xyz helps developers compose protocols bulit on top of Solana blockchain like lego bricks.

dbricks REST API has routes for a number of meaningful protocols, including Serum, Mango, Saber, and others. The routes are standartized, all returning an array of instructions and signers . The only thing left to do is to add the owner's signature and fire off the transaction to Solana.

This is where the SDK comes in. With the SDK you don't need to worry about how signing works and what the correct methods to call are.

Just follow the below steps:

// instantiate the Builder class
const builder = new Builder({
  ownerPubkey: ownerKp.publicKey,
  connectionUrl: 'https://solana-api.projectserum.com',
  committment: 'processed',
});

// buy 1 MNGO for 0.3 USDC
builder.addBrick({
  protocol: Protocol.Serum,
  action: Action.Serum.PlaceOrder,
  args: {
    marketPubkey: '3d4rzwpy9iGdCZvgxcu7B1YocYffVLsQXPXkBZKt2zLc',
    side: 'buy',
    price: '0.3',
    size: '1',
    orderType: 'limit',
  },
});

// settle Serum market (see docs if unclear why)
builder.addBrick({
  protocol: Protocol.Serum,
  action: Action.Serum.SettleMarket,
  args: {
    marketPubkey: '3d4rzwpy9iGdCZvgxcu7B1YocYffVLsQXPXkBZKt2zLc',
  },
});

// deposit the just acquired 1 MNGO token into Mango Markets
builder.addBrick({
  protocol: Protocol.Mango,
  action: Action.Mango.Deposit,
  args: {
    mintPubkey: 'MangoCzJ36AjZyKwVj3VnYU4GTonjfVEnJmvvWaxLac',
    quantity: '1',
    mangoAccountNumber: '0',
  },
});

// sell 1 MNGO back to USDC
builder.addBrick({
  protocol: Protocol.Mango,
  action: Action.Mango.PlaceSpotOrder,
  args: {
    marketPubkey: '3d4rzwpy9iGdCZvgxcu7B1YocYffVLsQXPXkBZKt2zLc',
    side: 'sell',
    price: '0.2',
    size: '1',
    orderType: 'limit',
    mangoAccountNumber: '0',
  },
});

// withdraw USDC from Mango Markets
builder.addBrick({
  protocol: Protocol.Mango,
  action: Action.Mango.Withdraw,
  args: {
    mintPubkey: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v',
    quantity: '0.2',
    isBorrow: false,
    mangoAccountNumber: '0',
  },
});

// fire off the transaction to Solana
await builder.build({
  keypair: ownerKp,
});

And just like that you now have less USDC than you started with! How awesome is that:)

On a more serious note, we think dbricks is powerful because:

  • it saves you from having to learn rust/solana in great detail
  • it exposes a standartized interfaces to all protocols
  • it lets you combine instructions to various protocols into a single transaction (where possible, limited by transaction size)
  • it automatically splits transactions for you when they're too large

Below we dive deeper into some of the more specific features of the SDK.

Transaction splitting

Solana caps transaction size at 1232 bytes. This means sometimes, especially when attempting multi-protocol operations, all the instructions won't fit into a single transaction. To solve the issue our SDK recursively splits the transaction until it arrives at a minimum set of maximally sized transactions.

Currently the code that does that is relatively naive, splitting the transaction down the middle. This will be optimized in the future.

Transaction signing

Before the transaction is sent to Solana's runtime for processing it needs to be signed.

The main signer of the transaction is the owner Keypair, which is done client-side. There are 3 ways to sign & send the transaction:

  1. Using a loaded Keypair (ideal for backends)
function loadKeypairSync(path: string): Keypair {
  const secretKey = JSON.parse(fs.readFileSync(path, 'utf8'));
  return Keypair.fromSecretKey(Uint8Array.from(secretKey));
}

const ownerKp = loadKeypairSync(process.env.KEYPAIR_PATH as string);

//... instantiate and compose the builder as needed ...

await builder.build({
  keypair: ownerKp,
});
  1. Using Solana's official Wallet Adapter ( ideal for frontends)
const wallet = getPhantomWallet();
const connectedAdapter = wallet.adapter();
await connectedAdapter.connect();

//... instantiate and compose the builder as needed ...

await builder.build({
  connectedAdapter: connectedAdapter,
});
  1. Using a callback (all other usecases)
function loadKeypairSync(path: string): Keypair {
  const secretKey = JSON.parse(fs.readFileSync(path, 'utf8'));
  return Keypair.fromSecretKey(Uint8Array.from(secretKey));
}

const ownerKp = loadKeypairSync(process.env.KEYPAIR_PATH as string);

async function sign(tx: Transaction): Promise<Transaction> {
  tx.sign(ownerKp);
  winston.debug('Transaction signed!');
  return tx;
}

//... instantiate and compose the builder as needed ...

await builder.build({
  signCallback: sign,
});

Note that all of the above methods automatically include additional signers, which are returned by dbricks server.