1

I have tried all sorts of ways but it's not working for me when i change my value to 0 it works fine the transaction gets submitted.

But if i change it to any other number lets say 1, 10, 20 I get this error.

I have created my owntoken

enter image description here

MyAbi File

    {
        "anonymous": false,
        "inputs": [
            {
                "indexed": true,
                "internalType": "address",
                "name": "previousOwner",
                "type": "address"
            },
            {
                "indexed": true,
                "internalType": "address",
                "name": "newOwner",
                "type": "address"
            }
        ],
        "name": "OwnershipTransferred",
        "type": "event"
    },
    {
        "inputs": [],
        "name": "owner",
        "outputs": [
            {
                "internalType": "address",
                "name": "",
                "type": "address"
            }
        ],
        "stateMutability": "view",
        "type": "function"
    },
    {
        "inputs": [],
        "name": "renounceOwnership",
        "outputs": [],
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "inputs": [
            {
                "internalType": "address",
                "name": "newOwner",
                "type": "address"
            }
        ],
        "name": "transferOwnership",
        "outputs": [],
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [
            {
                "name": "_owner",
                "type": "address"
            }
        ],
        "name": "balanceOf",
        "outputs": [
            {
                "name": "balance",
                "type": "uint256"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": false,
        "inputs": [
            {
                "name": "_to",
                "type": "address"
            },
            {
                "name": "_value",
                "type": "uint256"
            }
        ],
        "name": "transfer",
        "outputs": [
            {
                "name": "success",
                "type": "bool"
            }
        ],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "anonymous": false,
        "inputs": [
            {
                "indexed": true,
                "name": "_from",
                "type": "address"
            },
            {
                "indexed": true,
                "name": "_to",
                "type": "address"
            },
            {
                "indexed": false,
                "name": "_value",
                "type": "uint256"
            }
        ],
        "name": "Transfer",
        "type": "event"
    }
]

MyJavascriptCode


    var Tx = require('ethereumjs-tx').Transaction;
    var Web3 = require('web3');
    var web3 = new Web3(new Web3.providers.HttpProvider('https://ropsten.infura.io/v3/$[project_key]'));

    var amount = web3.utils.toHex(1);
    var privateKey = Buffer.from('myprivatekey', 'hex');
    var abiArray = JSON.parse(fs.readFileSync('MyAbi.json'));
    var contractAddress = '0x37FC4b48CE93'; // ONLYONE address
    const amountToSend = 10
    console.log("23")
    
    var contract = new web3.eth.Contract(abiArray, contractAddress, {from: fromAddress});
    console.log("24")
 

    var count = await web3.eth.getTransactionCount(fromAddress);
    // count = '0x' + count.toString(16)

    var rawTransaction = {
        "gasPrice":web3.utils.toHex(10000000000),
        "gasLimit":web3.utils.toHex(8000000),
        "to":contractAddress,
        "value": 1,
        "data":contract.methods.transfer(toAddress, amount).encodeABI(),
        "nonce":web3.utils.toHex(count)
    };

    var transaction = new Tx(rawTransaction, {chain:'ropsten'});
    transaction.sign(privateKey)

    var result = await web3.eth.sendSignedTransaction('0x' + transaction.serialize().toString('hex'), function(err, res){
        if(err){
            console.log("err-->", err)
        }else{
            console.log("response--->", res)
        }
    });
    return result;
}

sendOnlyone(fromAdd, '0x11cE'); //fromaddress , toaddress```
1
  • How did you solved it?
    – Taha Malik
    Commented Jun 27, 2022 at 3:01

1 Answer 1

2

According to your abi, you transfer method is not payable, but you set the value of you transaction payload to one wei { … value: 1, (…) }

To use your transfer method (and, therefore, transfer your token), set the value of your transaction to 0 and the parameter of your transfer function to the desired amount.

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.