Skip to content

Commit

Permalink
Upgrade bin package; use generated system program client.
Browse files Browse the repository at this point in the history
  • Loading branch information
gagliardetto committed Aug 31, 2021
1 parent f4fb638 commit 3853032
Show file tree
Hide file tree
Showing 38 changed files with 2,558 additions and 627 deletions.
18 changes: 16 additions & 2 deletions cmd/slnc/cmd/token_registry_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,24 @@ var tokenRegistryRegisterCmd = &cobra.Command{

tokenRegistryProgramID := tokenregistry.ProgramID()

createAccountInstruction := system.NewCreateAccountInstruction(uint64(lamport), tokenregistry.TOKEN_META_SIZE, tokenRegistryProgramID, registrarPubKey, tokenMetaAccount.PublicKey())
createAccountInstruction := system.NewCreateAccountInstruction(
lamport,
tokenregistry.TOKEN_META_SIZE,
tokenRegistryProgramID,
registrarPubKey,
tokenMetaAccount.PublicKey(),
).
Build()
registerTokenInstruction := tokenregistry.NewRegisterTokenInstruction(logo, name, symbol, website, tokenMetaAccount.PublicKey(), registrarPubKey, tokenAddress)

trx, err := solana.NewTransaction([]solana.Instruction{createAccountInstruction, registerTokenInstruction}, blockHashResult.Value.Blockhash, solana.TransactionPayer(registrarPubKey))
trx, err := solana.NewTransaction(
[]solana.Instruction{
createAccountInstruction,
registerTokenInstruction,
},
blockHashResult.Value.Blockhash,
solana.TransactionPayer(registrarPubKey),
)
if err != nil {
return fmt.Errorf("unable to craft transaction: %w", err)
}
Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ require (
github.com/fatih/color v1.7.0
github.com/gagliardetto/treeout v0.1.2
github.com/google/go-cmp v0.5.1
github.com/google/gofuzz v1.0.0
github.com/gorilla/rpc v1.2.0
github.com/gorilla/websocket v1.4.2
github.com/json-iterator/go v1.1.11
github.com/klauspost/compress v1.13.1
github.com/kr/pretty v0.2.1 // indirect
github.com/logrusorgru/aurora v2.0.3+incompatible
github.com/magiconair/properties v1.8.1
github.com/mr-tron/base58 v1.2.0
Expand All @@ -47,7 +47,8 @@ require (
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf // indirect
golang.org/x/tools v0.0.0-20200601175630-2caf76543d99 // indirect
google.golang.org/api v0.29.0
gopkg.in/yaml.v2 v2.4.0 // indirect
)

replace github.com/dfuse-io/binary => github.com/gagliardetto/binary v0.3.0
replace github.com/dfuse-io/binary => github.com/gagliardetto/binary v0.4.0

replace github.com/google/gofuzz => github.com/gagliardetto/gofuzz v1.2.1
8 changes: 5 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/gagliardetto/binary v0.3.0 h1:SN0+zufFFc5CFYl5T0/wRuUOjx2YkhqKbx82WuSmSL4=
github.com/gagliardetto/binary v0.3.0/go.mod h1:GDFX6qH3BQZPWTeYaA4ZW98T94zs2skRoG3oMz/0jw0=
github.com/gagliardetto/binary v0.4.0 h1:dxLndYArHtdZYbLYwnpLY86mlAa9gWgxG2zSDPFINjM=
github.com/gagliardetto/binary v0.4.0/go.mod h1:55fxN6CKhVnsBhSr3Hmyn7i2igseIzN9/NC+gHvv42k=
github.com/gagliardetto/gofuzz v1.2.1 h1:fHBiDgCYYb8kBRqyI+bhU59/IKATHArFUAY3iVUrdPA=
github.com/gagliardetto/gofuzz v1.2.1/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gagliardetto/treeout v0.1.2 h1:WXO7LDJTwINO37OQfNlf7s095Z1bAiwN2ACaZQic33Q=
github.com/gagliardetto/treeout v0.1.2/go.mod h1:loUefvXTrlRG5rYmJmExNryyBRh8f89VZhmMOyCyqok=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
Expand Down Expand Up @@ -134,7 +136,6 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.1 h1:JFrFEBb2xKufg6XkJsJr+WbKb4FQlURi5RUcBveYu9k=
github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
Expand Down Expand Up @@ -494,6 +495,7 @@ golang.org/x/tools v0.0.0-20191115202509-3a792d9c32b2/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191125144606-a911d9008d1f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191130070609-6e064ea0cf2d/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191216052735-49a3e744a425/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.0.0-20191216173652-a0e659d51361/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.0.0-20191227053925-7b8e75db28f4/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.0.0-20200117161641-43d50277825c/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
Expand Down
2 changes: 1 addition & 1 deletion programs/serum/instruction.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (i *Instruction) MarshalWithEncoder(encoder *bin.Encoder) error {
return fmt.Errorf("unable to write instruction version: %w", err)
}

err = encoder.WriteUint32(i.TypeID, binary.LittleEndian)
err = encoder.WriteUint32(i.TypeID.Uint32(), binary.LittleEndian)
if err != nil {
return fmt.Errorf("unable to write variant type: %w", err)
}
Expand Down
7 changes: 4 additions & 3 deletions programs/serum/instruction_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package serum

import (
"encoding/binary"
"encoding/hex"
"testing"

Expand All @@ -20,7 +21,7 @@ func TestDecodeInstruction(t *testing.T) {
hexData: "000900000001000000b80600000000000010eb09000000000000000000168106e091da511601000000",
expectInstruction: &Instruction{
BaseVariant: bin.BaseVariant{
TypeID: 9,
TypeID: bin.TypeIDFromUint32(9, binary.LittleEndian),
Impl: &InstructionNewOrderV2{
Side: SideAsk,
LimitPrice: 1720,
Expand All @@ -38,7 +39,7 @@ func TestDecodeInstruction(t *testing.T) {
hexData: "0002000000ffff",
expectInstruction: &Instruction{
BaseVariant: bin.BaseVariant{
TypeID: 2,
TypeID: bin.TypeIDFromUint32(2, binary.LittleEndian),
Impl: &InstructionMatchOrder{
Limit: 65535,
},
Expand All @@ -51,7 +52,7 @@ func TestDecodeInstruction(t *testing.T) {
hexData: "0005000000",
expectInstruction: &Instruction{
BaseVariant: bin.BaseVariant{
TypeID: 5,
TypeID: bin.TypeIDFromUint32(5, binary.LittleEndian),
Impl: &InstructionSettleFunds{},
},
Version: 0,
Expand Down
2 changes: 1 addition & 1 deletion programs/serum/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (s *Slab) UnmarshalWithDecoder(decoder *bin.Decoder) error {
}

func (s *Slab) MarshalWithEncoder(encoder *bin.Encoder) error {
err := encoder.WriteUint32(s.TypeID, binary.LittleEndian)
err := encoder.WriteUint32(s.TypeID.Uint32(), binary.LittleEndian)
if err != nil {
return err
}
Expand Down
13 changes: 7 additions & 6 deletions programs/serum/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package serum

import (
"encoding/base64"
"encoding/binary"
"encoding/hex"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -89,7 +90,7 @@ func TestDecoder_Orderbook(t *testing.T) {
assert.Equal(t, 101, len(ob.Nodes))
assert.Equal(t, &Slab{
BaseVariant: bin.BaseVariant{
TypeID: 1,
TypeID: bin.TypeIDFromUint32(1, binary.LittleEndian),
Impl: &SlabInnerNode{
PrefixLen: 57,
Key: bin.Uint128{
Expand All @@ -108,7 +109,7 @@ func TestDecoder_Orderbook(t *testing.T) {
}, ob.Nodes[0])
assert.Equal(t, &Slab{
BaseVariant: bin.BaseVariant{
TypeID: 3,
TypeID: bin.TypeIDFromUint32(3, binary.LittleEndian),
Impl: &SlabFreeNode{
Next: 2,
Padding: [64]byte{
Expand All @@ -125,7 +126,7 @@ func TestDecoder_Orderbook(t *testing.T) {
}, ob.Nodes[1])
assert.Equal(t, &Slab{
BaseVariant: bin.BaseVariant{
TypeID: 2,
TypeID: bin.TypeIDFromUint32(2, binary.LittleEndian),
Impl: &SlabLeafNode{
OwnerSlot: 1,
FeeTier: 5,
Expand Down Expand Up @@ -154,7 +155,7 @@ func TestDecoder_Slabs(t *testing.T) {
slabData: "0100000035000000010babffffffffff4105000000000000400000003f00000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
expectSlab: &Slab{
BaseVariant: bin.BaseVariant{
TypeID: 1,
TypeID: bin.TypeIDFromUint32(1, binary.LittleEndian),
Impl: &SlabInnerNode{
PrefixLen: 53,
Key: bin.Uint128{
Expand All @@ -175,7 +176,7 @@ func TestDecoder_Slabs(t *testing.T) {
slabData: "0200000014060000b2cea5ffffffffff23070000000000005ae01b52d00a090c6dc6fce8e37a225815cff2223a99c6dfdad5aae56d3db670e62c000000000000140b0fadcf8fcebf",
expectSlab: &Slab{
BaseVariant: bin.BaseVariant{
TypeID: 2,
TypeID: bin.TypeIDFromUint32(2, binary.LittleEndian),
Impl: &SlabLeafNode{
OwnerSlot: 20,
FeeTier: 6,
Expand All @@ -196,7 +197,7 @@ func TestDecoder_Slabs(t *testing.T) {
slabData: "030000003400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
expectSlab: &Slab{
BaseVariant: bin.BaseVariant{
TypeID: 3,
TypeID: bin.TypeIDFromUint32(3, binary.LittleEndian),
Impl: &SlabFreeNode{
Next: 52,
},
Expand Down
152 changes: 101 additions & 51 deletions programs/system/AdvanceNonceAccount.go
Original file line number Diff line number Diff line change
@@ -1,81 +1,131 @@
package system

import (
"encoding/binary"
"fmt"

bin "github.com/dfuse-io/binary"
solana "github.com/gagliardetto/solana-go"
ag_binary "github.com/dfuse-io/binary"
ag_solanago "github.com/gagliardetto/solana-go"
ag_format "github.com/gagliardetto/solana-go/text/format"
ag_treeout "github.com/gagliardetto/treeout"
)

func NewAdvanceNonceAccountInstruction(
nonceAccount solana.PublicKey,
nonceAuthority solana.PublicKey,
) *Instruction {
return NewAdvanceNonceAccountBuilder().
WithNonceAccount(nonceAccount).
WithNonceAuthority(nonceAuthority).
Build()
}

// Consumes a stored nonce, replacing it with a successor.
// Consumes a stored nonce, replacing it with a successor
type AdvanceNonceAccount struct {
// [0] = [WRITE] Nonce account.
// [1] = [] RecentBlockhashes sysvar.
// [2] = [SIGNER] Nonce authority.
solana.AccountMetaSlice `bin:"-"`

// [0] = [WRITE] NonceAccount
// ··········· Nonce account
//
// [1] = [] $(SysVarRecentBlockHashesPubkey)
// ··········· RecentBlockhashes sysvar
//
// [2] = [SIGNER] NonceAuthorityAccount
// ··········· Nonce authority
ag_solanago.AccountMetaSlice `bin:"-" borsh_skip:"true"`
}

// NewAdvanceNonceAccountBuilder initializes a new AdvanceNonceAccount builder.
func NewAdvanceNonceAccountBuilder() *AdvanceNonceAccount {
nb := &AdvanceNonceAccount{
AccountMetaSlice: make(solana.AccountMetaSlice, 3),
// NewAdvanceNonceAccountInstructionBuilder creates a new `AdvanceNonceAccount` instruction builder.
func NewAdvanceNonceAccountInstructionBuilder() *AdvanceNonceAccount {
nd := &AdvanceNonceAccount{
AccountMetaSlice: make(ag_solanago.AccountMetaSlice, 3),
}
nb.AccountMetaSlice[1] = solana.Meta(solana.SysVarRecentBlockHashesPubkey)
return nb
nd.AccountMetaSlice[1] = ag_solanago.Meta(ag_solanago.SysVarRecentBlockHashesPubkey)
return nd
}

func (ins *AdvanceNonceAccount) WithNonceAccount(nonceAccount solana.PublicKey) *AdvanceNonceAccount {
ins.AccountMetaSlice[0] = solana.Meta(nonceAccount).WRITE()
return ins
// Nonce account
func (inst *AdvanceNonceAccount) SetNonceAccount(nonceAccount ag_solanago.PublicKey) *AdvanceNonceAccount {
inst.AccountMetaSlice[0] = ag_solanago.Meta(nonceAccount).WRITE()
return inst
}

func (ins *AdvanceNonceAccount) GetNonceAccount() *solana.PublicKey {
ac := ins.AccountMetaSlice[0]
if ac == nil {
return nil
}
return &ac.PublicKey
func (inst *AdvanceNonceAccount) GetNonceAccount() *ag_solanago.AccountMeta {
return inst.AccountMetaSlice[0]
}

// RecentBlockhashes sysvar
func (inst *AdvanceNonceAccount) SetSysVarRecentBlockHashesPubkeyAccount(SysVarRecentBlockHashesPubkey ag_solanago.PublicKey) *AdvanceNonceAccount {
inst.AccountMetaSlice[1] = ag_solanago.Meta(SysVarRecentBlockHashesPubkey)
return inst
}

func (inst *AdvanceNonceAccount) GetSysVarRecentBlockHashesPubkeyAccount() *ag_solanago.AccountMeta {
return inst.AccountMetaSlice[1]
}

// Nonce authority
func (inst *AdvanceNonceAccount) SetNonceAuthorityAccount(nonceAuthorityAccount ag_solanago.PublicKey) *AdvanceNonceAccount {
inst.AccountMetaSlice[2] = ag_solanago.Meta(nonceAuthorityAccount).SIGNER()
return inst
}

func (ins *AdvanceNonceAccount) WithNonceAuthority(nonceAuthority solana.PublicKey) *AdvanceNonceAccount {
ins.AccountMetaSlice[2] = solana.Meta(nonceAuthority).SIGNER()
return ins
func (inst *AdvanceNonceAccount) GetNonceAuthorityAccount() *ag_solanago.AccountMeta {
return inst.AccountMetaSlice[2]
}

func (ins *AdvanceNonceAccount) GetNonceAuthority() *solana.PublicKey {
ac := ins.AccountMetaSlice[2]
if ac == nil {
return nil
func (inst AdvanceNonceAccount) Build() *Instruction {
return &Instruction{BaseVariant: ag_binary.BaseVariant{
Impl: inst,
TypeID: ag_binary.TypeIDFromUint32(Instruction_AdvanceNonceAccount, binary.LittleEndian),
}}
}

// ValidateAndBuild validates the instruction parameters and accounts;
// if there is a validation error, it returns the error.
// Otherwise, it builds and returns the instruction.
func (inst AdvanceNonceAccount) ValidateAndBuild() (*Instruction, error) {
if err := inst.Validate(); err != nil {
return nil, err
}
return &ac.PublicKey
return inst.Build(), nil
}

func (ins *AdvanceNonceAccount) Validate() error {
for accIndex, acc := range ins.AccountMetaSlice {
func (inst *AdvanceNonceAccount) Validate() error {
// Check whether all accounts are set:
for accIndex, acc := range inst.AccountMetaSlice {
if acc == nil {
return fmt.Errorf("ins.AccountMetaSlice[%v] is nil", accIndex)
return fmt.Errorf("ins.AccountMetaSlice[%v] is not set", accIndex)
}
}
return nil
}

func (ins *AdvanceNonceAccount) Build() *Instruction {
return &Instruction{
BaseVariant: bin.BaseVariant{
func (inst *AdvanceNonceAccount) EncodeToTree(parent ag_treeout.Branches) {
parent.Child(ag_format.Program(ProgramName, ProgramID)).
//
ParentFunc(func(programBranch ag_treeout.Branches) {
programBranch.Child(ag_format.Instruction("AdvanceNonceAccount")).
//
ParentFunc(func(instructionBranch ag_treeout.Branches) {

TypeID: Instruction_AdvanceNonceAccount,
// Parameters of the instruction:
instructionBranch.Child("Params").ParentFunc(func(paramsBranch ag_treeout.Branches) {})

Impl: ins,
},
}
// Accounts of the instruction:
instructionBranch.Child("Accounts").ParentFunc(func(accountsBranch ag_treeout.Branches) {
accountsBranch.Child(ag_format.Meta("NonceAccount", inst.AccountMetaSlice[0]))
accountsBranch.Child(ag_format.Meta("$(SysVarRecentBlockHashesPubkey)", inst.AccountMetaSlice[1]))
accountsBranch.Child(ag_format.Meta("NonceAuthorityAccount", inst.AccountMetaSlice[2]))
})
})
})
}

func (inst AdvanceNonceAccount) MarshalWithEncoder(encoder *ag_binary.Encoder) error {
return nil
}

func (inst *AdvanceNonceAccount) UnmarshalWithDecoder(decoder *ag_binary.Decoder) error {
return nil
}

// NewAdvanceNonceAccountInstruction declares a new AdvanceNonceAccount instruction with the provided parameters and accounts.
func NewAdvanceNonceAccountInstruction(
// Accounts:
nonceAccount ag_solanago.PublicKey,
SysVarRecentBlockHashesPubkey ag_solanago.PublicKey,
nonceAuthorityAccount ag_solanago.PublicKey) *AdvanceNonceAccount {
return NewAdvanceNonceAccountInstructionBuilder().
SetNonceAccount(nonceAccount).
SetSysVarRecentBlockHashesPubkeyAccount(SysVarRecentBlockHashesPubkey).
SetNonceAuthorityAccount(nonceAuthorityAccount)
}
Loading

0 comments on commit 3853032

Please sign in to comment.