-
Notifications
You must be signed in to change notification settings - Fork 431
feat(token): add p-token (SIMD-0266) and missing SPL Token instructions #366
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hxuan190
wants to merge
3
commits into
solana-foundation:main
Choose a base branch
from
hxuan190:feat/ptoken-instructions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| // 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" | ||
| "fmt" | ||
| ag_gofuzz "github.com/gagliardetto/gofuzz" | ||
| ag_solanago "github.com/gagliardetto/solana-go" | ||
| 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) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestAmountToUiAmount_Validate(t *testing.T) { | ||
| mint := ag_solanago.NewWallet().PublicKey() | ||
|
|
||
| t.Run("missing amount returns error", func(t *testing.T) { | ||
| ix := NewAmountToUiAmountInstructionBuilder().SetMintAccount(mint) | ||
| ag_require.Error(t, ix.Validate()) | ||
| }) | ||
|
|
||
| t.Run("missing mint returns error", func(t *testing.T) { | ||
| ix := NewAmountToUiAmountInstructionBuilder().SetAmount(1000) | ||
| ag_require.Error(t, ix.Validate()) | ||
| }) | ||
|
|
||
| t.Run("all fields set passes validation", func(t *testing.T) { | ||
| ag_require.NoError(t, NewAmountToUiAmountInstruction(1000, mint).Validate()) | ||
| }) | ||
| } | ||
|
|
||
| func TestAmountToUiAmount_EncodeDecode_EdgeCases(t *testing.T) { | ||
| for _, amount := range []uint64{0, 1, 1_000_000_000, ^uint64(0)} { | ||
| amount := amount | ||
| t.Run(fmt.Sprintf("amount=%d", amount), func(t *testing.T) { | ||
| params := &AmountToUiAmount{Amount: &amount} | ||
| buf := new(bytes.Buffer) | ||
| ag_require.NoError(t, encodeT(*params, buf)) | ||
| got := new(AmountToUiAmount) | ||
| ag_require.NoError(t, decodeT(got, buf.Bytes())) | ||
| ag_require.Equal(t, amount, *got.Amount) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| // 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" | ||
| "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) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Batch.MarshalWithEncoder silently truncates if a sub-instruction has > 255 accounts or > 255 bytes of data — please return an explicit error. |
||
| for _, ix := range obj.Instructions { | ||
| data, err := ix.Data() | ||
| if err != nil { | ||
| return fmt.Errorf("unable to encode batch sub-instruction: %w", err) | ||
| } | ||
| if err = encoder.WriteUint8(uint8(len(ix.Accounts()))); err != nil { | ||
| return err | ||
| } | ||
| if err = encoder.WriteUint8(uint8(len(data))); 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 is a convenience wrapper that returns the fully-encoded batch | ||
| // instruction bytes (discriminator + sub-instruction headers + data). | ||
| func BuildBatchData(instructions []*Instruction) ([]byte, error) { | ||
| return NewBatchInstruction(instructions...).Build().Data() | ||
| } | ||
|
|
||
| func NewBatchInstruction(instructions ...*Instruction) *Batch { | ||
| b := NewBatchInstructionBuilder() | ||
| for _, ix := range instructions { | ||
| b.AddInstruction(ix) | ||
| } | ||
| return b | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| }) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this instruction delivesr their result via
sol_get_return_data