Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace @727-ventures/typechain-* with @prosopo/typechain-* #64

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions contracts/build-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ do
$CP_CMD ./target/ink/$i/$i.wasm $DIR/$i/
$CP_CMD ./target/ink/$i/$i.json $DIR/$i/

# NOTE: Currently disabled until `typechain-polkadot` dependencies are upted to support ink! v5
if [[ "$@" != *"--skip-types"* ]]; then
echo "IMPORTANT: Type generation is currently disabled as it's not yet working with ink! v5"
# echo "Generate types via typechain into './typed-contracts'…"
# Because of an open issue, this uses the npx-installed version (https://github.com/Brushfam/typechain-polkadot/issues/115)
# npx @727-ventures/typechain-polkadot --in $DIR/$i/ --out typed-contracts --yes
echo "Generate types via @prosopo/typechain-polkadot into './typed-contracts'…"
# Using the npx-installed version because of this issue: https://github.com/Brushfam/typechain-polkadot/issues/115
npx @prosopo/typechain-polkadot --in $DIR/$i/ --out typed-contracts --yes
fi
done
2 changes: 1 addition & 1 deletion contracts/deployments/greeter/greeter.contract

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions contracts/deployments/greeter/greeter.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"source": {
"hash": "0x405bb1e5c253833517f814020d6299705b7ffdbb628041506d5d09aa2cf9b465",
"hash": "0xd2a960874747af3b3da183bef32563be6cd8bc56bcc3efe53dd528f66c3a3a68",
"language": "ink! 5.0.0",
"compiler": "rustc 1.77.1",
"build_info": {
Expand Down Expand Up @@ -153,7 +153,7 @@
"args": [],
"default": false,
"docs": [
" Returns the current value of `message`."
" Returns the current value of 'message'."
],
"label": "greet",
"mutates": false,
Expand Down Expand Up @@ -181,7 +181,7 @@
],
"default": false,
"docs": [
" Sets `message` to the given value."
" Sets 'message' to the given value."
],
"label": "set_message",
"mutates": true,
Expand Down
Binary file modified contracts/deployments/greeter/greeter.wasm
Binary file not shown.
4 changes: 2 additions & 2 deletions contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@
"lint:format": "prettier . --write"
},
"devDependencies": {
"@727-ventures/typechain-types": "^1.1.2",
"@polkadot/api": "^11.0.2",
"@polkadot/api-contract": "^11.0.2",
"@polkadot/keyring": "^12.6.2",
"@polkadot/types": "^11.0.2",
"@polkadot/util": "^12.6.2",
"@polkadot/util-crypto": "^12.6.2",
"@prosopo/typechain-types": "^1.1.15",
"@scio-labs/use-inkathon": "^0.10.0",
"@types/bn.js": "~5.1.5",
"@types/eslint": "^8.56.10",
"@types/eslint-config-prettier": "^6.11.3",
"@types/node": "^20.12.7",
"@types/node": "^20.12.8",
"@typescript-eslint/eslint-plugin": "^7.8.0",
"@typescript-eslint/parser": "^7.8.0",
"bn.js": "^5.2.1",
Expand Down
4 changes: 2 additions & 2 deletions contracts/src/greeter/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ mod greeter {
Self::new(default_message)
}

/// Returns the current value of `message`.
/// Returns the current value of 'message'.
#[ink(message)]
pub fn greet(&self) -> String {
self.message.clone()
}

/// Sets `message` to the given value.
/// Sets 'message' to the given value.
#[ink(message)]
pub fn set_message(&mut self, new_value: String) {
self.message = new_value.clone();
Expand Down
45 changes: 45 additions & 0 deletions contracts/typed-contracts/build-extrinsic/greeter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* This file is auto-generated */

import type { ContractPromise } from '@polkadot/api-contract';
import type { GasLimit, GasLimitAndRequiredValue } from '@prosopo/typechain-types';
import { buildSubmittableExtrinsic } from '@prosopo/typechain-types';
import type * as ArgumentTypes from '../types-arguments/greeter';
import type BN from 'bn.js';
import type { ApiPromise } from '@polkadot/api';



export default class Methods {
readonly __nativeContract : ContractPromise;
readonly __apiPromise: ApiPromise;

constructor(
nativeContract : ContractPromise,
apiPromise: ApiPromise,
) {
this.__nativeContract = nativeContract;
this.__apiPromise = apiPromise;
}
/**
* greet
*
*/
"greet" (
__options: GasLimit,
){
return buildSubmittableExtrinsic( this.__apiPromise, this.__nativeContract, "greet", [], __options);
}

/**
* setMessage
*
* @param { string } newValue,
*/
"setMessage" (
newValue: string,
__options: GasLimit,
){
return buildSubmittableExtrinsic( this.__apiPromise, this.__nativeContract, "setMessage", [newValue], __options);
}

}
83 changes: 83 additions & 0 deletions contracts/typed-contracts/constructors/greeter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import {CodePromise} from "@polkadot/api-contract";
import type {KeyringPair} from "@polkadot/keyring/types";
import type {ApiPromise} from "@polkadot/api";
import {_genValidGasLimitAndValue, _signAndSend, SignAndSendSuccessResponse} from "@prosopo/typechain-types";
import type {ConstructorOptions} from "@prosopo/typechain-types";
import type {WeightV2} from "@polkadot/types/interfaces";
import type * as ArgumentTypes from '../types-arguments/greeter';
import { ContractFile } from '../contract-info/greeter';
import type BN from 'bn.js';

export default class Constructors {
readonly nativeAPI: ApiPromise;
readonly signer: KeyringPair;

constructor(
nativeAPI: ApiPromise,
signer: KeyringPair,
) {
this.nativeAPI = nativeAPI;
this.signer = signer;
}

/**
* new
*
* @param { string } initValue,
*/
async "new" (
initValue: string,
__options ? : ConstructorOptions,
) {
const __contract = JSON.parse(ContractFile);
const code = new CodePromise(this.nativeAPI, __contract, __contract.source.wasm);
const gasLimit = (await _genValidGasLimitAndValue(this.nativeAPI, __options)).gasLimit as WeightV2;

const storageDepositLimit = __options?.storageDepositLimit;
const tx = code.tx["new"]!({ gasLimit, storageDepositLimit, value: __options?.value }, initValue);
let response;

try {
response = await _signAndSend(this.nativeAPI.registry, tx, this.signer, (event: any) => event);
}
catch (error) {
console.log(error);
}

return {
result: response as SignAndSendSuccessResponse,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
address: (response as SignAndSendSuccessResponse)!.result!.contract.address.toString(),
};
}
/**
* default
*
*/
async "default" (
__options ? : ConstructorOptions,
) {
const __contract = JSON.parse(ContractFile);
const code = new CodePromise(this.nativeAPI, __contract, __contract.source.wasm);
const gasLimit = (await _genValidGasLimitAndValue(this.nativeAPI, __options)).gasLimit as WeightV2;

const storageDepositLimit = __options?.storageDepositLimit;
const tx = code.tx["default"]!({ gasLimit, storageDepositLimit, value: __options?.value }, );
let response;

try {
response = await _signAndSend(this.nativeAPI.registry, tx, this.signer, (event: any) => event);
}
catch (error) {
console.log(error);
}

return {
result: response as SignAndSendSuccessResponse,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
address: (response as SignAndSendSuccessResponse)!.result!.contract.address.toString(),
};
}
}
8 changes: 8 additions & 0 deletions contracts/typed-contracts/contract-info/greeter.ts

Large diffs are not rendered by default.

107 changes: 107 additions & 0 deletions contracts/typed-contracts/contracts/greeter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/* This file is auto-generated */

import type { ApiPromise } from '@polkadot/api';
import { Abi } from '@polkadot/api-contract';
import type { KeyringPair } from '@polkadot/keyring/types';
import { ContractPromise } from '@polkadot/api-contract';
import { ContractAbi } from '../contract-info/greeter';
import QueryMethods from '../query/greeter';
import BuildExtrinsicMethods from '../build-extrinsic/greeter';
import TxSignAndSendMethods from '../tx-sign-and-send/greeter';
import MixedMethods from '../mixed-methods/greeter';
import EventsClass from '../events/greeter';


export default class Contract {
readonly query : QueryMethods;
readonly buildExtrinsic : BuildExtrinsicMethods;
readonly tx : TxSignAndSendMethods;
readonly methods : MixedMethods;
readonly events: EventsClass;

readonly address : string;
readonly signer : KeyringPair;

readonly nativeContract : ContractPromise;
readonly nativeAPI : ApiPromise;
readonly contractAbi: Abi;

/**
* @constructor

* @param address - The address of the contract.
* @param signer - The signer to use for signing transactions.
* @param nativeAPI - The API instance to use for queries.
*/
constructor(
address : string,
signer : KeyringPair,
nativeAPI : ApiPromise,
) {
this.address = address;
this.nativeContract = new ContractPromise(nativeAPI, ContractAbi, address);
this.nativeAPI = nativeAPI;
this.signer = signer;
this.contractAbi = new Abi(ContractAbi);

this.query = new QueryMethods(this.nativeContract, this.nativeAPI, signer.address);
this.buildExtrinsic = new BuildExtrinsicMethods(this.nativeContract, this.nativeAPI);
this.tx = new TxSignAndSendMethods(nativeAPI, this.nativeContract, signer);
this.methods = new MixedMethods(nativeAPI, this.nativeContract, signer);
this.events = new EventsClass(this.nativeContract, nativeAPI);
}

/**
* name
*
* @returns The name of the contract.
*/
get name() : string {
return this.nativeContract.abi.info.contract.name.toString();
}

/**
* abi
*
* @returns The abi of the contract.
*/
get abi() : Abi {
return this.contractAbi;
}

/**
* withSigner
*
* @param signer - The signer to use for signing transactions.
* @returns New instance of the contract class with new signer.
* @example
* ```typescript
* const contract = new Contract(address, signerAlice, api);
* await contract.mint(signerBob.address, 100);
* await contract.withSigner(signerBob).transfer(signerAlice.address, 100);
* ```
*/
withSigner(signer : KeyringPair) : Contract {
return new Contract(this.address, signer, this.nativeAPI);
}

/**
* withAddress
*
* @param address - The address of the contract.
* @returns New instance of the contract class to interact with new contract.
*/
withAddress(address : string) : Contract {
return new Contract(address, this.signer, this.nativeAPI);
}

/**
* withAPI
*
* @param api - The API instance to use for queries.
* @returns New instance of the contract class to interact with new API.
*/
withAPI(api : ApiPromise) : Contract {
return new Contract(this.address, this.signer, api);
}
}
3 changes: 3 additions & 0 deletions contracts/typed-contracts/data/greeter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"0": {"name":"string","isResult":false,"isPrimitive":true,"isConvertable":false},"2": {"name":"Result<null, LangError","body":{"ok":{"name":"null","isResult":false,"isPrimitive":true,"isConvertable":false},"err":{"name":"LangError","body":{"CouldNotReadInput":null},"isResult":false,"isPrimitive":false,"isConvertable":false}},"isResult":true,"isPrimitive":false,"isConvertable":false},"3": {"name":"null","isResult":false,"isPrimitive":true,"isConvertable":false},"4": {"name":"LangError","body":{"CouldNotReadInput":null},"isResult":false,"isPrimitive":false,"isConvertable":false},"5": {"name":"Result<string, LangError","body":{"ok":{"name":"string","isResult":false,"isPrimitive":true,"isConvertable":false},"err":{"name":"LangError","body":{"CouldNotReadInput":null},"isResult":false,"isPrimitive":false,"isConvertable":false}},"isResult":true,"isPrimitive":false,"isConvertable":false},"6": {"name":"Option","body":{"0":{"name":"AccountId","body":"string | number[]","isResult":false,"isPrimitive":true,"isConvertable":false},"1":{"name":"null","isResult":false,"isPrimitive":true,"isConvertable":false}},"isResult":false,"isPrimitive":false,"isConvertable":false},"7": {"name":"AccountId","body":"string | number[]","isResult":false,"isPrimitive":true,"isConvertable":false},"8": {"name":"Array","body":{"0":{"name":"number","isResult":false,"isPrimitive":true,"isConvertable":false}},"isResult":false,"isPrimitive":false,"isConvertable":false},"9": {"name":"number","isResult":false,"isPrimitive":true,"isConvertable":false}
}
3 changes: 3 additions & 0 deletions contracts/typed-contracts/event-data/greeter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"Greeted": {"name":"Greeted","body":{"from":{"name":"Option","body":{"0":{"name":"AccountId","body":"string | number[]","isResult":false,"isPrimitive":true,"isConvertable":false},"1":{"name":"null","isResult":false,"isPrimitive":true,"isConvertable":false}},"isResult":false,"isPrimitive":false,"isConvertable":false},"message":{"name":"string","isResult":false,"isPrimitive":true,"isConvertable":false}},"isResult":false,"isPrimitive":false,"isConvertable":false}
}
8 changes: 8 additions & 0 deletions contracts/typed-contracts/event-types/greeter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type {ReturnNumber} from "@prosopo/typechain-types";
import type * as ReturnTypes from '../types-returns/greeter';

