Subscribe에서 특정 이벤트만 읽어오거나 전체 이벤트 중에 특정 이벤트만 확인하는 방법

안녕하세요

Disposable subscribe(String eventName, EventFilterOptions paramsOption, Consumer callback)

만약 이벤트명이 a~j인 10개의 이벤트가 있고 그 중에 a~e 이벤트 5개만 subscribe하고 싶다면 어떻게 해야되나요…?

contract.subscribe(“allEvents”, new EventFilterOptions(), callback → {});
을 이용해서 전체 이벤트를 subscribe할 수 있던데 callback 된거로 이벤트명 알 수 있는 방법을 모르겠어서 구분을 할 수 가 없더라고요

안녕하세요

일부의 이벤트만 subscribe 하고 싶은 경우에는 따로따로 이벤트 별로 .subscribe 함수를 호출하는 방법이 있습니다. 만약 callback에서 일부 이벤트만 이벤트 이름으로 구분하고 싶은 경우에는 callback으로 전달되는 객체에 이벤트 이름이 저장되어 있는 event 필드가 있습니다.

아래는 간단한 이벤트를 발생시켜서 callback 에서 이를 출력한 내용이고, 출력된 내용에서 event 필드가 있는 것을 확인하실 수 있습니다.

const subscription = contract.subscribe('callevent', (error, data) => {
        console.log(data)
})

// console.log output
{
  address: '0xE6A480c067DE0C666FC71d34b71F6677b02C272f',
  blockNumber: 17,
  transactionHash: '0xb8341369c8e48971e6f65b3de1c69d5956dc9b8a45789a9e608e7a54df9c9c0f',
  transactionIndex: 0,
  blockHash: '0x5dea1ddd2c664ae08a83a0376dd807cfd70d9900bd415d6e6b13dd1c79c1432c',
  logIndex: 0,
  id: 'log_25af752a',
  returnValues: Result {
    '0': '0xcA7A99380131e6C76cfa622396347107aeEDCA2D',
    who: '0xcA7A99380131e6C76cfa622396347107aeEDCA2D'
  },
  event: 'callevent',
  signature: '0x90a042becc42ba1b13a5d545701bf5ceff20b24d9e5cc63b67f96ef814d80f09',
  raw: {
    data: '0x000000000000000000000000ca7a99380131e6c76cfa622396347107aeedca2d',
    topics: [
      '0x90a042becc42ba1b13a5d545701bf5ceff20b24d9e5cc63b67f96ef814d80f09'
    ]
  }
}
1 Like