@@ -3,13 +3,21 @@ const {
33    GetProofsRequest, 
44  } , 
55}  =  require ( '@dashevo/dapi-grpc' ) ; 
6- const  {  StateTransitionTypes }  =  require ( '@dashevo/wasm-dpp' ) ; 
6+ 
7+ const  { 
8+   StateTransitionTypes, 
9+   TokenTransition, 
10+   DocumentTransition, 
11+   TokenTransitionType, 
12+ }  =  require ( '@dashevo/wasm-dpp' ) ; 
13+ const  {  GetDataContractRequest }  =  require ( '@dashevo/dapi-grpc/clients/platform/v0/web/platform_pb' ) ; 
714
815/** 
916 * @param  {PlatformPromiseClient } driveClient 
17+  * @param  {DashPlatformProtocol } dpp 
1018 * @return  {fetchProofForStateTransition } 
1119 */ 
12- function  fetchProofForStateTransitionFactory ( driveClient )  { 
20+ function  fetchProofForStateTransitionFactory ( driveClient ,   dpp )  { 
1321  /** 
1422   * @typedef  {fetchProofForStateTransition } 
1523   * @param  {AbstractStateTransition } stateTransition 
@@ -22,25 +30,160 @@ function fetchProofForStateTransitionFactory(driveClient) {
2230
2331    const  requestV0  =  new  GetProofsRequestV0 ( ) ; 
2432
33+     let  dataContractsCache  =  { } ; 
34+ 
2535    if  ( stateTransition . isDocumentStateTransition ( ) )  { 
26-       const  {  DocumentRequest }  =  GetProofsRequestV0 ; 
36+       const  { 
37+         DocumentRequest, 
38+         IdentityTokenBalanceRequest, 
39+         IdentityTokenInfoRequest, 
40+         TokenStatusRequest, 
41+       }  =  GetProofsRequestV0 ; 
2742
28-       const  documentsList  =  stateTransition . getTransitions ( ) . map ( ( documentTransition )  =>  { 
29-         const  documentRequest  =  new  DocumentRequest ( ) ; 
30-         documentRequest . setContractId ( documentTransition . getDataContractId ( ) . toBuffer ( ) ) ; 
31-         documentRequest . setDocumentType ( documentTransition . getType ( ) ) ; 
32-         documentRequest . setDocumentId ( documentTransition . getId ( ) . toBuffer ( ) ) ; 
43+       const  documentsList  =  [ ] ; 
44+       const  identityTokenBalancesList  =  [ ] ; 
45+       const  identityTokenInfosList  =  [ ] ; 
46+       const  tokenStatusesList  =  [ ] ; 
3347
34-         const  status  =  documentTransition . hasPrefundedBalance ( ) 
35-           ? DocumentRequest . DocumentContestedStatus . CONTESTED 
36-           : DocumentRequest . DocumentContestedStatus . NOT_CONTESTED ; 
48+       for  ( const  batchedTransition  of  stateTransition . getTransitions ( ) )  { 
49+         if  ( batchedTransition  instanceof  TokenTransition )  { 
50+           switch  ( batchedTransition . getTransitionType ( ) )  { 
51+             case  TokenTransitionType . Burn : { 
52+               const  request  =  new  IdentityTokenBalanceRequest ( { 
53+                 tokenId : batchedTransition . getTokenId ( ) 
54+                   . toBuffer ( ) , 
55+                 identityId : stateTransition . getOwnerId ( ) 
56+                   . toBuffer ( ) , 
57+               } ) ; 
3758
38-         documentRequest . setDocumentContestedStatus ( status ) ; 
59+               identityTokenBalancesList . push ( request ) ; 
60+               break ; 
61+             } 
62+             case  TokenTransitionType . Mint : { 
63+               // Fetch data contract to determine correct recipient identity 
64+               const  dataContractId  =  batchedTransition . getDataContractId ( ) ; 
65+               const  dataContractIdString  =  dataContractId . toString ( ) ; 
3966
40-         return  documentRequest ; 
41-       } ) ; 
67+               if  ( ! dataContractsCache [ dataContractIdString ] )  { 
68+                 const  dataContractRequestV0  =  new  GetDataContractRequest . GetDataContractRequestV0 ( { 
69+                   id : dataContractId . toBuffer ( ) , 
70+                 } ) ; 
71+ 
72+                 const  dataContractRequest  =  new  GetDataContractRequest ( ) ; 
73+                 dataContractRequest . setV0 ( dataContractRequestV0 ) ; 
74+ 
75+                 const  dataContractResponse  =  await  driveClient . getDataContract ( dataContractRequest ) ; 
76+ 
77+                 const  dataContractBuffer  =  Buffer . from ( 
78+                   dataContractResponse . getV0 ( ) . getDataContract_asU8 ( ) , 
79+                 ) ; 
80+ 
81+                 dataContractsCache [ dataContractIdString ]  =  await  dpp . dataContract 
82+                   . createFromBuffer ( dataContractBuffer ) ; 
83+               } 
84+ 
85+               const  dataContract  =  dataContractsCache [ dataContractIdString ] ; 
86+ 
87+               const  tokenConfiguration  =  dataContract . getTokenConfiguration ( 
88+                 batchedTransition . getTokenContractPosition ( ) , 
89+               ) ; 
90+ 
91+               const  request  =  new  IdentityTokenBalanceRequest ( { 
92+                 tokenId : batchedTransition . getTokenId ( ) 
93+                   . toBuffer ( ) , 
94+                 identityId : batchedTransition . toTransition ( ) . getRecipientId ( tokenConfiguration ) 
95+                   . toBuffer ( ) , 
96+               } ) ; 
97+ 
98+               identityTokenBalancesList . push ( request ) ; 
99+               break ; 
100+             } 
101+             case  TokenTransitionType . Transfer : { 
102+               const  requestSender  =  new  IdentityTokenBalanceRequest ( { 
103+                 tokenId : batchedTransition . getTokenId ( ) 
104+                   . toBuffer ( ) , 
105+                 identityId : stateTransition . getOwnerId ( ) . toBuffer ( ) , 
106+               } ) ; 
107+ 
108+               const  requestRecipient  =  new  IdentityTokenBalanceRequest ( { 
109+                 tokenId : batchedTransition . getTokenId ( ) 
110+                   . toBuffer ( ) , 
111+                 identityId : batchedTransition . toTransition ( ) . getRecipientId ( ) 
112+                   . toBuffer ( ) , 
113+               } ) ; 
114+ 
115+               identityTokenBalancesList . push ( requestSender ,  requestRecipient ) ; 
116+               break ; 
117+             } 
118+             case  TokenTransitionType . DestroyFrozenFunds : { 
119+               const  request  =  new  IdentityTokenBalanceRequest ( { 
120+                 tokenId : batchedTransition . getTokenId ( ) 
121+                   . toBuffer ( ) , 
122+                 identityId : batchedTransition . toTransition ( ) . getFrozenIdentityId ( ) 
123+                   . toBuffer ( ) , 
124+               } ) ; 
125+ 
126+               identityTokenBalancesList . push ( request ) ; 
127+               break ; 
128+             } 
129+             case  TokenTransitionType . EmergencyAction :
130+             { 
131+               const  request  =  new  TokenStatusRequest ( { 
132+                 tokenId : batchedTransition . getTokenId ( ) 
133+                   . toBuffer ( ) , 
134+               } ) ; 
42135
43-       requestV0 . setDocumentsList ( documentsList ) ; 
136+               tokenStatusesList . push ( request ) ; 
137+               break ; 
138+             } 
139+             case  TokenTransitionType . Freeze :
140+             case  TokenTransitionType . Unfreeze : { 
141+               const  request  =  new  IdentityTokenInfoRequest ( { 
142+                 tokenId : batchedTransition . getTokenId ( ) 
143+                   . toBuffer ( ) , 
144+                 identityId : batchedTransition . toTransition ( ) . getFrozenIdentityId ( ) 
145+                   . toBuffer ( ) , 
146+               } ) ; 
147+ 
148+               identityTokenInfosList . push ( request ) ; 
149+               break ; 
150+             } 
151+             default :
152+               throw  new  Error ( `Unsupported token transition type ${ batchedTransition . getTransitionType ( ) }  ` ) ; 
153+           } 
154+         }  else  if  ( batchedTransition  instanceof  DocumentTransition )  { 
155+           const  documentRequest  =  new  DocumentRequest ( ) ; 
156+           documentRequest . setContractId ( batchedTransition . getDataContractId ( ) . toBuffer ( ) ) ; 
157+           documentRequest . setDocumentType ( batchedTransition . getType ( ) ) ; 
158+           documentRequest . setDocumentId ( batchedTransition . getId ( ) . toBuffer ( ) ) ; 
159+ 
160+           const  status  =  batchedTransition . hasPrefundedBalance ( ) 
161+             ? DocumentRequest . DocumentContestedStatus . CONTESTED 
162+             : DocumentRequest . DocumentContestedStatus . NOT_CONTESTED ; 
163+ 
164+           documentRequest . setDocumentContestedStatus ( status ) ; 
165+ 
166+           documentsList . push ( documentRequest ) ; 
167+         }  else  { 
168+           throw  new  Error ( `Unsupported batched transition type ${ batchedTransition . constructor . name }  ` ) ; 
169+         } 
170+       } 
171+ 
172+       if  ( documentsList . length  >  0 )  { 
173+         requestV0 . setDocumentsList ( documentsList ) ; 
174+       } 
175+ 
176+       if  ( identityTokenBalancesList . length  >  0 )  { 
177+         requestV0 . setIdentityTokenBalancesList ( identityTokenBalancesList ) ; 
178+       } 
179+ 
180+       if  ( identityTokenInfosList . length  >  0 )  { 
181+         requestV0 . setIdentityTokenInfosList ( identityTokenInfosList ) ; 
182+       } 
183+ 
184+       if  ( tokenStatusesList . length  >  0 )  { 
185+         requestV0 . setTokenStatusesList ( tokenStatusesList ) ; 
186+       } 
44187    }  if  ( stateTransition . isIdentityStateTransition ( ) )  { 
45188      const  {  IdentityRequest }  =  GetProofsRequestV0 ; 
46189
0 commit comments