@@ -3,8 +3,9 @@ source: rs/client-gen-js/tests/generator.rs
33expression : generated
44-- -
55import {GearApi , HexString } from " @gear-js/api" ;
6- import {QueryBuilderWithHeader , TransactionBuilderWithHeader , TypeResolver } from " sails-js" ;
6+ import {QueryBuilderWithHeader , TransactionBuilderWithHeader , TypeResolver , ZERO_ADDRESS } from " sails-js" ;
77import {InterfaceId , SailsMessageHeader } from " sails-js-parser-idl-v2" ;
8+ import {IStructField } from " sails-js-types" ;
89
910export class CodecProgram {
1011 private _typeResolver: TypeResolver ;
@@ -42,7 +43,7 @@ export class CodecTest {
4243 return this ._typeResolver .registry ;
4344 }
4445 public get interfaceId(): InterfaceId {
45- return InterfaceId .from (" 0x577754119419635e " );
46+ return InterfaceId .from (" 0x3d3ab867eece8ee1 " );
4647 }
4748 /**
4849 * Available via both codecs (default)
@@ -90,4 +91,40 @@ export class CodecTest {
9091 this ._programId ,
9192 );
9293 }
94+
95+ /**
96+ * Available through both codecs
97+ */
98+ public subscribeToBothEventEvent<T = number >(callback : (eventData : T ) => void | Promise <void >): Promise <() => void > {
99+ const interfaceIdu64 = this .interfaceId .asU64 ();
100+ const eventFields = {" fields" :[{" type" :" u32" }]}.fields as IStructField [];
101+ const typeStr = this ._typeResolver .getStructDef (eventFields , {}, true );
102+ return this ._api .gearEvents .subscribeToGearEvent (" UserMessageSent" , ({ data : { message } }) => {
103+ if (! message .source .eq (this ._programId )) return ;
104+ if (! message .destination .eq (ZERO_ADDRESS )) return ;
105+
106+ const { ok, header } = SailsMessageHeader .tryFromBytes (message .payload );
107+ if (ok && header .interfaceId .asU64 () === interfaceIdu64 && header .entryId === 0 ) {
108+ callback (this .registry .createType (` ([u8; 16], ${typeStr })` , message .payload )[1 ].toJSON () as T );
109+ }
110+ });
111+ }
112+
113+ /**
114+ * SCALE only
115+ */
116+ public subscribeToScaleOnlyEventEvent<T = number >(callback : (eventData : T ) => void | Promise <void >): Promise <() => void > {
117+ const interfaceIdu64 = this .interfaceId .asU64 ();
118+ const eventFields = {" fields" :[{" type" :" u32" }]}.fields as IStructField [];
119+ const typeStr = this ._typeResolver .getStructDef (eventFields , {}, true );
120+ return this ._api .gearEvents .subscribeToGearEvent (" UserMessageSent" , ({ data : { message } }) => {
121+ if (! message .source .eq (this ._programId )) return ;
122+ if (! message .destination .eq (ZERO_ADDRESS )) return ;
123+
124+ const { ok, header } = SailsMessageHeader .tryFromBytes (message .payload );
125+ if (ok && header .interfaceId .asU64 () === interfaceIdu64 && header .entryId === 1 ) {
126+ callback (this .registry .createType (` ([u8; 16], ${typeStr })` , message .payload )[1 ].toJSON () as T );
127+ }
128+ });
129+ }
93130}
0 commit comments