All Products
Search
Document Center

Blockchain as a Service:Event interface

Last Updated:Jul 31, 2024

Subscribe to an account

  • Function prototype

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

eventId

BigInteger

The returned result. If the eventId is 0, the listening failed.

  • Example

// event handler 
IEventCallback handler = new IEventCallback() {
        @Override
        public void onEvent(Message message) {
            PushAccountEvent accountEvent = (PushAccountEvent) message;
            //code
        }
    };

// account: Tester001
Identity identity = new Identity("c60a9d48105950a0cca07a4c6320b98c303ad42d694a634529e8e1a0a16fcdb5");

// listen account
BigInteger eventId = sdk.getEventService().listenAccount(identity, handler, EventModelType.PUSH)

if (eventId.longValue() == 0) {
    System.out.println("listen failed");
}

Subscribe to a contract

  • Function prototype

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

/event handler
IEventCallback handler = new IEventCallback() {
    @Override
    public void onEvent(Message message) {
        PushContractEvent eventContractMessage = (PushContractEvent) message;
        // code
    }
};

Identity identity = new Identity("a7937b64b8caa58f03721bb6bacf5c78cb235febe0e70b1b84cd99541461a08e");

//listen contract
BigInteger result = sdk.getEventService().listenContract(identity, handler, EventModelType.PUSH)

Subscribe to a topic event

  • Function prototype

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

//Evm contract
contract logAndPrecomileContract {

    event TEST(identity id, uint a, uint b);
      ...


    function test(identity id, uint ba) public payable returns(uint) {
        if (!id.send(ba  / 2))
            throw; // also reverts the transfer to Sharer
        emit TEST(id, ba, id.balance);
        return id.balance;
    }
      ...
}
//deploy Evm contract

// evnet handler
IEventCallback handler = new IEventCallback() {
    @Override
    public void onEvent(Message message) {
        PushTopicsEvent eventTopicsMessage = (PushTopicsEvent) message;
        //code
    }
};

// add contract
List<String> topics = new ArrayList<>();
// The system contract. An event will be returned when any contract is called.
topics.add("call_contract");

// The EVM contract. Calling the contract triggers an internal TEST event of the contract.
IHash hashTool = HashFactory.getHash();
byte[] sha256 = hashTool.hash("TEST(identity,uint256,uint256)".getBytes());
topics.add(ByteUtils.toHexString(sha256));

// listen topics
BigInteger result = sdk.getEventService().listenTopics(topics, handler, VMTypeEnum.EVM, EventModelType.PUSH)

// Calling the EVM contract triggers two topic events: call_contract and TEST.
EVMParameter parameters = new EVMParameter("test(identity,uint256)");
parameters.addIdentity(identity);
parameters.addUint(value);
CallContractRequest request = new CallContractRequest(accId, contractId, parameters, BigInteger.ZERO, VMTypeEnum.EVM);
sdk.getContractService().callContract(request);

// Unsubscribe from the topic.
sdk.getEventService().unListenTopics(result);
  • WASM example

//Wasm contract
class logAndPrecomileContract : public Contract {
    public:
    INTERFACE uint64_t test(const Identity& id, uint64_t ba){
        uint64_t balance = 0;
        bool ret = GetBalance(id, balance);

        Log(id, {"hello", "abc"});
        return balance;
    }
}
//deploy Wasm contract

// evnet handler
IEventCallback handler = new IEventCallback() {
    @Override
    public void onEvent(Message message) {
        PushTopicsEvent eventTopicsMessage = (PushTopicsEvent) message;
        //code
    }
};

// add contract
List<String> topics = new ArrayList<>();
// The system contract. An event will be returned when any contract is called.
topics.add("call_contract");
topics.add("hello"); // Listen to a hello event.
topics.add("abc");   // Listen to an abc event.

// listen topics
BigInteger result = sdk.getEventService().listenTopics(topics, handler, VMTypeEnum.WASM, EventModelType.PUSH)

// Calling the WASM contract triggers call_contract.
WASMParameter parameters = new WASMParameter("test");
parameters.addIdentity(identity);
parameters.addUInt64(value);
CallContractRequest request = new CallContractRequest(accId, contractId, parameters, BigInteger.ZERO, VMTypeEnum.WASM);
sdk.getContractService().callContract(request);

// Unsubscribe from the topic.
sdk.getEventService().unListenTopics(result);

Subscribe to a block event

  • Function prototype

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

// event handler
IEventCallback handler = new IEventCallback() {
    @Override
    public void onEvent(Message message) {
        // code
    }
};
// listen block
BigInteger result = sdk.getEventService().listenBlock(handler, EventModelType.PUSH)

Unsubscribe from an account event

  • Function prototype

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

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

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

public boolean unListenBlock(BigInteger eventId)
  • Request parameter

Parameter

Required

Type

Description

eventId

true

BigInteger

The ID of the event to unsubscribe from.