All Products
Search
Document Center

Blockchain as a Service:Event interface

Last Updated:May 08, 2020

Subscribe to an account

  • Function prototype
  1. public BigInteger listenAccount(Identity id, IEventCallback handler, EventModelType type)
  • Request parameter
Parameter Required Type Description
id true Identity The account’s business ID.
handler true IEventCallback Performs callback when a response is received or a timeout or an error occurs.
type true EventModelType The event model type. 1 indicates PULL, and 2 indicates PUSH.
  • Response field
Response field Field type Description
result BigInteger The returned result. If the result is 0, the listening failed.
  • Example
  1. // event handler
  2. IEventCallback handler = new IEventCallback() {
  3. @Override
  4. public void onEvent(Message message) {
  5. PushAccountEvent accountEvent = (PushAccountEvent) message;
  6. //code
  7. }
  8. };
  9. // account: Tester001
  10. Identity identity = new Identity("c60a9d48105950a0cca07a4c6320b98c303ad42d694a634529e8e1a0a16fcdb5");
  11. // listen account
  12. BigInteger eventId = sdk.getEventService().listenAccount(identity, handler, EventModelType.PUSH)
  13. if (eventId.longValue() == 0) {
  14. System.out.println("listen failed");
  15. }

Subscribe to a contract

  • Function prototype
  1. public BigInteger listenContract(Identity id, IEventCallback handler, EventModelType type)
  • Request parameter
Parameter Required Type Description
id true Identity The account’s business ID.
handler true IEventCallback Performs callback when a response is received or a timeout or an error occurs.
type true EventModelType The event model type. 1 indicates PULL, and 2 indicates PUSH.
  • Response field
Response field Field type Description
result BigInteger The returned result. If the result is 0, the listening failed.

  • Example
  1. /event handler
  2. IEventCallback handler = new IEventCallback() {
  3. @Override
  4. public void onEvent(Message message) {
  5. PushContractEvent eventContractMessage = (PushContractEvent) message;
  6. // code
  7. }
  8. };
  9. Identity identity = new Identity("a7937b64b8caa58f03721bb6bacf5c78cb235febe0e70b1b84cd99541461a08e");
  10. //listen contract
  11. BigInteger result = sdk.getEventService().listenContract(identity, handler, EventModelType.PUSH)

Subscribe to a topic event

  • Function prototype
  1. public BigInteger listenTopics(List<String> topics, IEventCallback handler, VMTypeEnum vmType, EventModelType type)
  • Request parameter
Parameter Required Type Description
topics true List The list of subscribed topics.
handler true IEventCallback Performs callback when a response is received or a timeout or an error occurs.
vmType true VMTypeEnum The contract type. 1 indicates EVM, and 2 indicates WASM.
type true EventModelType The event model type. 1 indicates PULL, and 2 indicates PUSH.
  • Response field
