1
1
import { PublicKey } from '@near-js/crypto' ;
2
2
import {
3
- Action ,
4
- actionCreators ,
5
- buildDelegateAction ,
6
- DelegateAction ,
7
- Signature ,
8
- Transaction ,
3
+ Action ,
4
+ actionCreators ,
5
+ buildDelegateAction ,
6
+ DelegateAction ,
7
+ Signature ,
8
+ Transaction ,
9
9
} from '@near-js/transactions' ;
10
10
import { baseDecode , DEFAULT_FUNCTION_CALL_GAS } from '@near-js/utils' ;
11
11
import { DEFAULT_META_TRANSACTION_BLOCK_HEIGHT_TTL } from '../constants' ;
@@ -20,102 +20,102 @@ interface TransactionOptions {
20
20
}
21
21
22
22
export class TransactionComposer {
23
- private actions : Action [ ] = [ ] ;
24
- receiver : string | undefined ;
25
- sender : string | undefined ;
26
- blockHeader : BlockHeader | undefined ;
27
- nonce : bigint | undefined ;
28
- publicKey : PublicKey | undefined ;
23
+ private actions : Action [ ] = [ ] ;
24
+ receiver : string | undefined ;
25
+ sender : string | undefined ;
26
+ blockHeader : BlockHeader | undefined ;
27
+ nonce : bigint | undefined ;
28
+ publicKey : PublicKey | undefined ;
29
29
30
- static init ( transaction : TransactionOptions ) {
31
- const composer = new TransactionComposer ( ) ;
32
- composer . receiver = transaction . receiver ;
33
- composer . sender = transaction . sender ;
34
- composer . blockHeader = transaction . blockHeader ;
35
- composer . nonce = transaction . nonce ;
36
- composer . publicKey = transaction . publicKey ;
30
+ static init ( transaction : TransactionOptions ) {
31
+ const composer = new TransactionComposer ( ) ;
32
+ composer . receiver = transaction . receiver ;
33
+ composer . sender = transaction . sender ;
34
+ composer . blockHeader = transaction . blockHeader ;
35
+ composer . nonce = transaction . nonce ;
36
+ composer . publicKey = transaction . publicKey ;
37
37
38
- return composer ;
39
- }
38
+ return composer ;
39
+ }
40
40
41
- private buildTransactionObject ( transaction ?: TransactionOptions ) {
42
- const hash = ( transaction ?. blockHeader ?. hash || this . blockHeader ?. hash ) ! ;
43
- return {
44
- actions : this . actions ,
45
- blockHash : baseDecode ( hash ) ,
46
- nonce : ( transaction ?. nonce || this . nonce ) ! ,
47
- publicKey : ( transaction ?. publicKey || this . publicKey ) ! ,
48
- receiverId : ( transaction ?. receiver || this . receiver ) ! ,
49
- signerId : ( transaction ?. sender || this . sender ) ! ,
50
- } ;
51
- }
41
+ private buildTransactionObject ( transaction ?: TransactionOptions ) {
42
+ const hash = ( transaction ?. blockHeader ?. hash || this . blockHeader ?. hash ) ! ;
43
+ return {
44
+ actions : this . actions ,
45
+ blockHash : baseDecode ( hash ) ,
46
+ nonce : ( transaction ?. nonce || this . nonce ) ! ,
47
+ publicKey : ( transaction ?. publicKey || this . publicKey ) ! ,
48
+ receiverId : ( transaction ?. receiver || this . receiver ) ! ,
49
+ signerId : ( transaction ?. sender || this . sender ) ! ,
50
+ } ;
51
+ }
52
52
53
- toTransaction ( transaction ?: TransactionOptions ) : Transaction {
54
- return new Transaction ( this . buildTransactionObject ( transaction ) ) ;
55
- }
53
+ toTransaction ( transaction ?: TransactionOptions ) : Transaction {
54
+ return new Transaction ( this . buildTransactionObject ( transaction ) ) ;
55
+ }
56
56
57
- toDelegateAction ( transaction ?: TransactionOptions ) : DelegateAction {
58
- const { actions, nonce, publicKey, receiverId, signerId } = this . buildTransactionObject ( transaction ) ;
59
- const blockHeader = transaction ?. blockHeader || this . blockHeader ;
57
+ toDelegateAction ( transaction ?: TransactionOptions ) : DelegateAction {
58
+ const { actions, nonce, publicKey, receiverId, signerId } = this . buildTransactionObject ( transaction ) ;
59
+ const blockHeader = transaction ?. blockHeader || this . blockHeader ;
60
60
61
- return buildDelegateAction ( {
62
- actions,
63
- maxBlockHeight : BigInt ( blockHeader ! . height ) + DEFAULT_META_TRANSACTION_BLOCK_HEIGHT_TTL ,
64
- nonce : BigInt ( nonce ) + BigInt ( 1 ) ,
65
- publicKey,
66
- receiverId,
67
- senderId : signerId ,
68
- } ) ;
69
- }
61
+ return buildDelegateAction ( {
62
+ actions,
63
+ maxBlockHeight : BigInt ( blockHeader ! . height ) + DEFAULT_META_TRANSACTION_BLOCK_HEIGHT_TTL ,
64
+ nonce : BigInt ( nonce ) + BigInt ( 1 ) ,
65
+ publicKey,
66
+ receiverId,
67
+ senderId : signerId ,
68
+ } ) ;
69
+ }
70
70
71
- addFullAccessKey ( publicKey : PublicKey ) {
72
- this . actions . push ( actionCreators . addKey ( publicKey , actionCreators . fullAccessKey ( ) ) ) ;
73
- return this ;
74
- }
71
+ addFullAccessKey ( publicKey : PublicKey ) {
72
+ this . actions . push ( actionCreators . addKey ( publicKey , actionCreators . fullAccessKey ( ) ) ) ;
73
+ return this ;
74
+ }
75
75
76
- addFunctionCallAccessKey ( publicKey : PublicKey , contractId : string , methodNames : string [ ] , allowance ?: bigint ) {
77
- const accessKey = actionCreators . functionCallAccessKey ( contractId , methodNames , allowance )
78
- this . actions . push ( actionCreators . addKey ( publicKey , accessKey ) ) ;
79
- return this ;
80
- }
76
+ addFunctionCallAccessKey ( publicKey : PublicKey , contractId : string , methodNames : string [ ] , allowance ?: bigint ) {
77
+ const accessKey = actionCreators . functionCallAccessKey ( contractId , methodNames , allowance ) ;
78
+ this . actions . push ( actionCreators . addKey ( publicKey , accessKey ) ) ;
79
+ return this ;
80
+ }
81
81
82
- createAccount ( ) {
83
- this . actions . push ( actionCreators . createAccount ( ) ) ;
84
- return this ;
85
- }
82
+ createAccount ( ) {
83
+ this . actions . push ( actionCreators . createAccount ( ) ) ;
84
+ return this ;
85
+ }
86
86
87
- deleteAccount ( beneficiaryId : string ) {
88
- this . actions . push ( actionCreators . deleteAccount ( beneficiaryId ) ) ;
89
- return this ;
90
- }
87
+ deleteAccount ( beneficiaryId : string ) {
88
+ this . actions . push ( actionCreators . deleteAccount ( beneficiaryId ) ) ;
89
+ return this ;
90
+ }
91
91
92
- deleteKey ( publicKey : PublicKey ) {
93
- this . actions . push ( actionCreators . deleteKey ( publicKey ) ) ;
94
- return this ;
95
- }
92
+ deleteKey ( publicKey : PublicKey ) {
93
+ this . actions . push ( actionCreators . deleteKey ( publicKey ) ) ;
94
+ return this ;
95
+ }
96
96
97
- deployContract ( code : Uint8Array ) {
98
- this . actions . push ( actionCreators . deployContract ( code ) ) ;
99
- return this ;
100
- }
97
+ deployContract ( code : Uint8Array ) {
98
+ this . actions . push ( actionCreators . deployContract ( code ) ) ;
99
+ return this ;
100
+ }
101
101
102
- functionCall ( method : string , args : object , gas : bigint = DEFAULT_FUNCTION_CALL_GAS * BigInt ( 10 ) , deposit = BigInt ( 0 ) ) {
103
- this . actions . push ( actionCreators . functionCall ( method , args , gas , deposit ) ) ;
104
- return this ;
105
- }
102
+ functionCall ( method : string , args : object , gas : bigint = DEFAULT_FUNCTION_CALL_GAS * BigInt ( 10 ) , deposit = BigInt ( 0 ) ) {
103
+ this . actions . push ( actionCreators . functionCall ( method , args , gas , deposit ) ) ;
104
+ return this ;
105
+ }
106
106
107
- signedDelegate ( delegateAction : DelegateAction , signature : Signature ) {
108
- this . actions . push ( actionCreators . signedDelegate ( { delegateAction, signature } ) ) ;
109
- return this ;
110
- }
107
+ signedDelegate ( delegateAction : DelegateAction , signature : Signature ) {
108
+ this . actions . push ( actionCreators . signedDelegate ( { delegateAction, signature } ) ) ;
109
+ return this ;
110
+ }
111
111
112
- stake ( stake : bigint , publicKey : PublicKey ) {
113
- this . actions . push ( actionCreators . stake ( stake , publicKey ) ) ;
114
- return this ;
115
- }
112
+ stake ( stake : bigint , publicKey : PublicKey ) {
113
+ this . actions . push ( actionCreators . stake ( stake , publicKey ) ) ;
114
+ return this ;
115
+ }
116
116
117
- transfer ( deposit : bigint ) {
118
- this . actions . push ( actionCreators . transfer ( deposit ) ) ;
119
- return this ;
120
- }
117
+ transfer ( deposit : bigint ) {
118
+ this . actions . push ( actionCreators . transfer ( deposit ) ) ;
119
+ return this ;
120
+ }
121
121
}
0 commit comments