Skip to content
This repository has been archived by the owner on Sep 15, 2022. It is now read-only.

Commit

Permalink
Update packages and handlebars
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorVicente committed Jul 19, 2019
1 parent 8b4671f commit 722687d
Show file tree
Hide file tree
Showing 13 changed files with 561 additions and 361 deletions.
7 changes: 7 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
semi: true,
trailingComma: 'all',
singleQuote: true,
printWidth: 120,
tabWidth: 2,
};
118 changes: 85 additions & 33 deletions contract.handlebars
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
// tslint:disable:no-consecutive-blank-lines ordered-imports align trailing-comma whitespace class-name
// tslint:disable:no-consecutive-blank-lines ordered-imports align trailing-comma
// tslint:disable:whitespace no-unbound-method no-trailing-whitespace
// tslint:disable:no-unused-variable
// tslint:disable:no-unbound-method
import { BaseContract } from '@0x/base-contract';
import { BlockParam, BlockParamLiteral, CallData, ContractAbi, ContractArtifact, DecodedLogArgs, MethodAbi, TxData, TxDataPayable, SupportedProvider } from 'ethereum-types';
import { schemas } from '@0x/json-schemas';
import {
BlockParam,
BlockParamLiteral,
CallData,
ContractAbi,
ContractArtifact,
DecodedLogArgs,
MethodAbi,
TransactionReceiptWithDecodedLogs,
TxData,
TxDataPayable,
SupportedProvider,
} from 'ethereum-types';
import { BigNumber, classUtils, logUtils, providerUtils } from '@0x/utils';
import { SimpleContractArtifact } from '@0x/types';
import { Web3Wrapper } from '@0x/web3-wrapper';
import { assert } from '@0x/assert';
import { PolyResponse } from '../polyResponse';
import * as ethers from 'ethers';
// tslint:enable:no-unused-variable

