Last active
June 11, 2022 16:52
-
-
Save numtel/8fe7ebfc16d6091ec42a8e10f1eeacaa to your computer and use it in GitHub Desktop.
ABI directly on chain
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const zlib = require('zlib'); | |
const Web3 = require('web3'); | |
const solc = require('solc'); | |
(async function() { | |
// Stick whatever metadata you want in a zip | |
const file = fs.readFileSync('VerifiedGroup.abi'); | |
const zip = zlib.gzipSync(file); | |
console.log('Zipped size', zip.length, 'Original Size', file.length); | |
// Embed the zip into a contract | |
const input = { | |
language: 'Solidity', | |
sources: { | |
'test.sol': { | |
content: ` | |
contract Test { | |
function data() public pure returns (bytes memory out) { | |
out = hex"${zip.toString('hex')}"; | |
} | |
}` | |
} | |
}, | |
settings: { | |
outputSelection: { | |
'*': { | |
'*': ['*'] | |
} | |
} | |
} | |
}; | |
const output = JSON.parse(solc.compile(JSON.stringify(input))); | |
const abi = output.contracts['test.sol']['Test'].abi; | |
const data = output.contracts['test.sol']['Test'].evm.bytecode.object; | |
// Deploy to test Ganache chain | |
const web3 = new Web3('http://localhost:8545'); | |
const accounts = await new Promise((resolve, reject) => { | |
web3.eth.getAccounts((error, accounts) => { | |
if(error) reject(error); | |
else resolve(accounts); | |
}); | |
}); | |
const contract = new web3.eth.Contract(abi); | |
console.log('Super simple ABI for retrieving', JSON.stringify(abi, null, 2)); | |
const deployment = contract.deploy({ data }); | |
const gas = await deployment.estimateGas(); | |
const deployed = await deployment.send({ from: accounts[0], gas }); | |
console.log('Deployed to', deployed.options.address); | |
// Read back the data | |
const contract2 = new web3.eth.Contract(abi, deployed.options.address); | |
const fromContract = await contract2.methods.data().call(); | |
const unzip = zlib.gunzipSync(Buffer.from(fromContract.slice(2), 'hex')); | |
console.log(unzip.toString('utf8')); | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ node index.js | |
Zipped size 882 Original Size 8506 | |
Super simple ABI for retrieving [ | |
{ | |
"inputs": [], | |
"name": "data", | |
"outputs": [ | |
{ | |
"internalType": "bytes", | |
"name": "out", | |
"type": "bytes" | |
} | |
], | |
"stateMutability": "pure", | |
"type": "function", | |
"constant": true, | |
"signature": "0x73d4a13a" | |
} | |
] | |
Deployed to 0xA9D374AAcC3fe3395470eDeBA7266f1DA6f73fcd | |
[{"inputs": ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment