Help with web3.js

Hello everyone!

I have deployed a contract to ethereum with web3.js that just simple changes a number on the frontend when the contract call finishes.

Im trying to create a simple tool to benchmark harmony txs vs eth txs for users to experience the speed of the harmony chain.

You can try it on Ethereum at https://s-server.nl however fees and time is at the order on this side im working on implementing this page on both chains but im having no luck on harmony i managed to deploy the contract trough remix.

This is what it looks like! (on both sides)

pragma solidity ^0.6.6;

contract CoolNumberContract {
    uint public coolNumber = 10;
    
    function setCoolNumber(uint _coolNumber) public {
        coolNumber = _coolNumber;
    }
}

And here it is in the explorer:

i couldnt find it in the documentation how this snippet would work on the harmony side.

async function loadWeb3() {
            if (window.ethereum) {
                window.web3 = new Web3(window.ethereum);
                window.ethereum.enable();
            }
        }

        async function loadContract() {
            return await new window.web3.eth.Contract([
	{
		"inputs": [],
		"name": "coolNumber",
		"outputs": [
			{
				"internalType": "uint256",
				"name": "",
				"type": "uint256"
			}
		],
		"stateMutability": "view",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "uint256",
				"name": "_coolNumber",
				"type": "uint256"
			}
		],
		"name": "setCoolNumber",
		"outputs": [],
		"stateMutability": "nonpayable",
		"type": "function"
	}
], '0x5feC37dBd31212B6d8E9B40b7533d5d617f9B3C9');
        }

Anyone can help me out?

Im using metamask

should work exactly the same. tried loading harmony network in metamask, however giving error:

web3.min.js:1 Uncaught (in promise) Error: Couldn't decode uint256 from ABI: 0x
    at o.formatOutputUInt [as _outputFormatter] (web3.min.js:1)
    at o.decode (web3.min.js:1)
    at web3.min.js:1
    at Array.forEach (<anonymous>)
    at b.decodeParameters (web3.min.js:1)
    at M.d._decodeMethodReturn (web3.min.js:1)
    at f.outputFormatter (web3.min.js:1)
    at f.formatOutput (web3.min.js:1)
    at o (web3.min.js:1)
    at web3.min.js:1

Then there seems to be something wrong with the ABI?
i took it directly from the harmony one remix.

Im using vanilla JS with web3 from a CDN.

This the the whole JS part

</div>
    <script type="text/javascript">

        async function loadWeb3() {
            if (window.ethereum) {
                window.web3 = new Web3(window.ethereum);
                window.ethereum.enable();
            }
        }

        async function loadContract() {
            return await new window.web3.eth.Contract([
	{
		"inputs": [],
		"name": "coolNumber",
		"outputs": [
			{
				"internalType": "uint256",
				"name": "",
				"type": "uint256"
			}
		],
		"stateMutability": "view",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "uint256",
				"name": "_coolNumber",
				"type": "uint256"
			}
		],
		"name": "setCoolNumber",
		"outputs": [],
		"stateMutability": "nonpayable",
		"type": "function"
	}
], '0xd736657F66cb117eCDea54070D861Bd6Ed246f1b');
        }

        async function printCoolNumber() {
            updateStatus('fetching current number');
            const coolNumber = await window.contract.methods.coolNumber().call();
            updateStatus(`<span> The current number is: <b>${coolNumber}</b><span>`);
            updateHeader(`<span><h1><b>${coolNumber}</b></h1><span>`);
        }

        async function getCurrentAccount() {
            const accounts = await window.web3.eth.getAccounts();
            return accounts[0];r
        }

        async function changeCoolNumber() {
            const value = Math.floor(Math.random() * 10);
            updateStatus(`Updating the Number with: <b>${value}</b>`);
            const account = await getCurrentAccount();
            const coolNumber = await window.contract.methods.setCoolNumber(value).send({ from: account });
            updateStatus('Updated.');
        }

        async function load() {
            await loadWeb3();
            window.contract = await loadContract();
            const coolNumber = await window.contract.methods.coolNumber().call();
            updateHeader(`<span><h1><b>${coolNumber}</b></h1><span>`);
            updateStatus('Ready');
        }


        function updateHeader(status) {
            const statusEl = document.getElementById('nmbr');
            statusEl.innerHTML = status;
        }

        function updateStatus(status) {
            const statusEl = document.getElementById('status');
            statusEl.innerHTML = status;
            console.log(status);
        }

        load();
    </script>

can you try remix.ethereum.org instead of harmony remix and see if you get a different ABI? ethereum remix works without any additional steps, just needs adding harmony network to metamask.

Okay I will try that Ganesha, just saw the dev call updating the docs for a web3 newbie like me would be awesome thanks!

1 Like

Hey ganesha after using the ethereum remix the ABI seems correct!
My little first project Dapp now is successfully deployed on harmony mainnet!

Thanks for the help

Debug info:

Harmony remix ABI:

[
	{
		"inputs": [],
		"name": "coolNumber",
		"outputs": [
			{
				"internalType": "uint256",
				"name": "",
				"type": "uint256"
			}
		],
		"stateMutability": "view",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "uint256",
				"name": "_coolNumber",
				"type": "uint256"
			}
		],
		"name": "setCoolNumber",
		"outputs": [],
		"stateMutability": "nonpayable",
		"type": "function"
	}
]

Eth remix on harmony trough metamask:

[
	{
		"inputs": [
			{
				"internalType": "uint256",
				"name": "_coolNumber",
				"type": "uint256"
			}
		],
		"name": "setCoolNumber",
		"outputs": [],
		"stateMutability": "nonpayable",
		"type": "function"
	},
	{
		"inputs": [],
		"name": "coolNumber",
		"outputs": [
			{
				"internalType": "uint256",
				"name": "",
				"type": "uint256"
			}
		],
		"stateMutability": "view",
		"type": "function"
	}
]

There seems a bit off difference on how the JSON is formatted and some values appear to be missing? Mabey this helps to fix later on harmony remix for the fellow developers!

awesome. thanks for updating the status. would you mind creating an issue here: Issues · harmony-one/remixide · GitHub with details of this problem so that it is tracked and fixed? thanks.