Skip to content

Commit

Permalink
Xf/v4.0.4 (#60)
Browse files Browse the repository at this point in the history
* Add optional gas parameter in ContractEntryDefinition

* added named outputs

* Version numbers and CHANGELOG.
  • Loading branch information
xf00f authored Aug 26, 2019
1 parent a79a5d9 commit 5c7bc62
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 14 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All notable changes to this project will be documented in this file.

## [4.0.4] - 2019-08-26

- Add optional `gas` property to ABI interface as Vyper includes an estimate.
- Better typing on contract functions with multiple return types.

## [4.0.3] - 2019-06-27

- Fix `web3` undefined error in `eth.fromCurrentProvider()`.
Expand Down Expand Up @@ -172,6 +177,7 @@ All notable changes to this project will be documented in this file.

- Initial release of Typescript port from web3.js.

[4.0.4]: https://github.com/xf00f/web3x/compare/v4.0.3...v4.0.4
[4.0.3]: https://github.com/xf00f/web3x/compare/v4.0.2...v4.0.3
[4.0.2]: https://github.com/xf00f/web3x/compare/v4.0.1...v4.0.2
[4.0.1]: https://github.com/xf00f/web3x/compare/v4.0.0...v4.0.1
Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"4.0.3"
"4.0.4"
2 changes: 1 addition & 1 deletion web3x-codegen/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "web3x-codegen",
"version": "4.0.1",
"version": "4.0.4",
"license": "MIT",
"description": "Contract interface code generator for web3x.",
"repository": {
Expand Down
77 changes: 66 additions & 11 deletions web3x-codegen/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@

import fs from 'fs';
import { ensureDirSync } from 'fs-extra';
import ts, { ClassElement } from 'typescript';
import ts, {
ClassElement,
PropertySignature,
SyntaxKind,
TypeNode
} from 'typescript';
import { AbiInput, AbiOutput, ContractAbiDefinition, ContractEntryDefinition } from 'web3x/contract';
import { ContractBuildData, loadDataFromConfig } from './sources';
import { Config } from './sources/config';
Expand Down Expand Up @@ -264,31 +269,81 @@ function makeParameter(input: AbiInput, index: number) {
);
}

function getCallReturnType(outputs: AbiOutput[]) {
if (outputs.length === 1) {
return ts.createTypeReferenceNode('TxCall', [getTsTypeFromSolidityType(outputs[0], true)]);
function generateReturnTypes(outputs: AbiOutput[]): ReadonlyArray<TypeNode> {
if (outputs.length === 0){
return []
} else if (outputs.length === 1) {
// original return value.
return [getTsTypeFromSolidityType(outputs[0], true)]
} else {
const returnType = ts.createTupleTypeNode(outputs.map(i => getTsTypeFromSolidityType(i, true)));
return ts.createTypeReferenceNode('TxCall', [returnType]);
// multiple return values: return an object.
const propSigs: PropertySignature[] = [];
for (let index = 0; index < outputs.length; index++){
const output = outputs[index];
const type = getTsTypeFromSolidityType(output as AbiInput, true);
if (output.name) {
// name exists for the output: create a key for that
const nameSig = ts.createPropertySignature(
undefined,
ts.createStringLiteral(output.name),
undefined,
type,
undefined
);
propSigs.push(nameSig);
}
// always create a key for the index.
const indexSig = ts.createPropertySignature(
undefined,
ts.createNumericLiteral(index.toString()),
undefined,
type,
undefined
);
propSigs.push(indexSig);
}
return [ts.createTypeLiteralNode(propSigs)]
}
}

function getOutputType(name: string, definition: ContractEntryDefinition) {
if (!definition.stateMutability) {
if (definition.outputs && definition.outputs.length) {
return getCallReturnType(definition.outputs);
return ts.createTypeReferenceNode(
'TxCall', generateReturnTypes(definition.outputs)
);
} else {
return ts.createTypeReferenceNode('TxSend', [ts.createTypeReferenceNode(`${name}TransactionReceipt`, undefined)]);
return ts.createTypeReferenceNode(
'TxSend',
[
ts.createTypeReferenceNode(
`${name}TransactionReceipt`, undefined
)
]
);
}
}
if (definition.stateMutability === 'view' || definition.stateMutability === 'pure') {
if (definition.outputs && definition.outputs.length) {
return getCallReturnType(definition.outputs);
return ts.createTypeReferenceNode(
'TxCall', generateReturnTypes(definition.outputs)
);
} else {
return undefined;
return ts.createTypeReferenceNode(
'TxCall',
[ts.createKeywordTypeNode(SyntaxKind.VoidKeyword)]
);
}
} else {
return ts.createTypeReferenceNode('TxSend', [ts.createTypeReferenceNode(`${name}TransactionReceipt`, undefined)]);
return ts.createTypeReferenceNode(
'TxSend',
[
ts.createTypeReferenceNode(
`${name}TransactionReceipt`,
undefined
)
]
);
}
}

Expand Down
2 changes: 1 addition & 1 deletion web3x/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "web3x",
"version": "4.0.3",
"version": "4.0.4",
"license": "LGPL-3.0",
"description": "Typescript port of web3.js",
"repository": {
Expand Down
1 change: 1 addition & 0 deletions web3x/src/contract/abi/contract-abi-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface ContractEntryDefinition {
type: 'function' | 'constructor' | 'event' | 'fallback';
stateMutability?: 'pure' | 'view' | 'payable' | 'nonpayable';
signature?: string;
gas?: number;
}

export type ContractAbiDefinition = ContractEntryDefinition[];

0 comments on commit 5c7bc62

Please sign in to comment.