dop-stick
Version:
Source control tooling for versionable-upgradeable smart contracts
86 lines • 3.42 kB
JavaScript
;
/* global ethers */
Object.defineProperty(exports, "__esModule", { value: true });
exports.getContractFactory = exports.findAddressPositionInFacets = exports.removeSelectors = exports.getSelector = exports.getSelectors = exports.FacetCutAction = void 0;
const abi_1 = require("@ethersproject/abi");
exports.FacetCutAction = { Add: 0, Replace: 1, Remove: 2 };
// get function selectors from ABI
function getSelectors(contract) {
const signatures = Object.keys(contract.interface.functions);
console.log("FUNCTION_SIG: ", signatures);
// the init signature acting as a constructor to the diamond would not be added to the diamond
const selectors = signatures.reduce((acc, val) => {
if (val !== "init(bytes)") {
acc.push(contract.interface.getSighash(val));
}
return acc;
}, []);
// adding more properties to the "selectors" array
selectors.contract = contract;
selectors.remove = remove;
selectors.get = get;
return selectors;
}
exports.getSelectors = getSelectors;
// get function selector from function signature
function getSelector(func) {
const abiInterface = new abi_1.Interface([func]);
return abiInterface.getSighash(func);
}
exports.getSelector = getSelector;
// used with getSelectors to remove selectors from an array of selectors
function remove(functionNames) {
const selectors = this.filter((v) => {
var _a;
for (const functionName of functionNames) {
if (v === ((_a = this.contract) === null || _a === void 0 ? void 0 : _a.interface.getSighash(functionName))) {
return false;
}
}
return true;
});
selectors.contract = this.contract;
selectors.remove = this.remove;
selectors.get = this.get;
return selectors;
}
function get(functionNames) {
const selectors = this.filter((v) => {
var _a;
for (const functionName of functionNames) {
if (v === ((_a = this.contract) === null || _a === void 0 ? void 0 : _a.interface.getSighash(functionName))) {
return true;
}
}
return false;
});
selectors.contract = this.contract;
selectors.remove = this.remove;
selectors.get = this.get;
return selectors;
}
// remove selectors using an array of signatures
function removeSelectors(selectors, signatures) {
const iface = new abi_1.Interface(signatures.map((v) => "function " + v));
const removeSelectors = signatures.map((v) => iface.getSighash(v));
return selectors.filter((v) => !removeSelectors.includes(v));
}
exports.removeSelectors = removeSelectors;
// find a particular address position in the return value of diamondLoupeFacet.facets()
function findAddressPositionInFacets(facetAddress, facets) {
for (let i = 0; i < facets.length; i++) {
if (facets[i].facetAddress === facetAddress) {
return i;
}
}
return undefined;
}
exports.findAddressPositionInFacets = findAddressPositionInFacets;
// Helper function to get contract factory (to be used instead of hardhat's getContractFactory)
async function getContractFactory(name, signer) {
// You'll need to implement this based on your needs
// This is just a placeholder
throw new Error('getContractFactory needs to be implemented');
}
exports.getContractFactory = getContractFactory;
//# sourceMappingURL=eunice.js.map