forked from ceptor-club/blockmagic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulate.ts
36 lines (32 loc) · 1.39 KB
/
simulate.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
import { task } from "hardhat/config";
import { decodeResult, ReturnType, simulateScript } from "@chainlink/functions-toolkit";
import { ethers } from "ethers@v5";
import { loadScript } from "@/functions";
import { getSecretByName } from "@/secrets";
task("simulate", "Simulate an oracle function call")
.addParam("name", "The function to call")
.addOptionalVariadicPositionalParam("args", "The arguments to pass to the function")
.setAction(async params => {
const { name, args } = params;
console.log("Simulating %s with args %s", name, args);
const { source } = await loadScript(name);
const { secrets } = await getSecretByName(name);
const response = await simulateScript({
source,
args,
bytesArgs: [], // bytesArgs - arguments can be encoded off-chain to bytes.
secrets,
});
console.log("Simulation result", response);
const errorString = response.errorString;
if (errorString) {
console.log(`❌ Error during simulation: `, errorString);
} else {
const returnType = ReturnType.string;
const responseBytesHexstring = response.responseBytesHexstring;
if (responseBytesHexstring && ethers.utils.arrayify(responseBytesHexstring).length > 0) {
const decodedResponse = decodeResult(responseBytesHexstring, returnType);
console.log(`✅ Decoded response to ${returnType}: `, decodedResponse);
}
}
});