Skip to content

Commit

Permalink
Remove long functions
Browse files Browse the repository at this point in the history
  • Loading branch information
DimaStebaev committed Sep 14, 2023
1 parent 0874959 commit 936ca5f
Show file tree
Hide file tree
Showing 7 changed files with 262 additions and 179 deletions.
1 change: 0 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ module.exports = {
"never"
],

"max-lines-per-function": "warn",
"max-params": "warn",
"max-statements": "warn",
"multiline-comment-style": "warn",
Expand Down
56 changes: 32 additions & 24 deletions src/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import {promises as fs} from "fs";
import {SkaleManifestData} from "./types/SkaleManifestData";

Check warning on line 4 in src/deploy.ts

View workflow job for this annotation

GitHub Actions / test (20.x)

Imports should be sorted alphabetically

Check warning on line 4 in src/deploy.ts

View workflow job for this annotation

GitHub Actions / test (16.x)

Imports should be sorted alphabetically

Check warning on line 4 in src/deploy.ts

View workflow job for this annotation

GitHub Actions / test (18.x)

Imports should be sorted alphabetically

Check warning on line 4 in src/deploy.ts

View workflow job for this annotation

GitHub Actions / test (18.x)

Imports should be sorted alphabetically

Check warning on line 4 in src/deploy.ts

View workflow job for this annotation

GitHub Actions / test (20.x)

Imports should be sorted alphabetically

Check warning on line 4 in src/deploy.ts

View workflow job for this annotation

GitHub Actions / test (16.x)

Imports should be sorted alphabetically
import {Artifact} from "hardhat/types";

Check warning on line 5 in src/deploy.ts

View workflow job for this annotation

GitHub Actions / test (20.x)

Imports should be sorted alphabetically

Check warning on line 5 in src/deploy.ts

View workflow job for this annotation

GitHub Actions / test (16.x)

Imports should be sorted alphabetically

Check warning on line 5 in src/deploy.ts

View workflow job for this annotation

GitHub Actions / test (18.x)

Imports should be sorted alphabetically

Check warning on line 5 in src/deploy.ts

View workflow job for this annotation

GitHub Actions / test (18.x)

Imports should be sorted alphabetically

Check warning on line 5 in src/deploy.ts

View workflow job for this annotation

GitHub Actions / test (20.x)

Imports should be sorted alphabetically

Check warning on line 5 in src/deploy.ts

View workflow job for this annotation

GitHub Actions / test (16.x)

Imports should be sorted alphabetically

interface LibraryArtifacts {
[key: string]: unknown
}

