1- import { BaseKey , BaseTransaction , Entry , Recipient , TransactionRecipient , TransactionType } from '@bitgo/sdk-core' ;
2- import { TxData , TransactionExplanation } from './iface' ;
3- import { BaseCoin as CoinConfig } from '@bitgo/statics' ;
41import TonWeb from 'tonweb' ;
52import { BN } from 'bn.js' ;
63import { Cell } from 'tonweb/dist/types/boc/cell' ;
7- import { WITHDRAW_OPCODE } from './transactionBuilder' ;
84
9- const WALLET_ID = 698983191 ;
5+ import { BaseKey , BaseTransaction , Entry , Recipient , TransactionRecipient , TransactionType } from '@bitgo/sdk-core' ;
6+ import { BaseCoin as CoinConfig } from '@bitgo/statics' ;
7+ import { TransactionExplanation , TxData } from './iface' ;
8+ import { WITHDRAW_OPCODE , WALLET_ID , JETTON_TRANSFER_OPCODE } from './constants' ;
109
1110export class Transaction extends BaseTransaction {
1211 public recipient : Recipient ;
@@ -19,8 +18,8 @@ export class Transaction extends BaseTransaction {
1918 expireTime : number ;
2019 sender : string ;
2120 publicKey : string ;
22- private unsignedMessage : string ;
23- private finalMessage : string ;
21+ protected unsignedMessage : string ;
22+ protected finalMessage : string ;
2423
2524 constructor ( coinConfig : Readonly < CoinConfig > ) {
2625 super ( coinConfig ) ;
@@ -88,7 +87,7 @@ export class Transaction extends BaseTransaction {
8887 this . _id = originalTxId . replace ( / \/ / g, '_' ) . replace ( / \+ / g, '-' ) ;
8988 }
9089
91- private createSigningMessage ( walletId , seqno , expireAt ) {
90+ protected createSigningMessage ( walletId , seqno , expireAt ) {
9291 const message = new TonWeb . boc . Cell ( ) ;
9392 message . bits . writeUint ( walletId , 32 ) ;
9493 // expireAt should be set as per the provided arg value, regardless of the seqno
@@ -98,7 +97,7 @@ export class Transaction extends BaseTransaction {
9897 return message ;
9998 }
10099
101- private createOutMsg ( address , amount , payload ) {
100+ protected createOutMsg ( address , amount , payload ) {
102101 let payloadCell = new TonWeb . boc . Cell ( ) ;
103102 if ( payload ) {
104103 if ( payload . refs ) {
@@ -152,8 +151,7 @@ export class Transaction extends BaseTransaction {
152151 }
153152
154153 const header = TonWeb . Contract . createExternalMessageHeader ( this . sender ) ;
155- const resultMessage = TonWeb . Contract . createCommonMsgInfo ( header , stateInit , body ) ;
156- return resultMessage ;
154+ return TonWeb . Contract . createCommonMsgInfo ( header , stateInit , body ) ;
157155 }
158156
159157 loadInputsAndOutputs ( ) : void {
@@ -176,11 +174,8 @@ export class Transaction extends BaseTransaction {
176174 fromRawTransaction ( rawTransaction : string ) : void {
177175 try {
178176 const cell = TonWeb . boc . Cell . oneFromBoc ( TonWeb . utils . base64ToBytes ( rawTransaction ) ) ;
179-
180177 const parsed = this . parseTransaction ( cell ) ;
181- parsed . value = parsed . value . toString ( ) ;
182- parsed . fromAddress = parsed . fromAddress . toString ( true , true , this . fromAddressBounceable ) ;
183- parsed . toAddress = parsed . toAddress . toString ( true , true , this . toAddressBounceable ) ;
178+
184179 this . sender = parsed . fromAddress ;
185180 this . recipient = { address : parsed . toAddress , amount : parsed . value } ;
186181 this . withdrawAmount = parsed . withdrawAmount ;
@@ -214,7 +209,7 @@ export class Transaction extends BaseTransaction {
214209 } ;
215210 }
216211
217- private parseTransaction ( cell : Cell ) : any {
212+ protected parseTransaction ( cell : Cell ) : any {
218213 const slice = ( cell as any ) . beginParse ( ) ;
219214
220215 // header
@@ -253,7 +248,7 @@ export class Transaction extends BaseTransaction {
253248 const bodySlice = slice . loadBit ( ) ? slice . loadRef ( ) : slice ;
254249
255250 return {
256- fromAddress : externalDestAddress ,
251+ fromAddress : externalDestAddress . toString ( true , true , this . fromAddressBounceable ) ,
257252 publicKey,
258253 ...this . parseTransactionBody ( bodySlice ) ,
259254 } ;
@@ -285,7 +280,7 @@ export class Transaction extends BaseTransaction {
285280 const sourceAddress = order . loadAddress ( ) ;
286281 if ( sourceAddress !== null ) throw Error ( 'invalid externalSourceAddress' ) ;
287282 const destAddress = order . loadAddress ( ) ;
288- const value = order . loadCoins ( ) ;
283+ const value = order . loadCoins ( ) . toString ( ) ;
289284
290285 if ( order . loadBit ( ) ) throw Error ( 'invalid currencyCollection' ) ;
291286 const ihrFees = order . loadCoins ( ) ;
@@ -321,13 +316,47 @@ export class Transaction extends BaseTransaction {
321316 withdrawAmount = order . loadCoins ( ) . toNumber ( ) . toString ( ) ;
322317 payload = WITHDRAW_OPCODE + queryId . toString ( 16 ) . padStart ( 16 , '0' ) + withdrawAmount ;
323318 this . transactionType = TransactionType . SingleNominatorWithdraw ;
319+ } else if ( opcode === JETTON_TRANSFER_OPCODE ) {
320+ const queryId = order . loadUint ( 64 ) . toNumber ( ) ;
321+ if ( queryId !== 0 ) throw new Error ( 'invalid queryId for jetton transfer' ) ;
322+
323+ const jettonAmount = order . loadCoins ( ) ;
324+ if ( ! jettonAmount . gt ( new BN ( 0 ) ) ) throw new Error ( 'invalid jettonAmount' ) ;
325+
326+ const jettonRecipient = order . loadAddress ( ) ;
327+ if ( ! jettonRecipient ) throw new Error ( 'invalid jettonRecipient' ) ;
328+
329+ const forwarderAddress = order . loadAddress ( ) ;
330+ if ( ! forwarderAddress ) throw new Error ( 'invalid forwarderAddress' ) ;
331+
332+ order . loadBit ( ) ; // skip bit
333+
334+ const forwardTonAmount = order . loadCoins ( ) ;
335+ if ( ! forwardTonAmount . gt ( new BN ( 0 ) ) ) throw new Error ( 'invalid forwardTonAmount' ) ;
336+
337+ let message = '' ;
338+ if ( order . loadBit ( ) ) {
339+ order = order . loadRef ( ) ;
340+ const messageOpcode = order . loadUint ( 32 ) . toNumber ( ) ;
341+ if ( messageOpcode !== 0 ) throw new Error ( 'invalid message opcode' ) ;
342+ const messageBytes = order . loadBits ( order . getFreeBits ( ) ) ;
343+ message = new TextDecoder ( ) . decode ( messageBytes ) ;
344+ }
345+
346+ payload = {
347+ jettonAmount : jettonAmount . toString ( ) ,
348+ jettonRecipient : jettonRecipient . toString ( true , true , this . toAddressBounceable ) ,
349+ forwarderAddress : forwarderAddress . toString ( true , true , this . fromAddressBounceable ) ,
350+ forwardTonAmount : forwardTonAmount . toString ( ) ,
351+ message : message ,
352+ } ;
324353 } else {
325354 payload = '' ;
326355 }
327356 }
328357 }
329358 return {
330- toAddress : destAddress ,
359+ toAddress : destAddress . toString ( true , true , this . fromAddressBounceable ) ,
331360 value,
332361 bounce,
333362 seqno,
0 commit comments