Skip to content

perf: add NewTransaction optional isSkipSign#181

Open
aitimate wants to merge 1 commit into
blocto:mainfrom
aitimate:main
Open

perf: add NewTransaction optional isSkipSign#181
aitimate wants to merge 1 commit into
blocto:mainfrom
aitimate:main

Conversation

@aitimate
Copy link
Copy Markdown

ref: https://solana.com/docs/rpc/http/simulatetransaction

According to the official documentation, sending a simulated transaction does not require signing. This means that users can not only simulate transactions for their own accounts but also predict and track the transaction status of other users. Currently, this functionality is not supported by the library, so I made some optimizations.

// NewTransaction create a new tx by message and signer. it will reserve signatures slot.
func NewTransaction(param NewTransactionParam, isSkipSign ...bool) (Transaction, error) {
	signatures := make([]Signature, 0, param.Message.Header.NumRequireSignatures)
	for i := uint8(0); i < param.Message.Header.NumRequireSignatures; i++ {
		signatures = append(signatures, make([]byte, 64))
	}

	if len(isSkipSign) == 0 || !isSkipSign[0] {
		m := map[common.PublicKey]uint8{}
		for i := uint8(0); i < param.Message.Header.NumRequireSignatures; i++ {
			m[param.Message.Accounts[i]] = i
		}

		data, err := param.Message.Serialize()
		if err != nil {
			return Transaction{}, fmt.Errorf("failed to serialize message, err: %v", err)
		}
		for _, signer := range param.Signers {
			idx, ok := m[signer.PublicKey]
			if !ok {
				return Transaction{}, fmt.Errorf("%w, %v is not a signer", ErrTransactionAddNotNecessarySignatures, signer.PublicKey)
			}
			signatures[idx] = signer.Sign(data)
		}
	}

	return Transaction{
		Signatures: signatures,
		Message:    param.Message,
	}, nil
}

Below is an example of the optimized usage, which you can test:

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/blocto/solana-go-sdk/client"
	"github.com/blocto/solana-go-sdk/common"
	"github.com/blocto/solana-go-sdk/program/system"
	"github.com/blocto/solana-go-sdk/rpc"
	"github.com/blocto/solana-go-sdk/types"
	"log"
	"testing"
)

func TestComputeUnitLimit(t *testing.T) {
	account := types.Account{PublicKey: common.PublicKeyFromString("9WzDXwBbmkg8ZTbNMqUxvQRAyrZzDsGYdLVL9zYtAWWM")}
	receiver := common.PublicKeyFromString("7mhcgF1DVsj5iv4CxZDgp51H6MBBwqamsH1KnqXhSRc5")

	instruction := system.Transfer(
		system.TransferParam{
			From:   account.PublicKey,
			To:     receiver,
			Amount: uint64(1e9),
		},
	)

	c := client.NewClient(rpc.MainnetRPCEndpoint)
	tx, err := NewTransaction(types.NewTransactionParam{
		Message: types.NewMessage(types.NewMessageParam{
			FeePayer: account.PublicKey,
			Instructions: []types.Instruction{
				//cmptbdgprog.SetComputeUnitLimit(cmptbdgprog.SetComputeUnitLimitParam{
				//	Units: 5000,
				//}),
				instruction,
			},
			RecentBlockhash: "11111111111111111111111111111111",
		}),
		Signers: []types.Account{{PublicKey: account.PublicKey}},
	}, true)
	if err != nil {
		log.Fatalf("Failed to create transaction: %v", err)
	}
	simulateResult, err := c.SimulateTransactionWithConfig(context.Background(), tx, client.SimulateTransactionConfig{
		ReplaceRecentBlockhash: true,
	})
	if err != nil {
		log.Fatalf("Simulation failed: %v", err)
	}
	marshal, _ := json.Marshal(simulateResult)
	log.Println(string(marshal))
}

func NewTransaction(param types.NewTransactionParam, isSkipSign ...bool) (types.Transaction, error) {
	signatures := make([]types.Signature, 0, param.Message.Header.NumRequireSignatures)
	for i := uint8(0); i < param.Message.Header.NumRequireSignatures; i++ {
		signatures = append(signatures, make([]byte, 64))
	}
	if len(isSkipSign) == 0 || !isSkipSign[0] {
		m := map[common.PublicKey]uint8{}
		for i := uint8(0); i < param.Message.Header.NumRequireSignatures; i++ {
			m[param.Message.Accounts[i]] = i
		}

		data, err := param.Message.Serialize()
		if err != nil {
			return types.Transaction{}, fmt.Errorf("failed to serialize message, err: %v", err)
		}
		for _, signer := range param.Signers {
			idx, ok := m[signer.PublicKey]
			if !ok {
				return types.Transaction{}, fmt.Errorf("%w, %v is not a signer", types.ErrTransactionAddNotNecessarySignatures, signer.PublicKey)
			}
			signatures[idx] = signer.Sign(data)
		}
	}
	return types.Transaction{
		Signatures: signatures,
		Message:    param.Message,
	}, nil
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant