Skip to content

Commit

Permalink
contract event tracking in js/ts
Browse files Browse the repository at this point in the history
  • Loading branch information
JanKuczma committed May 22, 2024
1 parent 9411a05 commit 9467bb1
Show file tree
Hide file tree
Showing 17 changed files with 2,065 additions and 0 deletions.
3 changes: 3 additions & 0 deletions js/contract-events-ts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# .gitignore
node_modules/
dist/
13 changes: 13 additions & 0 deletions js/contract-events-ts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## Example

1. Install dependencies `npm install`
2. Run `npm run getEventsBlockRange` to print events from the example contracts configured in `config.json`.
3. Run `npm run subscribeEvents` to subscribe to events in on-going blocks.

## Instructions for tracking any contract events

1. Place `.json` (or `.contract`) files (these are analogous to ABI files from Solidity) in the `contract_metadata` folder. These are obtained when compiling your ink! contract (available in `/target/ink`).
2. Configure the `ws_endpoint` in the `config.json` file. This should be `ws://localhost:9944` for local node, `wss://ws.test.azero.dev` for Aleph Zero testnet, and `wss://ws.azero.dev` for Aleph Zero Mainnet.
3. Configure the contract addresses you want to track in the `config.json` file.
3. Note that the metadata files (in `contract_metadata`) include the `hash` of the contract wasm binary, and it is used to link by our script to link a contract address to the metadata file. In case the on-chain contract you want to track comes from the same code, but for some reason has a hash incompatible with what's in the metadata file (this can happen if you recompile the contract after deploying to chain) the script will give an error that no metadata was found for the given contract. In this case you can manually "override" the metadata for a contract, by just specifying the name of the metadata file in the `contract_metadata` directory which should be used to decode the events emitted by this contract. If you do that, we cannot unfortunately verify if the override is correct, and if it isn't, the script can crash or produce incorrect results.
4. Run `npm run subscribeEvents` or customize and run the `npm run getEventsBlockRange` script to your needs.
70 changes: 70 additions & 0 deletions js/contract-events-ts/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"ws_endpoint": "wss://ws.test.azero.dev",
"metadata_dir": "contract_metadata",
"contracts": {
"5E5tXEx14YzZpRhjisdKbQ8dWftgidpxddZLmGyzRDezzyMf": {
"name": "flipper",
"metadata_override": "flipper.contract"
},
"5ChpoH1doT2mdnsyhfcLXEKhvqSsiYNDohXWeCZJzFupxAh6": {
"name": "token-1",
"metadata_override": "psp22v5.contract"
},
"5FmVtQGGUDVSpqYVkU3zLBQProQ4hHPjGQjUahxCYLJJvjfK": {
"name": "router"
},
"5EFDb7mKbougLtr5dnwd5KDfZ3wK55JPGPLiryKq4uRMPR46": {
"name": "wazero"
},
"5FTJdEQpHfUzXL3m2EFrSCwDF4qd6vuhYmfv3ZiLnSds6QbT": {
"name": "factory"
},
"5CPvHPDGGf4pxAxmMKV3KcMrp2zvtM2whgZ4fY3ni11qL27p": {
"name": "pair-1"
},
"5HQMZtqtWvsvZRC5rskPcQueZ7L35m8bG2EXWDSwtndJJBcp": {
"name": "pair-2",
"metadata_override": "pair_contract.contract"
},
"5CVGYujZnkBvNsUypdMuEYT2qRzFWhZHufteSfYguQMLkaE3": {
"name": "token-1",
"metadata_override": "psp22.contract"
},
"5FDkUXLExhgFT92UQvMQVG8H4Z4Ku4Mx9heUYpchxZMdY7LD": {
"name": "token-2",
"metadata_override": "psp22.contract"
},
"5DgnLZDNJ2bN4AcG4PzGMDdpL5ukd1kttmuMjXYNCG91vCkX": {
"name": "token-3",
"metadata_override": "psp22.contract"
},
"5GkV8efVcUhZ2PRkP6bNzJ9ATVh32uJMY89zMiK5rkA49yfU": {
"name": "token-4",
"metadata_override": "psp22.contract"
},
"5F84uFXvpEn4n6fAyRbP6mg32YHy8R4KEokZfFMW1svNTmbZ": {
"name": "token-5",
"metadata_override": "psp22.contract"
},
"5DuyRY19RZsxnyffwKRSox8rj5VUHf49fHLjUcZpfnwFrYGZ": {
"name": "token-6",
"metadata_override": "psp22.contract"
},
"5E3bkdogtK4ro2vC5vKP7QDJRzw38kHqXp5p5BiurQ9hBSbF": {
"name": "token-7",
"metadata_override": "psp22.contract"
},
"5CnV23shYarqBGZVmzuCxJvbd2TwxvQsjgAxoeGnhu2Zkxkp": {
"name": "token-8",
"metadata_override": "psp22.contract"
},
"5D5W2iUTvWs3mVSLDCdnNU3pTJyUHpiCeTEp9td4Hk3jwPqt": {
"name": "token-9",
"metadata_override": "psp22.contract"
},
"5H8UXMbPdVTCbsYQWBGuVj4k6XDo75wqQ8QdeRbwziQYcTdc": {
"name": "token-10",
"metadata_override": "psp22.contract"
}
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"source":{"hash":"0xc6c276cde1043f7099e8866606f5528a2c927a54ba42f94bed0ce59fb3af8bc2","language":"ink! 4.3.0","compiler":"rustc 1.76.0","wasm":"0x0061736d01000000012b0860027f7f0060037f7f7f0060000060047f7f7f7f017f60017f0060047f7f7f7f006000017f60017f017f02b10109057365616c310b6765745f73746f726167650003057365616c301176616c75655f7472616e736665727265640000057365616c3005696e7075740000057365616c300663616c6c65720000057365616c300f686173685f626c616b65325f3235360001057365616c300d6465706f7369745f6576656e740005057365616c320b7365745f73746f726167650003057365616c300b7365616c5f72657475726e000103656e76066d656d6f727902010210030e0d010001040600070002040002020608017f01418080040b0711020463616c6c0013066465706c6f7900140ab60e0d2c01017f037f2002200346047f200005200020036a200120036a2d00003a0000200341016a21030c010b0b1a0b2601017f230041106b22022400200220003a000f20012002410f6a4101100a200241106a24000b4701027f024002402000280208220320026a22042003490d00200420002802044b0d00200420036b2002470d01200028020020036a200120021008200020043602080f0b000b000b2601017f230041106b220124002001410036020c20002001410c6a4104100a200141106a24000b4d02017f027e230041206b2200240020004200370308200042003703002000411036021c20002000411c6a10012000290308210120002903002102200041206a2400410541042001200284501b0b0a00200120004120100a0b4701017f4101410220002802042201047f2000200141016b36020420002000280200220041016a36020020002d00000520000b41ff017122004101461b410020001b410220011b0b3c01027f027f200145044041908004210141010c010b410121024190800441013a000041918004210141020b2103200120023a0000200020031012000b12004190800441003b0100410041021012000b8b0101047f230041106b22012400200142808001370208200141908004360204200141046a100b024020012802082204200128020c2202490d00200128020421032001410036020c2001200420026b3602082001200220036a3602042000200141046a1009200128020c220020012802084b0d00200320022001280204200010061a200141106a24000f0b000b0d0020004190800420011007000bff07020a7f017e230041106b22022400024002400240100c41ff01714105470d0020024180800136020441908004200241046a100220022802042200418180014f0d0020004104490d02419380042d00002100419280042d00002101419180042d000021030240419080042d00002204412f470440200441e300470d04410121052003413a47200141a5014772200041d1004772450d010c040b200341860147200141db004772200041d90147720d030b200242808001370208200241908004360204200241046a100b20022802082203200228020c2200490d00200228020421012002200320006b220336020420012000200020016a2200200241046a100020032002280204220149720d002002200136020820022000360204200241046a100e41ff017122004102460d002002200041004722013a000420050d01230041106b220024002000419080043602044190800441003a000020004280808180103702082001200041046a1009200028020c2200418180014f0440000b410020001012000b000b230041f0006b22002400200241046a220120012d000041017322033a000020004180800136020841908004200041086a1003419080042d0000210141918004290000210a4190800441043b0100200041216a41a880042900003700002000411a6a41a18004290000370100200041126a419980042900003701002000200a37010a200020013a0009200020033a0008200041918004360250200042ffff808010370254200041d0006a2201418080044100100a2001418080044110100a024002402000280258220120002802544b0d0020002802502104200041c8006a22054200370300200041406b4200370300200041386a22064200370300200042003703300240200141214f0440200041e8006a22074200370300200041e0006a22084200370300200041d8006a220942003703002000420037035020042001200041d0006a100420052007290300370300200041406b200829030037030020062009290300370300200020002903503703300c010b200041306a2004200110080b200041e8006a200041c8006a290300370300200041e0006a200041406b290300370300200041d8006a200041386a29030037030020002000290330370350200042ffff00370234200041918004360230200041d0006a200041306a100d200028023841016a2201452001418180014f720d0020004180800120016b36025420002001419080046a2204360250200141808001460d00200441003a000020004101360258200041086a410172200041d0006a2204100d2003200410092000280258220320002802544b0d00419080042001200028025020031005200041f0006a24000c010b000b20022d0004101141004100100f000b41014101100f000be30101057f230041106b2200240002400240100c41ff01714105470d0020004180800136020c419080042000410c6a1002200028020c2201418180014f0d00024020014104490d002000419480043602042000200141046b360208419380042d00002101419280042d00002102419180042d00002103419080042d0000220441ed014704402004419b0147200341ae0147722002419d0147200141de004772720d01200041046a100e220041ff01714102460d01200010111010000b200341cb00472002419d0147720d002001411b460d020b41014101100f000b000b410010111010000b0b180100418080040b10466c69707065723a3a466c6970706564","build_info":{"build_mode":"Release","cargo_contract_version":"3.2.0","rust_toolchain":"stable-x86_64-unknown-linux-gnu","wasm_opt_settings":{"keep_debug_symbols":false,"optimization_passes":"Z"}}},"contract":{"name":"flipper","version":"0.1.0","authors":["[your_name] <[your_email]>"]},"spec":{"constructors":[{"args":[{"label":"init_value","type":{"displayName":["bool"],"type":0}}],"default":false,"docs":["Constructor that initializes the `bool` value to the given `init_value`."],"label":"new","payable":false,"returnType":{"displayName":["ink_primitives","ConstructorResult"],"type":1},"selector":"0x9bae9d5e"},{"args":[],"default":false,"docs":["Constructor that initializes the `bool` value to `false`.","","Constructors can delegate to other constructors."],"label":"default","payable":false,"returnType":{"displayName":["ink_primitives","ConstructorResult"],"type":1},"selector":"0xed4b9d1b"}],"docs":[],"environment":{"accountId":{"displayName":["AccountId"],"type":5},"balance":{"displayName":["Balance"],"type":8},"blockNumber":{"displayName":["BlockNumber"],"type":11},"chainExtension":{"displayName":["ChainExtension"],"type":12},"hash":{"displayName":["Hash"],"type":9},"maxEventTopics":4,"timestamp":{"displayName":["Timestamp"],"type":10}},"events":[{"args":[{"docs":[],"indexed":false,"label":"who","type":{"displayName":["AccountId"],"type":5}},{"docs":[],"indexed":false,"label":"to_value","type":{"displayName":["bool"],"type":0}}],"docs":[],"label":"Flipped"}],"lang_error":{"displayName":["ink","LangError"],"type":3},"messages":[{"args":[],"default":false,"docs":[" A message that can be called on instantiated contracts."," This one flips the value of the stored `bool` from `true`"," to `false` and vice versa."],"label":"flip","mutates":true,"payable":false,"returnType":{"displayName":["ink","MessageResult"],"type":1},"selector":"0x633aa551"},{"args":[],"default":false,"docs":[" Simply returns the current value of our `bool`."],"label":"get","mutates":false,"payable":false,"returnType":{"displayName":["ink","MessageResult"],"type":4},"selector":"0x2f865bd9"}]},"storage":{"root":{"layout":{"struct":{"fields":[{"layout":{"leaf":{"key":"0x00000000","ty":0}},"name":"value"}],"name":"Flipper"}},"root_key":"0x00000000"}},"types":[{"id":0,"type":{"def":{"primitive":"bool"}}},{"id":1,"type":{"def":{"variant":{"variants":[{"fields":[{"type":2}],"index":0,"name":"Ok"},{"fields":[{"type":3}],"index":1,"name":"Err"}]}},"params":[{"name":"T","type":2},{"name":"E","type":3}],"path":["Result"]}},{"id":2,"type":{"def":{"tuple":[]}}},{"id":3,"type":{"def":{"variant":{"variants":[{"index":1,"name":"CouldNotReadInput"}]}},"path":["ink_primitives","LangError"]}},{"id":4,"type":{"def":{"variant":{"variants":[{"fields":[{"type":0}],"index":0,"name":"Ok"},{"fields":[{"type":3}],"index":1,"name":"Err"}]}},"params":[{"name":"T","type":0},{"name":"E","type":3}],"path":["Result"]}},{"id":5,"type":{"def":{"composite":{"fields":[{"type":6,"typeName":"[u8; 32]"}]}},"path":["ink_primitives","types","AccountId"]}},{"id":6,"type":{"def":{"array":{"len":32,"type":7}}}},{"id":7,"type":{"def":{"primitive":"u8"}}},{"id":8,"type":{"def":{"primitive":"u128"}}},{"id":9,"type":{"def":{"composite":{"fields":[{"type":6,"typeName":"[u8; 32]"}]}},"path":["ink_primitives","types","Hash"]}},{"id":10,"type":{"def":{"primitive":"u64"}}},{"id":11,"type":{"def":{"primitive":"u32"}}},{"id":12,"type":{"def":{"variant":{}},"path":["ink_env","types","NoChainExtension"]}}],"version":"4"}
1 change: 1 addition & 0 deletions js/contract-events-ts/contract_metadata/flipper.contract
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"source":{"hash":"0xc6c276cde1043f7099e8866606f5528a2c927a54ba42f94bed0ce59fb3af8bc2","language":"ink! 4.3.0","compiler":"rustc 1.76.0","wasm":"0x0061736d01000000012b0860027f7f0060037f7f7f0060000060047f7f7f7f017f60017f0060047f7f7f7f006000017f60017f017f02b10109057365616c310b6765745f73746f726167650003057365616c301176616c75655f7472616e736665727265640000057365616c3005696e7075740000057365616c300663616c6c65720000057365616c300f686173685f626c616b65325f3235360001057365616c300d6465706f7369745f6576656e740005057365616c320b7365745f73746f726167650003057365616c300b7365616c5f72657475726e000103656e76066d656d6f727902010210030e0d010001040600070002040002020608017f01418080040b0711020463616c6c0013066465706c6f7900140ab60e0d2c01017f037f2002200346047f200005200020036a200120036a2d00003a0000200341016a21030c010b0b1a0b2601017f230041106b22022400200220003a000f20012002410f6a4101100a200241106a24000b4701027f024002402000280208220320026a22042003490d00200420002802044b0d00200420036b2002470d01200028020020036a200120021008200020043602080f0b000b000b2601017f230041106b220124002001410036020c20002001410c6a4104100a200141106a24000b4d02017f027e230041206b2200240020004200370308200042003703002000411036021c20002000411c6a10012000290308210120002903002102200041206a2400410541042001200284501b0b0a00200120004120100a0b4701017f4101410220002802042201047f2000200141016b36020420002000280200220041016a36020020002d00000520000b41ff017122004101461b410020001b410220011b0b3c01027f027f200145044041908004210141010c010b410121024190800441013a000041918004210141020b2103200120023a0000200020031012000b12004190800441003b0100410041021012000b8b0101047f230041106b22012400200142808001370208200141908004360204200141046a100b024020012802082204200128020c2202490d00200128020421032001410036020c2001200420026b3602082001200220036a3602042000200141046a1009200128020c220020012802084b0d00200320022001280204200010061a200141106a24000f0b000b0d0020004190800420011007000bff07020a7f017e230041106b22022400024002400240100c41ff01714105470d0020024180800136020441908004200241046a100220022802042200418180014f0d0020004104490d02419380042d00002100419280042d00002101419180042d000021030240419080042d00002204412f470440200441e300470d04410121052003413a47200141a5014772200041d1004772450d010c040b200341860147200141db004772200041d90147720d030b200242808001370208200241908004360204200241046a100b20022802082203200228020c2200490d00200228020421012002200320006b220336020420012000200020016a2200200241046a100020032002280204220149720d002002200136020820022000360204200241046a100e41ff017122004102460d002002200041004722013a000420050d01230041106b220024002000419080043602044190800441003a000020004280808180103702082001200041046a1009200028020c2200418180014f0440000b410020001012000b000b230041f0006b22002400200241046a220120012d000041017322033a000020004180800136020841908004200041086a1003419080042d0000210141918004290000210a4190800441043b0100200041216a41a880042900003700002000411a6a41a18004290000370100200041126a419980042900003701002000200a37010a200020013a0009200020033a0008200041918004360250200042ffff808010370254200041d0006a2201418080044100100a2001418080044110100a024002402000280258220120002802544b0d0020002802502104200041c8006a22054200370300200041406b4200370300200041386a22064200370300200042003703300240200141214f0440200041e8006a22074200370300200041e0006a22084200370300200041d8006a220942003703002000420037035020042001200041d0006a100420052007290300370300200041406b200829030037030020062009290300370300200020002903503703300c010b200041306a2004200110080b200041e8006a200041c8006a290300370300200041e0006a200041406b290300370300200041d8006a200041386a29030037030020002000290330370350200042ffff00370234200041918004360230200041d0006a200041306a100d200028023841016a2201452001418180014f720d0020004180800120016b36025420002001419080046a2204360250200141808001460d00200441003a000020004101360258200041086a410172200041d0006a2204100d2003200410092000280258220320002802544b0d00419080042001200028025020031005200041f0006a24000c010b000b20022d0004101141004100100f000b41014101100f000be30101057f230041106b2200240002400240100c41ff01714105470d0020004180800136020c419080042000410c6a1002200028020c2201418180014f0d00024020014104490d002000419480043602042000200141046b360208419380042d00002101419280042d00002102419180042d00002103419080042d0000220441ed014704402004419b0147200341ae0147722002419d0147200141de004772720d01200041046a100e220041ff01714102460d01200010111010000b200341cb00472002419d0147720d002001411b460d020b41014101100f000b000b410010111010000b0b180100418080040b10466c69707065723a3a466c6970706564","build_info":{"build_mode":"Release","cargo_contract_version":"3.2.0","rust_toolchain":"stable-x86_64-unknown-linux-gnu","wasm_opt_settings":{"keep_debug_symbols":false,"optimization_passes":"Z"}}},"contract":{"name":"flipper","version":"0.1.0","authors":["[your_name] <[your_email]>"]},"spec":{"constructors":[{"args":[{"label":"init_value","type":{"displayName":["bool"],"type":0}}],"default":false,"docs":["Constructor that initializes the `bool` value to the given `init_value`."],"label":"new","payable":false,"returnType":{"displayName":["ink_primitives","ConstructorResult"],"type":1},"selector":"0x9bae9d5e"},{"args":[],"default":false,"docs":["Constructor that initializes the `bool` value to `false`.","","Constructors can delegate to other constructors."],"label":"default","payable":false,"returnType":{"displayName":["ink_primitives","ConstructorResult"],"type":1},"selector":"0xed4b9d1b"}],"docs":[],"environment":{"accountId":{"displayName":["AccountId"],"type":5},"balance":{"displayName":["Balance"],"type":8},"blockNumber":{"displayName":["BlockNumber"],"type":11},"chainExtension":{"displayName":["ChainExtension"],"type":12},"hash":{"displayName":["Hash"],"type":9},"maxEventTopics":4,"timestamp":{"displayName":["Timestamp"],"type":10}},"events":[{"args":[{"docs":[],"indexed":false,"label":"who","type":{"displayName":["AccountId"],"type":5}},{"docs":[],"indexed":false,"label":"to_value","type":{"displayName":["bool"],"type":0}}],"docs":[],"label":"Flipped"}],"lang_error":{"displayName":["ink","LangError"],"type":3},"messages":[{"args":[],"default":false,"docs":[" A message that can be called on instantiated contracts."," This one flips the value of the stored `bool` from `true`"," to `false` and vice versa."],"label":"flip","mutates":true,"payable":false,"returnType":{"displayName":["ink","MessageResult"],"type":1},"selector":"0x633aa551"},{"args":[],"default":false,"docs":[" Simply returns the current value of our `bool`."],"label":"get","mutates":false,"payable":false,"returnType":{"displayName":["ink","MessageResult"],"type":4},"selector":"0x2f865bd9"}]},"storage":{"root":{"layout":{"struct":{"fields":[{"layout":{"leaf":{"key":"0x00000000","ty":0}},"name":"value"}],"name":"Flipper"}},"root_key":"0x00000000"}},"types":[{"id":0,"type":{"def":{"primitive":"bool"}}},{"id":1,"type":{"def":{"variant":{"variants":[{"fields":[{"type":2}],"index":0,"name":"Ok"},{"fields":[{"type":3}],"index":1,"name":"Err"}]}},"params":[{"name":"T","type":2},{"name":"E","type":3}],"path":["Result"]}},{"id":2,"type":{"def":{"tuple":[]}}},{"id":3,"type":{"def":{"variant":{"variants":[{"index":1,"name":"CouldNotReadInput"}]}},"path":["ink_primitives","LangError"]}},{"id":4,"type":{"def":{"variant":{"variants":[{"fields":[{"type":0}],"index":0,"name":"Ok"},{"fields":[{"type":3}],"index":1,"name":"Err"}]}},"params":[{"name":"T","type":0},{"name":"E","type":3}],"path":["Result"]}},{"id":5,"type":{"def":{"composite":{"fields":[{"type":6,"typeName":"[u8; 32]"}]}},"path":["ink_primitives","types","AccountId"]}},{"id":6,"type":{"def":{"array":{"len":32,"type":7}}}},{"id":7,"type":{"def":{"primitive":"u8"}}},{"id":8,"type":{"def":{"primitive":"u128"}}},{"id":9,"type":{"def":{"composite":{"fields":[{"type":6,"typeName":"[u8; 32]"}]}},"path":["ink_primitives","types","Hash"]}},{"id":10,"type":{"def":{"primitive":"u64"}}},{"id":11,"type":{"def":{"primitive":"u32"}}},{"id":12,"type":{"def":{"variant":{}},"path":["ink_env","types","NoChainExtension"]}}],"version":"4"}

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions js/contract-events-ts/contract_metadata/psp22.contract

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Loading

0 comments on commit 9467bb1

Please sign in to comment.