1
- import { AptosClient , AptosAccount , Types , TxnBuilderTypes , HexString , BCS } from "aptos" ;
1
+ import { AptosClient , AptosAccount , Types , TxnBuilderTypes , HexString , BCS , TransactionBuilderEd25519 } from "aptos" ;
2
2
import { TypeTagParser } from "aptos/dist/transaction_builder/builder_utils" ;
3
- import { UserTransaction , WriteSetChange_WriteResource } from "aptos/dist/generated" ;
3
+ import { $TransactionPayload_EntryFunctionPayload , TransactionPayload_EntryFunctionPayload , TransactionSignature , UserTransaction , WriteSetChange_WriteResource } from "aptos/dist/generated" ;
4
4
import { AccountAddress , Identifier , ModuleId , EntryFunction } from "aptos/dist/transaction_builder/aptos_types" ;
5
5
import { AptosParserRepo } from "./parserRepo" ;
6
- import { AtomicTypeTag , StructTag , TypeTag } from "./typeTag" ;
6
+ import { StructTag } from "./typeTag" ;
7
7
import { U128 , U64 , U8 } from "./builtinTypes" ;
8
- import { ActualStringClass , serializeMoveValue , serializeMoveValueWithoutTag , serializeVector } from "." ;
8
+ import { ActualStringClass , payloadArg , serializeMoveValueWithoutTag } from "." ;
9
9
10
10
type AcceptedScriptFuncArgType = any [ ] | U8 | U64 | U128 | HexString | boolean | ActualStringClass ;
11
11
@@ -15,59 +15,133 @@ export function buildPayload(
15
15
funcName : string ,
16
16
typeArguments : string [ ] ,
17
17
args : AcceptedScriptFuncArgType [ ] ,
18
- ) : TxnBuilderTypes . TransactionPayloadEntryFunction {
18
+ isJSON = false ,
19
+ ) : TxnBuilderTypes . TransactionPayloadEntryFunction | TransactionPayload_EntryFunctionPayload {
19
20
20
- const bytes = args . map ( arg => {
21
- const serializer = new BCS . Serializer ( ) ;
22
- serializeMoveValueWithoutTag ( serializer , arg ) ;
23
- return serializer . getBytes ( ) ;
24
- } ) ;
21
+ if ( isJSON ) {
22
+ // JSON
23
+ return {
24
+ type : "entry_function_payload" ,
25
+ function : `${ moduleAddress . toShortString ( ) } ::${ moduleName } ::${ funcName } ` ,
26
+ type_arguments : typeArguments ,
27
+ arguments : args . map ( v => payloadArg ( v ) ) ,
28
+ } ;
29
+ }
30
+ else {
31
+ // BCS
32
+ const bytes = args . map ( arg => {
33
+ const serializer = new BCS . Serializer ( ) ;
34
+ serializeMoveValueWithoutTag ( serializer , arg ) ;
35
+ return serializer . getBytes ( ) ;
36
+ } ) ;
25
37
26
-
27
- const scriptFunction = new EntryFunction (
28
- new ModuleId ( new AccountAddress ( moduleAddress . toUint8Array ( ) ) , new Identifier ( moduleName ) ) ,
29
- new Identifier ( funcName ) ,
30
- typeArguments . map ( str => new TypeTagParser ( str ) . parseTypeTag ( ) ) ,
31
- bytes ,
32
- ) ;
33
- return new TxnBuilderTypes . TransactionPayloadEntryFunction ( scriptFunction ) ;
38
+
39
+ const scriptFunction = new EntryFunction (
40
+ new ModuleId ( new AccountAddress ( moduleAddress . toUint8Array ( ) ) , new Identifier ( moduleName ) ) ,
41
+ new Identifier ( funcName ) ,
42
+ typeArguments . map ( str => new TypeTagParser ( str ) . parseTypeTag ( ) ) ,
43
+ bytes ,
44
+ ) ;
45
+ return new TxnBuilderTypes . TransactionPayloadEntryFunction ( scriptFunction ) ;
46
+ }
34
47
}
35
48
36
49
export async function sendPayloadTx (
37
50
client : AptosClient ,
38
51
account : AptosAccount ,
39
- payload : TxnBuilderTypes . TransactionPayload ,
52
+ payload : TxnBuilderTypes . TransactionPayload | TransactionPayload_EntryFunctionPayload ,
40
53
max_gas = 1000
41
54
) {
42
- console . log ( "Building tx..." ) ;
43
- // RawTransaction
44
- const rawTxn = await client . generateRawTransaction ( account . address ( ) , payload , { maxGasAmount : BigInt ( max_gas ) } ) ;
45
- // Signed BCS representation
46
- const bcsTxn = AptosClient . generateBCSTransaction ( account , rawTxn ) ;
47
- console . log ( "Submitting..." ) ;
48
- const txnResult = await client . submitSignedBCSTransaction ( bcsTxn ) ;
49
- console . log ( "Submitted" ) ;
50
- await client . waitForTransaction ( txnResult . hash ) ;
51
- console . log ( "Confirmed" ) ;
52
- const txDetails = ( await client . getTransactionByHash ( txnResult . hash ) ) as Types . UserTransaction ;
53
- console . log ( txDetails ) ;
54
- return txDetails ;
55
+ // send BCS transaction
56
+ if ( payload instanceof TxnBuilderTypes . TransactionPayloadEntryFunction ) {
57
+ console . log ( "Building tx..." ) ;
58
+ // RawTransaction
59
+ const rawTxn = await client . generateRawTransaction ( account . address ( ) , payload , { maxGasAmount : BigInt ( max_gas ) } ) ;
60
+ // Signed BCS representation
61
+ const bcsTxn = AptosClient . generateBCSTransaction ( account , rawTxn ) ;
62
+ console . log ( "Submitting..." ) ;
63
+ const txnResult = await client . submitSignedBCSTransaction ( bcsTxn ) ;
64
+ console . log ( "Submitted" ) ;
65
+ await client . waitForTransaction ( txnResult . hash ) ;
66
+ console . log ( "Confirmed" ) ;
67
+ const txDetails = ( await client . getTransactionByHash ( txnResult . hash ) ) as Types . UserTransaction ;
68
+ console . log ( txDetails ) ;
69
+ return txDetails ;
70
+ }
71
+ // send JSON transaction
72
+ else {
73
+ console . log ( "Building tx..." ) ;
74
+ const pld = payload as TransactionPayload_EntryFunctionPayload ;
75
+ // RawTransaction
76
+ const txn = await client . generateTransaction ( account . address ( ) , pld , { max_gas_amount : max_gas . toString ( ) } ) ;
77
+ // Signed json representation
78
+ console . log ( "Signing tx..." ) ;
79
+ const signedTxn = await client . signTransaction ( account , txn ) ;
80
+ console . log ( "Submitting..." ) ;
81
+ const txnResult = await client . submitTransaction ( signedTxn ) ;
82
+ console . log ( "Submitted" ) ;
83
+ await client . waitForTransaction ( txnResult . hash ) ;
84
+ console . log ( "Confirmed" ) ;
85
+ const txDetails = ( await client . getTransactionByHash ( txnResult . hash ) ) as Types . UserTransaction ;
86
+ console . log ( txDetails ) ;
87
+ return txDetails ;
88
+ }
89
+ }
90
+
91
+ export type SimulationKeys = {
92
+ pubkey : HexString ;
93
+ address : HexString ;
94
+ }
95
+
96
+ export function getSimulationKeys ( account : AptosAccount ) : SimulationKeys {
97
+ return {
98
+ pubkey : account . pubKey ( ) ,
99
+ address : account . address ( ) ,
100
+ }
55
101
}
56
102
57
103
export async function simulatePayloadTx (
58
104
client : AptosClient ,
59
- account : AptosAccount ,
60
- payload : TxnBuilderTypes . TransactionPayload ,
105
+ keys : SimulationKeys ,
106
+ payload : TxnBuilderTypes . TransactionPayload | TransactionPayload_EntryFunctionPayload ,
61
107
max_gas = 1000
62
108
) {
63
- const rawTxn = await client . generateRawTransaction ( account . address ( ) , payload , { maxGasAmount : BigInt ( max_gas ) } ) ;
64
- const bcsTxn = AptosClient . generateBCSSimulation ( account , rawTxn ) ;
65
- const outputs = await client . submitBCSSimulation ( bcsTxn ) ;
66
- return outputs [ 0 ] ;
109
+ if ( payload instanceof TxnBuilderTypes . TransactionPayload ) {
110
+ const rawTxn = await client . generateRawTransaction ( keys . address , payload , { maxGasAmount : BigInt ( max_gas ) } ) ;
111
+ const bcsTxn = generateBCSSimulation ( keys . pubkey , rawTxn ) ;
112
+ const outputs = await client . submitBCSSimulation ( bcsTxn ) ;
113
+ return outputs [ 0 ] ;
114
+ }
115
+ else {
116
+ const pld = payload as TransactionPayload_EntryFunctionPayload ;
117
+ const txn = await client . generateTransaction ( keys . address , pld , { max_gas_amount : max_gas . toString ( ) } ) ;
118
+ const transactionSignature : TransactionSignature = {
119
+ type : "ed25519_signature" ,
120
+ public_key : keys . pubkey . hex ( ) ,
121
+ // use invalid signature for simulation
122
+ signature : HexString . fromUint8Array ( new Uint8Array ( 64 ) ) . hex ( ) ,
123
+ } ;
124
+
125
+ const request = { ...txn , signature : transactionSignature } ;
126
+ const outputs = await client . client . transactions . simulateTransaction ( request ) ;
127
+ return outputs [ 0 ] ;
128
+ }
129
+ }
130
+
131
+ export function generateBCSSimulation ( pubkey : HexString , rawTxn : TxnBuilderTypes . RawTransaction ) : Uint8Array {
132
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
133
+ const txnBuilder = new TransactionBuilderEd25519 ( ( _signingMessage : TxnBuilderTypes . SigningMessage ) => {
134
+ // @ts -ignore
135
+ const invalidSigBytes = new Uint8Array ( 64 ) ;
136
+ return new TxnBuilderTypes . Ed25519Signature ( invalidSigBytes ) ;
137
+ } , pubkey . toUint8Array ( ) ) ;
138
+
139
+ return txnBuilder . sign ( rawTxn ) ;
67
140
}
68
141
69
142
export function takeSimulationValue < T > ( tx : UserTransaction , tag : StructTag , repo : AptosParserRepo ) : T {
70
143
if ( ! tx . success ) {
144
+ console . log ( tx ) ;
71
145
throw new Error ( "Simulation failed" ) ;
72
146
}
73
147
const valueData = tx . changes . filter ( change => {
0 commit comments