After running command: npm simulate run, I get the following error:
simulate
node ./Functions-simulate-script.js
secp256k1 unavailable, reverting to browser version
undefined
syntax error, RAM exceeded, or other error
error: Module not found “file:///tmp/FunctionsScript-1715359176579.ts”.
My code is:
swa-func.js
tconst { ethers } = await import('npm:[email protected]');
const abiCoder = ethers.AbiCoder.defaultAbiCoder();
const characterId = args[0];
const url = `https://swapi.dev/api/people/${characterId}/`;
const req = Functions.makeHttpRequest({
url,
})
const res = await req;
if (res.error) {
console.log(res.error);
throw Error("Request failed");
}
console.log("res data available")
console.log("response", res.data)
const character = res.data.name;
const height = res.data.height;
console.log(characterId)
console.log(character)
console.log(height)
const encoded = abiCoder.encode([`string`, `string`, `string`], [characterId, character, height]);
return ethers.getBytes(encoded);
// return Functions.encodeString(`${character} ${height}`);
Functions-request-config.js
const fs = require("fs")
// const { Location, ReturnType, CodeLanguage } = require("@chainlink/functions-toolkit")
// Loads environment variables from .env.enc file (if it exists)
require("@chainlink/env-enc").config()
const Location = {
Inline: 0,
Remote: 1,
}
const CodeLanguage = {
JavaScript: 0,
}
const ReturnType = {
uint: "uint256",
uint256: "uint256",
int: "int256",
int256: "int256",
string: "string",
bytes: "Buffer",
Buffer: "Buffer",
}
// Configure the request by setting the fields below
const requestConfig = {
// String containing the source code to be executed
source: fs.readFileSync("./swa-func.js").toString(),
//source: fs.readFileSync("./API-request-example.js").toString(),
// Location of source code (only Inline is currently supported)
codeLocation: Location.Inline,
// Optional. Secrets can be accessed within the source code with `secrets.varName` (ie: secrets.apiKey). The secrets object can only contain string values.
secrets: {},
// Optional if secrets are expected in the sourceLocation of secrets (only Remote or DONHosted is supported)
// secretsLocation: Location.DONHosted,
// Args (string only array) can be accessed within the source code with `args[index]` (ie: args[0]).
args: ["2"],
// Code language (only JavaScript is currently supported)
codeLanguage: CodeLanguage.JavaScript,
// Expected type of the returned value
expectedReturnType: ReturnType.bytes,
// expectedReturnType: ReturnType.string,
// Redundant URLs which point to encrypted off-chain secrets
secretsURLs: [],
}
module.exports = requestConfig
Function-simulate-script.js
const { simulateScript } = require("@chainlink/functions-toolkit");
const requestConfig = require('./Functions-request-config');
async function main() {
const { responseBytesHexstring, capturedTerminalOutput, errorString } = await simulateScript(requestConfig);
console.log(responseBytesHexstring);
console.log(errorString)
console.log(capturedTerminalOutput);
}
// node Functions-simulate-script.js
main();
package.json
{
"scripts": {
"simulate": "node ./Functions-simulate-script.js"
},
"dependencies": {
"@chainlink/env-enc": "^1.0.5",
"@chainlink/functions-toolkit": "^0.2.8",
"secp256k1": "^5.0.0"
}
}
How do I get it to work with chainlink functions toolkit @chainlink. It works fine when I run node swa-example.js
const characterId = “2”;
async function main() {
const url = https://swapi.dev/api/people/${characterId}/
;
const res = await fetch(url);
const json = await res.json();
console.log(json);
}
main()
thoroughProgrammer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.