Skip to content

Commit

Permalink
Milestone 3 main functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
varex83 committed Feb 1, 2023
1 parent 6ebf49e commit 46b10d4
Show file tree
Hide file tree
Showing 23 changed files with 1,275 additions and 116 deletions.
5 changes: 2 additions & 3 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ module.exports = {
"import/no-cycle": "error",
"import/no-self-import": "error",
"@typescript-eslint/consistent-type-assertions": [
"warn",
{ assertionStyle: "never" }
"off",
],
"@typescript-eslint/no-non-null-assertion": "warn",
"@typescript-eslint/no-non-null-assertion": "off",
},
};
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
## Overview :page_facing_up:
![Typechain](https://user-images.githubusercontent.com/43150707/215465145-ef41167c-ef40-4f3c-8fd8-ca56a2480810.png)

If you have any questions regarding Typechain-Polkadot, you can join the [Brushfam Element channel](https://matrix.to/#/!utTuYglskDvqRRMQta:matrix.org?via=matrix.org&via=t2bot.io&via=web3.foundation) to find your answers and meet other ink! smart contracts developers.

Typechain was created by [Supercolony](supercolony.net) to to improve developers’ experience working with ink! smartcontracts.
## Overview :page_facing_up:

Typechain is maintained by [Brushfam](https://www.brushfam.io/) team to improve developers’ experience working with ink! smart contracts.

Nowadays, when technologies are growing faster and faster, we should think about optimizations of different routine processes and making older stuff better. One of these optimizations is to make code typesafe that will be flexible in different situations.

Expand All @@ -16,16 +19,16 @@ Interaction with blockchain is done with polkadot.js library, which only has abs
Install the package as dependency:

```bash
yarn add @727-ventures/typechain-polkadot
npm i @727-ventures/typechain-polkadot
```

Pass the folder with artifacts(in the example it is `artifacts`) as input argument
and the output folder(in the example it is `typed_contracts`):
```bash
npx typechain-polkadot --in artifacts --out typed_contracts
npx @727-ventures/typechain-polkadot --in artifacts --out typed_contracts
```

Import the contract what you want to use(in the example it is [`my_psp22`](https://github.com/Supercolony-net/openbrush-contracts/tree/main/examples/psp22)):
Import the contract what you want to use(in the example it is [`my_psp22`](https://github.com/727-Ventures/openbrush-contracts/tree/main/examples/psp22)):
```typescript
import MyPSP22 from "../typed_contracts/contracts/my_psp22"
```
Expand Down Expand Up @@ -72,13 +75,12 @@ Given, that a front-end developer needs to do this with every contracts update,

When contracts descriptions come both with ABI and source code (`*.contract` files), our tool will provide means for deployment as well.

Also, Typechain-Polkadot uses awesome tool Handlebars for generating code from templates. It is a very flexible and powerful tool, which allows to generate code from templates with a lot of different logic.
For example, you can generate code for different contracts with different logic, or you can generate code for different methods with different logic.

## Roadmap 🚗

Typechain participates in the Web3 Grants, you can find the roadmap there:
- [Grant I](https://github.com/w3f/Grants-Program/blob/master/applications/typechain-polkadot.md)
- [Grant I](https://github.com/w3f/Grants-Program/blob/master/applications/typechain-polkadot-follow-up.md)
- [Delivery milestone 1](https://github.com/w3f/Grant-Milestone-Delivery/blob/master/deliveries/typechain-polkadot-milestone-1.md#milestone-delivery-mailbox)
- Grant II - Comming soon....

-----------
#### Made with ❤️ by [Supercolony](https://supercolony.net)
- [Delivery milestone 2](https://github.com/w3f/Grant-Milestone-Delivery/blob/master/deliveries/typechain-polkadot-milestone-2.md)
21 changes: 12 additions & 9 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ This directory contains examples of parsed contracts and pieces of advice how to
- Add to your package.json file the following and install it with `npm i`:
```json
"dependencies": {
"@727-ventures/typechain-polkadot": "0.4.11",
"@types/node": "^17.0.34",
"ts-node": "^10.7.0",
"typescript": "^4.6.4",
"@polkadot/api": "^9.6.1",
"@polkadot/api-contract": "^9.6.1",
"@types/bn.js": "^5.1.0"
"@727-ventures/typechain-polkadot": "0.1.6",
"@types/node": "^17.0.34",
"ts-node": "^10.7.0",
"typescript": "^4.6.4",
"@polkadot/api": "^9.6.1",
"@polkadot/api-contract": "^9.6.1",
"@polkadot/keyring": "^9.6.1",
"@types/bn.js": "^5.1.0"
}
```
- Run typechain with
```bash
npx @727-ventures/typechain-polkadot --in artifacts --out out
```
npx @727-ventures/typechain-polkadot --in path/to/artifacts --out path/to/output
```

> Note: you should move metadata.json to artifacts directory and rename it to <contract-name>.json, also if you want to deploy the contract you should also move <contract-name>.contract to artifacts directory from your target dir
8 changes: 4 additions & 4 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"publish": "npx lerna publish",
"start-typechain": "npx @727-ventures/typechain-polkadot --in ./artifacts --out ./out",
"gen-test-ts": "npx @727-ventures/typechain-polkadot --in tests/artifacts --out tests/generated",
"test": "npx jest --no-cache --maxWorkers 1",
"only-test": "npx jest --no-cache --maxWorkers 1",
"test": "npm run gen-test-ts && npm run only-test",
"lint": "eslint \"{packages, examples}/**/*.{js,jsx,ts,tsx}\"",
"lint:fix": "eslint --fix \"{packages, examples}/**/*.{js,jsx,ts,tsx}\"",
"gen-docs": "npx typedoc --out docs-generated"
Expand Down
2 changes: 1 addition & 1 deletion packages/typechain-compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function main() {
__writeFileSync(
artifactsPath,
`${targetInfo.name}.json`,
FsAPI.readFileSync(PathAPI.resolve(targetInfo.path, 'metadata.json'), "utf8")
FsAPI.readFileSync(PathAPI.resolve(targetInfo.path, 'my_psp22.json'), "utf8")
);

__writeFileSync(
Expand Down
33 changes: 17 additions & 16 deletions packages/typechain-polkadot-parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ export class TypeParser {
event.label.toString(),
false,
false,
false,
eventBodyStructure,
),
eventBody,
Expand Down Expand Up @@ -241,7 +242,7 @@ ${event.args.map((arg) => {
compositeName,
`ArgumentTypes.${compositeName}`,
`ReturnTypes.${compositeName}`,
new TypeTS(compositeName, false, true, 'string | number[]'),
new TypeTS(compositeName, false,false, true, 'string | number[]'),
`export type ${compositeName} = string | number[]`,
`export type ${compositeName} = string | number[]`,
);
Expand Down Expand Up @@ -275,7 +276,7 @@ ${event.args.map((arg) => {
compositeName,
`ArgumentTypes.${compositeName}`,
tsReturnTypePrefixed,
new TypeTS(compositeName, false, false, typeBody),
new TypeTS(compositeName, false,false, false, typeBody),
compositeBodyArgs,
compositeBodyReturns,
);
Expand All @@ -298,7 +299,7 @@ ${event.args.map((arg) => {
'ReturnNumber',
parsePrimitiveArgs(typeName),
'ReturnNumber',
new TypeTS('ReturnNumber', true, false),
new TypeTS('ReturnNumber', false, true, false),
);
}

Expand All @@ -308,7 +309,7 @@ ${event.args.map((arg) => {
parsePrimitiveReturns(typeName),
parsePrimitiveArgs(typeName),
parsePrimitiveReturns(typeName),
new TypeTS(parsePrimitiveReturns(typeName), false, true),
new TypeTS(parsePrimitiveReturns(typeName), false,false, true),
);
};

Expand All @@ -326,7 +327,7 @@ ${event.args.map((arg) => {
`Array<${this.generateType(type).tsReturnType}>`,
`Array<${this.generateType(type).tsArgTypePrefixed}>`,
`Array<${this.generateType(type).tsReturnTypePrefixed}>`,
new TypeTS('Array', false, false,{'0': this.generateType(type).typeDescription}),
new TypeTS('Array', false, false, false,{'0': this.generateType(type).typeDescription}),
);
};

Expand All @@ -344,7 +345,7 @@ ${event.args.map((arg) => {
`Array<${this.generateType(type).tsReturnType}>`,
`Array<${this.generateType(type).tsArgTypePrefixed}>`,
`Array<${this.generateType(type).tsReturnTypePrefixed}>`,
new TypeTS('Array', false, false,{'0': this.generateType(type).typeDescription}),
new TypeTS('Array', false, false, false,{'0': this.generateType(type).typeDescription}),
);
};

Expand All @@ -362,7 +363,7 @@ ${event.args.map((arg) => {
'null',
'null',
'null',
new TypeTS('null', false, true),
new TypeTS('null', false,false, true),
);
}

Expand All @@ -380,7 +381,7 @@ ${event.args.map((arg) => {
`[${type.map(type => this.generateType(type).tsReturnType).join(', ')}]`,
`[${type.map(type => this.generateType(type).tsArgTypePrefixed).join(', ')}]`,
`[${type.map(type => this.generateType(type).tsReturnTypePrefixed).join(', ')}]`,
new TypeTS('Tuple', false, false, typeBody),
new TypeTS('Tuple', false, false, false, typeBody),
);
};

Expand All @@ -406,7 +407,7 @@ ${event.args.map((arg) => {
`Result<${typeOk.tsReturnType}, ${typeErr.tsReturnType}>`,
`Result<${typeOk.tsArgTypePrefixed}, ${typeErr.tsArgTypePrefixed}>`,
`Result<${typeOk.tsReturnTypePrefixed}, ${typeErr.tsReturnTypePrefixed}>`,
new TypeTS(`Result<${typeOk.typeDescription.name}, ${typeErr.typeDescription.name}`, false, false, {
new TypeTS(`Result<${typeOk.typeDescription.name}, ${typeErr.typeDescription.name}`, true, false, false, {
'ok': typeOk.typeDescription,
'err': typeErr.typeDescription,
}),
Expand All @@ -421,7 +422,7 @@ ${event.args.map((arg) => {
generatedType.tsReturnType,
`${generatedType.tsArgTypePrefixed} | null`,
generatedType.tsReturnTypePrefixed,
new TypeTS('Result', false, false,{'0': generatedType.typeDescription}),
new TypeTS('Result', false, false, false,{'0': generatedType.typeDescription}),
);
} else {
return new TypeInfo(
Expand All @@ -430,7 +431,7 @@ ${event.args.map((arg) => {
'null',
'null',
'null',
new TypeTS('null', false, true),
new TypeTS('null', false, false, true),
);
}
} else if (variantName == 'Option') {
Expand All @@ -442,9 +443,9 @@ ${event.args.map((arg) => {
`${generatedType.tsReturnTypePrefixed} | null`,
`${generatedType.tsArgTypePrefixed} | null`,
`${generatedType.tsReturnTypePrefixed} | null`,
new TypeTS('Option', false, false,{
new TypeTS('Option', false, false, false,{
'0': generatedType.typeDescription,
'1': new TypeTS('null', false, true),
'1': new TypeTS('null', false, false, true),
}),
);
}
Expand All @@ -455,7 +456,7 @@ ${event.args.map((arg) => {
'null',
'null',
'null',
new TypeTS('null', false, true),
new TypeTS('null', false, false, true),
);
}
}
Expand Down Expand Up @@ -488,7 +489,7 @@ ${event.args.map((arg) => {
variantName,
`ArgumentTypes.${variantName}`,
tsReturnTypePrefixed,
new TypeTS(variantName, false, false, typeBody),
new TypeTS(variantName, false, false, false, typeBody),
body,
body,
);
Expand Down Expand Up @@ -539,7 +540,7 @@ ${event.args.map((arg) => {
variantName,
`ArgumentTypes.${variantName}`,
tsReturnTypePrefixed,
new TypeTS(variantName, false, false, typeBody),
new TypeTS(variantName, false, false, false, typeBody),
bodyArgs,
bodyReturns,
);
Expand Down
9 changes: 6 additions & 3 deletions packages/typechain-polkadot-parser/src/types/TypeInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class TypeInfo {
// For enums and composites
bodyArgType ?: string;
bodyReturnType ?: string;
isResult?: boolean;

/**
* @constructor
Expand All @@ -51,7 +52,7 @@ export class TypeInfo {
tsReturnTypePrefixed: string,
typeDescription: TypeTS,
bodyArgType?: string,
bodyReturnType?: string
bodyReturnType?: string,
) {
this.id = id;
this.tsArgType = tsArgType;
Expand All @@ -68,7 +69,7 @@ export class TypeInfo {
* @returns {TypeInfo} - A new empty TypeInfo object
*/
static get EMPTY_TYPE_INFO() {
return new TypeInfo(0, '', '', '', '', new TypeTS('', false, true), '', '');
return new TypeInfo(0, '', '', '', '', new TypeTS('', false,false, true), '', '');
}
}

Expand All @@ -77,13 +78,15 @@ export class TypeTS {
body: null | undefined | string | {
[index: string]: TypeTS | null;
};
isResult: boolean;
isPrimitive: boolean;
isConvertable: boolean;

constructor(name: string, isConvertable: boolean, isPrimitive: boolean, body?: null | undefined | string | {
constructor(name: string, isResult: boolean, isConvertable: boolean, isPrimitive: boolean, body?: null | undefined | string | {
[index: string]: TypeTS | null;
}) {
this.name = name;
this.isResult = isResult;
this.isConvertable = isConvertable;
this.isPrimitive = isPrimitive;
this.body = body;
Expand Down
Loading

0 comments on commit 46b10d4

Please sign in to comment.