@bradthomasbrown/abi
v1.0.0
Published
Minimal, unoptimized, unaudited, and experimental ABI encoder/decoder library in JavaScript/TypeScript.
Readme
abi
This is a simple, minimal implementation of Ethereum ABI encoding and decoding in TypeScript/JavaScript.
Installation
npm i @bradthomasbrown/abiUsage
/* Example ABI with an overload
[
{
"inputs": [
{
"internalType": "uint256",
"name": "a",
"type": "uint256"
}
],
"name": "_df639c_",
"outputs": [
{
"internalType": "uint256",
"name": "value",
"type": "uint256"
}
],
"stateMutability": "pure",
"type": "function"
},
{
"inputs": [
{
"internalType": "string",
"name": "a",
"type": "string"
}
],
"name": "_df639c_",
"outputs": [
{
"internalType": "string",
"name": "text",
"type": "string"
}
],
"stateMutability": "pure",
"type": "function"
}
]
*/
import { encode, decode, _655c97_, _869645_ } from "@bradthomasbrown/abi";
import * as _0b1577_ from "./0b1577/dist/0b1577";
function bufferToHex(buffer:ArrayBuffer):string {
return new Uint8Array(buffer).toHex();
}
// these first usage examples are how to use `abi` in a low-level and direct way
const abiJson = await Bun.file("./test/0b1577/src/0b1577.abi").json();
const _0a_ = abiJson[0]; // get the first abi fragment, the uint overload
const _6d_ = [0x123456789n]; // an array of values to be used in ABI encoding
const _aa_ = _655c97_(_0a_, "inputs"); // builds a "first pass" metadata tree
const _56_ = _869645_(_aa_, _6d_); // builds a "second pass" metadata tree for encoding. necessary since string arrays etc. final buffer size is dependent on actual values used
const _ae_ = encode(false, _56_, _6d_); // performs the actual encoding, creates the final buffer immediately since second pass cause size to be known. only the final buffer is worked on
console.log(bufferToHex(_ae_));
// b1e56dfc0000000000000000000000000000000000000000000000000000000123456789
// sample output for us to decode for first overload
const _f4_ = Uint8Array.fromHex("0x0000000000000000000000000000000000000000000000000000000123456789".slice(2)).buffer;
const _dd_ = abiJson[0];
const _3e_ = _655c97_(_dd_, "outputs");
console.log(decode(_f4_, _3e_));
// { value: 4886718345n }
// sample output for us to decode for second overload
const _4d_ = Uint8Array.fromHex("0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000b68656c6c6f20776f726c64000000000000000000000000000000000000000000".slice(2)).buffer;
const _54_ = abiJson[1];
const _1c_ = _655c97_(_54_, "outputs");
console.log(decode(_4d_, _1c_));
// { text: "hello world" }
// using compiled modules from c880f7 would look like below
// where 0b1577 is the contract name
// and df639c is the overloaded function name
// type signature will look like `_df639c_(includeSelector: boolean, overload: 0, a: bigint): ArrayBuffer (+1 overload)`
console.log(bufferToHex(_0b1577_.encode._df639c_(true, 0, 0x123456789n)));
// b1e56dfc0000000000000000000000000000000000000000000000000000000123456789
console.log(bufferToHex(_0b1577_.encode._df639c_(false, 0, 0x123456789n)));
// 0000000000000000000000000000000000000000000000000000000123456789
console.log(bufferToHex(_0b1577_.encode._df639c_(true, 1, "hello world")));
// cb7f477c0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000b68656c6c6f20776f726c64000000000000000000000000000000000000000000
console.log(bufferToHex(_0b1577_.encode._df639c_(false, 1, "hello world")));
// 0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000b68656c6c6f20776f726c64000000000000000000000000000000000000000000
console.log(_0b1577_.decode._df639c_(0, _f4_));
// { value: 4886718345n }
console.log(_0b1577_.decode._df639c_(1, _4d_));
// { text: "hello world" }