Call a smart contract function with privateKey

trying to call a smart contract function
expect the call to be signed without metamasks, but:

  1. metamask pops up and offers to sign the transaction
  2. when I try to sign I get the following error:
    <<MetaMask - RPC Error: [ethjs-query] while formatting outputs from RPC ‘{“value”:{“code”:-32603,“data”:{“code”:-32000,“message”:“transaction underpriced”}}}’ >>

what am I doing wrong?

code:

document.getElementById(“buy”).addEventListener(“click”, async function() {

const web3 = await Moralis.enableWeb3({ privateKey: "0058233e........e619e3f4795a" });

const contract  = "0x1b02dA8Cb0d097eB8D57A175b88c7D8b47997506"; // Sushiswap router  
const USDC      = "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174"; 
const USDT      = "0xc2132D05D31c914a87C6611C10748AEb04B58e8F"; 
let masterAdres = "0x8/////////////////8904";

const ABI = [................]; 
const options = { 
    contractAddress: contract, 
    functionName: "swapExactTokensForTokens", 
    abi: ABI, 
    params: { 
        amountIn:       1000000, 
        amountOutMin:   1, 
        path:           [USDC, USDT], 
        to:             masterAdres, 
        deadline:       Date.now() + 1000 * 60 * 5 
    }, 
}; 
try { 
    const res = await Moralis.executeFunction(options);  
    console.log("result", res); 
} 
catch (e) { 
    console.log(e);  
} 

});

you may have to do that from node js, you can look at this example:

[quote=“How to use Moralis.transfer from nodejs [SOLVED], post:1, topic:11871”]
moralisSecret
[/quote]

what is “moralisSecret”? is it “CLI API Secret” from server details?

no, it is something else that you can find in your account settings in admin interface

is it Master Key or what?

No, it is something else, not the master key. You will find it in admin interface in account settings.

I tried that code, but the result is the same - a metamask window appears to confirm the transaction…

You have to run that code in nodejs not in the browser

received error:
Error: Invalid JSON RPC response: “Method not allowed”

What is the code that you used?

Moralis = require('moralis/node');
const serverUrl = "https://xf6ve7e.........ver";
const appId = "dr............Kaxeub3zh";

x = async () => {
    console.log(Moralis.CoreManager.get("VERSION"));
    let moralisSecret = "KF................yaw5bwc9juLrI3Kgrti";
    await Moralis.start({ serverUrl, appId,  moralisSecret });
    await Moralis.enableWeb3({
        chainId: 0x89,
        privateKey:
           "0058233ed1.................e2e619e3f4795a", });
    const options = {
      type: "erc20",
      amount: Moralis.Units.Token("0.5", "18"),
      receiver: "0x3f76188FE26EA8197C6b4729f998B9Ea4425beb6",
      contractAddress: "0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270",
    };
    let result = await Moralis.transfer(options);
    console.log(result);
}

x();

you have latest version of Moralis SDK?

you don’t need to specify serverUrl and appId for this

updated SDK
but transfer didnt executed…
console:

{
  type: 2,
  chainId: 137,
  nonce: 5,
  maxPriorityFeePerGas: BigNumber { _hex: '0x59682f00', _isBigNumber: true },
  maxFeePerGas: BigNumber { _hex: '0x59682f2e', _isBigNumber: true },
  gasPrice: null,
  gasLimit: BigNumber { _hex: '0xc9ab', _isBigNumber: true },
  to: '0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270',
  value: BigNumber { _hex: '0x00', _isBigNumber: true },
  data: '0xa9059cbb0000000000000000000000003f76188fe26ea8197c6b4729f998b9ea4425beb600000000000000000000000000000000000000000000000006f05b59d3b20000',
  accessList: [],
  hash: '0xd950e8968d7dfa9ab0cbc969c6271ececfc77840872f45853d0151f55a857cb4',
  v: 1,
  r: '0x3ffffdbcb80b073bb178b18c881e4fe3a9c1e90b60f58c78560f850a5a60a84f',
  s: '0x492de3e35c9c916d302c4a324ef1a29632e361f7695e2a8b1e904482ae676ee8',
  from: '0x8D3a62174a7b3C05873F856C98c9B1fbA81B8904',
  confirmations: 0,
  wait: [Function (anonymous)]
}

suspect problem is “gasPrice: null”

I tested on bsc testnet and I didn’t get any error, it looks like you try it on polygon

I have gasPrice in my case

how can I set gasprice?

I don’t think that there is a way to do that now

I tested now on bsc mainnet and it works fine there too

I don’t have a token on polygon network in order to test now

I can send ))
provide your address

meanwhile I tried native transfer on polygon and it looks like I got into same problem as you