I have a file called defichain.js and am trying to communicate with the defichain blockchain via OCEAN REST API in my project. I am using Node.js version 16.14.2. I am running the .js script with the latest version of truffle.
I already managed to print out the WhaleApiClient object in the console to check its available functions, so at least it is recognizing the client.
Anyways, when I am trying to get the current stats of the DeFi-Chain Blockchain (e.g. number of masternodes), I get TypeError: fn is not a function.
Clearly I am not calling any function named fn here.
const { WhaleApiClient } = require('@defichain/whale-api-client')
const client = new WhaleApiClient({
url: 'https://ocean.defichain.com',
timeout: 60000,
version: 'v0',
network: 'mainnet'
});
console.log(client); // prints client object with all of its categories
console.log(client.stats.get()) // prints Promise { <pending> }
// it seems like something is wrong here :(
client.stats.get().then((data) => {
console.log(data)
})
I am expecting an output in the console something similar to:
{
"data":{
"count":{
"blocks":1831387,
"prices":97,
"tokens":146,
"masternodes":11044
},
"burned":{
"address":156009915.966001,
"fee":237127,
"auction":473292.86501403,
"payback":49335487.46135226,
"emission":82354933.12182175,
"total":288423830.22026604
},
"tvl":{
"dex":874830839.7451664,
"masternodes":956478286.6812595,
"loan":260332219.5450214,
"total":2091641345.9714475
},
"price":{
"usd":4.164032593301086,
"usdt":4.164032593301086
},
"masternodes":{
"locked":[
{
"weeks":0,
"count":7435,
"tvl":655918414.0967871
},
{
"weeks":520,
"count":2859,
"tvl":238099383.6849561
},
{
"weeks":260,
"count":750,
"tvl":62460488.89951629
}
]
},
"loan":{
"count":{
"collateralTokens":6,
"loanTokens":28,
"openAuctions":8,
"openVaults":9915,
"schemes":6
},
"value":{
"collateral":260332219.5450214,
"loan":136835351.84522375
}
},
"emission":{
"masternode":84.5329931,
"dex":64.54739497,
"community":12.45295518,
"anchor":0.05072487,
"burned":92.04027360305092,
"total":253.62434172305092
},
"net":{
"version":2070000,
"subversion":"/DeFiChain:2.7.0/",
"protocolversion":70028
},
"blockchain":{
"difficulty":22297887949.45695
}
}
}
For more information on Ocean REST API, Defichain and its Jellyfish library:
Ocean REST API is a quite unknown project and still in its early stages. Ocean REST API is a global infrastructure project hosted by DeFiChain to simplify building decentralized light applications. Powered by the Jellyfish Ecosystem, Ocean Nodes are globally distributed around the world, any API request is served by the node nearest to the requester with auto fail-over.
https://jellyfish.defichain.com/ocean/
CodePudding user response:
I solved it by myself after a long headache. The error showed following:
TypeError: fn is not a function
at Object.exec (/usr/local/lib/node_modules/truffle/build/webpack:/packages/require/require.js:127:1)
at node:internal/util:360:7
at new Promise (<anonymous>)
at bound exec (node:internal/util:346:12)
at Object.run (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/commands/exec.js:75:1)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at Command.run (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/command.js:183:1)
Looking at the first line, I thought that the problem might be in node_modules/truffle/ and has nothing to do with my rest-api calls in the first place.
I found out that truffle exec <example_script> does not properly handle require statements for local node modules in example_script and it was a common bug in 2016 (see: https://github.com/trufflesuite/truffle/issues/255)
However, deep in the discussion I found an answer from Tim Coulter
"Due to technical reasons, truffle exec in Truffle 2.0 requires your script to output a module that's passed a callback function."
In the original post, he is referring to a post which leads to the documentation site of truffle which doesn't work anymore today.
I found this link here:
https://trufflesuite.com/docs/truffle/getting-started/writing-external-scripts/
In order for external scripts to be run correctly, Truffle expects them to export a function that takes a single parameter as a callback:
module.exports = function(callback) {
// TODO: implement your actions
// invoke callback
callback();
}
You can do anything you'd like within this script, as long as the callback is invoked when the script finishes. The callback accepts an error as its first and only parameter. If an error is provided, execution will halt and the process will return a non-zero exit code.