Skip to content

Commit d797d39

Browse files
authored
feat(examples): fix typescript example (#537)
1 parent 9c22599 commit d797d39

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

examples/typescript/index.ts

+27-12
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,28 @@ import * as fs from 'fs';
33
import * as os from 'os';
44
import * as path from 'path';
55

6-
interface DecodeArgsOptions {
7-
target: string;
8-
rpc_url?: string;
9-
default?: boolean;
10-
skip_resolving?: boolean;
11-
raw?: boolean;
6+
class DecodeResult {
7+
public name: string;
8+
public signature: string;
9+
public inputs: any[];
10+
public decoded_inputs: any[];
11+
12+
constructor(path: string) {
13+
// walk the directory and find the json file
14+
const files = fs.readdirSync(path);
15+
const jsonFile = files.find((file) => file.endsWith('.json'));
16+
if (!jsonFile) {
17+
throw new Error('`decoded.json` not found');
18+
}
19+
20+
const data = JSON.parse(fs.readFileSync
21+
(path + '/' + jsonFile, 'utf8'));
22+
23+
this.name = data.name;
24+
this.signature = data.signature;
25+
this.inputs = data.inputs;
26+
this.decoded_inputs = data.decoded_inputs;
27+
}
1228
}
1329

1430
class DecodeArgs {
@@ -44,7 +60,7 @@ class Decoder {
4460
try {
4561
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'decoder-'));
4662

47-
const command = ['decode', this.args.target, ];
63+
const command = ['decode', this.args.target, "--output", tempDir];
4864

4965
if (this.args.rpc_url) {
5066
command.push('--rpc-url', this.args.rpc_url);
@@ -62,9 +78,8 @@ class Decoder {
6278
// Execute heimdall command
6379
execSync(`heimdall ${command.join(' ')}`, { stdio: 'inherit' });
6480

65-
// Here you would read and parse the output from `tempDir`
66-
// For now, we return null since the original code doesn't show the parsing step.
67-
return null;
81+
let result = new DecodeResult(tempDir);
82+
return result
6883
} catch (e) {
6984
console.error("Error: ", e);
7085
return null;
@@ -89,15 +104,15 @@ function main() {
89104
}
90105

91106
const args = new DecodeArgs(
92-
"0x000000000000000000000000008dfede2ef0e61578c3bba84a7ed4b9d25795c30000000000000000000000000000000000000001431e0fae6d7217caa00000000000000000000000000000000000000000000000000000000000000000002710fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc7c0000000000000000000000000000000000000000000000a6af776004abf4e612ad000000000000000000000000000000000000000000000000000000012a05f20000000000000000000000000000000000000000000000000000000000000111700000000000000000000000001c5f545f5b46f76e440fa02dabf88fdc0b10851a00000000000000000000000000000000000000000000000000000002540be400",
107+
"0xc47f00270000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000b6a6265636b65722e657468000000000000000000000000000000000000000000",
93108
"",
94109
false,
95110
false,
96111
true
97112
);
98113

99114
const decoded = new Decoder(args).decode();
100-
console.log("Decoded Result:", decoded);
115+
console.log(decoded);
101116
}
102117

103118
main();

0 commit comments

Comments
 (0)