(solved) Http request failed with status code 400 when calling contract function

Hey guys,

I’m trying to compile, deploy and call functions from my working sophia contract,
following this guide - https://github.com/aeternity/aepp-sdk-js/blob/develop/docs/examples/node/aecontract.md

I created a dummy_func just to test a simple function call with a returned sum value,
and I’m trying to call it from index.js

you can find my full code here - Hash-Time-Lock/index.js at master · axiomatic-aardvark/Hash-Time-Lock · GitHub

The contract compiles and I get the bytecode. But after that it crashes with:

Http request for http://localhost:3080/encode-calldata failed with status code 400. Status: Bad Request.
Error data: {“info”:{“error”:“wrong_type”,“data”:{},“path”:[“arguments”]},“parameter”:“body”,“reason”:“validation_error”}

Maybe I’m messing something up when passing the initial state values.

Thoughts?
Thanks in advance :sweat_smile:

@philipp.chain maybe I’m missing something simple?

I would suggest you use the newer js-sdk syntax for this, e.g.

const fs = require("fs");
const Universal = require("@aeternity/aepp-sdk").Universal;

const main = async () => {
    const client = await Universal({
        url: 'https://sdk-testnet.aepps.com/',
        internalUrl: 'https://sdk-testnet.aepps.com/',
        compilerUrl: 'https://compiler.aepps.com',
        keypair: {
            secretKey: '',
            publicKey: ''
        }
    });

    let contractSource = fs.readFileSync('./contracts/HashTimeLock.aes', 'utf-8');
    const contract = await client.getContractInstance(contractSource);
    await contract.deploy(initarg);

    const result = await contract.methods.dummy_func(2);
    console.log(result)
};

main();
2 Likes

Thank you, seems like a better syntax for sure.

But now I get this, both with the compiler/node that you gave me and with my local ones :confused:

(node:8155) UnhandledPromiseRejectionWarning: Error: Can not get node info. Node is not connected
at Object.getNodeInfo (/home/arthas/work/contracts/sophia-docs/htl-project/node_modules/@Albert/aepp-sdk/dist/aepp-sdk.js:1:110930)
at Object.getVmVersion (/home/arthas/work/contracts/sophia-docs/htl-project/node_modules/@Albert/aepp-sdk/dist/aepp-sdk.js:1:144131)
at Object. (/home/arthas/work/contracts/sophia-docs/htl-project/node_modules/@Albert/aepp-sdk/dist/aepp-sdk.js:1:132507)
at u (/home/arthas/work/contracts/sophia-docs/htl-project/node_modules/@Albert/aepp-sdk/dist/aepp-sdk.js:1:56480)
at Generator._invoke (/home/arthas/work/contracts/sophia-docs/htl-project/node_modules/@Albert/aepp-sdk/dist/aepp-sdk.js:1:56268)
at Generator.O.forEach.t.(anonymous function) [as next] (/home/arthas/work/contracts/sophia-docs/htl-project/node_modules/@Albert/aepp-sdk/dist/aepp-sdk.js:1:56903)
at n (/home/arthas/work/contracts/sophia-docs/htl-project/node_modules/@Albert/aepp-sdk/dist/aepp-sdk.js:1:2435)
at c (/home/arthas/work/contracts/sophia-docs/htl-project/node_modules/@Albert/aepp-sdk/dist/aepp-sdk.js:1:2646)
at /home/arthas/work/contracts/sophia-docs/htl-project/node_modules/@Albert/aepp-sdk/dist/aepp-sdk.js:1:2705
at new Promise ()
warning.js:18
(node:8155) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
warning.js:18
(node:8155) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

1 Like

Sorry there was an issue in my example, it is url instead of host

1 Like

Still can’t get it to work :frowning: maybe I’m putting this wrong:

  await contract.deploy( {contracts:{}} );

I also tried it like this

  await contract.deploy({});

I’m not really sure what I have to pass in, I’m pretty sure that the error is because of it

(node:11714) UnhandledPromiseRejectionWarning: ValidationError: “Argument” must be an array
at Object.exports.process (/home/arthas/work/contracts/sophia-docs/htl-project/node_modules/joi-browser/dist/joi-browser.js:4011:17)
at _class._validateWithOptions (/home/arthas/work/contracts/sophia-docs/htl-project/node_modules/joi-browser/dist/joi-browser.js:1700:29)
at _class.root.validate (/home/arthas/work/contracts/sophia-docs/htl-project/node_modules/joi-browser/dist/joi-browser.js:4552:23)
at Ua (/home/arthas/work/contracts/sophia-docs/htl-project/node_modules/@Albert/aepp-sdk/dist/aepp-sdk.js:1:175303)
at /home/arthas/work/contracts/sophia-docs/htl-project/node_modules/@Albert/aepp-sdk/dist/aepp-sdk.js:1:178383
at u (/home/arthas/work/contracts/sophia-docs/htl-project/node_modules/@Albert/aepp-sdk/dist/aepp-sdk.js:1:56480)
at Generator._invoke (/home/arthas/work/contracts/sophia-docs/htl-project/node_modules/@Albert/aepp-sdk/dist/aepp-sdk.js:1:56268)
at Generator.O.forEach.t.(anonymous function) [as next] (/home/arthas/work/contracts/sophia-docs/htl-project/node_modules/@Albert/aepp-sdk/dist/aepp-sdk.js:1:56903)
at n (/home/arthas/work/contracts/sophia-docs/htl-project/node_modules/@Albert/aepp-sdk/dist/aepp-sdk.js:1:2435)
at c (/home/arthas/work/contracts/sophia-docs/htl-project/node_modules/@Albert/aepp-sdk/dist/aepp-sdk.js:1:2646)
warning.js:18
(node:11714) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
warning.js:18
(node:11714) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

1 Like

Your contract init doesn’t need any arguments, so you need to pass none, just .deploy()

2 Likes

Worked! Thanks a lot, again :grin: I’m off to test the other functions

2 Likes

In general as best practice, as status in your contract, you wouldn’t use a string, but a algebraic datatype similar to enum, so the compiler can check your status model to be correct.

Also for general style you mix a lot of camelCase and snake_case, which should be unified for readability, but thats both just personal opinions.

1 Like

Yeah I still can’t get used to snake_case and I keep messing them up :grinning:

Anyway I didn’t see an enum type in the sophia documentation thats why I reverted to string, but I’ll
check it again

Thanks for the feedback! And if you have any other recommendations on how to make the contract better I’ll be glad to hear them

here is a reference to the documentation: protocol/sophia.md at master · aeternity/protocol · GitHub

1 Like

@philipp.chain I just have a quick question about this, when using the new sdk syntax you helped me with, how do I get the txHash for the deployed contract?

I tried console.log(contract.txHash)

but it doesn’t work

just try console.log(contract) and you will see all attibutes of your contract, including the one you look for

2 Likes