const _deployLibrary = async (libraryName: string) => {

Check warning on line 11 in src/deploy.ts

View workflow job for this annotation

GitHub Actions / test (20.x)

Unexpected dangling '_' in '_deployLibrary'

Check warning on line 11 in src/deploy.ts

View workflow job for this annotation

GitHub Actions / test (16.x)

Unexpected dangling '_' in '_deployLibrary'

Check warning on line 11 in src/deploy.ts

View workflow job for this annotation

GitHub Actions / test (18.x)

Unexpected dangling '_' in '_deployLibrary'

Check warning on line 11 in src/deploy.ts

View workflow job for this annotation

GitHub Actions / test (18.x)

Unexpected dangling '_' in '_deployLibrary'

Check warning on line 11 in src/deploy.ts

View workflow job for this annotation

GitHub Actions / test (20.x)

Unexpected dangling '_' in '_deployLibrary'

Check warning on line 11 in src/deploy.ts

View workflow job for this annotation

GitHub Actions / test (16.x)

Unexpected dangling '_' in '_deployLibrary'
const
Library = await ethers.getContractFactory(libraryName);
Expand Down Expand Up @@ -69,6 +73,32 @@ export const getManifestFile = async function getManifestFile () {
return (await Manifest.forNetwork(ethers.provider)).file;
};

const updateManifest = async (libraryArtifacts: LibraryArtifacts) => {
const manifest = JSON.parse(await fs.readFile(
await getManifestFile(),
"utf-8"
)) as SkaleManifestData;
if (manifest.libraries === undefined) {
Object.assign(
manifest,
{"libraries": libraryArtifacts}
);
} else {
Object.assign(
libraryArtifacts,
manifest.libraries
);
}
await fs.writeFile(
await getManifestFile(),
JSON.stringify(
manifest,
null,
4
)
);
};

export const getContractFactory = async (contract: string) => {
const {linkReferences} = await artifacts.readArtifact(contract);
if (!Object.keys(linkReferences).length) {
Expand All @@ -82,7 +112,7 @@ export const getContractFactory = async (contract: string) => {
}

const libraries = await deployLibraries(libraryNames);
const libraryArtifacts: { [key: string]: unknown } = {};
const libraryArtifacts: LibraryArtifacts = {};
for (const [
libraryName,
libraryAddress
Expand All @@ -94,29 +124,7 @@ export const getContractFactory = async (contract: string) => {
};
}

const manifest = JSON.parse(await fs.readFile(
await getManifestFile(),
"utf-8"
)) as SkaleManifestData;
if (manifest.libraries === undefined) {
Object.assign(
manifest,
{"libraries": libraryArtifacts}
);
} else {
Object.assign(
libraryArtifacts,
manifest.libraries
);
}
await fs.writeFile(
await getManifestFile(),
JSON.stringify(
manifest,
null,
4
)
);
await updateManifest(libraryArtifacts);

return await getLinkedContractFactory(
contract,
Expand Down
96 changes: 53 additions & 43 deletions src/gnosis-safe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,64 +26,55 @@ const URLS = {
}
};

const defaultOptions = {
// Max gas to use in the transaction
"safeTxGas": "0",

// Gas costs not related to the transaction execution
// (signature check, refund payment...)
"baseGas": "0",

// Gas price used for the refund calculation
"gasPrice": "0",

/* Token address (hold by the Safe)
* to be used as a refund to the sender,
* if `null` is Ether
*/
"gasToken": ethers.constants.AddressZero,

// Address of receiver of gas payment (or `null` if tx.origin)
"refundReceiver": ethers.constants.AddressZero
};

// Public functions

export const createMultiSendTransaction = async (
safeAddress: string,
transactions: UnsignedTransaction[]
) => {
const safeTransactionData: MetaTransactionData[] = [];
for (const transaction of transactions) {
safeTransactionData.push({
"to": transaction.to
? transaction.to
: ethers.constants.AddressZero,
"data": transaction.data
? transaction.data.toString()
: "0x",
"value": transaction.value
? transaction.value.toString()
: "0",
"operation": 0
});
}

const
safeService = await getSafeService();
const safeTransactionData = getSafeTransactionData(transactions);
const safeService = await getSafeService();
const nonce = await safeService.getNextNonce(safeAddress);
console.log(
"Will send tx to Gnosis with nonce",
nonce
);

const options = {
// Max gas to use in the transaction
"safeTxGas": "0",

// Gas costs not related to the transaction execution
// (signature check, refund payment...)
"baseGas": "0",

// Gas price used for the refund calculation
"gasPrice": "0",

/* Token address (hold by the Safe)
* to be used as a refund to the sender,
* if `null` is Ether
*/
"gasToken": ethers.constants.AddressZero,

// Address of receiver of gas payment (or `null` if tx.origin)
"refundReceiver": ethers.constants.AddressZero,

// Nonce of the Safe,
// Transaction cannot be executed until
// Safe's nonce is not equal to this nonce
nonce
...defaultOptions,
...{
// Nonce of the Safe,
// Transaction cannot be executed until
// Safe's nonce is not equal to this nonce
nonce
}
};
const ethAdapter = await getEthAdapter();
const safeSdk = await Safe.create({ethAdapter,
safeAddress});
const safeSdk = await Safe.create({
ethAdapter,
safeAddress
});
const safeTransaction = await safeSdk.createTransaction({
safeTransactionData,
options
Expand All @@ -102,6 +93,25 @@ export const createMultiSendTransaction = async (

// Private functions

const getSafeTransactionData = (transactions: UnsignedTransaction[]) => {
const safeTransactionData: MetaTransactionData[] = [];
for (const transaction of transactions) {
safeTransactionData.push({
"to": transaction.to
? transaction.to
: ethers.constants.AddressZero,
"data": transaction.data
? transaction.data.toString()
: "0x",
"value": transaction.value
? transaction.value.toString()
: "0",
"operation": 0
});
}
return safeTransactionData;
};

const estimateSafeTransaction = async (
safeAddress: string,
safeTransactionData: SafeTransactionDataPartial | MetaTransactionData[]
Expand Down
39 changes: 23 additions & 16 deletions src/multiSend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,27 @@ const padWithZeros = (
targetLength: number
) => ("0".repeat(targetLength) + value).slice(-targetLength);

const getOperationBytes = (operation: 0 | 1) => {
if (operation === 0) {
return "00";
} else if (operation === 1) {
return "01";
}
throw Error("Operation has an incorrect value");
};

const getToBytes = (to: string) => {
let _to = to;
if (to.startsWith("0x")) {
_to = _to.slice(2);
}
_to = padWithZeros(
_to,
20 * 2
);
return _to;
};

export const encodeTransaction = (
/* Operation as a uint8 with 0 for a call
* or 1 for a delegatecall (=> 1 byte)
Expand All @@ -20,23 +41,9 @@ export const encodeTransaction = (
// Data as bytes.
data: string
) => {
let _operation = "";
if (operation === 0) {
_operation = "00";
} else if (operation === 1) {
_operation = "01";
} else {
throw Error("Operation has an incorrect value");
}
const _operation = getOperationBytes(operation);

let _to = to;
if (to.startsWith("0x")) {
_to = _to.slice(2);
}
_to = padWithZeros(
_to,
20 * 2
);
const _to = getToBytes(to);

const _value = padWithZeros(
BigNumber.from(value).toHexString().
Expand Down
4 changes: 4 additions & 0 deletions src/submitters/auto-submitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export class AutoSubmitter extends Submitter {
}

console.log("Owner is a contract");
return AutoSubmitter.getSubmitterForContractOwner(owner);
}

private static async getSubmitterForContractOwner (owner: string) {
if (ethers.utils.getAddress(owner) ===
ethers.utils.getAddress(MARIONETTE_ADDRESS)) {
console.log("Marionette owner is detected");
Expand Down
5 changes: 5 additions & 0 deletions src/types/ContractToUpgrade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface ContractToUpgrade {
proxyAddress: string,
implementationAddress: string,
name: string
}
Loading

0 comments on commit 936ca5f

Please sign in to comment.