Skip to content

Commit

Permalink
codeComplete (#367)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkent600 authored Nov 1, 2018
1 parent 5b3023b commit a906cba
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 43 deletions.
14 changes: 10 additions & 4 deletions lib/contractWrapperFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export class ContractWrapperFactory<TWrapper extends IContractWrapper>
* @param rest Optional arguments to the Arc contracts constructor.
*/
public async new(...rest: Array<any>): Promise<TWrapper> {

await this.ensureSolidityContract();

let gas;
Expand Down Expand Up @@ -77,6 +78,7 @@ export class ContractWrapperFactory<TWrapper extends IContractWrapper>
* @param address
*/
public async at(address: string): Promise<TWrapper> {

await this.ensureSolidityContract();

const getWrapper = (): Promise<TWrapper> => {
Expand All @@ -93,6 +95,7 @@ export class ContractWrapperFactory<TWrapper extends IContractWrapper>
* Returns undefined if not found.
*/
public async deployed(): Promise<TWrapper> {

await this.ensureSolidityContract();

const getWrapper = (): Promise<TWrapper> => {
Expand All @@ -103,15 +106,18 @@ export class ContractWrapperFactory<TWrapper extends IContractWrapper>
}

public async ensureSolidityContract(): Promise<any> {
if (!this.solidityContract) {
this.solidityContract = await Utils.requireContract(this.solidityContractName);
}
return this.solidityContract;
/**
* requireContract caches and uncaches the contract appropriately
*/
return Utils.requireContract(this.solidityContractName)
.then((contract: any): any => this.solidityContract = contract);
}

protected async estimateConstructorGas(...params: Array<any>): Promise<number> {

const web3 = await Utils.getWeb3();
await this.ensureSolidityContract();

const callData = (web3.eth.contract(this.solidityContract.abi).new as any).getData(
...params,
{
Expand Down
22 changes: 10 additions & 12 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import { providers as Web3Providers, Web3 } from "web3";
import { Address, Hash, SchemePermissions } from "./commonTypes";
import { ConfigService } from "./configService";
import { LoggingService } from "./loggingService";
// haven't figured out how to get web3 typings to properly expose the Web3 constructor.
// v1.0 may improve on this entire Web3 typings experience
/* tslint:disable-next-line:no-var-requires */
const webConstructor = require("web3");

export class Utils {

Expand Down Expand Up @@ -62,12 +58,17 @@ export class Utils {

LoggingService.debug("Utils.getWeb3: getting web3");

Utils.contractCache.clear();
Utils.clearContractCache();

let preWeb3;

let globalWeb3;

// haven't figured out how to get web3 typings to properly expose the Web3 constructor.
// v1.0 may improve on this entire Web3 typings experience
/* tslint:disable-next-line:no-var-requires */
const webConstructor = require("web3");

if ((typeof global !== "undefined") &&
(global as any).web3 &&
((typeof window === "undefined") || (global as any).web3 !== (window as any).web3)) {
Expand Down Expand Up @@ -354,18 +355,15 @@ export class Utils {
return Utils.getTokenBalance(agentAddress, genTokenAddress);
}

/**
* Returns the truffle artifact json for the given contract
* @param contractName
*/
public static getTruffleArtifactForContract(contractName: string): any {
return require(`../migrated_contracts/${contractName}.json`);
}
private static contractCache: Map<string, Contract> = new Map<string, string>();

private static web3: Web3 = undefined;
private static alreadyTriedAndFailed: boolean = false;
private static networkId: number;

private static clearContractCache(): void {
Utils.contractCache.clear();
}
}

export { Web3 } from "web3";
56 changes: 30 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@daostack/arc.js",
"version": "0.0.0-alpha.86",
"version": "0.0.0-alpha.87",
"description": "A JavaScript library for interacting with @daostack/arc ethereum smart contracts",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
5 changes: 5 additions & 0 deletions test/absoluteVote.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { assert } from "chai";
import { InitializeArcJs } from "../lib";
import {
AbsoluteVoteFactory,
} from "../lib/wrappers/absoluteVote";
import * as helpers from "./helpers";

beforeEach(async () => {
await InitializeArcJs();
});

describe("AbsoluteVote", () => {

it("can get params hash", async () => {
Expand Down

0 comments on commit a906cba

Please sign in to comment.