Response field Field type Description
result BigInteger The returned result. If the result is 0, the listening failed.
  • EVM example
  1. //Evm contract
  2. contract logAndPrecomileContract {
  3. event TEST(identity id, uint a, uint b);
  4. ...
  5. function test(identity id, uint ba) public payable returns(uint) {
  6. if (!id.send(ba / 2))
  7. throw; // also reverts the transfer to Sharer
  8. emit TEST(id, ba, id.balance);
  9. return id.balance;
  10. }
  11. ...
  12. }
  1. //deploy Evm contract
  2. // evnet handler
  3. IEventCallback handler = new IEventCallback() {
  4. @Override
  5. public void onEvent(Message message) {
  6. PushTopicsEvent eventTopicsMessage = (PushTopicsEvent) message;
  7. //code
  8. }
  9. };
  10. // add contract
  11. List<String> topics = new ArrayList<>();
  12. // The system contract. An event will be returned when any contract is called.
  13. topics.add("call_contract");
  14. // The EVM contract. Calling the contract triggers an internal TEST event of the contract.
  15. IHash hashTool = HashFactory.getHash();
  16. byte[] sha256 = hashTool.hash("TEST(identity,uint256,uint256)".getBytes());
  17. topics.add(ByteUtils.toHexString(sha256));
  18. // listen topics
  19. BigInteger result = sdk.getEventService().listenTopics(topics, handler, VMTypeEnum.EVM, EventModelType.PUSH)
  20. // Calling the EVM contract triggers two topic events: call_contract and TEST.
  21. EVMParameter parameters = new EVMParameter("test(identity,uint256)");
  22. parameters.addIdentity(identity);
  23. parameters.addUint(value);
  24. CallContractRequest request = new CallContractRequest(accId, contractId, parameters, BigInteger.ZERO, VMTypeEnum.EVM);
  25. sdk.getContractService().callContract(request);
  26. // Unsubscribe from the topic.
  27. sdk.getEventService().unListenTopics(result);
  • WASM example
  1. //Wasm contract
  2. class logAndPrecomileContract : public Contract {
  3. public:
  4. INTERFACE test(const Identity& id, uint64_t ba){
  5. uint64_t balance = 0;
  6. bool ret = GetBalance(id, balance);
  7. Log(pack(id), {String2Bytes("hello"), String2Bytes("abc")});
  8. Result(pack(balance));
  9. }
  10. }
  1. //deploy Wasm contract
  2. // evnet handler
  3. IEventCallback handler = new IEventCallback() {
  4. @Override
  5. public void onEvent(Message message) {
  6. PushTopicsEvent eventTopicsMessage = (PushTopicsEvent) message;
  7. //code
  8. }
  9. };
  10. // add contract
  11. List<String> topics = new ArrayList<>();
  12. // The system contract. An event will be returned when any contract is called.
  13. topics.add("call_contract");
  14. topics.add("hello"); // Listen to a hello event.
  15. topics.add("abc"); // Listen to an abc event.
  16. // listen topics
  17. BigInteger result = sdk.getEventService().listenTopics(topics, handler, VMTypeEnum.WASM, EventModelType.PUSH)
  18. // Calling the WASM contract triggers call_contract.
  19. WASMParameter parameters = new WASMParameter("test");
  20. parameters.addIdentity(identity);
  21. parameters.addUInt64(value);
  22. CallContractRequest request = new CallContractRequest(accId, contractId, parameters, BigInteger.ZERO, VMTypeEnum.WASM);
  23. sdk.getContractService().callContract(request);
  24. // Unsubscribe from the topic.
  25. sdk.getEventService().unListenTopics(result);

Subscribe to a block event

  • Function prototype
  1. public BigInteger listenBlock(IEventCallback handler, EventModelType type)
  • Request parameter
Parameter Required Type Description
handler true IEventCallback Performs callback when a response is received or a timeout or an error occurs.
type true EventModelType The event model type. 1 indicates PULL, and 2 indicates PUSH.
  • Response field
Response field Field type Description
result BigInteger The returned result. If the result is 0, the listening failed.
  • Example
  1. // event handler
  2. IEventCallback handler = new IEventCallback() {
  3. @Override
  4. public void onEvent(Message message) {
  5. // code
  6. }
  7. };
  8. // listen block
  9. BigInteger result = sdk.getEventService().listenBlock(handler, EventModelType.PUSH)

Unsubscribe from an account event

  • Function prototype
  1. public boolean unListenAccount(BigInteger eventId)
  • Request parameter
Parameter Required Type Description
eventId true BigInteger The ID of the event to unsubscribe from.

Unsubscribe from a contract event

  • Function prototype
  1. public boolean unListenContract(BigInteger eventId)
  • Request parameter
Parameter Required Type Description
eventId true BigInteger The ID of the event to unsubscribe from.

Unsubscribe from a topic event

  • Function prototype
  1. public boolean unListenTopics(BigInteger eventId)
  • Request parameter
Parameter Required Type Description
eventId true BigInteger The ID of the event to unsubscribe from.

Unsubscribe from a block event

  • Function prototype
  1. public boolean unListenBlock(BigInteger eventId)
  • Request parameter
Parameter Required Type Description
eventId true BigInteger The ID of the event to unsubscribe from.