Skip to content

Instantly share code, notes, and snippets.

@thaarok
Created October 16, 2025 15:26
Show Gist options
  • Select an option

  • Save thaarok/152835edb3d7698d2caf00edc609e82e to your computer and use it in GitHub Desktop.

Select an option

Save thaarok/152835edb3d7698d2caf00edc609e82e to your computer and use it in GitHub Desktop.
Trivial function call ABI encoder/decoder online (Ethereum/Web3)
<!DOCTYPE html>
<head>
<script src="//cdnjs.cloudflare.com/ajax/libs/web3/1.6.0/web3.min.js" integrity="sha512-+BhnLgfzIDDjssoEWHPmdgWRvbwIEdj0Xfiys7uSqfQWpMEOJ4ymJ88O6B1cB0j+4zjb5GhO+sb/kEicggvUQQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
</head>
<h1>Encode eth_call using ABI</h1>
ABI:<br>
<textarea id="abiField" style="width: 100%; height: 10em;">
{
"inputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"name": "proposals",
"outputs": [
{
"internalType": "bytes32",
"name": "name",
"type": "bytes32"
},
{
"internalType": "uint256",
"name": "voteCount",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
}
</textarea>
<input type="button" value="Evaluate ABI" onclick="evalAbi()">
<div id="methodParams">
</div>
<input type="button" value="Encode call" onclick="encodeCall()"><br>
<input type="button" value="Decode call" onclick="decodeCall()"><br>
Encoded message:<br>
<textarea id="outputField" style="width: 100%; height: 10em;">
</textarea>
<script>
var abi;
function evalAbi() {
console.log("parsing abi", abiField.value);
abi = JSON.parse(abiField.value);
methodParams.innerHTML = '';
var id = 0;
abi.inputs.forEach(function(input){
methodParams.innerHTML += input.name + ': <input type="text" id="input'+id+'"> ('+input.type+')<br>';
id++;
});
}
function encodeCall() {
var id = 0;
var params = [];
abi.inputs.forEach(function(input){
params.push(document.getElementById("input"+id).value);
id++;
});
console.log("encoding params", params);
const web3 = new Web3(window.ethereum);
var output = web3.eth.abi.encodeFunctionCall(abi, params);
console.log("done", output);
outputField.value = output;
}
function decodeCall() {
evalAbi();
const paramsHexa = outputField.value.slice(10); // skip 0x and func signature
const types = abi.inputs.map(i => i.type);
const web3 = new Web3(window.ethereum);
const params = web3.eth.abi.decodeParameters(types, paramsHexa);
console.log('decoded params', params);
var id = 0;
abi.inputs.forEach(function(input){
document.getElementById("input"+id).value = params[id];
id++;
});
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment