0

Here's my current issue...

I have a function in a Solidity smart contract that takes two arguments:

function safeMint(address _to, string memory _uri)
        public
    {
        // do things here
    }

I'm developing a JavaScript Object for my FrontEnd to deal with it. The object uses ethers.js to connect to the smart contract and call functions. I want to be able to pass the object the function name and arguments, so that the object makes the calls and returns any needed thing. My reading function is currently working:

/**
     * @function callFunction - Makes a read-only call to the smart contract's specified function
     * @param {string} _functionName - the name of the function to be called 
     * @param  {...any} _args the arguments to be passed on to the function
     * @returns result of the function call
     */
    async callFunction(_functionName, ..._args) {
        if(this.#readerContract != null && this.#readerContract != undefined)
            return await this.#readerContract.functions[_functionName](_args);
    }

    /**
     * @function sendFunction - Makes a full call to the smart contract's specified function
     * @param {string} _functionName - the name of the function to be called 
     * @param  {...any} _args the arguments to be passed on to the function
     * @returns the promise of the called function
     */sendFunction(_functionName, ..._args) {
        if(this.#fullContract != null && this.#fullContract != undefined)
            return this.#fullContract.functions[_functionName](_args);
    }

when I use it with the following code:

result = await smartPlug.callFunction("getTokenCap");

How do I make a call to a smart contract method using arguments? I have tried the following:

result = await sp.sendFunction("setUserName", ebUserName.value);

But I get an error that says: "Paused on promise rejection - TypeError: str.charCodeAt is not a function" running it in Google Chrome.

2
  • The questions is unrelated to Ethereum it is about using javascript call or apply: stackoverflow.com/questions/8659390/….
    – Ismael
    Oct 12, 2022 at 3:51
  • Thanks! Your answer led me to some searching in the web, that took me to an example of using JavaScript ES6 way to do exactly what I want: send an array as arguments to a function using the spread notation: return this.#fullContract.functions[_functionName](..._args); I will test it now, and return soon with the results. Oct 13, 2022 at 12:09

1 Answer 1

0

Mr. Ismael's answer led me to find the JavaScript ES6 way to send arrays as function arguments. Then, the final code for my object methods became:

/**
     * @function callFunction - Makes a read-only call to the smart contract's specified function
     * @param {string} _functionName - the name of the function to be called 
     * @param  {...any} _args the arguments to be passed on to the function
     * @returns result of the function call
     */
    async callFunction(_functionName, ..._args) {
        if(this.#readerContract != null && this.#readerContract != undefined)
            return this.#readerContract.functions[_functionName](..._args);
    }

    /**
     * @function sendFunction - Makes a full call to the smart contract's specified function
     * @param {string} _functionName - the name of the function to be called 
     * @param  {...any} _args the arguments to be passed on to the function
     * @returns the promise of the called function
     */sendFunction(_functionName, ..._args) {
        if(this.#fullContract != null && this.#fullContract != undefined)
            return this.#fullContract.functions[_functionName](..._args);
    }

This will be all...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.