Skip to content

Commit 7d521ab

Browse files
committed
feat: contract helper to extract event
1 parent 7af5632 commit 7d521ab

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/contract-helper/index.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Slice } from '@ton/core'
2+
3+
const CHANGE_NUMBER_NOTIFICATION_EVENT_CODE = 0x5c690c2f
4+
5+
6+
7+
const hexToString = (hexString: string): string => {
8+
const hex = hexString.toString()
9+
let resultString = ''
10+
for (let i = 0; i < hex.length; i += 2) {
11+
resultString += String.fromCharCode(parseInt(hex.slice(i, i + 2), 16))
12+
}
13+
return resultString
14+
}
15+
16+
export const extractEventFromMessage = (message: Slice): { event: number } | { error: string } => {
17+
const operationCode = message.loadUint(32)
18+
let event = 0
19+
20+
if (operationCode === CHANGE_NUMBER_NOTIFICATION_EVENT_CODE) {
21+
const eventString = hexToString(message.loadUintBig(13 * 8).toString(16)!)
22+
if (eventString === 'ChangedNumber') {
23+
event = message.loadUint(8)
24+
} else {
25+
return { error: 'Unsupported slice format' }
26+
}
27+
}
28+
return { event }
29+
}

0 commit comments

Comments
 (0)