UNPKG

send-4844-tx

Version:
132 lines (112 loc) 4.54 kB
const {Send4844Tx, EncodeBlobs, DecodeBlobs, BLOB_SIZE} = require("./send-4844-tx"); const {ethers, Contract} = require("ethers"); const crypto = require('crypto'); const fs = require('fs'); const os = require('os'); const stringToHex = (s) => ethers.utils.hexlify(ethers.utils.toUtf8Bytes(s)); async function readFile(contract, name) { const result = await contract.read(name); return result[0]; } const saveFile = (data) => { console.log(data); const exp = new Date(); const path = `${os.tmpdir()}/${exp.getTime()}`; fs.writeFileSync(path, data); return path; } const filePath = '/Users/Downloads/4.mp3'; const name = filePath.substring(filePath.lastIndexOf("/") + 1); const hexName = stringToHex(name); const contractAddress = '0xd70DF3F322240563167823A9657ee3B0e496D4fa' const contractABI = [ 'function read(bytes memory name) public view returns (bytes memory, bool)', 'function writeChunk(bytes memory name, uint256[] memory chunkIds, uint256[] memory sizes) public payable', 'function getChunkHash(bytes memory name, uint256 chunkId) public view returns (bytes32)', "function putHashes(uint256 count) public payable", "function upfrontPayment() public view returns (uint256)", "function setStorageAddr(address addr) external" ] async function uploadFile() { const provider = new ethers.providers.JsonRpcProvider('http://65.109.115.36:32809'); const contract = new Contract(contractAddress, contractABI, provider); const content = fs.readFileSync(filePath); const blobs = EncodeBlobs(content); const send4844Tx = new Send4844Tx('https://rpc.dencun-devnet-8.ethpandaops.io/', 'private key'); const blobLength = blobs.length; for (let i = 0; i < blobLength; i += 2) { const dataHash = await contract.getChunkHash(hexName, i); const localHash = send4844Tx.getBlobHash(blobs[i]); console.log(dataHash === localHash); let blobArr = []; let indexArr = []; let lenArr = []; if (i + 1 < blobLength) { blobArr = [blobs[i], blobs[i + 1]]; indexArr = [i, i + 1]; lenArr = [BLOB_SIZE, BLOB_SIZE]; } else { blobArr = [blobs[i]]; indexArr = [i]; lenArr = [BLOB_SIZE]; } const tx = await contract.populateTransaction.writeChunk(hexName, indexArr, lenArr, { value: 10 }); const hash = await send4844Tx.sendTx(blobArr, tx); console.log(hash); const txReceipt = await send4844Tx.getTxReceipt(hash); console.log(txReceipt); } } async function read() { console.log(hexName); const providerRead = new ethers.providers.JsonRpcProvider('http://65.109.63.154:9545'); const contractRead = new Contract(contractAddress, contractABI, providerRead); const blobs = await readFile(contractRead, hexName); console.log(blobs.length); const data = DecodeBlobs(blobs); const path = saveFile(data); console.log(path); } // uploadFile(); // read(); async function uploadFile2() { const provider = new ethers.providers.JsonRpcProvider('http://65.109.115.36:32809'); const contract = new Contract(contractAddress, contractABI, provider); const count = 800; let price = await contract.upfrontPayment(); price = price.mul(count); console.log(price); const content = crypto.randomBytes(4096 * 31); const blobs = EncodeBlobs(content); const tx = await contract.populateTransaction.putHashes(count, { value: price }); const send4844Tx = new Send4844Tx('http://65.109.115.36:32809', '0x6dc949249af2bfd6fe2c200fb537de8907d30140d94050963242a65c8e256df7'); const hash = await send4844Tx.sendTx(blobs, tx); console.log(hash); const txReceipt = await send4844Tx.getTxReceipt(hash); console.log(txReceipt); } uploadFile2(); async function batchBlob() { const currentIndex = await contract.lastKvIdx(); const totalCount = MAX_BLOB - currentIndex; console.log(totalCount); let maxCount = 400; for (let i = 0; i < totalCount; i += maxCount) { if (i + maxCount > totalCount) { maxCount = totalCount - i; } let txReceipt = await upload(maxCount); if (!txReceipt || txReceipt.status === 0) { for (let j = 0; j < 10; j++) { txReceipt = await upload(maxCount); if (txReceipt && txReceipt.status === 1) { break; } } } } }