Skip to content

Commit

Permalink
Upgrade to Arc v56 (#362)
Browse files Browse the repository at this point in the history
Upgrade to Arc v56, add reputation initialization contract wrappers.
  • Loading branch information
dkent600 authored Oct 25, 2018
1 parent d33a5b3 commit eb917dc
Show file tree
Hide file tree
Showing 30 changed files with 1,828 additions and 123 deletions.
18 changes: 16 additions & 2 deletions lib/contractWrapperBase.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BigNumber } from "bignumber.js";
import { promisify } from "es6-promisify";
import { Address, Hash, SchemePermissions } from "./commonTypes";
import { Address, DefaultSchemePermissions, Hash, SchemePermissions } from "./commonTypes";
import { ConfigService } from "./configService";
import { ControllerService } from "./controllerService";
import {
Expand Down Expand Up @@ -193,6 +193,20 @@ export abstract class ContractWrapperBase implements IContractWrapper {
return Math.max(Math.min((await func.estimateGas(...params)), maxGasLimit), 21000);
}

/**
* TODO: getDefaultPermissions should be moved to a new subclass `SchemeWrapper`
* which itself would be a base class for a new class `UniversalScheme`
*/

/**
* Any scheme that needs greater permissions should override this
* @hidden - for internal use only.
* This method will eventually be moved (see comment above)
*/
public getDefaultPermissions(): SchemePermissions {
return DefaultSchemePermissions.MinimumPermissions as number;
}

/**
* invoked to let base classes know that the `contract` is available.
*/
Expand Down Expand Up @@ -325,7 +339,7 @@ export abstract class ContractWrapperBase implements IContractWrapper {
}

if (ConfigService.get("estimateGas") && !web3Params.gas) {
await this.estimateGas(func, params)
await this.estimateGas(func, params, web3Params)
.then((gas: number) => {
// side-effect of altering web3Params allows caller to know what we used
Object.assign(web3Params, { gas });
Expand Down
13 changes: 7 additions & 6 deletions lib/contractWrapperFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ export class ContractWrapperFactory<TWrapper extends IContractWrapper>
return this.getHydratedWrapper(getWrapper);
}

public async ensureSolidityContract(): Promise<any> {
if (!this.solidityContract) {
this.solidityContract = await Utils.requireContract(this.solidityContractName);
}
return this.solidityContract;
}

protected async estimateConstructorGas(...params: Array<any>): Promise<number> {
const web3 = await Utils.getWeb3();
await this.ensureSolidityContract();
Expand Down Expand Up @@ -177,10 +184,4 @@ export class ContractWrapperFactory<TWrapper extends IContractWrapper>
addressMap.set(wrapper.address, wrapper);
}
}

private async ensureSolidityContract(): Promise<void> {
if (!this.solidityContract) {
this.solidityContract = await Utils.requireContract(this.solidityContractName);
}
}
}
11 changes: 7 additions & 4 deletions lib/controllerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,16 @@ export class ControllerService {
* TODO: check for previous and future versions of UController here
*/
const UControllerContract = await Utils.requireContract("UController");
const ControllerContract = await Utils.requireContract("Controller");
const uControllerAddress = (await UControllerContract.deployed()).address;

this.isUController = uControllerAddress === controllerAddress;
this.controller = this.isUController ?
await UControllerContract.at(controllerAddress) :
await ControllerContract.at(controllerAddress);

if (this.isUController) {
this.controller = await UControllerContract.at(controllerAddress);
} else {
const ControllerContract = await Utils.requireContract("Controller");
this.controller = await ControllerContract.at(controllerAddress);
}
}
}
return this.controller;
Expand Down
1 change: 1 addition & 0 deletions lib/iContractWrapperBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export interface IContractWrapperFactory<TWrapper extends IContractWrapper> {
new: (...rest: Array<any>) => Promise<TWrapper>;
at: (address: string) => Promise<TWrapper>;
deployed: () => Promise<TWrapper>;
ensureSolidityContract(): Promise<any>;
}

