Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions programs/token/AmountToUiAmount.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Copyright 2021 github.com/gagliardetto
//
// 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.

package token

import (
"errors"

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

// Convert an Amount of tokens to a UiAmount string, using the given mint.
// In this version of the program, the mint can only specify the number of decimals.
//
// Return data can be fetched using sol_get_return_data and deserialized
// with String::from_utf8.
type AmountToUiAmount struct {
// The amount of tokens to reformat.
Amount *uint64

// [0] = [] mint
// ··········· The mint to calculate for.
ag_solanago.AccountMetaSlice `bin:"-" borsh_skip:"true"`
}

func NewAmountToUiAmountInstructionBuilder() *AmountToUiAmount {
nd := &AmountToUiAmount{
AccountMetaSlice: make(ag_solanago.AccountMetaSlice, 1),
}
return nd
}

func (inst *AmountToUiAmount) SetAmount(amount uint64) *AmountToUiAmount {
inst.Amount = &amount
return inst
}

func (inst *AmountToUiAmount) SetMintAccount(mint ag_solanago.PublicKey) *AmountToUiAmount {
inst.AccountMetaSlice[0] = ag_solanago.Meta(mint)
return inst
}

func (inst *AmountToUiAmount) GetMintAccount() *ag_solanago.AccountMeta {
return inst.AccountMetaSlice[0]
}

func (inst AmountToUiAmount) Build() *Instruction {
return &Instruction{BaseVariant: ag_binary.BaseVariant{
Impl: inst,
TypeID: ag_binary.TypeIDFromUint8(Instruction_AmountToUiAmount),
}}
}

func (inst AmountToUiAmount) ValidateAndBuild() (*Instruction, error) {
if err := inst.Validate(); err != nil {
return nil, err
}
return inst.Build(), nil
}

func (inst *AmountToUiAmount) Validate() error {
if inst.Amount == nil {
return errors.New("Amount parameter is not set")
}
if inst.AccountMetaSlice[0] == nil {
return errors.New("accounts.Mint is not set")
}
return nil
}

func (inst *AmountToUiAmount) EncodeToTree(parent ag_treeout.Branches) {
parent.Child(ag_format.Program(ProgramName, ProgramID)).
ParentFunc(func(programBranch ag_treeout.Branches) {
programBranch.Child(ag_format.Instruction("AmountToUiAmount")).
ParentFunc(func(instructionBranch ag_treeout.Branches) {
instructionBranch.Child("Params").ParentFunc(func(paramsBranch ag_treeout.Branches) {
paramsBranch.Child(ag_format.Param("Amount", *inst.Amount))
})
instructionBranch.Child("Accounts").ParentFunc(func(accountsBranch ag_treeout.Branches) {
accountsBranch.Child(ag_format.Meta("mint", inst.AccountMetaSlice[0]))
})
})
})
}

func (obj AmountToUiAmount) MarshalWithEncoder(encoder *ag_binary.Encoder) (err error) {
err = encoder.Encode(obj.Amount)
if err != nil {
return err
}
return nil
}

func (obj *AmountToUiAmount) UnmarshalWithDecoder(decoder *ag_binary.Decoder) (err error) {
err = decoder.Decode(&obj.Amount)
if err != nil {
return err
}
return nil
}

func NewAmountToUiAmountInstruction(
amount uint64,
mint ag_solanago.PublicKey,
) *AmountToUiAmount {
return NewAmountToUiAmountInstructionBuilder().
SetAmount(amount).
SetMintAccount(mint)
}
44 changes: 44 additions & 0 deletions programs/token/AmountToUiAmount_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2021 github.com/gagliardetto
//
// 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.

package token

import (
"bytes"
ag_gofuzz "github.com/gagliardetto/gofuzz"
ag_require "github.com/stretchr/testify/require"
"strconv"
"testing"
)

func TestEncodeDecode_AmountToUiAmount(t *testing.T) {
fu := ag_gofuzz.New().NilChance(0)
for i := 0; i < 1; i++ {
t.Run("AmountToUiAmount"+strconv.Itoa(i), func(t *testing.T) {
{
params := new(AmountToUiAmount)
fu.Fuzz(params)
params.AccountMetaSlice = nil
buf := new(bytes.Buffer)
err := encodeT(*params, buf)
ag_require.NoError(t, err)
got := new(AmountToUiAmount)
err = decodeT(got, buf.Bytes())
got.AccountMetaSlice = nil
ag_require.NoError(t, err)
ag_require.Equal(t, params, got)
}
})
}
}
173 changes: 173 additions & 0 deletions programs/token/Batch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
// Copyright 2021 github.com/gagliardetto
//
// 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.

package token

import (
"bytes"
"errors"
"fmt"

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

// Batch allows executing multiple token instructions in a single CPI call,
// reducing the overhead of multiple cross-program invocations.
//
// Each sub-instruction in the batch is prefixed with a 2-byte header:
// - byte 0: number of accounts for this sub-instruction
// - byte 1: length of instruction data for this sub-instruction
//
// This instruction is only available in the p-token (Pinocchio) implementation.
type Batch struct {
Instructions []*Instruction

ag_solanago.AccountMetaSlice `bin:"-" borsh_skip:"true"`
}

func NewBatchInstructionBuilder() *Batch {
return &Batch{
AccountMetaSlice: make(ag_solanago.AccountMetaSlice, 0),
}
}

func (inst *Batch) AddInstruction(ix *Instruction) *Batch {
inst.Instructions = append(inst.Instructions, ix)
return inst
}

func (inst Batch) Build() *Instruction {
accounts := make(ag_solanago.AccountMetaSlice, 0)
for _, ix := range inst.Instructions {
accounts = append(accounts, ix.Accounts()...)
}
inst.AccountMetaSlice = accounts

return &Instruction{BaseVariant: ag_binary.BaseVariant{
Impl: inst,
TypeID: ag_binary.TypeIDFromUint8(Instruction_Batch),
}}
}

func (inst Batch) ValidateAndBuild() (*Instruction, error) {
if err := inst.Validate(); err != nil {
return nil, err
}
return inst.Build(), nil
}

func (inst *Batch) Validate() error {
if len(inst.Instructions) == 0 {
return errors.New("batch must contain at least one instruction")
}
return nil
}

func (inst *Batch) EncodeToTree(parent ag_treeout.Branches) {
parent.Child(ag_format.Program(ProgramName, ProgramID)).
ParentFunc(func(programBranch ag_treeout.Branches) {
programBranch.Child(ag_format.Instruction("Batch")).
ParentFunc(func(instructionBranch ag_treeout.Branches) {
instructionBranch.Child("Params").ParentFunc(func(paramsBranch ag_treeout.Branches) {
paramsBranch.Child(ag_format.Param("InstructionCount", len(inst.Instructions)))
})
instructionBranch.Child("Accounts").ParentFunc(func(accountsBranch ag_treeout.Branches) {
for i, acc := range inst.AccountMetaSlice {
accountsBranch.Child(ag_format.Meta(fmt.Sprintf("[%v]", i), acc))
}
})
})
})
}

func (obj Batch) MarshalWithEncoder(encoder *ag_binary.Encoder) (err error) {
for _, ix := range obj.Instructions {
accountCount := uint8(len(ix.Accounts()))

data, err := ix.Data()
if err != nil {
return fmt.Errorf("unable to encode batch sub-instruction: %w", err)
}
// data includes the discriminator byte from the outer Instruction encoding,
// but for batch sub-instructions we need the raw inner data (discriminator + params).
// The ix.Data() already produces [discriminator | params], which is what we need.
dataLen := uint8(len(data))

if err = encoder.WriteUint8(accountCount); err != nil {
return err
}
if err = encoder.WriteUint8(dataLen); err != nil {
return err
}
if _, err = encoder.Write(data); err != nil {
return err
}
}
return nil
}

func (obj *Batch) UnmarshalWithDecoder(decoder *ag_binary.Decoder) (err error) {
for decoder.HasRemaining() {
accountCount, err := decoder.ReadUint8()
if err != nil {
return err
}
dataLen, err := decoder.ReadUint8()
if err != nil {
return err
}
_ = accountCount

data, err := decoder.ReadNBytes(int(dataLen))
if err != nil {
return err
}
ix := new(Instruction)
if err = ag_binary.NewBinDecoder(data).Decode(ix); err != nil {
return fmt.Errorf("unable to decode batch sub-instruction: %w", err)
}
obj.Instructions = append(obj.Instructions, ix)
}
return nil
}

// BuildBatchData constructs the complete instruction data for a batch,
// including the batch discriminator (255) and all sub-instruction data.
func BuildBatchData(instructions []*Instruction) ([]byte, error) {
buf := new(bytes.Buffer)
buf.WriteByte(Instruction_Batch)
for _, ix := range instructions {
accountCount := uint8(len(ix.Accounts()))
data, err := ix.Data()
if err != nil {
return nil, fmt.Errorf("unable to encode batch sub-instruction: %w", err)
}
dataLen := uint8(len(data))
buf.WriteByte(accountCount)
buf.WriteByte(dataLen)
buf.Write(data)
}
return buf.Bytes(), nil
}

func NewBatchInstruction(instructions ...*Instruction) *Batch {
b := NewBatchInstructionBuilder()
for _, ix := range instructions {
b.AddInstruction(ix)
}
return b
}
38 changes: 38 additions & 0 deletions programs/token/Batch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2021 github.com/gagliardetto
//
// 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.

package token

import (
ag_require "github.com/stretchr/testify/require"
"testing"
)

func TestEncodeDecode_Batch(t *testing.T) {
t.Run("Batch_InstructionIDToName", func(t *testing.T) {
ag_require.Equal(t, "Batch", InstructionIDToName(Instruction_Batch))
ag_require.Equal(t, "WithdrawExcessLamports", InstructionIDToName(Instruction_WithdrawExcessLamports))
ag_require.Equal(t, "UnwrapLamports", InstructionIDToName(Instruction_UnwrapLamports))
})

t.Run("Batch_InstructionIDs", func(t *testing.T) {
ag_require.Equal(t, uint8(21), Instruction_GetAccountDataSize)
ag_require.Equal(t, uint8(22), Instruction_InitializeImmutableOwner)
ag_require.Equal(t, uint8(23), Instruction_AmountToUiAmount)
ag_require.Equal(t, uint8(24), Instruction_UiAmountToAmount)
ag_require.Equal(t, uint8(38), Instruction_WithdrawExcessLamports)
ag_require.Equal(t, uint8(45), Instruction_UnwrapLamports)
ag_require.Equal(t, uint8(255), Instruction_Batch)
})
}
Loading