Use Prettier for JS files (#3913)
Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
This commit is contained in:
@ -1,37 +1,38 @@
|
||||
const { time } = require('@openzeppelin/test-helpers');
|
||||
|
||||
function zip (...args) {
|
||||
function zip(...args) {
|
||||
return Array(Math.max(...args.map(array => array.length)))
|
||||
.fill()
|
||||
.map((_, i) => args.map(array => array[i]));
|
||||
}
|
||||
|
||||
function concatHex (...args) {
|
||||
function concatHex(...args) {
|
||||
return web3.utils.bytesToHex([].concat(...args.map(h => web3.utils.hexToBytes(h || '0x'))));
|
||||
}
|
||||
|
||||
function concatOpts (args, opts = null) {
|
||||
function concatOpts(args, opts = null) {
|
||||
return opts ? args.concat(opts) : args;
|
||||
}
|
||||
|
||||
class GovernorHelper {
|
||||
constructor (governor) {
|
||||
constructor(governor) {
|
||||
this.governor = governor;
|
||||
}
|
||||
|
||||
delegate (delegation = {}, opts = null) {
|
||||
delegate(delegation = {}, opts = null) {
|
||||
return Promise.all([
|
||||
delegation.token.delegate(delegation.to, { from: delegation.to }),
|
||||
delegation.value &&
|
||||
delegation.token.transfer(...concatOpts([ delegation.to, delegation.value ]), opts),
|
||||
delegation.value && delegation.token.transfer(...concatOpts([delegation.to, delegation.value]), opts),
|
||||
delegation.tokenId &&
|
||||
delegation.token.ownerOf(delegation.tokenId).then(owner =>
|
||||
delegation.token.transferFrom(...concatOpts([ owner, delegation.to, delegation.tokenId ], opts)),
|
||||
),
|
||||
delegation.token
|
||||
.ownerOf(delegation.tokenId)
|
||||
.then(owner =>
|
||||
delegation.token.transferFrom(...concatOpts([owner, delegation.to, delegation.tokenId], opts)),
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
propose (opts = null) {
|
||||
propose(opts = null) {
|
||||
const proposal = this.currentProposal;
|
||||
|
||||
return this.governor.methods[
|
||||
@ -41,104 +42,90 @@ class GovernorHelper {
|
||||
](...concatOpts(proposal.fullProposal, opts));
|
||||
}
|
||||
|
||||
queue (opts = null) {
|
||||
queue(opts = null) {
|
||||
const proposal = this.currentProposal;
|
||||
|
||||
return proposal.useCompatibilityInterface
|
||||
? this.governor.methods['queue(uint256)'](...concatOpts(
|
||||
[ proposal.id ],
|
||||
opts,
|
||||
))
|
||||
: this.governor.methods['queue(address[],uint256[],bytes[],bytes32)'](...concatOpts(
|
||||
proposal.shortProposal,
|
||||
opts,
|
||||
));
|
||||
? this.governor.methods['queue(uint256)'](...concatOpts([proposal.id], opts))
|
||||
: this.governor.methods['queue(address[],uint256[],bytes[],bytes32)'](
|
||||
...concatOpts(proposal.shortProposal, opts),
|
||||
);
|
||||
}
|
||||
|
||||
execute (opts = null) {
|
||||
execute(opts = null) {
|
||||
const proposal = this.currentProposal;
|
||||
|
||||
return proposal.useCompatibilityInterface
|
||||
? this.governor.methods['execute(uint256)'](...concatOpts(
|
||||
[ proposal.id ],
|
||||
opts,
|
||||
))
|
||||
: this.governor.methods['execute(address[],uint256[],bytes[],bytes32)'](...concatOpts(
|
||||
proposal.shortProposal,
|
||||
opts,
|
||||
));
|
||||
? this.governor.methods['execute(uint256)'](...concatOpts([proposal.id], opts))
|
||||
: this.governor.methods['execute(address[],uint256[],bytes[],bytes32)'](
|
||||
...concatOpts(proposal.shortProposal, opts),
|
||||
);
|
||||
}
|
||||
|
||||
cancel (opts = null) {
|
||||
cancel(opts = null) {
|
||||
const proposal = this.currentProposal;
|
||||
|
||||
return proposal.useCompatibilityInterface
|
||||
? this.governor.methods['cancel(uint256)'](...concatOpts(
|
||||
[ proposal.id ],
|
||||
opts,
|
||||
))
|
||||
: this.governor.methods['$_cancel(address[],uint256[],bytes[],bytes32)'](...concatOpts(
|
||||
proposal.shortProposal,
|
||||
opts,
|
||||
));
|
||||
? this.governor.methods['cancel(uint256)'](...concatOpts([proposal.id], opts))
|
||||
: this.governor.methods['$_cancel(address[],uint256[],bytes[],bytes32)'](
|
||||
...concatOpts(proposal.shortProposal, opts),
|
||||
);
|
||||
}
|
||||
|
||||
vote (vote = {}, opts = null) {
|
||||
vote(vote = {}, opts = null) {
|
||||
const proposal = this.currentProposal;
|
||||
|
||||
return vote.signature
|
||||
// if signature, and either params or reason →
|
||||
? vote.params || vote.reason
|
||||
? vote.signature({
|
||||
proposalId: proposal.id,
|
||||
support: vote.support,
|
||||
reason: vote.reason || '',
|
||||
params: vote.params || '',
|
||||
}).then(({ v, r, s }) => this.governor.castVoteWithReasonAndParamsBySig(...concatOpts(
|
||||
[ proposal.id, vote.support, vote.reason || '', vote.params || '', v, r, s ],
|
||||
opts,
|
||||
)))
|
||||
: vote.signature({
|
||||
proposalId: proposal.id,
|
||||
support: vote.support,
|
||||
}).then(({ v, r, s }) => this.governor.castVoteBySig(...concatOpts(
|
||||
[ proposal.id, vote.support, v, r, s ],
|
||||
opts,
|
||||
)))
|
||||
? // if signature, and either params or reason →
|
||||
vote.params || vote.reason
|
||||
? vote
|
||||
.signature({
|
||||
proposalId: proposal.id,
|
||||
support: vote.support,
|
||||
reason: vote.reason || '',
|
||||
params: vote.params || '',
|
||||
})
|
||||
.then(({ v, r, s }) =>
|
||||
this.governor.castVoteWithReasonAndParamsBySig(
|
||||
...concatOpts([proposal.id, vote.support, vote.reason || '', vote.params || '', v, r, s], opts),
|
||||
),
|
||||
)
|
||||
: vote
|
||||
.signature({
|
||||
proposalId: proposal.id,
|
||||
support: vote.support,
|
||||
})
|
||||
.then(({ v, r, s }) =>
|
||||
this.governor.castVoteBySig(...concatOpts([proposal.id, vote.support, v, r, s], opts)),
|
||||
)
|
||||
: vote.params
|
||||
// otherwise if params
|
||||
? this.governor.castVoteWithReasonAndParams(...concatOpts(
|
||||
[ proposal.id, vote.support, vote.reason || '', vote.params ],
|
||||
opts,
|
||||
))
|
||||
: vote.reason
|
||||
// otherwise if reason
|
||||
? this.governor.castVoteWithReason(...concatOpts(
|
||||
[ proposal.id, vote.support, vote.reason ],
|
||||
opts,
|
||||
))
|
||||
: this.governor.castVote(...concatOpts(
|
||||
[ proposal.id, vote.support ],
|
||||
opts,
|
||||
));
|
||||
? // otherwise if params
|
||||
this.governor.castVoteWithReasonAndParams(
|
||||
...concatOpts([proposal.id, vote.support, vote.reason || '', vote.params], opts),
|
||||
)
|
||||
: vote.reason
|
||||
? // otherwise if reason
|
||||
this.governor.castVoteWithReason(...concatOpts([proposal.id, vote.support, vote.reason], opts))
|
||||
: this.governor.castVote(...concatOpts([proposal.id, vote.support], opts));
|
||||
}
|
||||
|
||||
waitForSnapshot (offset = 0) {
|
||||
waitForSnapshot(offset = 0) {
|
||||
const proposal = this.currentProposal;
|
||||
return this.governor.proposalSnapshot(proposal.id)
|
||||
return this.governor
|
||||
.proposalSnapshot(proposal.id)
|
||||
.then(blockNumber => time.advanceBlockTo(blockNumber.addn(offset)));
|
||||
}
|
||||
|
||||
waitForDeadline (offset = 0) {
|
||||
waitForDeadline(offset = 0) {
|
||||
const proposal = this.currentProposal;
|
||||
return this.governor.proposalDeadline(proposal.id)
|
||||
return this.governor
|
||||
.proposalDeadline(proposal.id)
|
||||
.then(blockNumber => time.advanceBlockTo(blockNumber.addn(offset)));
|
||||
}
|
||||
|
||||
waitForEta (offset = 0) {
|
||||
waitForEta(offset = 0) {
|
||||
const proposal = this.currentProposal;
|
||||
return this.governor.proposalEta(proposal.id)
|
||||
.then(timestamp => time.increaseTo(timestamp.addn(offset)));
|
||||
return this.governor.proposalEta(proposal.id).then(timestamp => time.increaseTo(timestamp.addn(offset)));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -146,7 +133,7 @@ class GovernorHelper {
|
||||
* 1) an array of objects [{ target, value, data, signature? }]
|
||||
* 2) an object of arrays { targets: [], values: [], data: [], signatures?: [] }
|
||||
*/
|
||||
setProposal (actions, description) {
|
||||
setProposal(actions, description) {
|
||||
let targets, values, signatures, data, useCompatibilityInterface;
|
||||
|
||||
if (Array.isArray(actions)) {
|
||||
@ -160,33 +147,25 @@ class GovernorHelper {
|
||||
({ targets, values, signatures = [], data } = actions);
|
||||
}
|
||||
|
||||
const fulldata = zip(signatures.map(s => s && web3.eth.abi.encodeFunctionSignature(s)), data)
|
||||
.map(hexs => concatHex(...hexs));
|
||||
const fulldata = zip(
|
||||
signatures.map(s => s && web3.eth.abi.encodeFunctionSignature(s)),
|
||||
data,
|
||||
).map(hexs => concatHex(...hexs));
|
||||
|
||||
const descriptionHash = web3.utils.keccak256(description);
|
||||
|
||||
// condensed version for queueing end executing
|
||||
const shortProposal = [
|
||||
targets,
|
||||
values,
|
||||
fulldata,
|
||||
descriptionHash,
|
||||
];
|
||||
const shortProposal = [targets, values, fulldata, descriptionHash];
|
||||
|
||||
// full version for proposing
|
||||
const fullProposal = [
|
||||
targets,
|
||||
values,
|
||||
...(useCompatibilityInterface ? [ signatures ] : []),
|
||||
data,
|
||||
description,
|
||||
];
|
||||
const fullProposal = [targets, values, ...(useCompatibilityInterface ? [signatures] : []), data, description];
|
||||
|
||||
// proposal id
|
||||
const id = web3.utils.toBN(web3.utils.keccak256(web3.eth.abi.encodeParameters(
|
||||
[ 'address[]', 'uint256[]', 'bytes[]', 'bytes32' ],
|
||||
shortProposal,
|
||||
)));
|
||||
const id = web3.utils.toBN(
|
||||
web3.utils.keccak256(
|
||||
web3.eth.abi.encodeParameters(['address[]', 'uint256[]', 'bytes[]', 'bytes32'], shortProposal),
|
||||
),
|
||||
);
|
||||
|
||||
this.currentProposal = {
|
||||
id,
|
||||
|
||||
Reference in New Issue
Block a user