-
Notifications
You must be signed in to change notification settings - Fork 431
Add SetAccounts implementation to be able to access accounts for Create instruction after decoding #337
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
Add SetAccounts implementation to be able to access accounts for Create instruction after decoding #337
Changes from all commits
d587f06
b492372
08cab13
d672f74
4d45872
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,9 +46,6 @@ type Create struct { | |
| // | ||
| // [5] = [] TokenProgram | ||
| // ··········· SPL token program ID | ||
| // | ||
| // [6] = [] SysVarRent | ||
| // ··········· SysVarRentPubkey | ||
|
Comment on lines
-49
to
-51
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. I'm so confused why this was here.
Contributor
Author
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. Yeah it wasn't clear to me either 😅 |
||
| solana.AccountMetaSlice `bin:"-" borsh_skip:"true"` | ||
| } | ||
|
|
||
|
|
@@ -73,8 +70,18 @@ func (inst *Create) SetMint(mint solana.PublicKey) *Create { | |
| return inst | ||
| } | ||
|
|
||
| func (inst Create) Build() *Instruction { | ||
| func (inst *Create) SetAccounts(accounts []*solana.AccountMeta) error { | ||
| inst.AccountMetaSlice = accounts | ||
| if len(accounts) < 6 { | ||
| return fmt.Errorf("insufficient accounts, Create requires at-least 6 accounts not %d", len(accounts)) | ||
| } | ||
| inst.Payer = accounts[0].PublicKey | ||
| inst.Wallet = accounts[2].PublicKey | ||
| inst.Mint = accounts[3].PublicKey | ||
| return nil | ||
| } | ||
|
|
||
| func (inst Create) Build() *Instruction { | ||
| // Find the associatedTokenAddress; | ||
| associatedTokenAddress, _, _ := solana.FindAssociatedTokenAddress( | ||
| inst.Wallet, | ||
|
|
@@ -112,11 +119,6 @@ func (inst Create) Build() *Instruction { | |
| IsSigner: false, | ||
| IsWritable: false, | ||
| }, | ||
| { | ||
| PublicKey: solana.SysVarRentPubkey, | ||
| IsSigner: false, | ||
| IsWritable: false, | ||
| }, | ||
| } | ||
|
|
||
| inst.AccountMetaSlice = keys | ||
|
|
@@ -139,13 +141,13 @@ func (inst Create) ValidateAndBuild() (*Instruction, error) { | |
|
|
||
| func (inst *Create) Validate() error { | ||
| if inst.Payer.IsZero() { | ||
| return errors.New("Payer not set") | ||
| return errors.New("payer not set") | ||
|
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. ty for thinking of go |
||
| } | ||
| if inst.Wallet.IsZero() { | ||
| return errors.New("Wallet not set") | ||
| return errors.New("wallet not set") | ||
| } | ||
| if inst.Mint.IsZero() { | ||
| return errors.New("Mint not set") | ||
| return errors.New("mint not set") | ||
| } | ||
| _, _, err := solana.FindAssociatedTokenAddress( | ||
| inst.Wallet, | ||
|
|
@@ -164,19 +166,17 @@ func (inst *Create) EncodeToTree(parent treeout.Branches) { | |
| programBranch.Child(format.Instruction("Create")). | ||
| // | ||
| ParentFunc(func(instructionBranch treeout.Branches) { | ||
|
|
||
| // Parameters of the instruction: | ||
| instructionBranch.Child("Params[len=0]").ParentFunc(func(paramsBranch treeout.Branches) {}) | ||
|
|
||
| // Accounts of the instruction: | ||
| instructionBranch.Child("Accounts[len=7").ParentFunc(func(accountsBranch treeout.Branches) { | ||
| instructionBranch.Child("Accounts[len=6").ParentFunc(func(accountsBranch treeout.Branches) { | ||
| accountsBranch.Child(format.Meta(" payer", inst.AccountMetaSlice.Get(0))) | ||
| accountsBranch.Child(format.Meta("associatedTokenAddress", inst.AccountMetaSlice.Get(1))) | ||
| accountsBranch.Child(format.Meta(" wallet", inst.AccountMetaSlice.Get(2))) | ||
| accountsBranch.Child(format.Meta(" tokenMint", inst.AccountMetaSlice.Get(3))) | ||
| accountsBranch.Child(format.Meta(" systemProgram", inst.AccountMetaSlice.Get(4))) | ||
| accountsBranch.Child(format.Meta(" tokenProgram", inst.AccountMetaSlice.Get(5))) | ||
| accountsBranch.Child(format.Meta(" sysVarRent", inst.AccountMetaSlice.Get(6))) | ||
| }) | ||
| }) | ||
| }) | ||
|
|
@@ -200,3 +200,19 @@ func NewCreateInstruction( | |
| SetWallet(walletAddress). | ||
| SetMint(splTokenMintAddress) | ||
| } | ||
|
|
||
| func (inst *Create) GetPayerAccount() *solana.AccountMeta { | ||
| return inst.AccountMetaSlice.Get(0) | ||
| } | ||
|
|
||
| func (inst *Create) GetAssociatedTokenAddressAccount() *solana.AccountMeta { | ||
| return inst.AccountMetaSlice.Get(1) | ||
| } | ||
|
|
||
| func (inst *Create) GetWalletAccount() *solana.AccountMeta { | ||
| return inst.AccountMetaSlice.Get(2) | ||
| } | ||
|
|
||
| func (inst *Create) GetMintAccount() *solana.AccountMeta { | ||
| return inst.AccountMetaSlice.Get(3) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| // 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 associatedtokenaccount | ||
|
|
||
| import ( | ||
| "encoding/hex" | ||
| "testing" | ||
|
|
||
| bin "github.com/gagliardetto/binary" | ||
| solana "github.com/gagliardetto/solana-go" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestEncodingInstruction(t *testing.T) { | ||
| t.Run("should encode", func(t *testing.T) { | ||
| t.Run("Create", func(t *testing.T) { | ||
| // Build an instruction and ensure current encoding matches implementation | ||
| payer := solana.NewWallet().PublicKey() | ||
| wallet := solana.NewWallet().PublicKey() | ||
| mint := solana.NewWallet().PublicKey() | ||
| ix := NewCreateInstructionBuilder(). | ||
| SetPayer(payer). | ||
| SetWallet(wallet). | ||
| SetMint(mint). | ||
| Build() | ||
| data, err := ix.Data() | ||
| require.NoError(t, err) | ||
| encodedHex := hex.EncodeToString(data) | ||
| // Current ATA Create encodes no payload bytes | ||
| require.Equal(t, "", encodedHex) | ||
| }) | ||
| }) | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| hexData string | ||
| expectInstruction *Instruction | ||
| }{ | ||
| { | ||
| name: "Create", | ||
| hexData: "", | ||
| expectInstruction: &Instruction{ | ||
| BaseVariant: bin.BaseVariant{ | ||
| TypeID: bin.TypeIDFromUint8(0), | ||
| Impl: &Create{}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| t.Run("should encode", func(t *testing.T) { | ||
| for _, test := range tests { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| data, err := test.expectInstruction.Data() | ||
| require.NoError(t, err) | ||
| encodedHex := hex.EncodeToString(data) | ||
| require.Equal(t, test.hexData, encodedHex) | ||
| }) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("should decode", func(t *testing.T) { | ||
| for _, test := range tests { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| data, err := hex.DecodeString(test.hexData) | ||
| require.NoError(t, err) | ||
| var instruction *Instruction | ||
| err = bin.NewBinDecoder(data).Decode(&instruction) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, test.expectInstruction, instruction) | ||
| }) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| func TestDecodeSetsAccountsAndGetters(t *testing.T) { | ||
| payer := solana.NewWallet().PublicKey() | ||
| wallet := solana.NewWallet().PublicKey() | ||
| mint := solana.NewWallet().PublicKey() | ||
|
|
||
| // Build an instruction to obtain correctly ordered accounts and data | ||
| ix := NewCreateInstructionBuilder(). | ||
| SetPayer(payer). | ||
| SetWallet(wallet). | ||
| SetMint(mint). | ||
| Build() | ||
|
|
||
| accounts := ix.Accounts() | ||
| data, err := ix.Data() | ||
| require.NoError(t, err) | ||
|
|
||
| decoded, err := DecodeInstruction(accounts, data) | ||
| require.NoError(t, err) | ||
|
|
||
| create, ok := decoded.Impl.(*Create) | ||
| require.True(t, ok) | ||
|
|
||
| // Check decoded fields populated via SetAccounts | ||
| assert.Equal(t, payer, create.Payer) | ||
| assert.Equal(t, wallet, create.Wallet) | ||
| assert.Equal(t, mint, create.Mint) | ||
|
|
||
| // Check getters return expected account metas | ||
| require.NotNil(t, create.GetPayerAccount()) | ||
| require.NotNil(t, create.GetAssociatedTokenAddressAccount()) | ||
| require.NotNil(t, create.GetWalletAccount()) | ||
| require.NotNil(t, create.GetMintAccount()) | ||
|
|
||
| assert.True(t, create.GetPayerAccount().IsSigner) | ||
| assert.True(t, create.GetPayerAccount().IsWritable) | ||
| assert.Equal(t, payer, create.GetPayerAccount().PublicKey) | ||
| assert.Equal(t, wallet, create.GetWalletAccount().PublicKey) | ||
| assert.Equal(t, mint, create.GetMintAccount().PublicKey) | ||
|
|
||
| // Verify associated token address is correctly derived and placed at index 1 | ||
| ata, _, err := solana.FindAssociatedTokenAddress(wallet, mint) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, ata, create.GetAssociatedTokenAddressAccount().PublicKey) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -484,16 +484,24 @@ func (tx *Transaction) MarshalBinary() ([]byte, error) { | |
| return nil, fmt.Errorf("failed to encode tx.Message to binary: %w", err) | ||
| } | ||
|
|
||
| var signatureCount []byte | ||
| bin.EncodeCompactU16Length(&signatureCount, len(tx.Signatures)) | ||
| output := make([]byte, 0, len(signatureCount)+len(signatureCount)*64+len(messageContent)) | ||
| output = append(output, signatureCount...) | ||
| for _, sig := range tx.Signatures { | ||
| output = append(output, sig[:]...) | ||
| signatures := tx.Signatures | ||
| for i := len(signatures); i < int(tx.Message.Header.NumRequiredSignatures); i++ { | ||
|
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. nit: If |
||
| // append dummy signatures to the transaction, without it serialized transaction will be invalid | ||
| // reference: https://github.com/solana-labs/solana-web3.js/blob/4e9988cfc561f3ed11f4c5016a29090a61d129a8/src/transaction/versioned.ts#L36 | ||
| signatures = append(signatures, SignatureFromBytes(make([]byte, SignatureLength))) | ||
| } | ||
| output = append(output, messageContent...) | ||
|
|
||
| return output, nil | ||
| var signaturesCountBytes []byte | ||
| bin.EncodeCompactU16Length(&signaturesCountBytes, len(signatures)) | ||
|
|
||
| binaryTx := make([]byte, 0, len(signaturesCountBytes)+len(signatures)*64+len(messageContent)) | ||
| binaryTx = append(binaryTx, signaturesCountBytes...) | ||
| for _, sig := range signatures { | ||
| binaryTx = append(binaryTx, sig[:]...) | ||
| } | ||
|
|
||
| binaryTx = append(binaryTx, messageContent...) | ||
| return binaryTx, nil | ||
| } | ||
|
|
||
| func (tx Transaction) MarshalWithEncoder(encoder *bin.Encoder) error { | ||
|
|
||
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.
nice clean up