export interface Greeted {
from: ReturnTypes.AccountId | null;
message: string;
}

59 changes: 59 additions & 0 deletions contracts/typed-contracts/events/greeter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import type * as EventTypes from '../event-types/greeter';
import type {ContractPromise} from "@polkadot/api-contract";
import type {ApiPromise} from "@polkadot/api";
import EVENT_DATA_TYPE_DESCRIPTIONS from '../event-data/greeter.json';
import {getEventTypeDescription} from "../shared/utils";
import {handleEventReturn} from "@prosopo/typechain-types";

export default class EventsClass {
readonly __nativeContract : ContractPromise;
readonly __api : ApiPromise;

constructor(
nativeContract : ContractPromise,
api : ApiPromise,
) {
this.__nativeContract = nativeContract;
this.__api = api;
}

public subscribeOnGreetedEvent(callback : (event : EventTypes.Greeted) => void) {
const callbackWrapper = (args: any[], event: any) => {
const _event: Record < string, any > = {};

for (let i = 0; i < args.length; i++) {
_event[event.args[i]!.name] = args[i]!.toJSON();
}

callback(handleEventReturn(_event, getEventTypeDescription('Greeted', EVENT_DATA_TYPE_DESCRIPTIONS)) as EventTypes.Greeted);
};

return this.__subscribeOnEvent(callbackWrapper, (eventName : string) => eventName == 'Greeted');
}


private __subscribeOnEvent(
callback : (args: any[], event: any) => void,
filter : (eventName: string) => boolean = () => true
) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return this.__api.query.system.events((events) => {
events.forEach((record: any) => {
const { event } = record;

if (event.method == 'ContractEmitted') {
const [address, data] = record.event.data;

if (address.toString() === this.__nativeContract.address.toString()) {
const {args, event} = this.__nativeContract.abi.decodeEvent(record);

if (filter(event.identifier.toString()))
callback(args, event);
}
}
});
});
}

}
Loading