Skip to content

Commit

Permalink
Merge branch 'master' of github.com:dfuse-io/solana-go
Browse files Browse the repository at this point in the history
  • Loading branch information
billettc committed Nov 16, 2020
2 parents fb91647 + e22f933 commit 90034de
Show file tree
Hide file tree
Showing 14 changed files with 524 additions and 565 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ go 1.14

require (
github.com/GeertJohan/go.rice v1.0.0
github.com/dfuse-io/binary v0.0.0-20201113132610-5a1e246afeec
github.com/dfuse-io/binary v0.0.0-20201113172957-3eda1bb6412c
github.com/dfuse-io/logging v0.0.0-20201110202154-26697de88c79
github.com/google/go-cmp v0.4.1 // indirect
github.com/gorilla/rpc v1.2.0
github.com/gorilla/websocket v1.4.2
github.com/logrusorgru/aurora v2.0.3+incompatible
github.com/lunixbochs/struc v0.0.0-20200707160740-784aaebc1d40
github.com/mr-tron/base58 v1.2.0
github.com/onsi/gomega v1.10.1 // indirect
github.com/pkg/errors v0.9.1
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ github.com/dfuse-io/binary v0.0.0-20201113125316-ded9ef4ba4f3 h1:49mP2UMaYvSB5BU
github.com/dfuse-io/binary v0.0.0-20201113125316-ded9ef4ba4f3/go.mod h1:GDFX6qH3BQZPWTeYaA4ZW98T94zs2skRoG3oMz/0jw0=
github.com/dfuse-io/binary v0.0.0-20201113132610-5a1e246afeec h1:VmIyiZuZzW1ENZyIHBwyCaj/w3rHDdfFrtSXzFeXIGg=
github.com/dfuse-io/binary v0.0.0-20201113132610-5a1e246afeec/go.mod h1:GDFX6qH3BQZPWTeYaA4ZW98T94zs2skRoG3oMz/0jw0=
github.com/dfuse-io/binary v0.0.0-20201113172717-8fc2fd76c8d2 h1:qeVi/lwj1tYUxdEYvnAY3ibS6utdFRN7xcN2dwT1m9E=
github.com/dfuse-io/binary v0.0.0-20201113172717-8fc2fd76c8d2/go.mod h1:GDFX6qH3BQZPWTeYaA4ZW98T94zs2skRoG3oMz/0jw0=
github.com/dfuse-io/binary v0.0.0-20201113172957-3eda1bb6412c h1:ufKbFyLlpy/Rg6iErIBcsTqnql66Fq7iZynLfhYpNKg=
github.com/dfuse-io/binary v0.0.0-20201113172957-3eda1bb6412c/go.mod h1:GDFX6qH3BQZPWTeYaA4ZW98T94zs2skRoG3oMz/0jw0=
github.com/dfuse-io/dauth v0.0.0-20200529171443-21c0e2d262c2/go.mod h1:tab5biQg59hbrHjaXTjaLcsP3zblx6B0I0Keu03xB44=
github.com/dfuse-io/derr v0.0.0-20200406214256-c690655246a1/go.mod h1:JW/hUKChGd6ytDtvwx4JFo57m1pnFvMxaq9WbDAb2fQ=
github.com/dfuse-io/derr v0.0.0-20200417132224-d333cfd0e9a0/go.mod h1:/DjjRCyTi/KIiI1E1hnhvfWwg+K06jo+qM4VcjHvHq4=
Expand Down
4 changes: 4 additions & 0 deletions nativetypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ func (p Signature) String() string {

type PublicKey [32]byte

func (p PublicKey) Equals(pb PublicKey) bool {
return p.String() == pb.String()
}

func MustPublicKeyFromBase58(in string) PublicKey {
out, _ := PublicKeyFromBase58(in)
return out
Expand Down
26 changes: 26 additions & 0 deletions registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package solana

import "fmt"

type InstructionDecoder func(*CompiledInstruction) (interface{}, error)

var instructionDecoderRegistry = map[string]InstructionDecoder{}

func RegisterInstructionDecoder(programID PublicKey, decoder InstructionDecoder) {
p := programID.String()
if _, found := instructionDecoderRegistry[p]; found {
panic(fmt.Sprintf("unable to re-register instruction decoder for program %q", p))
}
instructionDecoderRegistry[p] = decoder
}

func DecodeInstruction(programID PublicKey, inst *CompiledInstruction) (interface{}, error) {
p := programID.String()

decoder, found := instructionDecoderRegistry[p]
if !found {
return nil, fmt.Errorf("unknown programID, cannot find any instruction decoder %q", p)
}

return decoder(inst)
}
4 changes: 2 additions & 2 deletions rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ func (c *Client) GetAccountDataIn(ctx context.Context, account solana.PublicKey,
return bin.NewDecoder(resp.Value.Data).Decode(inVar)
}

func (c *Client) GetConfirmedTransaction(ctx context.Context, signature string) (out TransactionParsed, err error) {
params := []interface{}{signature, "jsonParsed"}
func (c *Client) GetConfirmedTransaction(ctx context.Context, signature string) (out TransactionWithMeta, err error) {
params := []interface{}{signature, "json"}

err = c.rpcClient.CallFor(&out, "getConfirmedTransaction", params...)
return
Expand Down
5 changes: 0 additions & 5 deletions rpc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
"fmt"
"testing"

"gotest.tools/assert"

"go.uber.org/zap"

"github.com/dfuse-io/solana-go"
Expand Down Expand Up @@ -68,8 +66,6 @@ func TestClient_GetConfirmedTransaction(t *testing.T) {
require.NoError(t, err)
fmt.Println(string(d))

assert.Equal(t, false, trx.Transaction.Message.Instructions[0].IsParsed())

signature = "4ZK6ofUodMP8NrB8RGkKFpXWVKMk5eqjkBTbq7DKiDu34gbdrpgctJHp3cU79ZGEBgTaohbjy56KJwhraVmgYq9i"
trx, err = c.GetConfirmedTransaction(context.Background(), signature)
require.NoError(t, err)
Expand All @@ -78,5 +74,4 @@ func TestClient_GetConfirmedTransaction(t *testing.T) {
require.NoError(t, err)
fmt.Println(string(d))

assert.Equal(t, true, trx.Transaction.Message.Instructions[0].IsParsed())
}
8 changes: 7 additions & 1 deletion rpc/ws/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@ func (c *Client) handleNewSubscriptionMessage(requestID, subID uint64) {
zap.Uint64("message_id", requestID),
zap.Uint64("subscription_id", subID),
)
callBack := c.subscriptionByRequestID[requestID]
callBack, found := c.subscriptionByRequestID[requestID]
if !found {
zlog.Error("cannot find websocket message handler for a new stream.... this should not happen",
zap.Uint64("request_id", requestID),
zap.Uint64("subscription_id", subID),
)
}
callBack.subID = subID
c.subscriptionByWSSubID[subID] = callBack
return
Expand Down
193 changes: 193 additions & 0 deletions serum/instruction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
package serum

import (
"fmt"

bin "github.com/dfuse-io/binary"
"github.com/dfuse-io/solana-go"
)

var DEX_PROGRAM_ID = solana.MustPublicKeyFromBase58("EUqojwWA2rd19FZrzeBncJsm38Jm1hEhE3zsmX3bRc2o")

func init() {
solana.RegisterInstructionDecoder(DEX_PROGRAM_ID, registryDecodeInstruction)
}

func registryDecodeInstruction(rawInstruction *solana.CompiledInstruction) (interface{}, error) {
inst, err := DecodeInstruction(rawInstruction)
if err != nil {
return nil, err
}
return inst, nil
}

func DecodeInstruction(rawInstruction *solana.CompiledInstruction) (*Instruction, error) {
var inst *Instruction
if err := bin.NewDecoder(rawInstruction.Data).Decode(&inst); err != nil {
return nil, fmt.Errorf("unable to decode instruction for serum program: %w", err)
}
return inst, nil
}

type Instruction struct {
bin.BaseVariant
Version uint8
}

func (i *Instruction) UnmarshalBinary(decoder *bin.Decoder) (err error) {
i.Version, err = decoder.ReadUint8()
if err != nil {
return fmt.Errorf("unable to read version: %w", err)
}
return i.BaseVariant.UnmarshalBinaryVariant(decoder, InstructionDefVariant)
}

func (i *Instruction) MarshalBinary(encoder *bin.Encoder) error {
err := encoder.WriteUint8(i.Version)
if err != nil {
return fmt.Errorf("unable to write instruction version: %w", err)
}

err = encoder.WriteUint32(i.TypeID)
if err != nil {
return fmt.Errorf("unable to write variant type: %w", err)
}
return encoder.Encode(i.Impl)
}

var InstructionDefVariant = bin.NewVariantDefinition(bin.Uint32TypeIDEncoding, []bin.VariantType{
{"initialize_market", (*InstructionInitializeMarket)(nil)},
{"new_order", (*InstructionNewOrder)(nil)},
{"match_orders", (*InstructionMatchOrder)(nil)},
{"consume_events", (*InstructionConsumeEvents)(nil)},
{"cancel_order", (*InstructionCancelOrder)(nil)},
{"settle_funds", (*InstructionSettleFunds)(nil)},
{"cancel_order_by_client_id", (*InstructionCancelOrderByClientId)(nil)},
})

type InstructionInitializeMarket struct {
BaseLotSize uint64
QuoteLotSize uint64
FeeRateBps uint16
VaultSignerNonce uint64
QuoteDustThreshold uint64
}

type InstructionNewOrder struct {
Side uint32
LimitPrice uint64
MaxQuantity uint64
OrderType uint32
ClientID uint64
}

type InstructionMatchOrder struct {
Limit uint16
}

type InstructionConsumeEvents struct {
Limit uint16
}

type InstructionCancelOrder struct {
Side uint32
OrderID bin.Uint128
OpenOrders solana.PublicKey
OpenOrderSlot uint8
}

type InstructionSettleFunds struct {
}

type InstructionCancelOrderByClientId struct {
ClientID uint64
}

type SideLayoutType string

const (
SideLayoutTypeUnknown SideLayoutType = "UNKNOWN"
SideLayoutTypeBid SideLayoutType = "BID"
SideLayoutTypeAsk SideLayoutType = "ASK"
)

type SideLayout uint32

func (s SideLayout) getSide() SideLayoutType {
switch s {
case 0:
return SideLayoutTypeBid
case 1:
return SideLayoutTypeAsk
}
return SideLayoutTypeUnknown
}

type OrderType string

const (
OrderTypeUnknown OrderType = "UNKNOWN"
OrderTypeLimit OrderType = "LIMIT"
OrderTypeImmediateOrCancel OrderType = "IMMEDIATE_OR_CANCEL"
OrderTypePostOnly OrderType = "POST_ONLY"
)

type OrderTypeLayout uint32

func (o OrderTypeLayout) getOrderType() OrderType {
switch o {
case 0:
return OrderTypeLimit
case 1:
return OrderTypeImmediateOrCancel
case 2:
return OrderTypePostOnly
}
return OrderTypeUnknown
}

//
//export const INSTRUCTION_LAYOUT = new VersionedLayout(
//0,
//union(u32('instruction')),
//);
//INSTRUCTION_LAYOUT.inner.addVariant(
//0,
//struct([
//u64('baseLotSize'),
//u64('quoteLotSize'),
//u16('feeRateBps'),
//u64('vaultSignerNonce'),
//u64('quoteDustThreshold'),
//]),
//'initializeMarket',
//);
//INSTRUCTION_LAYOUT.inner.addVariant(
//1,
//struct([
//sideLayout('side'),
//u64('limitPrice'),
//u64('maxQuantity'),
//orderTypeLayout('orderType'),
//u64('clientId'),
//]),
//'newOrder',
//);
//INSTRUCTION_LAYOUT.inner.addVariant(2, struct([u16('limit')]), 'matchOrders');
//INSTRUCTION_LAYOUT.inner.addVariant(3, struct([u16('limit')]), 'consumeEvents');
//INSTRUCTION_LAYOUT.inner.addVariant(
//4,
//struct([
//sideLayout('side'),
//u128('orderId'),
//publicKeyLayout('openOrders'),
//u8('openOrdersSlot'),
//]),
//'cancelOrder',
//);
//INSTRUCTION_LAYOUT.inner.addVariant(5, struct([]), 'settleFunds');
//INSTRUCTION_LAYOUT.inner.addVariant(
//6,
//struct([u64('clientId')]),
//'cancelOrderByClientId',
//);
Loading

0 comments on commit 90034de

Please sign in to comment.