Gnosis Tx-Builder from a script
⚡🚀 Transform an array of transactions in a Transaction builder json for the Gnosis UX, based on ethers-js
Install
npm install @morpho-labs/gnosis-tx-builder
yarn add @morpho-labs/gnosis-tx-builder
Usage
Generate JSON
Generate a Tx builder json file:
import { ZeroAddress, parseEther } from "ethers";
import fs from "fs";
import TxBuilder, {
ParsingError,
ChecksumParsingError,
TransactionParsingError,
} from "@morpho-labs/gnosis-tx-builder";
const safeAddress = "0x12341234123412341234123412341232412341234";
const transactions = [
{
to: ZeroAddress,
value: parseEther("1").toString(),
data: "0x",
},
];
const batchJson = TxBuilder.batch(safeAddress, transactions);
fs.writeFileSync("batchTx.json", JSON.stringify(batchJson, null, 2));
Now, with the json file, go to the Gnosis dApp, and select Transaction Builder app
And then, drag and drop the batchTx.json
file
And tada! 🎉🎉
Parse JSON
Parse transactions from a Tx Builder JSON file:
import fs from "fs";
import TxBuilder from "@morpho-labs/gnosis-tx-builder";
const batchJson = fs.readFileSync("batchTx.json");
try {
const batch = TxBuilder.parse(batchJson);
console.log(batch);
} catch (e: ParsingError) {
if (e instanceof ChecksumParsingError) console.debug(e.params);
if (e instanceof TransactionParsingError) console.debug(e.params);
console.debug(e.params);
}
Error Codes
You can import the error codes from the package
import { ErrorCode } from "@morpho-labs/gnosis-tx-builder";
Available codes
export enum ErrorCode {
wrongFormat = "WRONG_FORMAT",
wrongTxFormat = "WRONG_TRANSACTION_FORMAT",
invalidChecksum = "INVALID_CHECKSUM",
}