2020-11-09 10:09:50 -08:00
|
|
|
// Copyright 2020 dfuse Platform Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2020-08-14 15:29:24 -07:00
|
|
|
package system
|
|
|
|
|
|
|
|
import (
|
2020-11-18 05:39:50 -08:00
|
|
|
"fmt"
|
|
|
|
|
2020-11-12 14:57:43 -08:00
|
|
|
bin "github.com/dfuse-io/binary"
|
2020-08-19 08:28:49 -07:00
|
|
|
solana "github.com/dfuse-io/solana-go"
|
2020-08-14 15:29:24 -07:00
|
|
|
)
|
|
|
|
|
2020-11-18 13:45:59 -08:00
|
|
|
var PROGRAM_ID = solana.MustPublicKeyFromBase58("11111111111111111111111111111111")
|
2020-11-18 05:39:50 -08:00
|
|
|
|
|
|
|
func init() {
|
2020-11-18 13:45:59 -08:00
|
|
|
solana.RegisterInstructionDecoder(PROGRAM_ID, registryDecodeInstruction)
|
2020-11-18 05:39:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func registryDecodeInstruction(accounts []solana.PublicKey, rawInstruction *solana.CompiledInstruction) (interface{}, error) {
|
|
|
|
inst, err := DecodeInstruction(accounts, rawInstruction)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return inst, nil
|
|
|
|
}
|
|
|
|
|
2020-11-18 13:45:59 -08:00
|
|
|
func DecodeInstruction(accounts []solana.PublicKey, compiledInstruction *solana.CompiledInstruction) (*Instruction, error) {
|
|
|
|
var inst *Instruction
|
2020-11-18 05:39:50 -08:00
|
|
|
if err := bin.NewDecoder(compiledInstruction.Data).Decode(&inst); err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to decode instruction for serum program: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := inst.Impl.(solana.AccountSettable); ok {
|
2020-11-18 13:53:31 -08:00
|
|
|
err := v.SetAccounts(accounts, compiledInstruction.Accounts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to set accounts for instruction: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-11-18 05:39:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return inst, nil
|
|
|
|
}
|
|
|
|
|
2020-11-18 13:45:59 -08:00
|
|
|
type Instruction struct {
|
2020-11-12 14:57:43 -08:00
|
|
|
bin.BaseVariant
|
2020-08-14 15:29:24 -07:00
|
|
|
}
|
|
|
|
|
2020-11-18 13:45:59 -08:00
|
|
|
func (i *Instruction) String() string {
|
|
|
|
return fmt.Sprintf("%s", i.Impl)
|
|
|
|
}
|
|
|
|
|
|
|
|
var InstructionImplDef = bin.NewVariantDefinition(bin.Uint32TypeIDEncoding, []bin.VariantType{
|
2020-11-12 14:57:43 -08:00
|
|
|
{"create_account", (*CreateAccount)(nil)},
|
|
|
|
{"assign", (*Assign)(nil)},
|
|
|
|
{"transfer", (*Transfer)(nil)},
|
|
|
|
})
|
2020-11-06 11:43:44 -08:00
|
|
|
|
2020-11-18 13:45:59 -08:00
|
|
|
func (i *Instruction) UnmarshalBinary(decoder *bin.Decoder) error {
|
|
|
|
return i.BaseVariant.UnmarshalBinaryVariant(decoder, InstructionImplDef)
|
2020-11-09 07:02:28 -08:00
|
|
|
}
|
2020-11-06 11:43:44 -08:00
|
|
|
|
2020-08-14 15:29:24 -07:00
|
|
|
type CreateAccount struct {
|
|
|
|
// prefixed with byte 0x00
|
2020-11-12 14:57:43 -08:00
|
|
|
Lamports bin.Uint64
|
|
|
|
Space bin.Uint64
|
2020-08-14 15:29:24 -07:00
|
|
|
Owner solana.PublicKey
|
|
|
|
}
|
|
|
|
|
2020-11-18 13:45:59 -08:00
|
|
|
func (i *CreateAccount) String() string {
|
|
|
|
out := "Create Account\n"
|
|
|
|
out += fmt.Sprintf("Lamports: %d, space: %d, Owner: %s", i.Lamports, i.Space, i.Owner.String())
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2020-08-14 15:29:24 -07:00
|
|
|
type Assign struct {
|
|
|
|
// prefixed with byte 0x01
|
|
|
|
Owner solana.PublicKey
|
|
|
|
}
|
|
|
|
|
2020-11-18 13:45:59 -08:00
|
|
|
func (i *Assign) String() string {
|
|
|
|
out := "Assign\n"
|
|
|
|
out += fmt.Sprintf("Owner: %s", i.Owner.String())
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2020-08-14 15:29:24 -07:00
|
|
|
type Transfer struct {
|
|
|
|
// Prefixed with byte 0x02
|
2020-11-12 14:57:43 -08:00
|
|
|
Lamports bin.Uint64
|
2020-08-14 15:29:24 -07:00
|
|
|
}
|
|
|
|
|
2020-11-18 13:45:59 -08:00
|
|
|
func (t *Transfer) String() string {
|
|
|
|
out := "Transfer\n"
|
|
|
|
out += fmt.Sprintf("Lamports: %d", t.Lamports)
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2020-08-14 15:29:24 -07:00
|
|
|
type CreateAccountWithSeed struct {
|
|
|
|
// Prefixed with byte 0x03
|
|
|
|
Base solana.PublicKey
|
2020-11-13 06:21:00 -08:00
|
|
|
SeedSize int `bin:"sizeof=Seed"`
|
2020-08-14 15:29:24 -07:00
|
|
|
Seed string
|
2020-11-12 14:57:43 -08:00
|
|
|
Lamports bin.Uint64
|
|
|
|
Space bin.Uint64
|
2020-08-14 15:29:24 -07:00
|
|
|
Owner solana.PublicKey
|
|
|
|
}
|
|
|
|
|
2020-11-18 13:45:59 -08:00
|
|
|
func (i *CreateAccountWithSeed) String() string {
|
|
|
|
out := "Create Account With Seed\n"
|
|
|
|
out += fmt.Sprintf("Base: %s SeedSize: %d Seed: %s Lamports: %d Space: %d Owner: %s", i.Base, i.SeedSize, i.Seed, i.Lamports, i.Space, i.Owner.String())
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2020-08-14 15:29:24 -07:00
|
|
|
type AdvanceNonceAccount struct {
|
|
|
|
// Prefix with 0x04
|
|
|
|
}
|
|
|
|
|
2020-11-18 13:45:59 -08:00
|
|
|
func (i *AdvanceNonceAccount) String() string {
|
|
|
|
out := "Advance Nonce Account\n"
|
|
|
|
out += "Accounts:"
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2020-08-14 15:29:24 -07:00
|
|
|
type WithdrawNonceAccount struct {
|
|
|
|
// Prefix with 0x05
|
2020-11-12 14:57:43 -08:00
|
|
|
Lamports bin.Uint64
|
2020-08-14 15:29:24 -07:00
|
|
|
}
|
|
|
|
|
2020-11-18 13:45:59 -08:00
|
|
|
func (i *WithdrawNonceAccount) String() string {
|
|
|
|
out := "Withdraw Nonce Account\n"
|
|
|
|
out += fmt.Sprintf("Lamports: %d", i.Lamports)
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2020-08-14 15:29:24 -07:00
|
|
|
type InitializeNonceAccount struct {
|
|
|
|
// Prefix with 0x06
|
|
|
|
AuthorizedAccount solana.PublicKey
|
|
|
|
}
|
|
|
|
|
2020-11-18 13:45:59 -08:00
|
|
|
func (i *InitializeNonceAccount) String() string {
|
|
|
|
out := "Initialize Nonce Account\n"
|
|
|
|
out += fmt.Sprintf("AuthorizedAccount: %s", i.AuthorizedAccount.String())
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2020-08-14 15:29:24 -07:00
|
|
|
type AuthorizeNonceAccount struct {
|
|
|
|
// Prefix with 0x07
|
|
|
|
AuthorizeAccount solana.PublicKey
|
|
|
|
}
|
|
|
|
|
2020-11-18 13:45:59 -08:00
|
|
|
func (i *AuthorizeNonceAccount) String() string {
|
|
|
|
out := "Authorize Nonce Account\n"
|
|
|
|
out += fmt.Sprintf("AuthorizedAccount: %s", i.AuthorizeAccount.String())
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2020-08-14 15:29:24 -07:00
|
|
|
type Allocate struct {
|
|
|
|
// Prefix with 0x08
|
2020-11-12 14:57:43 -08:00
|
|
|
Space bin.Uint64
|
2020-08-14 15:29:24 -07:00
|
|
|
}
|
|
|
|
|
2020-11-18 13:45:59 -08:00
|
|
|
func (i *Allocate) String() string {
|
|
|
|
out := "Allocate"
|
|
|
|
out += fmt.Sprintf("Space: %d", i.Space)
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2020-08-14 15:29:24 -07:00
|
|
|
type AllocateWithSeed struct {
|
|
|
|
// Prefixed with byte 0x09
|
|
|
|
Base solana.PublicKey
|
2020-11-13 06:21:00 -08:00
|
|
|
SeedSize int `bin:"sizeof=Seed"`
|
2020-08-14 15:29:24 -07:00
|
|
|
Seed string
|
2020-11-12 14:57:43 -08:00
|
|
|
Space bin.Uint64
|
2020-08-14 15:29:24 -07:00
|
|
|
Owner solana.PublicKey
|
|
|
|
}
|
|
|
|
|
2020-11-18 13:45:59 -08:00
|
|
|
func (i *AllocateWithSeed) String() string {
|
|
|
|
out := "Allocate With Seed\n"
|
2020-11-18 13:53:31 -08:00
|
|
|
out += fmt.Sprintf("Base: %s SeedSize: %d Seed: %s Space: %d Owner: %s", i.Base, i.SeedSize, i.Seed, i.Space, i.Owner)
|
2020-11-18 13:45:59 -08:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2020-08-14 15:29:24 -07:00
|
|
|
type AssignWithSeed struct {
|
|
|
|
// Prefixed with byte 0x0a
|
|
|
|
Base solana.PublicKey
|
2020-11-13 06:21:00 -08:00
|
|
|
SeedSize int `bin:"sizeof=Seed"`
|
2020-08-14 15:29:24 -07:00
|
|
|
Seed string
|
|
|
|
Owner solana.PublicKey
|
|
|
|
}
|
2020-11-18 13:45:59 -08:00
|
|
|
|
|
|
|
func (i *AssignWithSeed) String() string {
|
|
|
|
out := "Assign With Seed\n"
|
|
|
|
out += fmt.Sprintf("Base: %s SeedSize: %d Seed: %s Owner: %s", i.Base, i.SeedSize, i.Seed, i.Owner)
|
|
|
|
return out
|
|
|
|
}
|