Skip to content

Commit

Permalink
Merge branch 'master' into am9u/ch639/dapp-propose-reparameterization
Browse files Browse the repository at this point in the history
  • Loading branch information
am9u committed May 21, 2018
2 parents 39b5306 + 1014cba commit 6faed2e
Show file tree
Hide file tree
Showing 19 changed files with 505 additions and 191 deletions.
4 changes: 4 additions & 0 deletions packages/core/src/civil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ export class Civil {
this.contentProvider = new providerConstructor({ ethApi: this.ethApi });
}

public toBigNumber(num: number | string): any {
return this.ethApi.toBigNumber(num);
}

/**
* @returns Currently default user account used, undefined if none unlocked/found
*/
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/contracts/newsroom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,8 @@ export class Newsroom extends BaseWrapper<NewsroomContract> {
}

private async resolveSignedData(baseHeader: BaseContentHeader): Promise<SignedContentHeader | undefined> {
const [author, signature] = await this.instance.signedContent.callAsync(new BigNumber(baseHeader.id));
const id = this.ethApi.toBigNumber(baseHeader.id);
const [author, signature] = await this.instance.signedContent.callAsync(id);
if (is0x0Address(author)) {
return undefined;
}
Expand All @@ -449,7 +450,7 @@ export class Newsroom extends BaseWrapper<NewsroomContract> {
}

private async resolveBaseContentHeader(articleId: number | BigNumber): Promise<BaseContentHeader> {
const id = new BigNumber(articleId);
const id = this.ethApi.toBigNumber(typeof articleId === "number" ? articleId : articleId.toNumber());

const [contentHash, uri, timestamp] = await this.instance.content.callAsync(id);
return {
Expand Down
13 changes: 13 additions & 0 deletions packages/core/src/utils/ethapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,19 @@ export class EthApi {
};
}

/**
* Converts a given number into a BigNumber instance
* @param numberOrHexString
*/
public toBigNumber(numberOrHexString: number | string): any {
// This is a proxy method, for web3.toBigNumber, that exists
// to ensure that we're creating instances of BigNumber that are
// compatible with the version of web3 we're currently using. We
// can likely get rid of this (or modify it) if/when we switch to
// Web3 1.0.0 (or another modern version)
return this.web3.toBigNumber(numberOrHexString);
}

/**
* Awaits to confirm that the transaction was succesfull
* @param txHash Transaction hash which will be checked
Expand Down
58 changes: 40 additions & 18 deletions packages/dapp/src/apis/civilTCR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import { getCivil, getTCR } from "../helpers/civilInstance";
import { TwoStepEthTransaction, EthAddress } from "@joincivil/core";
import { getVoteSaltHash } from "@joincivil/utils";

function ensureWeb3BigNumber(num: number | BigNumber): any {
const tNum = typeof num === "number" ? num : num.toNumber();
const civil = getCivil();
return civil.toBigNumber(tNum);
}

export async function approveForChallenge(): Promise<TwoStepEthTransaction | void> {
const civil = getCivil();
const tcr = civil.tcrSingletonTrusted();
Expand Down Expand Up @@ -35,18 +41,19 @@ export async function approveForChallengeGrantedAppeal(): Promise<TwoStepEthTran
return approve(appealFee);
}

export async function approve(amount: BigNumber): Promise<TwoStepEthTransaction | void> {
export async function approve(amount: number | BigNumber): Promise<TwoStepEthTransaction | void> {
console.log("approve");
const civil = getCivil();
console.log("civil gotten.");
console.log("civil gotten.", civil);
const tcr = civil.tcrSingletonTrusted();
console.log("tcr gotten.");
const token = await tcr.getToken();
const amountBN = ensureWeb3BigNumber(amount);
console.log("token address: " + token.address);
const approvedTokens = await token.getApprovedTokensForSpender(tcr.address);
console.log("approved tokens: " + approvedTokens + " - amount: " + amount);
if (approvedTokens < amount) {
return token.approveSpender(tcr.address, amount);
if (approvedTokens < amountBN) {
return token.approveSpender(tcr.address, amountBN);
}
}

Expand Down Expand Up @@ -83,11 +90,15 @@ export async function challengeListing(address: EthAddress): Promise<TwoStepEthT
}

export async function commitVote(
pollID: BigNumber,
voteOption: BigNumber,
salt: BigNumber,
numTokens: BigNumber,
_pollID: BigNumber,
_voteOption: BigNumber,
_salt: BigNumber,
_numTokens: BigNumber,
): Promise<TwoStepEthTransaction> {
const pollID = ensureWeb3BigNumber(_pollID);
const voteOption = ensureWeb3BigNumber(_voteOption);
const salt = ensureWeb3BigNumber(_salt);
const numTokens = ensureWeb3BigNumber(_numTokens);
const civil = getCivil();
const tcr = civil.tcrSingletonTrusted();
const secretHash = getVoteSaltHash(voteOption.toString(), salt.toString());
Expand All @@ -97,10 +108,13 @@ export async function commitVote(
return voting.commitVote(pollID, secretHash, numTokens, prevPollID);
}

export async function depositTokens(address: EthAddress, numTokens: BigNumber): Promise<TwoStepEthTransaction> {
export async function depositTokens(
address: EthAddress,
numTokens: number | BigNumber,
): Promise<TwoStepEthTransaction> {
const civil = getCivil();
const tcr = civil.tcrSingletonTrusted();
return tcr.deposit(address, numTokens);
return tcr.deposit(address, ensureWeb3BigNumber(numTokens));
}

export async function appealChallenge(address: EthAddress): Promise<TwoStepEthTransaction> {
Expand Down Expand Up @@ -173,31 +187,39 @@ export async function requestVotingRights(numTokens: BigNumber): Promise<TwoStep
const voting = tcr.getVoting();
const eip = await tcr.getToken();

const numTokensBN = ensureWeb3BigNumber(numTokens);

const approvedTokensForSpender = await eip.getApprovedTokensForSpender(voting.address);
if (approvedTokensForSpender < numTokens) {
const approveSpenderReceipt = await eip.approveSpender(voting.address, numTokens);
if (approvedTokensForSpender < numTokensBN) {
const approveSpenderReceipt = await eip.approveSpender(voting.address, numTokensBN);
await approveSpenderReceipt.awaitReceipt();
}

return voting.requestVotingRights(numTokens);
return voting.requestVotingRights(numTokensBN);
}

export async function revealVote(
pollID: BigNumber,
voteOption: BigNumber,
salt: BigNumber,
_pollID: BigNumber,
_voteOption: BigNumber,
_salt: BigNumber,
): Promise<TwoStepEthTransaction> {
const pollID = ensureWeb3BigNumber(_pollID);
const voteOption = ensureWeb3BigNumber(_voteOption);
const salt = ensureWeb3BigNumber(_salt);
const civil = getCivil();
const tcr = civil.tcrSingletonTrusted();
const voting = tcr.getVoting();

return voting.revealVote(pollID, voteOption, salt);
}

export async function withdrawTokens(address: EthAddress, numTokens: BigNumber): Promise<TwoStepEthTransaction> {
export async function withdrawTokens(
address: EthAddress,
numTokens: number | BigNumber,
): Promise<TwoStepEthTransaction> {
const civil = getCivil();
const tcr = civil.tcrSingletonTrusted();
return tcr.withdraw(address, numTokens);
return tcr.withdraw(address, ensureWeb3BigNumber(numTokens));
}

export async function proposeReparameterization(
Expand Down
52 changes: 0 additions & 52 deletions packages/dapp/src/components/ListingListItem.tsx

This file was deleted.

111 changes: 0 additions & 111 deletions packages/dapp/src/components/Listings.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion packages/dapp/src/components/Main.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from "react";
import { Switch, Route, withRouter, RouteComponentProps } from "react-router-dom";
import Listings from "./Listings";
import Listings from "./listinglist/Listings";
import Newsroom from "./newsroom/Newsroom";
import Contracts from "./Contracts";
import ContractPage from "./ContractPage";
Expand Down
2 changes: 1 addition & 1 deletion packages/dapp/src/components/docs/json/Newsroom.json

Large diffs are not rendered by default.

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions packages/dapp/src/components/listinglist/ListItemStyle.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import styled from "styled-components";

export const SectionHeader = styled.span`
font-family: Libre-Franklin;
font-size: 12pt;
`;
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ import { Set } from "immutable";
import ListingListItem from "./ListingListItem";

const StyledUl = styled.ul`
display: flex;
flex-direction: column;
list-style: none;
padding: 0;
margin: 0;
width: 100%
color: black;
align-items: center;
`;

const StyledLI = styled.li`
width: 80%;
`;

export interface ListingListOwnProps {
Expand All @@ -21,13 +26,15 @@ class ListingList extends React.Component<ListingListOwnProps> {
}

public render(): JSX.Element {
let index = 0;
return (
<StyledUl>
{this.props.listings.map(l => {
index++;
return (
<li key={l}>
<ListingListItem listingAddress={l!} />
</li>
<StyledLI key={l}>
<ListingListItem listingAddress={l!} even={index % 2 === 0} />
</StyledLI>
);
})}
</StyledUl>
Expand Down
Loading

0 comments on commit 6faed2e

Please sign in to comment.