Skip to content

Commit 6e41f63

Browse files
committed
Enhance SimulateMultiTransaction method to support additional signers and update related documentation
1 parent 8bbeef6 commit 6e41f63

File tree

3 files changed

+57
-17
lines changed

3 files changed

+57
-17
lines changed

client.go

+43-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"time"
77

88
"github.com/aptos-labs/aptos-go-sdk/api"
9+
"github.com/aptos-labs/aptos-go-sdk/crypto"
910
"github.com/hasura/go-graphql-client"
1011
)
1112

@@ -749,8 +750,48 @@ func (client *Client) SimulateTransaction(rawTxn *RawTransaction, sender Transac
749750
return client.nodeClient.SimulateTransaction(rawTxn, sender, options...)
750751
}
751752

752-
func (client *Client) SimulateMultiTransaction(rawTxnWithData *RawTransactionWithData, sender TransactionSigner, options ...any) (data []*api.UserTransaction, err error) {
753-
return client.nodeClient.SimulateMultiTransaction(rawTxnWithData, sender, options...)
753+
// SimulateMultiTransaction simulates a multi-transaction on the Aptos blockchain.
754+
// It takes a raw transaction with data, a transaction signer, and optional parameters.
755+
// It returns a slice of UserTransaction and an error if the simulation fails.
756+
//
757+
// Parameters:
758+
// - rawTxnWithData: A pointer to RawTransactionWithData containing the transaction details.
759+
// - sender: A TransactionSigner that signs the transaction.
760+
// - options: Optional parameters for the simulation.
761+
//
762+
// Returns:
763+
// - data: A slice of UserTransaction containing the simulated transaction results.
764+
// - err: An error if the simulation fails.
765+
// SimulateMultiTransaction simulates a multi-signature transaction without broadcasting it to the network.
766+
// This function takes a raw transaction with data, a sender transaction signer, and additional signers.
767+
// It returns the simulated user transactions or an error if the simulation fails.
768+
//
769+
// Parameters:
770+
// - rawTxnWithData: A pointer to the raw transaction data to be simulated.
771+
// - sender: The primary transaction signer.
772+
// - additionalSigners: A slice of additional signers' account authenticators.
773+
// - options: Additional options for the simulation.
774+
//
775+
// Returns:
776+
// - data: A slice of pointers to the simulated user transactions.
777+
// - err: An error if the simulation fails.
778+
func (client *Client) SimulateMultiTransaction(rawTxnWithData *RawTransactionWithData, sender TransactionSigner, additionalSigners []crypto.AccountAuthenticator, options ...any) (data []*api.UserTransaction, err error) {
779+
return client.nodeClient.SimulateMultiTransaction(rawTxnWithData, sender, additionalSigners , options...)
780+
}
781+
782+
// SimulateTransactionWithSignedTxn simulates a transaction using a signed transaction.
783+
// This function sends the signed transaction to the node client for simulation and returns
784+
// the resulting user transactions and any error encountered during the simulation.
785+
//
786+
// Parameters:
787+
// - signedTxn: A pointer to the SignedTransaction struct representing the signed transaction to be simulated.
788+
// - options: Additional optional parameters that can be passed to the simulation.
789+
//
790+
// Returns:
791+
// - data: A slice of pointers to api.UserTransaction structs representing the simulated user transactions.
792+
// - err: An error object if an error occurred during the simulation, otherwise nil.
793+
func (client *Client) SimulateTransactionWithSignedTxn(signedTxn *SignedTransaction, options ...any) (data []*api.UserTransaction, err error) {
794+
return client.nodeClient.SimulateTransactionWithSignedTxn(signedTxn, options...)
754795
}
755796

756797
// GetChainId Retrieves the ChainId of the network

examples/simulate_mult_transaction/main.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ func example(networkConfig aptos.NetworkConfig) {
8282
panic("Failed to serialize transfer amount:" + err.Error())
8383
}
8484

85+
8586
rawTxn, err := client.BuildTransactionMultiAgent(alice.AccountAddress(), aptos.TransactionPayload{
8687
Payload: &aptos.EntryFunction{
8788
Module: aptos.ModuleId{
@@ -102,9 +103,7 @@ func example(networkConfig aptos.NetworkConfig) {
102103
// 2. Simulate transaction (optional)
103104
// This is useful for understanding how much the transaction will cost
104105
// and to ensure that the transaction is valid before sending it to the network
105-
// This is optional, but recommended
106-
// TODO: Support simulate transaction with multi-agent / fee payer
107-
simulationResult, err := client.SimulateMultiTransaction(rawTxn, alice)
106+
simulationResult, err := client.SimulateMultiTransaction(rawTxn, alice, []crypto.AccountAuthenticator{})
108107
if err != nil {
109108
panic("Failed to simulate transaction:" + err.Error())
110109
}
@@ -124,20 +123,20 @@ func example(networkConfig aptos.NetworkConfig) {
124123
panic("Failed to sign multiagent transaction with bob:" + err.Error())
125124
}
126125

127-
// 3.a. merge the signatures together into a single transaction
128-
signedTxn, ok := rawTxn.ToMultiAgentSignedTransaction(aliceAuth, []crypto.AccountAuthenticator{*bobAuth})
126+
// 4.a. merge the signatures together into a single transaction
127+
signedTxn, ok := rawTxn.ToFeePayerSignedTransaction(aliceAuth,bobAuth ,[]crypto.AccountAuthenticator{})
129128
if !ok {
130129
panic("Failed to build a signed multiagent transaction")
131130
}
132131

133-
// 4. Submit transaction
132+
// 5. Submit transaction
134133
submitResult, err := client.SubmitTransaction(signedTxn)
135134
if err != nil {
136135
panic("Failed to submit transaction:" + err.Error())
137136
}
138137
txnHash := submitResult.Hash
139138

140-
// 5. Wait for the transaction to complete
139+
// 6. Wait for the transaction to complete
141140
_, err = client.WaitForTransaction(txnHash)
142141
if err != nil {
143142
panic("Failed to wait for transaction:" + err.Error())

nodeClient.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ func (rc *NodeClient) SimulateTransaction(rawTxn *RawTransaction, sender Transac
733733
// SimulateMultiTransaction simulates a transaction with optional fee payer and secondary signers
734734
// If feePayerAddress is nil, no fee payer will be used
735735
// If secondarySignerAddresses is nil or empty, no secondary signers will be used
736-
func (rc *NodeClient) SimulateMultiTransaction(rawTxnWithData *RawTransactionWithData, sender TransactionSigner, options ...any) (data []*api.UserTransaction, err error) {
736+
func (rc *NodeClient) SimulateMultiTransaction(rawTxnWithData *RawTransactionWithData, sender TransactionSigner, additionalSigners []crypto.AccountAuthenticator, options ...any) (data []*api.UserTransaction, err error) {
737737
if( rawTxnWithData == nil ) {
738738
return nil, fmt.Errorf("rawTxnWithData is nil")
739739
}
@@ -745,28 +745,28 @@ func (rc *NodeClient) SimulateMultiTransaction(rawTxnWithData *RawTransactionWit
745745
Variant: crypto.AccountAuthenticatorNoAccount,
746746
Auth: &crypto.AccountAuthenticatorNoAccountAuthenticator{},
747747
},
748-
[]crypto.AccountAuthenticator{},
748+
additionalSigners,
749749
);
750750
if !ok {
751-
return nil, fmt.Errorf("failed to sign agent transaction")
751+
return nil, fmt.Errorf("failed to sign fee payer transaction")
752752
}
753-
return rc.simulateTransactionWithSignedTxn(signedFeePayerTxn, options...)
753+
return rc.SimulateTransactionWithSignedTxn(signedFeePayerTxn, options...)
754754
case MultiAgentRawTransactionWithDataVariant:
755755
signedAgentTxn, ok := rawTxnWithData.ToMultiAgentSignedTransaction(
756756
sender.SimulationAuthenticator(),
757-
[]crypto.AccountAuthenticator{},
757+
additionalSigners,
758758
)
759759
if !ok {
760-
return nil, fmt.Errorf("failed to sign agent transaction")
760+
return nil, fmt.Errorf("failed to sign multi agent transaction")
761761
}
762-
return rc.simulateTransactionWithSignedTxn(signedAgentTxn, options...)
762+
return rc.SimulateTransactionWithSignedTxn(signedAgentTxn, options...)
763763
default:
764764
return nil, fmt.Errorf("unsupported raw transaction with data variant %v", rawTxnWithData.Variant)
765765
}
766766
}
767767

768768
// Helper method to avoid code duplication
769-
func (rc *NodeClient) simulateTransactionWithSignedTxn(signedTxn *SignedTransaction, options ...any) ([]*api.UserTransaction, error) {
769+
func (rc *NodeClient) SimulateTransactionWithSignedTxn(signedTxn *SignedTransaction, options ...any) ([]*api.UserTransaction, error) {
770770
sblob, err := bcs.Serialize(signedTxn)
771771
if err != nil {
772772
return nil, err

0 commit comments

Comments
 (0)