1
- import {
2
- BaseKey ,
3
- BaseTransaction ,
4
- Entry ,
5
- Recipient ,
6
- TransactionRecipient ,
7
- TransactionType ,
8
- TransactionExplanation ,
9
- } from '@bitgo/sdk-core' ;
10
- import { TxData } from './iface' ;
1
+ import { BaseKey , BaseTransaction , Entry , Recipient , TransactionRecipient , TransactionType } from '@bitgo/sdk-core' ;
2
+ import { TxData , TransactionExplanation } from './iface' ;
11
3
import { BaseCoin as CoinConfig } from '@bitgo/statics' ;
12
4
import TonWeb from 'tonweb' ;
13
5
import { BN } from 'bn.js' ;
@@ -20,7 +12,8 @@ export class Transaction extends BaseTransaction {
20
12
public bounceable : boolean ;
21
13
public fromAddressBounceable : boolean ;
22
14
public toAddressBounceable : boolean ;
23
- public message : string ;
15
+ public message : string | Cell ;
16
+ public withdrawAmount : string ;
24
17
seqno : number ;
25
18
expireTime : number ;
26
19
sender : string ;
@@ -55,6 +48,7 @@ export class Transaction extends BaseTransaction {
55
48
destination : this . recipient . address ,
56
49
destinationAlias : otherFormat ,
57
50
amount : this . recipient . amount ,
51
+ withdrawAmount : this . withdrawAmount ,
58
52
seqno : this . seqno ,
59
53
expirationTime : this . expireTime ,
60
54
publicKey : this . publicKey ,
@@ -67,8 +61,15 @@ export class Transaction extends BaseTransaction {
67
61
return Buffer . from ( this . unsignedMessage , 'hex' ) ;
68
62
}
69
63
64
+ /**
65
+ * Set the transaction type.
66
+ * @param {TransactionType } transactionType The transaction type to be set.
67
+ */
68
+ set transactionType ( transactionType : TransactionType ) {
69
+ this . _type = transactionType ;
70
+ }
71
+
70
72
async build ( ) : Promise < void > {
71
- this . _type = TransactionType . Send ;
72
73
const signingMessage = this . createSigningMessage ( WALLET_ID , this . seqno , this . expireTime ) ;
73
74
const sendMode = 3 ;
74
75
signingMessage . bits . writeUint8 ( sendMode ) ;
@@ -118,7 +119,7 @@ export class Transaction extends BaseTransaction {
118
119
true ,
119
120
this . bounceable
120
121
) ;
121
- return TonWeb . Contract . createCommonMsgInfo ( orderHeader , undefined , payloadCell ) ;
122
+ return TonWeb . Contract . createCommonMsgInfo ( orderHeader , undefined , payloadCell ) ; // compare with commonmsg tonweb and ton
122
123
}
123
124
124
125
async createExternalMessage ( signingMessage : Cell , seqno : number , signature : string ) : Promise < Cell > {
@@ -164,12 +165,13 @@ export class Transaction extends BaseTransaction {
164
165
try {
165
166
const cell = TonWeb . boc . Cell . oneFromBoc ( TonWeb . utils . base64ToBytes ( rawTransaction ) ) ;
166
167
167
- const parsed = this . parseTransfer ( cell ) ;
168
+ const parsed = this . parseTransaction ( cell ) ;
168
169
parsed . value = parsed . value . toString ( ) ;
169
170
parsed . fromAddress = parsed . fromAddress . toString ( true , true , this . fromAddressBounceable ) ;
170
171
parsed . toAddress = parsed . toAddress . toString ( true , true , this . toAddressBounceable ) ;
171
172
this . sender = parsed . fromAddress ;
172
173
this . recipient = { address : parsed . toAddress , amount : parsed . value } ;
174
+ this . withdrawAmount = parsed . withdrawAmount ;
173
175
this . seqno = parsed . seqno ;
174
176
this . publicKey = parsed . publicKey as string ;
175
177
this . expireTime = parsed . expireAt ;
@@ -183,10 +185,11 @@ export class Transaction extends BaseTransaction {
183
185
184
186
/** @inheritDoc */
185
187
explainTransaction ( ) : TransactionExplanation {
186
- const displayOrder = [ 'id' , 'outputs' , 'outputAmount' , 'changeOutputs' , 'changeAmount' , 'fee' ] ;
188
+ const displayOrder = [ 'id' , 'outputs' , 'outputAmount' , 'changeOutputs' , 'changeAmount' , 'fee' , 'withdrawAmount' ] ;
187
189
188
190
const outputs : TransactionRecipient [ ] = [ this . recipient ] ;
189
191
const outputAmount = this . recipient . amount ;
192
+ const withdrawAmount = this . withdrawAmount ;
190
193
return {
191
194
displayOrder,
192
195
id : this . id ,
@@ -195,10 +198,11 @@ export class Transaction extends BaseTransaction {
195
198
changeOutputs : [ ] ,
196
199
changeAmount : '0' ,
197
200
fee : { fee : 'UNKNOWN' } ,
201
+ withdrawAmount,
198
202
} ;
199
203
}
200
204
201
- private parseTransfer ( cell : Cell ) : any {
205
+ private parseTransaction ( cell : Cell ) : any {
202
206
const slice = ( cell as any ) . beginParse ( ) ;
203
207
204
208
// header
@@ -239,11 +243,11 @@ export class Transaction extends BaseTransaction {
239
243
return {
240
244
fromAddress : externalDestAddress ,
241
245
publicKey,
242
- ...this . parseTransferBody ( bodySlice ) ,
246
+ ...this . parseTransactionBody ( bodySlice ) ,
243
247
} ;
244
248
}
245
249
246
- private parseTransferBody ( slice : any ) : any {
250
+ private parseTransactionBody ( slice : any ) : any {
247
251
const signature = Buffer . from ( slice . loadBits ( 512 ) ) . toString ( 'hex' ) ;
248
252
// signing message
249
253
@@ -288,23 +292,37 @@ export class Transaction extends BaseTransaction {
288
292
289
293
// order body
290
294
let payload ;
291
-
295
+ let withdrawAmount ;
296
+ this . transactionType = TransactionType . Send ;
292
297
if ( order . getFreeBits ( ) > 0 ) {
293
298
if ( order . loadBit ( ) ) {
294
299
order = order . loadRef ( ) ;
295
300
}
296
301
297
302
if ( order . getFreeBits ( ) > 32 ) {
298
- const op = order . loadUint ( 32 ) ;
299
- const payloadBytes = order . loadBits ( order . getFreeBits ( ) ) ;
300
- payload = op . eq ( new BN ( 0 ) ) ? new TextDecoder ( ) . decode ( payloadBytes ) : '' ;
303
+ const opcode = order . loadUint ( 32 ) . toNumber ( ) ;
304
+ if ( opcode === 0 ) {
305
+ const payloadBytes = order . loadBits ( order . getFreeBits ( ) ) ;
306
+ payload = new TextDecoder ( ) . decode ( payloadBytes ) ;
307
+ } else if ( opcode === 4096 ) {
308
+ const queryId = order . loadUint ( 64 ) . toNumber ( ) ;
309
+ withdrawAmount = ( order . loadCoins ( ) . toNumber ( ) / 1e9 ) . toString ( ) ;
310
+ payload = new TonWeb . boc . Cell ( ) ;
311
+ payload . bits . writeUint ( opcode , 32 ) ;
312
+ payload . bits . writeUint ( queryId , 64 ) ;
313
+ payload . bits . writeCoins ( TonWeb . utils . toNano ( withdrawAmount ) ) ;
314
+ this . transactionType = TransactionType . SingleNominatorWithdraw ;
315
+ } else {
316
+ payload = '' ;
317
+ }
301
318
}
302
319
}
303
320
return {
304
321
toAddress : destAddress ,
305
322
value,
306
323
bounce,
307
324
seqno,
325
+ withdrawAmount,
308
326
expireAt,
309
327
payload,
310
328
signature,
0 commit comments