-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
146 lines (130 loc) · 3.91 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { Address, toNano, Cell} from "@ton/core";
export const randomAddress = (wc: number = 0) => {
const buf = Buffer.alloc(32);
for (let i = 0; i < buf.length; i++) {
buf[i] = Math.floor(Math.random() * 256);
}
return new Address(wc, buf);
};
export const differentAddress = (old: Address) => {
let newAddr: Address;
do {
newAddr = randomAddress(old.workChain);
} while(newAddr.equals(old));
return newAddr;
}
const getRandom = (min:number, max:number) => {
return Math.random() * (max - min) + min;
}
export const getRandomInt = (min: number, max: number) => {
return Math.round(getRandom(min, max));
}
export const getRandomTon = (min:number, max:number): bigint => {
return toNano(getRandom(min, max).toFixed(9));
}
export type InternalTransfer = {
from: Address | null,
response: Address | null,
amount: bigint,
forwardAmount: bigint,
payload: Cell | null
};
export type JettonTransfer = {
to: Address,
response_address: Address | null,
amount: bigint,
custom_payload: Cell | null,
forward_amount: bigint,
forward_payload: Cell | null
}
export const parseTransfer = (body: Cell) => {
const ts = body.beginParse().skip(64 + 32);
return {
amount: ts.loadCoins(),
to: ts.loadAddress(),
response_address: ts.loadAddressAny(),
custom_payload: ts.loadMaybeRef(),
forward_amount: ts.loadCoins(),
forward_payload: ts.loadMaybeRef()
}
}
export const parseInternalTransfer = (body: Cell) => {
const ts = body.beginParse().skip(64 + 32);
return {
amount: ts.loadCoins(),
from: ts.loadAddressAny(),
response: ts.loadAddressAny(),
forwardAmount: ts.loadCoins(),
payload: ts.loadMaybeRef()
};
};
type JettonTransferNotification = {
amount: bigint,
from: Address | null,
payload: Cell | null
}
export const parseTransferNotification = (body: Cell) => {
const bs = body.beginParse().skip(64 + 32);
return {
amount: bs.loadCoins(),
from: bs.loadAddressAny(),
payload: bs.loadMaybeRef()
}
}
type JettonBurnNotification = {
amount: bigint,
from: Address,
response_address: Address | null,
}
export const parseBurnNotification = (body: Cell) => {
const ds = body.beginParse().skip(64 + 32);
const res = {
amount: ds.loadCoins(),
from: ds.loadAddress(),
response_address: ds.loadAddressAny(),
};
return res;
}
const testPartial = (cmp: any, match: any) => {
for (let key in match) {
if(!(key in cmp)) {
throw Error(`Unknown key ${key} in ${cmp}`);
}
if(match[key] instanceof Address) {
if(!(cmp[key] instanceof Address)) {
return false
}
if(!(match[key] as Address).equals(cmp[key])) {
return false
}
}
else if(match[key] instanceof Cell) {
if(!(cmp[key] instanceof Cell)) {
return false;
}
if(!(match[key] as Cell).equals(cmp[key])) {
return false;
}
}
else if(match[key] !== cmp[key]){
return false;
}
}
return true;
}
export const testJettonBurnNotification = (body: Cell, match: Partial<JettonBurnNotification>) => {
const res= parseBurnNotification(body);
return testPartial(res, match);
}
export const testJettonTransfer = (body: Cell, match: Partial<JettonTransfer>) => {
const res = parseTransfer(body);
return testPartial(res, match);
}
export const testJettonInternalTransfer = (body: Cell, match: Partial<InternalTransfer>) => {
const res = parseInternalTransfer(body);
return testPartial(res, match);
};
export const testJettonNotification = (body: Cell, match: Partial<JettonTransferNotification>) => {
const res = parseTransferNotification(body);
return testPartial(res, match);
}