#5 - BUIDL an Auto-compounding Bot on Saber

Authors: @wei_sol_arrow-up-right, @ironaddicteddogarrow-up-right

[Updated at 2022.3.31]

See the example repo herearrow-up-right

TL; DR

  • Build SDKs only using @solana/web3.js

  • Learn how to interact with Solana

  • Builld an auto-compounding bot with SDK

Introduction

Solana : Solana is a fast, low cost, decentralized blockchain with thousands of projects spanning DeFi, NFTs, Web3 and more.

Saber : Saber is a Curve-like AMM provider on Solana, support all kinds of stablecoin pair from various bridges

Solana 101

Account : Everything on Solana is an account * Accounts can only be owned by programs * Every Account is like a file in a computer * Accounts are used to store state * Only the account owner may debit an account and adjust its data * All accounts to be written to or read must be passed into Insructions * Developers should use the data field to save data inside accounts arrow-up-right >Image from https://paulx.dev/blog/2021/01/14/programming-on-solana-an-introduction/

Image from https://explorer.solana.com/address/6ZRCB7AAqGre6c72PRz3MHLC73VMYvJ8bi9KHf1HFpNk

Program : Program is just an account with excutable enable * Solana programs are stateless * Designed be upgradable (BPF loader 2)

Image from https://explorer.solana.com/address/TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA

Program Account : An Account owned by a Program other than System program * Data struct are very different from program to program

Image from https://explorer.solana.com/address/EzkjmZFzWccf2DRQ6uafahvfZh29ntwAmej8nDQ5tY1C

Keyair : A byte array that contain Public key and Private key * first 32 bytes are use as Private key * last 32 bytes are generated by Private key and use as Public key

[26,151,18,191,115,212,220,144,52,66,74,133,251,235,69,161,254,121,70,227,171,227,17,170,154,227,32,151,40,125,37,158,0,94,253,210,209,242,208,122,162,84,158,36,211,63,243,252,104,36,58,243,120,134,127,132,193,186,63,50,0,230,93,200] 12T1dsupQBqwgQYsXWqhhmzQgreRtvkP95W8rW3Pk23R

Address : A Public key encoded in Base58

Rent : Pay for space to store data on Solana * If Rent is paid for over 2 years, the account become rent-exempt

Image from https://explorer.solana.com/address/8UviNr47S8eL6J3WfDxMRa3hvLta1VDJwNWqsDgtN3Cv

RPC : The endpoint to read/write from/to Solana * Free RPCs can be slow or congested when there is high demand.

PDA (Program Derived Address) : A account without a Private key that only can be signed by a Program * PDA is generated by hashing a seed with the Program address

Instruction : The one and only way to interact with Programs * All accounts used when processing should be write into the Instruction

Raw instruction from https://explorer.solana.com/tx/v2s26c5vBzrc7rTEyEsCreBDHaVd41iJ8F4j1gLFAy1QA5q8tqJex2Y2h3xsDmJpU1R9wAyRqziyzNB85cgxFjh

Serialized instruction https://explorer.solana.com/tx/37oPunF5tLw6DEqQUk4gMHeb6n1wdCAPWBF7e291znWwPXW7gCvPh9Srv5m5UMoxZoHSMR4BcQau1RnmU5yQeWKj

Tx (Transaction) : A message send to RPC that contains one or more Instructions, Signatures and a Fee Payer * Every Tx have a size limit of 1232 bytes * A Tx can be signed by differents accounts * Fee are determined by the amount of Signer at the moment

Image from https://explorer.solana.com/tx/v2s26c5vBzrc7rTEyEsCreBDHaVd41iJ8F4j1gLFAy1QA5q8tqJex2Y2h3xsDmJpU1R9wAyRqziyzNB85cgxFjh

SPL (Solana Program Libraryarrow-up-right) : A example library organized by Solana lab * SPL is not a token protocol on Solana, spl-token is.

ATA (Associated Token Account) : A Token Account which is a PDA created by Associated Token Programarrow-up-right * There is only one ATA with every wallet and a token mint

Image from 白上フブキ.etharrow-up-right

Solana system model

Overview

  • Learn Solana basic from building a SDK

  • Build a bot that collect, sell, reinvest the yield from LP farming

Architecture

File Structure

Setup

Install Rust and Solana

See https://hackmd.io/@ironaddicteddog/solana-starter-kit#Install-Rust-and-Solana-Cli for more details.

Recover your Wallet (Using Phantom)

FIrst, click Show Secret Reconvery Phrase and copy your recovery phrase at this point.

Next, let's recover the wallet locally:

There should be a prompt asking for entering the recovery phrase in yout terminal. Paste your recovery phrase at this point.

  • Set keypair

Config to solana-mf

Scaffold

Add package.json

Install Dependencies

Part 1: Implement Common Modules

In this part, we will implement the common modules for the bot:

  • ids.ts

  • layouts.ts

  • utils.ts

ids.ts

In Solana, execution (programs) and states are decoupled. As a result, we have to be very clear on the scope of the the programs and states:

saber/ids.ts

raydium/ids.ts

layouts.ts

layouts play an important role in both reading and writing on-chain data.

  • Reading: It indicates how the stored bytes is arranged and what their types are.

  • Writing: It indicates how the instruction data should be assembled to call a certain program.

saber/layouts.ts

raydium/layouts.ts

utils.ts

Copy this code snippetarrow-up-right to utils.ts.

getAnchorInsByIdl

Calculate Anchor Identifier:

Part 2: Implement "Read" Modules

  • DataSizeFilter and MemCmpFilter

infos.tx

Copy this code snippetarrow-up-right to saber/infos.ts and this code snippetarrow-up-right to raydium/infos.ts.

index.ts

Copy this code snippetarrow-up-right to saber/index.ts and this code snippetarrow-up-right to raydium/index.ts.

Part 3: Implement "Write" Modules

  • Need to add Anchor identifier manually

  • One single Solana tx includes multiple ixs

    • Solana ix = Ethereum tx

    • Solana tx = Ethereum multicall

instructions.ts

Copy this code snippetarrow-up-right to saber/instructions.ts and this code snippetarrow-up-right to raydium/instructions.ts.

transactions.ts

Copy this code snippetarrow-up-right to saber/transactions.ts and this code snippetarrow-up-right to raydium/transactions.ts.

Part 4: Implement the Auto-compounding Bot

  • Load Keypair

  • Claim All mining rewards

  • Swap all SBR to USDC

  • Add all USDC swapped out to USDC-UST pool

  • Deposit all LP to farming

index.ts

Finally, let's run the bot:

References

  • https://github.com/DappioWonderland/auto-compounding-bot

  • https://hackmd.io/@ironaddicteddog/solana-starter-kit

  • https://hackmd.io/@ironaddicteddog/solana-anchor-escrow

Last updated