@morpho-labs/gnosis-tx-builder
Version:
Transform an array of transactions into a json for Gnosis Tx-Builder UX
54 lines (53 loc) • 2.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const constants_1 = require("./constants");
const errors_1 = require("./errors");
const utils_1 = require("./utils");
class TxBuilder {
}
exports.default = TxBuilder;
TxBuilder.batch = (safe, transactions, options = {}) => {
var _a, _b, _c, _d, _e, _f;
return (0, utils_1.addChecksum)({
version: "1.0",
chainId: (_b = (_a = options.chainId) === null || _a === void 0 ? void 0 : _a.toString()) !== null && _b !== void 0 ? _b : "1",
createdAt: (_c = options.createdAt) !== null && _c !== void 0 ? _c : Date.now(),
meta: {
name: (_d = options.name) !== null && _d !== void 0 ? _d : constants_1.DEFAULT_OPTIONS.name,
description: (_e = options.description) !== null && _e !== void 0 ? _e : constants_1.DEFAULT_OPTIONS.description,
txBuilderVersion: (_f = options.txBuilderVersion) !== null && _f !== void 0 ? _f : constants_1.DEFAULT_OPTIONS.txBuilderVersion,
createdFromSafeAddress: safe,
createdFromOwnerAddress: "",
},
transactions,
});
};
TxBuilder.parse = (batch) => {
var _a;
const parsedBatch = JSON.parse(batch);
if (Array.isArray(parsedBatch) ||
!parsedBatch.transactions ||
!Array.isArray(parsedBatch.transactions))
throw new errors_1.ParsingError(errors_1.ErrorCode.wrongFormat);
const checksum = (0, utils_1.calculateChecksum)(parsedBatch);
if (!((_a = parsedBatch.meta) === null || _a === void 0 ? void 0 : _a.checksum) || !checksum || checksum !== parsedBatch.meta.checksum)
throw new errors_1.ChecksumParsingError(parsedBatch.meta.checksum, checksum);
return parsedBatch.transactions.map((tx, i) => {
if (typeof tx !== "object")
throw new errors_1.TransactionParsingError(i);
const { to, value, data, contractMethod, contractInputsValues } = tx;
if (typeof to !== "string")
throw new errors_1.TransactionParsingError(i, "to");
if (typeof value !== "string")
throw new errors_1.TransactionParsingError(i, "value");
if (data !== null && data !== undefined && typeof data !== "string")
throw new errors_1.TransactionParsingError(i, "data");
if (contractInputsValues !== undefined &&
(typeof contractInputsValues !== "object" ||
!Object.values(contractInputsValues).every((v) => typeof v === "string")))
throw new errors_1.TransactionParsingError(i, "contractInputsValues");
const contractMethodError = new errors_1.TransactionParsingError(i, "contractMethod");
const validatedContractMethod = (0, errors_1.validateContractMethod)(contractMethod, contractMethodError);
return { to, value, data, contractMethod: validatedContractMethod, contractInputsValues };
});
};