Skip to content

Commit

Permalink
Expose Client712
Browse files Browse the repository at this point in the history
  • Loading branch information
bonustrack committed Nov 20, 2021
1 parent 01be6da commit e4604a7
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 27 deletions.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import Client712 from './sign';
import Client from './client';
import schemas from './schemas';
import utils from './utils';

export default {
Client,
Client712,
schemas,
utils
};
3 changes: 2 additions & 1 deletion src/sign/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ export default class Client {
if (web3 instanceof Wallet) signer = web3;
if (web3 instanceof Web3Provider) signer = web3.getSigner();
if (!message.from) message.from = address;
if (!message.timestamp) message.timestamp = parseInt((Date.now() / 1e3).toFixed());
if (!message.timestamp)
message.timestamp = parseInt((Date.now() / 1e3).toFixed());
const data: any = { domain, types, message };
const sig = await signer._signTypedData(domain, data.types, message);
console.log('Sign', { address, sig, data });
Expand Down
8 changes: 2 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,7 @@ export async function getSpaceUri(id, network = '1') {
try {
const hash = namehash(id);
const provider = getProvider(network);
uri = await call(
provider,
[abi],
[address, 'text', [hash, 'snapshot']]
);
uri = await call(provider, [abi], [address, 'text', [hash, 'snapshot']]);
} catch (e) {
console.log('getSpaceUriFromTextRecord failed', id, e);
}
Expand All @@ -185,7 +181,7 @@ export function clone(item) {
}

export async function sleep(time) {
return new Promise(resolve => {
return new Promise((resolve) => {
setTimeout(resolve, time);
});
}
Expand Down
14 changes: 7 additions & 7 deletions src/voting/quadratic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,33 @@ export default class ApprovalVoting {
const results = this.proposal.choices
.map((choice, i) =>
this.votes
.map(vote => quadraticMath(i, vote.choice, vote.balance))
.map((vote) => quadraticMath(i, vote.choice, vote.balance))
.reduce((a, b: any) => a + b, 0)
)
.map(sqrt => sqrt * sqrt);
.map((sqrt) => sqrt * sqrt);

return results
.map((res, i) => percentageOfTotal(i, results, results))
.map(p => (this.sumOfResultsBalance() / 100) * p);
.map((p) => (this.sumOfResultsBalance() / 100) * p);
}

resultsByStrategyScore() {
const results = this.proposal.choices
.map((choice, i) =>
this.strategies.map((strategy, sI) =>
this.votes
.map(vote => quadraticMath(i, vote.choice, vote.scores[sI]))
.map((vote) => quadraticMath(i, vote.choice, vote.scores[sI]))
.reduce((a, b: any) => a + b, 0)
)
)
.map(arr => arr.map(sqrt => [sqrt * sqrt]));
.map((arr) => arr.map((sqrt) => [sqrt * sqrt]));

return results.map((res, i) =>
this.strategies
.map((strategy, sI) => [
percentageOfTotal(0, results[i][sI], results.flat(2))
])
.map(p => [(this.sumOfResultsBalance() / 100) * p])
.map((p) => [(this.sumOfResultsBalance() / 100) * p])
);
}

Expand All @@ -76,7 +76,7 @@ export default class ApprovalVoting {
}% for ${choice}`;
}
})
.filter(el => el != null)
.filter((el) => el != null)
.join(', ');
}
}
14 changes: 7 additions & 7 deletions src/voting/rankedChoice.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getNumberWithOrdinal } from '../utils';

function irv(ballots, rounds) {
const candidates: any[] = [...new Set(ballots.map(vote => vote[0]).flat())];
const candidates: any[] = [...new Set(ballots.map((vote) => vote[0]).flat())];
const votes = Object.entries(
ballots.reduce((votes, [v], i, src) => {
votes[v[0]][0] += src[i][1];
Expand All @@ -11,7 +11,7 @@ function irv(ballots, rounds) {
))
: (votes[v[0]][1] = src[i][2]);
return votes;
}, Object.assign({}, ...candidates.map(c => ({ [c]: [0, []] }))))
}, Object.assign({}, ...candidates.map((c) => ({ [c]: [0, []] }))))
);

const votesWithoutScore = votes.map((vote: any) => [vote[0], vote[1][0]]);
Expand All @@ -30,7 +30,7 @@ function irv(ballots, rounds) {
const sortedByHighest = votes.sort((a: any, b: any) => b[1][0] - a[1][0]);

const totalPowerOfVotes = ballots
.map(bal => bal[1])
.map((bal) => bal[1])
.reduce((a, b: any) => a + b, 0);

rounds.push({
Expand All @@ -42,12 +42,12 @@ function irv(ballots, rounds) {
? rounds
: irv(
ballots
.map(ballot => [
ballot[0].filter(c => c != bottomCand),
.map((ballot) => [
ballot[0].filter((c) => c != bottomCand),
ballot[1],
ballot[2]
])
.filter(b => b[0].length > 0),
.filter((b) => b[0].length > 0),
rounds
);
}
Expand Down Expand Up @@ -97,7 +97,7 @@ export default class ApprovalVoting {

getChoiceString() {
return this.selected
.map(choice => {
.map((choice) => {
if (this.proposal.choices[choice - 1])
return this.proposal.choices[choice - 1];
})
Expand Down
12 changes: 6 additions & 6 deletions src/voting/weighted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,32 @@ export default class WeightedVoting {
resultsByVoteBalance() {
const results = this.proposal.choices.map((choice, i) =>
this.votes
.map(vote => weightedPower(i, vote.choice, vote.balance))
.map((vote) => weightedPower(i, vote.choice, vote.balance))
.reduce((a, b: any) => a + b, 0)
);

return results
.map((res, i) => percentageOfTotal(i, results, results))
.map(p => (this.sumOfResultsBalance() / 100) * p);
.map((p) => (this.sumOfResultsBalance() / 100) * p);
}

resultsByStrategyScore() {
const results = this.proposal.choices
.map((choice, i) =>
this.strategies.map((strategy, sI) =>
this.votes
.map(vote => weightedPower(i, vote.choice, vote.scores[sI]))
.map((vote) => weightedPower(i, vote.choice, vote.scores[sI]))
.reduce((a, b: any) => a + b, 0)
)
)
.map(arr => arr.map(pwr => [pwr]));
.map((arr) => arr.map((pwr) => [pwr]));

return results.map((res, i) =>
this.strategies
.map((strategy, sI) => [
percentageOfTotal(0, results[i][sI], results.flat(2))
])
.map(p => [(this.sumOfResultsBalance() / 100) * p])
.map((p) => [(this.sumOfResultsBalance() / 100) * p])
);
}

Expand All @@ -74,7 +74,7 @@ export default class WeightedVoting {
}% for ${choice}`;
}
})
.filter(el => el != null)
.filter((el) => el != null)
.join(', ');
}
}

0 comments on commit e4604a7

Please sign in to comment.