GithubHelp home page GithubHelp logo

Comments (6)

h4ckitt avatar h4ckitt commented on July 17, 2024 1

Thank You Very Much Chief.

You're The Best.

from solana-go.

gagliardetto avatar gagliardetto commented on July 17, 2024

Hi @first-giver

A client for the serum program hasn't been implemented yet.

What you see in https://github.com/gagliardetto/solana-go/tree/main/programs/serum is a legacy version, pretty primitive, incomplete and hard to use.

You could use it, but there's a lot of manual labor and things you will need to look up in the rust source code of the serum dex.

Example (not tested; some of the data might be wrong):

package main

import (
  "bytes"
  "context"
  "encoding/binary"
  "fmt"
  "os"

  "github.com/davecgh/go-spew/spew"
  bin "github.com/gagliardetto/binary"
  "github.com/gagliardetto/solana-go"
  "github.com/gagliardetto/solana-go/programs/serum"
  "github.com/gagliardetto/solana-go/rpc"
  confirm "github.com/gagliardetto/solana-go/rpc/sendAndConfirmTransaction"
  "github.com/gagliardetto/solana-go/rpc/ws"
  "github.com/gagliardetto/solana-go/text"
)

func main() {
  // Create a new RPC client (TODO: you need to select the appropriate network):
  rpcClient := rpc.New(rpc.DevNet_RPC)

  // NOTE: serum dex has different addresses on devnet/testnet;
  // And if you're testing on a local validator, you'll need to deploy the serum dex program yourself.
  serumDEXProgramID := serum.DEXProgramIDV2

  // Create a new WS client (used for confirming transactions).
  // NOTE: here too you need to select the appropriate net.
  wsClient, err := ws.Connect(context.Background(), rpc.DevNet_WS)
  if err != nil {
    panic(err)
  }

  // Load the account that will pay for the transaction and can sign stuff:
  owner, err := solana.PrivateKeyFromSolanaKeygenFile("/path/to/.config/solana/id.json")
  if err != nil {
    panic(err)
  }
  fmt.Println("owner public key:", owner.PublicKey())

  recent, err := rpcClient.GetRecentBlockhash(context.TODO(), rpc.CommitmentFinalized)
  if err != nil {
    panic(err)
  }

  instructions := []solana.Instruction{}
  signers := make([]solana.PrivateKey, 0)
  {
    data := &serum.Instruction{
      BaseVariant: bin.BaseVariant{
        TypeID: bin.TypeIDFromUint32(9, binary.LittleEndian),
        Impl: &serum.InstructionNewOrderV2{
          Side:              serum.SideAsk,
          LimitPrice:        1720,
          MaxQuantity:       650000,
          OrderType:         serum.OrderTypeLimit,
          ClientID:          1608306862011613462,
          SelfTradeBehavior: serum.SelfTradeBehaviorCancelProvide,
        },
      },
      Version: 0,
    }

    buf := new(bytes.Buffer)
    err = data.MarshalWithEncoder(bin.NewBinEncoder(buf))
    if err != nil {
      panic(err)
    }

    accounts := &serum.NewOrderV2Accounts{
      Market:          solana.Meta(solana.MustPublicKeyFromBase58("TODO")).WRITE(),
      OpenOrders:      solana.Meta(solana.MustPublicKeyFromBase58("TODO")).WRITE(),
      RequestQueue:    solana.Meta(solana.MustPublicKeyFromBase58("TODO")).WRITE(),
      Payer:           solana.Meta(solana.MustPublicKeyFromBase58("TODO")).WRITE(),
      Owner:           solana.Meta(owner.PublicKey()).SIGNER(),
      CoinVault:       solana.Meta(solana.MustPublicKeyFromBase58("TODO")).WRITE(),
      PCVault:         solana.Meta(solana.MustPublicKeyFromBase58("TODO")).WRITE(),
      SPLTokenProgram: solana.Meta(solana.TokenProgramID),
      RentSysvar:      solana.Meta(solana.SysVarRentPubkey),
    }
    // NOTE: accounts.Owner is marked as signer, so it must be added to the signers.
    signers = append(signers, owner)

    instruction := solana.NewInstruction(
      serumDEXProgramID, // NOTE: there are 3 version of serum dex.
      solana.AccountMetaSlice{
        accounts.Market,
        accounts.OpenOrders,
        accounts.RequestQueue,
        accounts.Payer,
        accounts.Owner,
        accounts.CoinVault,
        accounts.PCVault,
        accounts.SPLTokenProgram,
        accounts.RentSysvar,
      },
      buf.Bytes(),
    )
    instructions = append(instructions, instruction)
  }

  tx, err := solana.NewTransaction(
    instructions,
    recent.Value.Blockhash,
    solana.TransactionPayer(owner.PublicKey()),
  )
  if err != nil {
    panic(err)
  }

  _, err = tx.Sign(
    func(key solana.PublicKey) *solana.PrivateKey {
      for _, candidate := range signers {
        if candidate.PublicKey().Equals(key) {
          return &candidate
        }
      }
      return nil
    },
  )
  if err != nil {
    panic(fmt.Errorf("unable to sign transaction: %w", err))
  }
  spew.Dump(tx)
  // Pretty print the transaction:
  tx.EncodeTree(text.NewTreeEncoder(os.Stdout, "Transaction"))

  // Send transaction, and wait for confirmation:
  sig, err := confirm.SendAndConfirmTransaction(
    context.TODO(),
    rpcClient,
    wsClient,
    tx,
  )
  if err != nil {
    panic(err)
  }
  spew.Dump(sig)
}

I have no experience with trading on serum and the meaning of orders etc.

A more modern and easier to use serum client will be ready in a few weeks.

Good luck!

from solana-go.

h4ckitt avatar h4ckitt commented on July 17, 2024

Hi,
Great Work On Your Solana Lib.

Just Wanted To Ask:

You Said You Have No Experience Trading On Serum.

Did You Write The Example Code Above With The Help Of A Resource?

If Yes, Can I Please Get A Link?

I'm Also Trying To Create An Order On Serum But I'm New To Solana And Serum Docs On How To Do This Are Quite Non-Existent.

from solana-go.

gagliardetto avatar gagliardetto commented on July 17, 2024

hi @h4ckitt

Your best bet is to look directly into the source code of serum: here's the tests for v3: https://github.com/project-serum/serum-dex/blob/master/dex/src/tests.rs

Example instruction NewOrderV3: https://github.com/project-serum/serum-dex/blob/29ba51d005d9e933e2174ee0150f3771ae1b4975/dex/src/tests.rs#L257-L300

from solana-go.

gagliardetto avatar gagliardetto commented on July 17, 2024

Also:

https://github.com/search?q=language%3Arust+NewOrderV3&type=code

https://github.com/search?q=language%3Atypescript+NewOrderV3&type=code

from solana-go.

first-giver avatar first-giver commented on July 17, 2024

thank you for your patient!it‘s very useful, expect your new SDK!

from solana-go.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.