文档

事件接口

更新时间:

订阅账户

  • 函数原型

public BigInteger listenAccount(Identity id, IEventCallback handler, EventModelType type)
  • 请求参数

参数

必选

类型

说明

id

true

Identity

账户的业务 ID

handler

true

IEventCallback

当收到响应、超时或发生错误时回调。

type

true

EventModelType

事件模型类型。1:PULL,2:PUSH

  • 返回字段

返回字段

字段类型

说明

eventId

BigInteger

返回结果。如果 eventId 等于 0,则表示监听失败。

  • 示例

// 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");
}

订阅合约

  • 函数原型

public BigInteger listenContract(Identity id, IEventCallback handler, EventModelType type)
  • 请求参数

参数

必选

类型

说明

id

true

Identity

账户的业务 ID

handler

true

IEventCallback

当收到响应、超时或发生错误时回调。

type

true

EventModelType

事件模型类型。1:PULL,2:PUSH

  • 返回字段

返回字段

字段类型

说明

result

BigInteger

返回结果。如果 result 等于 0,则表示监听失败。

  • 使用样例

/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)

订阅主题事件

  • 函数原型

public BigInteger listenTopics(List<String> topics, IEventCallback handler, VMTypeEnum vmType, EventModelType type)
  • 请求参数

参数

必选

类型

说明

topics

true

List

订阅 Topic 列表

handler

true

IEventCallback

当收到响应、超时或发生错误时回调。

vmType

true

VMTypeEnum

合约类型。1:EVM,2:WASM

type

true

EventModelType

事件模型类型。1:PULL;2:PUSH

  • 返回字段

返回字段

字段类型

说明

result

BigInteger

返回结果。如果 result 等于 0,则表示监听失败。

  • EVM 示例

//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<>();
//系统合约,任意调用一个合约都会返回事件
topics.add("call_contract");

//EVM 合约,调用会触发合约内部 TEST 事件的方法
topics.add("TEST(identity,uint256,uint256)");

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

//调用 EVM 的合约,将触发两个 topic,一个 call_contract 的事件,一个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);

//结束后取消订阅主题
sdk.getEventService().unListenTopics(result);
  • WASM 示例

//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<>();
//系统合约,任意调用一个合约都会返回事件
topics.add("call_contract");
topics.add("hello"); //监听 hello 为主题的事件
topics.add("abc");   //监听 abc 为主题的事件

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

//调用 WASM,将触发 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);

//结束后取消订阅主题
sdk.getEventService().unListenTopics(result);

订阅区块事件

  • 函数原型

public BigInteger listenBlock(IEventCallback handler, EventModelType type)
  • 请求参数

参数

必选

类型

说明

handler

true

IEventCallback

当收到响应、超时或发生错误时回调。

type

true

EventModelType

事件模型类型。1:PULL,2:PUSH

  • 返回字段

返回字段

字段类型

说明

result

BigInteger

返回结果。如果 result 等于 0,则表示监听失败。

  • 示例

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

取消订阅账户事件

  • 函数原型

public boolean unListenAccount(BigInteger eventId)
  • 请求参数

参数

必选

类型

说明

eventId

true

BigInteger

订阅的事件 ID

取消订阅合约事件

  • 函数原型

public boolean unListenContract(BigInteger eventId)
  • 请求参数

参数

必选

类型

说明

eventId

true

BigInteger

订阅的事件 ID

取消订阅主题事件

  • 函数原型

public boolean unListenTopics(BigInteger eventId)
  • 请求参数

参数

必选

类型

说明

eventId

true

BigInteger

订阅的事件 ID

取消订阅区块事件

  • 函数原型

public boolean unListenBlock(BigInteger eventId)
  • 请求参数

参数

必选

类型

说明

eventId

true

BigInteger

订阅的事件 ID

  • 本页导读 (0)
文档反馈