export class ArcTransactionResult {
Expand Down
6 changes: 6 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ export * from "./wrappers/commonEventInterfaces";
export * from "./wrappers/contributionReward";
export * from "./wrappers/daoCreator";
export * from "./wrappers/daoToken";
export * from "./wrappers/locking4Reputation";
export * from "./wrappers/lockingEth4Reputation";
export * from "./wrappers/lockingToken4Reputation";
export * from "./wrappers/fixedReputationAllocation";
export * from "./wrappers/externalLocking4Reputation";
export * from "./wrappers/auction4Reputation";
export * from "./wrappers/genesisProtocol";
export * from "./wrappers/globalConstraintRegistrar";
export * from "./wrappers/iBurnableToken";
Expand Down
2 changes: 1 addition & 1 deletion lib/proposalService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class ProposalService {
(event: DecodedLogEntryEvent<NewProposalEventResult>): Promise<VotableProposal> => {
return Promise.resolve(
{
avatarAddress: event.args._avatar,
avatarAddress: event.args._organization,
numOfChoices: event.args._numOfChoices.toNumber(),
paramsHash: event.args._paramsHash,
proposalId: event.args._proposalId,
Expand Down
2 changes: 1 addition & 1 deletion lib/transactionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ export class TransactionService extends PubSubEventService {
public static async getTransactionDepth(tx: Hash | TransactionReceipt): Promise<number> {

const web3 = await Utils.getWeb3();
const lastBlockNum = await UtilsInternal.lastBlock();
const lastBlockNum = await UtilsInternal.lastBlockNumber();
let receipt: TransactionReceipt;

if (typeof tx === "string") {
Expand Down
10 changes: 9 additions & 1 deletion lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class Utils {

static get NULL_ADDRESS(): Address { return "0x0000000000000000000000000000000000000000"; }
static get NULL_HASH(): Hash { return "0x0000000000000000000000000000000000000000000000000000000000000000"; }
public static contractCache: Map<string, Contract> = new Map<string, string>();
/**
* Returns Truffle contract wrapper given the name of the contract (like "SchemeRegistrar").
* Optimized for synchronicity issues encountered with MetaMask.
Expand All @@ -24,8 +25,14 @@ export class Utils {
*/
public static async requireContract(contractName: string): Promise<any> {
try {
let contract = Utils.contractCache.get(contractName);
if (contract) {
LoggingService.debug(`requireContract: loaded from cache ${contractName}`);
return contract;
}

const artifact = require(`../migrated_contracts/${contractName}.json`);
const contract = new Contract(artifact);
contract = new Contract(artifact);
const myWeb3 = await Utils.getWeb3();

contract.setProvider(myWeb3.currentProvider);
Expand All @@ -34,6 +41,7 @@ export class Utils {
from: await Utils.getDefaultAccount(),
gas: ConfigService.get("defaultGasLimit"),
});
Utils.contractCache.set(contractName, contract);
LoggingService.debug(`requireContract: loaded ${contractName}`);
return contract;
} catch (ex) {
Expand Down
28 changes: 25 additions & 3 deletions lib/utilsInternal.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { promisify } from "es6-promisify";
import { FilterResult } from "web3";
import { fnVoid } from "./commonTypes";
import { BlockWithoutTransactionData, FilterResult } from "web3";
import { Address, fnVoid } from "./commonTypes";
import { Utils, Web3 } from "./utils";

/**
Expand All @@ -22,7 +22,25 @@ export class UtilsInternal {
/**
* Returns the last mined block in the chain.
*/
public static async lastBlock(): Promise<number> {
public static async lastBlock(): Promise<BlockWithoutTransactionData> {
const web3 = await Utils.getWeb3();
return promisify((callback: any): any => web3.eth.getBlock("latest", callback))() as any;
}

/**
* Returns the date of the last mined block in the chain.
*/
public static async lastBlockDate(): Promise<Date> {
const web3 = await Utils.getWeb3();
const block = await promisify((callback: any): any =>
web3.eth.getBlock("latest", callback))() as BlockWithoutTransactionData;
return new Date(block.timestamp * 1000);
}

/**
* Returns the last mined block in the chain.
*/
public static async lastBlockNumber(): Promise<number> {
const web3 = await Utils.getWeb3();
return promisify(web3.eth.getBlockNumber)();
}
Expand All @@ -43,6 +61,10 @@ export class UtilsInternal {
return (Utils as any).web3;
}

public static isNullAddress(address: Address): boolean {
return !address || !Number.parseInt(address, 16);
}

/**
* Returns promise of the maximum gasLimit that we dare to ever use, given the
* current state of the chain.
Expand Down
12 changes: 6 additions & 6 deletions lib/web3EventService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,10 @@ export class Web3EventService {

// handler that takes the events and issues givenCallback appropriately
const handleEvent =
(error: Error,
log: DecodedLogEntryEvent<TEventArgs> | Array<DecodedLogEntryEvent<TEventArgs>>,
async (error: Error,
log: DecodedLogEntryEvent<TEventArgs> | Array<DecodedLogEntryEvent<TEventArgs>>,
// singly true to issue callback on every arg rather than on the array
singly: boolean,
singly: boolean,
/*
* invoke this callback on every event (watch)
* or on the array of events (get), depending on the value of singly.
Expand All @@ -195,7 +195,7 @@ export class Web3EventService {
* when not singly, callback gets a promise of the array of entities.
* get is not singly. so get gets a promise of an array.
*/
callback?: (error: Error, args: TEntity | Promise<Array<TEntity>>) => void):
callback?: (error: Error, args: TEntity | Promise<Array<TEntity>>) => void):
Promise<Array<TEntity>> => {

const promiseOfEntities: Promise<Array<TEntity>> =
Expand All @@ -217,7 +217,7 @@ export class Web3EventService {
const transformedEntity = await transformEventCallback(event);
if (typeof transformedEntity !== "undefined") {
if (callback && singly) {
callback(error, transformedEntity);
await callback(error, transformedEntity);
}
entities.push(transformedEntity);
}
Expand All @@ -226,7 +226,7 @@ export class Web3EventService {
});
// invoke the given callback with the promise of an array of entities
if (callback && !singly) {
callback(error, promiseOfEntities);
await callback(error, promiseOfEntities);
}
return promiseOfEntities;
};
Expand Down
Loading

0 comments on commit eb917dc

Please sign in to comment.