-
Notifications
You must be signed in to change notification settings - Fork 652
/
parseSequenceFromLog.ts
195 lines (181 loc) · 5.7 KB
/
parseSequenceFromLog.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { TransactionResponse } from "@solana/web3.js";
import { TxInfo } from "@terra-money/terra.js";
import { TxInfo as XplaTxInfo } from "@xpla/xpla.js";
import { AptosClient, Types } from "aptos";
import { BigNumber, ContractReceipt } from "ethers";
import { FinalExecutionOutcome } from "near-api-js/lib/providers";
import { Implementation__factory } from "../ethers-contracts";
import { SuiTransactionBlockResponse } from "@mysten/sui.js";
export function parseSequenceFromLogEth(
receipt: ContractReceipt,
bridgeAddress: string
): string {
// TODO: dangerous!(?)
const bridgeLog = receipt.logs.filter((l) => {
return l.address === bridgeAddress;
})[0];
const {
args: { sequence },
} = Implementation__factory.createInterface().parseLog(bridgeLog);
return sequence.toString();
}
export function parseSequencesFromLogEth(
receipt: ContractReceipt,
bridgeAddress: string
): string[] {
// TODO: dangerous!(?)
const bridgeLogs = receipt.logs.filter((l) => {
return l.address === bridgeAddress;
});
return bridgeLogs.map((bridgeLog) => {
const {
args: { sequence },
} = Implementation__factory.createInterface().parseLog(bridgeLog);
return sequence.toString();
});
}
export function parseSequenceFromLogTerra(info: TxInfo): string {
// Scan for the Sequence attribute in all the outputs of the transaction.
// TODO: Make this not horrible.
let sequence = "";
const jsonLog = JSON.parse(info.raw_log);
jsonLog.map((row: any) => {
row.events.map((event: any) => {
event.attributes.map((attribute: any) => {
if (attribute.key === "message.sequence") {
sequence = attribute.value;
}
});
});
});
return sequence.toString();
}
export function parseSequenceFromLogXpla(info: XplaTxInfo): string {
// Scan for the Sequence attribute in all the outputs of the transaction.
// TODO: Make this not horrible.
let sequence = "";
const jsonLog = JSON.parse(info.raw_log);
jsonLog.map((row: any) => {
row.events.map((event: any) => {
event.attributes.map((attribute: any) => {
if (attribute.key === "message.sequence") {
sequence = attribute.value;
}
});
});
});
return sequence.toString();
}
export function parseSequencesFromLogTerra(info: TxInfo): string[] {
// Scan for the Sequence attribute in all the outputs of the transaction.
// TODO: Make this not horrible.
const sequences: string[] = [];
const jsonLog = JSON.parse(info.raw_log);
jsonLog.map((row: any) => {
row.events.map((event: any) => {
event.attributes.map((attribute: any) => {
if (attribute.key === "message.sequence") {
sequences.push(attribute.value.toString());
}
});
});
});
return sequences;
}
export function parseSequenceFromLogInjective(info: any): string {
// Scan for the Sequence attribute in all the outputs of the transaction.
let sequence = "";
const jsonLog = JSON.parse(info.rawLog);
jsonLog.map((row: any) => {
row.events.map((event: any) => {
event.attributes.map((attribute: any) => {
if (attribute.key === "message.sequence") {
sequence = attribute.value;
}
});
});
});
return sequence.toString();
}
const SOLANA_SEQ_LOG = "Program log: Sequence: ";
export function parseSequenceFromLogSolana(info: TransactionResponse) {
// TODO: better parsing, safer
const sequence = info.meta?.logMessages
?.filter((msg) => msg.startsWith(SOLANA_SEQ_LOG))?.[0]
?.replace(SOLANA_SEQ_LOG, "");
if (!sequence) {
throw new Error("sequence not found");
}
return sequence.toString();
}
export function parseSequencesFromLogSolana(info: TransactionResponse) {
// TODO: better parsing, safer
return info.meta?.logMessages
?.filter((msg) => msg.startsWith(SOLANA_SEQ_LOG))
.map((msg) => msg.replace(SOLANA_SEQ_LOG, ""));
}
export function parseSequenceFromLogAlgorand(
result: Record<string, any>
): string {
let sequence = "";
if (result["inner-txns"]) {
const innerTxns: [] = result["inner-txns"];
class iTxn {
"local-state-delta": [[Object]];
logs: Buffer[] | undefined;
"pool-error": string;
txn: { txn: [Object] } | undefined;
}
innerTxns.forEach((txn: iTxn) => {
if (txn.logs) {
sequence = BigNumber.from(txn.logs[0].slice(0, 8)).toString();
}
});
}
return sequence;
}
const NEAR_EVENT_PREFIX = "EVENT_JSON:";
export function parseSequenceFromLogNear(
outcome: FinalExecutionOutcome
): string | null {
for (const o of outcome.receipts_outcome) {
for (const l of o.outcome.logs) {
if (l.startsWith(NEAR_EVENT_PREFIX)) {
const body = JSON.parse(l.slice(NEAR_EVENT_PREFIX.length));
if (body.standard === "wormhole" && body.event === "publish") {
return body.seq.toString();
}
}
}
}
return null;
}
/**
* Given a transaction result, return the first WormholeMessage event sequence
* @param coreBridgeAddress Wormhole Core bridge address
* @param result the result of client.waitForTransactionWithResult(txHash)
* @returns sequence
*/
export function parseSequenceFromLogAptos(
coreBridgeAddress: string,
result: Types.UserTransaction
): string | null {
if (result.success) {
const event = result.events.find(
(e) => e.type === `${coreBridgeAddress}::state::WormholeMessage`
);
return event?.data.sequence || null;
}
return null;
}
export function parseSequenceFromLogSui(
originalCoreBridgePackageId: string,
response: SuiTransactionBlockResponse
): string | null {
const event = response.events?.find(
(e) =>
e.type ===
`${originalCoreBridgePackageId}::publish_message::WormholeMessage`
);
return event?.parsedJson?.sequence || null;
}