{{#if events}}
export type {{contractName}}EventArgs =
{{#each events}}
| {{@root.contractName}}{{name}}EventArgs{{#if @last}};{{/if}}
| {{@root.contractName}}{{name}}EventArgs{{#if @last}};{{/if}}
{{/each}}

export enum {{contractName}}Events {
{{#each events}}
{{name}} = '{{name}}',
{{/each}}
{{#each events}}
{{name}} = '{{name}}',
{{/each}}
}

{{#each events}}
Expand All @@ -32,51 +46,89 @@ export enum {{contractName}}Events {
// tslint:disable:no-parameter-reassignment
// tslint:disable-next-line:class-name
export class {{contractName}}Contract extends BaseContract {
private _defaultEstimateGasFactor: number;
private _defaultEstimateGasFactor: number;
{{#each methods}}
{{#this.constant}}
{{> call contractName=../contractName}}
{{/this.constant}}
{{^this.constant}}
{{> tx contractName=../contractName}}
{{/this.constant}}
{{#this.constant}}
{{> call contractName=../contractName}}
{{/this.constant}}
{{^this.constant}}
{{> tx contractName=../contractName}}
{{/this.constant}}
{{/each}}
public static async deployAsync(
bytecode: string,
abi: ContractAbi,
supportedProvider: SupportedProvider,
txDefaults: Partial<TxData>,
{{> typed_params inputs=ctor.inputs}}
public static async deployAsync(
bytecode: string,
abi: ContractAbi,
supportedProvider: SupportedProvider,
txDefaults: Partial<TxData>,
{{> typed_params inputs=ctor.inputs}}
): Promise<{{contractName}}Contract> {
assert.isHexString('bytecode', bytecode);
assert.doesConformToSchema('txDefaults', txDefaults, schemas.txDataSchema, [
schemas.addressSchema,
schemas.numberSchema,
schemas.jsNumber,
]);
const provider = providerUtils.standardizeOrThrow(supportedProvider);
const constructorAbi = BaseContract._lookupConstructorAbi(abi);
[{{> params inputs=ctor.inputs}}] = BaseContract._formatABIDataItemList(
constructorAbi.inputs,
[{{> params inputs=ctor.inputs}}],
BaseContract._bigNumberToString,
constructorAbi.inputs,
[{{> params inputs=ctor.inputs}}],
BaseContract._bigNumberToString,
);
const iface = new ethers.utils.Interface(abi);
const deployInfo = iface.deployFunction;
const txData = deployInfo.encode(bytecode, [{{> params inputs=ctor.inputs}}]);
const web3Wrapper = new Web3Wrapper(provider);
const txDataWithDefaults = await BaseContract._applyDefaultsToTxDataAsync(
{data: txData},
txDefaults,
web3Wrapper.estimateGasAsync.bind(web3Wrapper),
{data: txData},
txDefaults,
web3Wrapper.estimateGasAsync.bind(web3Wrapper),
);
const txHash = await web3Wrapper.sendTransactionAsync(txDataWithDefaults);
logUtils.log(`transactionHash: ${txHash}`);
const txReceipt = await web3Wrapper.awaitTransactionSuccessAsync(txHash);
logUtils.log(`{{contractName}} successfully deployed at ${txReceipt.contractAddress}`);
const contractInstance = new {{contractName}}Contract(abi, txReceipt.contractAddress as string, provider, txDefaults);
const contractInstance = new {{contractName}}Contract(txReceipt.contractAddress as string, provider, txDefaults);
contractInstance.constructorArgs = [{{> params inputs=ctor.inputs}}];
return contractInstance;
}


/**
* @returns The contract ABI
*/
public static ABI(): ContractAbi {
const abi = [
{{#each ABI}}
{ {{#if (isDefined this.constant)}}
constant: {{constant}},{{/if}}{{#if (isDefined this.anonymous)}}
anonymous: {{anonymous}},{{/if}}
inputs: [
{{#each inputs}}
{{> abi_type this}}
{{/each}}
],{{#this.name}}
name: '{{{this}}}',{{/this.name}}
outputs: [
{{#each outputs}}
{{> abi_type this}}
{{/each}}
],{{#if (isDefined this.payable)}}
payable: {{payable}},{{/if}}{{#this.stateMutability}}
stateMutability: '{{this}}',{{/this.stateMutability}}
type: '{{type}}',
},
{{/each}}
] as ContractAbi;
return abi;
}
constructor(abi: ContractAbi, address: string, supportedProvider: SupportedProvider, txDefaults?: Partial<TxData>, defaultEstimateGasFactor?: number) {
super('{{contractName}}', abi, address, supportedProvider, txDefaults);
constructor(address: string, supportedProvider: SupportedProvider, txDefaults?: Partial<TxData>, defaultEstimateGasFactor?: number) {
super('{{contractName}}', {{contractName}}Contract.ABI(), address, supportedProvider, txDefaults);
this._defaultEstimateGasFactor = defaultEstimateGasFactor === undefined ? 1.1 : defaultEstimateGasFactor;
this._web3Wrapper.abiDecoder.addABI(abi);
classUtils.bindAll(this, ['_abiEncoderByFunctionSignature', 'address', 'abi', '_web3Wrapper', '_defaultEstimateGasFactor']);
classUtils.bindAll(this, ['_abiEncoderByFunctionSignature', 'address', '_web3Wrapper', '_defaultEstimateGasFactor']);
}
} // tslint:disable:max-file-line-count
// tslint:enable:no-unbound-method
}

// tslint:disable:max-file-line-count
// tslint:enable:no-unbound-method no-parameter-reassignment no-consecutive-blank-lines ordered-imports align
// tslint:enable:trailing-comma whitespace no-trailing-whitespace
30 changes: 16 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
"build:ci": "yarn build",
"lint": "tslint --format stylish --project .",
"uglify": "node_modules/uglifyjs-folder/cli.js lib/src/generated-wrappers -eo lib/src/generated-wrappers --pattern \"**/*.js,!**/*min.js\" -x .js",
"pre_build": "yarn clean && yarn generate_contract_wrappers",
"pre_build": "yarn clean && yarn generate_contract_wrappers && yarn prettier_contract_wrappers",
"clean": "shx rm -rf src/generated-wrappers",
"prettier_contract_wrappers": "prettier --write src/generated-wrappers/*",
"generate_contract_wrappers": "node_modules/@0x/abi-gen/bin/abi-gen.js --abis ${npm_package_config_abis} --template contract.handlebars --partials 'partials/**/*.handlebars' --output src/generated-wrappers"
},
"config": {
Expand All @@ -31,23 +32,24 @@
},
"homepage": "https://github.com/PolymathNetwork/polymath-abi-wrappers/blob/master/README.md",
"devDependencies": {
"@0x/abi-gen": "2.0.9",
"@0x/types": "2.2.2",
"@0x/abi-gen": "^2.1.1",
"@0x/types": "^2.4.0",
"@polymathnetwork/contract-artifacts": "3.0.0-beta.5",
"@types/lodash": "^4.14.134",
"@types/node": "^10.14.10",
"shx": "^0.2.2",
"tslint": "5.11.0",
"@types/lodash": "^4.14.136",
"@types/node": "^12.6.8",
"prettier": "^1.18.2",
"shx": "^0.3.2",
"tslint": "5.18.0",
"uglifyjs-folder": "^1.5.1"
},
"dependencies": {
"@0x/base-contract": "^5.1.0",
"@0x/utils": "^4.3.3",
"@0x/web3-wrapper": "^6.0.6",
"ethereum-types": "^2.1.2",
"ethers": "^4.0.30",
"lodash": "^4.17.11",
"typescript": "3.2.2"
"@0x/base-contract": "^5.1.1",
"@0x/utils": "^4.4.0",
"@0x/web3-wrapper": "^6.0.7",
"ethereum-types": "^2.1.3",
"ethers": "^4.0.33",
"lodash": "^4.17.15",
"typescript": "3.5.3"
},
"publishConfig": {
"access": "public"
Expand Down
10 changes: 10 additions & 0 deletions partials/abi_type.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
name: '{{name}}',
type: '{{type}}',{{#if (isDefined indexed)}}
indexed: {{indexed}},{{/if}}{{#if components}}
components: [
{{#each components}}
{{> abi_type this}}
{{/each}}
]{{/if}}
},
4 changes: 2 additions & 2 deletions partials/call.handlebars
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
public {{this.tsName}} = {
public {{languageSpecificName}} = {
{{> callAsync}}
};
};
37 changes: 30 additions & 7 deletions partials/callAsync.handlebars
Original file line number Diff line number Diff line change
@@ -1,23 +1,46 @@
async callAsync(
{{> typed_params inputs=inputs}}
callData: Partial<CallData> = {},
callData: Partial<CallData> = {},
defaultBlock?: BlockParam,
): Promise<{{> return_type outputs=outputs}}> {
{{#each inputs}}
{{#assertionType name type}}{{/assertionType}}
{{/each}}
assert.doesConformToSchema('callData', callData, schemas.callDataSchema, [
schemas.addressSchema,
schemas.numberSchema,
schemas.jsNumber,
]);
if (defaultBlock !== undefined) {
assert.isBlockParam('defaultBlock', defaultBlock);
}
const self = this as any as {{contractName}}Contract;
const encodedData = self._strictEncodeArguments('{{this.functionSignature}}', [{{> params inputs=inputs}}]);
const callDataWithDefaults = await BaseContract._applyDefaultsToTxDataAsync(
{
to: self.address,
...callData,
data: encodedData,
},
self._web3Wrapper.getContractDefaults(),
{
to: self.address,
...callData,
data: encodedData,
},
self._web3Wrapper.getContractDefaults(),
);
callDataWithDefaults.from = callDataWithDefaults.from ? callDataWithDefaults.from.toLowerCase() : callDataWithDefaults.from;

const rawCallResult = await self._web3Wrapper.callAsync(callDataWithDefaults, defaultBlock);
BaseContract._throwIfRevertWithReasonCallResult(rawCallResult);
const abiEncoder = self._lookupAbiEncoder('{{this.functionSignature}}');
// tslint:disable boolean-naming
const result = abiEncoder.strictDecodeReturnValue<{{> return_type outputs=outputs}}>(rawCallResult);
// tslint:enable boolean-naming
return result;
},
getABIEncodedTransactionData(
{{> typed_params inputs=inputs}}
): string {
{{#each inputs}}
{{#assertionType name type}}{{/assertionType}}
{{/each}}
const self = this as any as {{contractName}}Contract;
const abiEncodedTransactionData = self._strictEncodeArguments('{{this.functionSignature}}', [{{> params inputs=inputs}}]);
return abiEncodedTransactionData;
},
2 changes: 1 addition & 1 deletion partials/event.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export interface {{@root.contractName}}{{name}}EventArgs extends DecodedLogArgs
{{#each inputs}}
{{name}}: {{#returnType type components}}{{/returnType}};
{{/each}}
}
}
3 changes: 3 additions & 0 deletions partials/normalized_params.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{#each inputs}}
{{name}}{{#ifEquals 'address' type}}.toLowerCase(){{/ifEquals}}{{#if @last}}{{else}},{{/if}}
{{/each}}
2 changes: 1 addition & 1 deletion partials/params.handlebars
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{{#each inputs}}
{{name}}{{#if @last}}{{else}},{{/if}}
{{/each}}
{{/each}}
2 changes: 1 addition & 1 deletion partials/return_type.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
{{/singleReturnValue}}
{{else}}
void
{{/if}}
{{/if}}
Loading

0 comments on commit 722687d

Please sign in to comment.