使用“@chainlink/functions-toolkit”运行 js 源代码“npm runsimulate”时出现错误

问题描述 投票:0回答:1

我一直在尝试在我的 JS 代码中实现 Chainlink Functions Toolkit。但是,运行此命令后:

npm run simulate

我收到以下错误:

secp256k1 unavailable, reverting to browser version
undefined
syntax error, RAM exceeded, or other error
error: Module not found "file:///tmp/FunctionsScript-1715359176579.ts"

这是我在代码中使用的文件:

swa-func.js

const { 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}`);

函数-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

函数模拟脚本.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"
  }
}

如何让它与 chainlink 函数工具包一起使用?

P.S.,API 工作正常,因为它在我运行时给出了预期的输出:

node swa-example.js

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()

编辑:我想提供存储库的链接:https://github.com/thoroughProgrammer/chainlink_functions_example_1

javascript deno chainlink
1个回答
1
投票

在 swa-func.js 文件中,使用 ethers

 库访问 
AbiCoder.defaultAbiCoder()
getBytes() 函数的方式。在 ethers v6 的情况下,它的工作原理如下。因此,您可以在第一行中将导入更改为 v6:

const { ethers } = await import('npm:[email protected]');
此外,您还可以使用 

characterId

height
parseInt() 值解析为 integer
,并在编码时使用 
uint256
,从而使编码后的响应适合于 
256 字节

这是修改后的代码:

const { 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 = parseInt(res.data.height); console.log(characterId) console.log(character) console.log(height) const encoded = abiCoder.encode([`uint256`, `string`, `uint256`], [parseInt(characterId), character, height]); console.log(encoded) return ethers.getBytes(encoded);


如果您想使用

ethers v5,那么代码如下:

const { ethers } = await import('npm:[email protected]'); const abiCoder = ethers.utils.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 = parseInt(res.data.height); console.log(characterId) console.log(character) console.log(height) const encoded = abiCoder.encode([`uint256`, `string`, `uint256`], [parseInt(characterId), character, height]); console.log(encoded) return ethers.utils.arrayify(encoded);
    
© www.soinside.com 2019 - 2024. All rights reserved.