@pezkuwi/api-contract 16.5.5 → 16.5.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Abi/index.js +359 -0
- package/Abi/toLatestCompatible.js +30 -0
- package/Abi/toV1.js +21 -0
- package/Abi/toV2.js +27 -0
- package/Abi/toV3.js +10 -0
- package/Abi/toV4.js +10 -0
- package/LICENSE +201 -0
- package/README.md +1 -1
- package/augment.js +1 -0
- package/base/Base.js +40 -0
- package/base/Blueprint.js +51 -0
- package/base/Code.js +74 -0
- package/base/Contract.js +127 -0
- package/{src/base/index.ts → base/index.js} +0 -3
- package/base/mock.js +38 -0
- package/{build/base → base}/util.d.ts +1 -1
- package/base/util.js +33 -0
- package/bundle-pezkuwi-api-contract.js +1238 -0
- package/{src/bundle.ts → bundle.js} +0 -6
- package/cjs/Abi/index.d.ts +31 -0
- package/cjs/Abi/index.js +363 -0
- package/cjs/Abi/toLatestCompatible.d.ts +15 -0
- package/cjs/Abi/toLatestCompatible.js +36 -0
- package/cjs/Abi/toV1.d.ts +3 -0
- package/cjs/Abi/toV1.js +24 -0
- package/cjs/Abi/toV2.d.ts +3 -0
- package/cjs/Abi/toV2.js +30 -0
- package/cjs/Abi/toV3.d.ts +3 -0
- package/cjs/Abi/toV3.js +13 -0
- package/cjs/Abi/toV4.d.ts +3 -0
- package/cjs/Abi/toV4.js +13 -0
- package/cjs/augment.d.ts +1 -0
- package/cjs/augment.js +3 -0
- package/cjs/base/Base.d.ts +13 -0
- package/cjs/base/Base.js +44 -0
- package/cjs/base/Blueprint.d.ts +24 -0
- package/cjs/base/Blueprint.js +57 -0
- package/cjs/base/Code.d.ts +22 -0
- package/cjs/base/Code.js +80 -0
- package/cjs/base/Contract.d.ts +25 -0
- package/cjs/base/Contract.js +133 -0
- package/cjs/base/index.d.ts +3 -0
- package/cjs/base/index.js +14 -0
- package/cjs/base/mock.d.ts +3 -0
- package/cjs/base/mock.js +41 -0
- package/{src/base/types.ts → cjs/base/types.d.ts} +6 -21
- package/cjs/base/types.js +2 -0
- package/cjs/base/util.d.ts +16 -0
- package/cjs/base/util.js +42 -0
- package/cjs/bundle.d.ts +4 -0
- package/cjs/bundle.js +10 -0
- package/cjs/index.js +5 -0
- package/cjs/package.json +3 -0
- package/cjs/packageDetect.d.ts +1 -0
- package/cjs/packageDetect.js +7 -0
- package/cjs/packageInfo.js +4 -0
- package/cjs/promise/index.js +23 -0
- package/cjs/promise/types.js +2 -0
- package/cjs/rx/index.js +23 -0
- package/cjs/rx/types.js +2 -0
- package/cjs/types.js +2 -0
- package/cjs/util.js +13 -0
- package/index.d.ts +2 -0
- package/index.js +2 -0
- package/package.json +384 -15
- package/packageDetect.d.ts +1 -0
- package/{src/packageDetect.ts → packageDetect.js} +0 -8
- package/packageInfo.d.ts +6 -0
- package/packageInfo.js +1 -0
- package/promise/index.d.ts +13 -0
- package/promise/index.js +17 -0
- package/{src/promise/types.ts → promise/types.d.ts} +0 -4
- package/promise/types.js +1 -0
- package/rx/index.d.ts +13 -0
- package/rx/index.js +17 -0
- package/{src/rx/types.ts → rx/types.d.ts} +0 -4
- package/rx/types.js +1 -0
- package/types.d.ts +79 -0
- package/types.js +1 -0
- package/util.d.ts +5 -0
- package/util.js +10 -0
- package/src/Abi/Abi.spec.ts +0 -235
- package/src/Abi/index.ts +0 -477
- package/src/Abi/toLatestCompatible.spec.ts +0 -219
- package/src/Abi/toLatestCompatible.ts +0 -52
- package/src/Abi/toV1.ts +0 -35
- package/src/Abi/toV2.ts +0 -58
- package/src/Abi/toV3.ts +0 -18
- package/src/Abi/toV4.ts +0 -21
- package/src/augment.ts +0 -4
- package/src/base/Base.ts +0 -52
- package/src/base/Blueprint.ts +0 -90
- package/src/base/Code.spec.ts +0 -47
- package/src/base/Code.ts +0 -142
- package/src/base/Contract.ts +0 -197
- package/src/base/mock.ts +0 -48
- package/src/base/util.ts +0 -56
- package/src/checkTypes.manual.ts +0 -45
- package/src/index.ts +0 -6
- package/src/mod.ts +0 -4
- package/src/packageInfo.ts +0 -6
- package/src/promise/index.ts +0 -28
- package/src/rx/index.ts +0 -28
- package/src/test/compare/ink_v0_delegator.test.json +0 -47
- package/src/test/compare/ink_v0_dns.test.json +0 -232
- package/src/test/compare/ink_v0_erc20.test.json +0 -253
- package/src/test/compare/ink_v0_erc721.test.json +0 -415
- package/src/test/compare/ink_v0_flipper.test.json +0 -9
- package/src/test/compare/ink_v0_flipperBundle.test.json +0 -9
- package/src/test/compare/ink_v0_incrementer.test.json +0 -9
- package/src/test/compare/ink_v0_multisigPlain.test.json +0 -562
- package/src/test/compare/ink_v1_flipper.test.json +0 -9
- package/src/test/compare/ink_v1_psp22.test.json +0 -531
- package/src/test/compare/ink_v2_erc20.test.json +0 -205
- package/src/test/compare/ink_v2_flipper.test.json +0 -9
- package/src/test/compare/ink_v3_flipper.test.json +0 -9
- package/src/test/compare/ink_v3_traitErc20.test.json +0 -205
- package/src/test/compare/ink_v4_erc20Contract.test.json +0 -253
- package/src/test/compare/ink_v4_erc20Metadata.test.json +0 -253
- package/src/test/compare/ink_v4_flipperContract.test.json +0 -155
- package/src/test/compare/ink_v4_flipperMetadata.test.json +0 -155
- package/src/test/compare/ink_v5_erc20.test.json +0 -370
- package/src/test/compare/ink_v5_erc20AnonymousTransferMetadata.test.json +0 -370
- package/src/test/compare/ink_v5_erc20Contract.test.json +0 -370
- package/src/test/compare/ink_v5_erc20Metadata.test.json +0 -370
- package/src/test/compare/ink_v5_flipperContract.test.json +0 -174
- package/src/test/compare/ink_v5_flipperMetadata.test.json +0 -174
- package/src/test/compare/ink_v6_erc20Contract.test.json +0 -418
- package/src/test/compare/ink_v6_erc20Metadata.test.json +0 -418
- package/src/test/compare/solang_v0_ints256.test.json +0 -9
- package/src/test/compare/user_v0_assetTransfer.test.json +0 -54
- package/src/test/compare/user_v0_enumExample.test.json +0 -303
- package/src/test/compare/user_v0_recursive.test.json +0 -27
- package/src/test/compare/user_v0_withString.test.json +0 -260
- package/src/test/compare/user_v3_ask.test.json +0 -71
- package/src/test/compare/user_v4_events.test.json +0 -1328
- package/src/test/contracts/index.ts +0 -20
- package/src/test/contracts/ink/index.ts +0 -13
- package/src/test/contracts/ink/v0/accumulator.wasm +0 -0
- package/src/test/contracts/ink/v0/adder.wasm +0 -0
- package/src/test/contracts/ink/v0/delegator.json +0 -252
- package/src/test/contracts/ink/v0/delegator.wasm +0 -0
- package/src/test/contracts/ink/v0/dns.json +0 -713
- package/src/test/contracts/ink/v0/dns.wasm +0 -0
- package/src/test/contracts/ink/v0/erc20.json +0 -704
- package/src/test/contracts/ink/v0/erc20.wasm +0 -0
- package/src/test/contracts/ink/v0/erc721.json +0 -1197
- package/src/test/contracts/ink/v0/erc721.wasm +0 -0
- package/src/test/contracts/ink/v0/flipper.contract.json +0 -107
- package/src/test/contracts/ink/v0/flipper.json +0 -106
- package/src/test/contracts/ink/v0/flipper.wasm +0 -0
- package/src/test/contracts/ink/v0/incrementer.json +0 -108
- package/src/test/contracts/ink/v0/incrementer.wasm +0 -0
- package/src/test/contracts/ink/v0/index.ts +0 -11
- package/src/test/contracts/ink/v0/multisig_plain.json +0 -1466
- package/src/test/contracts/ink/v0/multisig_plain.wasm +0 -0
- package/src/test/contracts/ink/v0/subber.wasm +0 -0
- package/src/test/contracts/ink/v0/trait-flipper.json +0 -103
- package/src/test/contracts/ink/v0/trait-flipper.wasm +0 -0
- package/src/test/contracts/ink/v1/flipper.contract.json +0 -111
- package/src/test/contracts/ink/v1/index.ts +0 -6
- package/src/test/contracts/ink/v1/psp22_minter_pauser.contract.json +0 -1722
- package/src/test/contracts/ink/v2/erc20.contract.json +0 -630
- package/src/test/contracts/ink/v2/flipper.contract.json +0 -103
- package/src/test/contracts/ink/v2/index.ts +0 -5
- package/src/test/contracts/ink/v3/flipper.contract.json +0 -105
- package/src/test/contracts/ink/v3/index.ts +0 -6
- package/src/test/contracts/ink/v3/trait_erc20.contract.json +0 -631
- package/src/test/contracts/ink/v4/erc20.contract.json +0 -1
- package/src/test/contracts/ink/v4/erc20.json +0 -821
- package/src/test/contracts/ink/v4/erc20.wasm +0 -0
- package/src/test/contracts/ink/v4/flipper.contract.json +0 -1
- package/src/test/contracts/ink/v4/flipper.json +0 -396
- package/src/test/contracts/ink/v4/flipper.wasm +0 -0
- package/src/test/contracts/ink/v4/index.ts +0 -7
- package/src/test/contracts/ink/v5/erc20.contract.json +0 -1
- package/src/test/contracts/ink/v5/erc20.json +0 -1025
- package/src/test/contracts/ink/v5/erc20.wasm +0 -0
- package/src/test/contracts/ink/v5/erc20_anonymous_transfer.json +0 -1025
- package/src/test/contracts/ink/v5/flipper.contract.json +0 -1
- package/src/test/contracts/ink/v5/flipper.json +0 -420
- package/src/test/contracts/ink/v5/flipper.wasm +0 -0
- package/src/test/contracts/ink/v5/index.ts +0 -8
- package/src/test/contracts/ink/v6/erc20.contract.json +0 -1
- package/src/test/contracts/ink/v6/erc20.json +0 -1081
- package/src/test/contracts/ink/v6/erc20.polkavm +0 -0
- package/src/test/contracts/ink/v6/index.ts +0 -5
- package/src/test/contracts/solang/index.ts +0 -7
- package/src/test/contracts/solang/v0/index.ts +0 -4
- package/src/test/contracts/solang/v0/ints256.json +0 -113
- package/src/test/contracts/solang/v0/ints256.sol +0 -13
- package/src/test/contracts/solang/v0/ints256.wasm +0 -0
- package/src/test/contracts/user/index.ts +0 -9
- package/src/test/contracts/user/v0/assetTransfer.json +0 -299
- package/src/test/contracts/user/v0/assetTransfer.wasm +0 -0
- package/src/test/contracts/user/v0/enumExample.json +0 -528
- package/src/test/contracts/user/v0/enumExample.wasm +0 -0
- package/src/test/contracts/user/v0/index.ts +0 -7
- package/src/test/contracts/user/v0/recursive.contract.json +0 -1
- package/src/test/contracts/user/v0/withString.json +0 -777
- package/src/test/contracts/user/v3/ask.json +0 -550
- package/src/test/contracts/user/v3/index.ts +0 -4
- package/src/test/contracts/user/v4/events.contract.json +0 -2990
- package/src/test/contracts/user/v4/index.ts +0 -4
- package/src/test/contracts/util.ts +0 -14
- package/src/types.ts +0 -98
- package/src/util.ts +0 -20
- package/tsconfig.build.json +0 -22
- package/tsconfig.build.tsbuildinfo +0 -1
- package/tsconfig.spec.json +0 -26
- /package/{build/Abi → Abi}/index.d.ts +0 -0
- /package/{build/Abi → Abi}/toLatestCompatible.d.ts +0 -0
- /package/{build/Abi → Abi}/toV1.d.ts +0 -0
- /package/{build/Abi → Abi}/toV2.d.ts +0 -0
- /package/{build/Abi → Abi}/toV3.d.ts +0 -0
- /package/{build/Abi → Abi}/toV4.d.ts +0 -0
- /package/{build/augment.d.ts → augment.d.ts} +0 -0
- /package/{build/base → base}/Base.d.ts +0 -0
- /package/{build/base → base}/Blueprint.d.ts +0 -0
- /package/{build/base → base}/Code.d.ts +0 -0
- /package/{build/base → base}/Contract.d.ts +0 -0
- /package/{build/base → base}/index.d.ts +0 -0
- /package/{build/base → base}/mock.d.ts +0 -0
- /package/{build/base → base}/types.d.ts +0 -0
- /package/{build/packageDetect.d.ts → base/types.js} +0 -0
- /package/{build/bundle.d.ts → bundle.d.ts} +0 -0
- /package/{build → cjs}/index.d.ts +0 -0
- /package/{build → cjs}/packageInfo.d.ts +0 -0
- /package/{build → cjs}/promise/index.d.ts +0 -0
- /package/{build → cjs}/promise/types.d.ts +0 -0
- /package/{build → cjs}/rx/index.d.ts +0 -0
- /package/{build → cjs}/rx/types.d.ts +0 -0
- /package/{build → cjs}/types.d.ts +0 -0
- /package/{build → cjs}/util.d.ts +0 -0
package/base/Base.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { isFunction } from '@pezkuwi/util';
|
|
2
|
+
import { Abi } from '../Abi/index.js';
|
|
3
|
+
export class Base {
|
|
4
|
+
abi;
|
|
5
|
+
api;
|
|
6
|
+
_decorateMethod;
|
|
7
|
+
_isWeightV1;
|
|
8
|
+
_isRevive;
|
|
9
|
+
constructor(api, abi, decorateMethod) {
|
|
10
|
+
if (!api || !api.isConnected || !api.tx) {
|
|
11
|
+
throw new Error('Your API has not been initialized correctly and is not connected to a chain');
|
|
12
|
+
}
|
|
13
|
+
this.abi = abi instanceof Abi
|
|
14
|
+
? abi
|
|
15
|
+
: new Abi(abi, api.registry.getChainProperties());
|
|
16
|
+
this.api = api;
|
|
17
|
+
this._decorateMethod = decorateMethod;
|
|
18
|
+
this._isWeightV1 = !api.registry.createType('Weight').proofSize;
|
|
19
|
+
this._isRevive = this.abi.isRevive;
|
|
20
|
+
if (this._isRevive) {
|
|
21
|
+
if (!api.tx.revive || !isFunction(api.tx.revive.instantiateWithCode) || api.tx.revive.instantiateWithCode.meta.args.length !== 6) {
|
|
22
|
+
throw new Error('The runtime does not expose api.tx.revive.instantiateWithCode with storageDepositLimit');
|
|
23
|
+
}
|
|
24
|
+
else if (!api.call.reviveApi || !isFunction(api.call.reviveApi.call)) {
|
|
25
|
+
throw new Error('Your runtime does not expose the api.call.reviveApi.call runtime interfaces');
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
if (!api.tx.contracts || !isFunction(api.tx.contracts.instantiateWithCode) || api.tx.contracts.instantiateWithCode.meta.args.length !== 6) {
|
|
30
|
+
throw new Error('The runtime does not expose api.tx.contracts.instantiateWithCode with storageDepositLimit');
|
|
31
|
+
}
|
|
32
|
+
else if (!api.call.contractsApi || !isFunction(api.call.contractsApi.call)) {
|
|
33
|
+
throw new Error('Your runtime does not expose the api.call.contractsApi.call runtime interfaces');
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
get registry() {
|
|
38
|
+
return this.api.registry;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { SubmittableResult } from '@pezkuwi/api';
|
|
2
|
+
import { BN_ZERO, isUndefined } from '@pezkuwi/util';
|
|
3
|
+
import { applyOnEvent } from '../util.js';
|
|
4
|
+
import { Base } from './Base.js';
|
|
5
|
+
import { Contract } from './Contract.js';
|
|
6
|
+
import { convertWeight, createBluePrintTx, encodeSalt } from './util.js';
|
|
7
|
+
export class BlueprintSubmittableResult extends SubmittableResult {
|
|
8
|
+
contract;
|
|
9
|
+
constructor(result, contract) {
|
|
10
|
+
super(result);
|
|
11
|
+
this.contract = contract;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export class Blueprint extends Base {
|
|
15
|
+
/**
|
|
16
|
+
* @description The on-chain code hash for this blueprint
|
|
17
|
+
*/
|
|
18
|
+
codeHash;
|
|
19
|
+
#tx = {};
|
|
20
|
+
constructor(api, abi, codeHash, decorateMethod) {
|
|
21
|
+
super(api, abi, decorateMethod);
|
|
22
|
+
this.codeHash = this.registry.createType('Hash', codeHash);
|
|
23
|
+
this.abi.constructors.forEach((c) => {
|
|
24
|
+
if (isUndefined(this.#tx[c.method])) {
|
|
25
|
+
this.#tx[c.method] = createBluePrintTx(c, (o, p) => this.#deploy(c, o, p));
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
get tx() {
|
|
30
|
+
return this.#tx;
|
|
31
|
+
}
|
|
32
|
+
#deploy = (constructorOrId, { gasLimit = BN_ZERO, salt, storageDepositLimit = null, value = BN_ZERO }, params) => {
|
|
33
|
+
const palletTx = this._isRevive
|
|
34
|
+
? this.api.tx.revive
|
|
35
|
+
: this.api.tx.contracts;
|
|
36
|
+
return palletTx.instantiate(value,
|
|
37
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
38
|
+
// @ts-ignore jiggle v1 weights, metadata points to latest
|
|
39
|
+
this._isWeightV1
|
|
40
|
+
? convertWeight(gasLimit).v1Weight
|
|
41
|
+
: convertWeight(gasLimit).v2Weight, storageDepositLimit, this.codeHash, this.abi.findConstructor(constructorOrId).toU8a(params), encodeSalt(salt)).withResultTransform((result) => new BlueprintSubmittableResult(result, applyOnEvent(result, ['Instantiated'], ([record]) => new Contract(this.api, this.abi, record.event.data[1], this._decorateMethod), this._isRevive)));
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
export function extendBlueprint(type, decorateMethod) {
|
|
45
|
+
return class extends Blueprint {
|
|
46
|
+
static __BlueprintType = type;
|
|
47
|
+
constructor(api, abi, codeHash) {
|
|
48
|
+
super(api, abi, codeHash, decorateMethod);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}
|
package/base/Code.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { SubmittableResult } from '@pezkuwi/api';
|
|
2
|
+
import { BN_ZERO, compactAddLength, isRiscV, isUndefined, isWasm, u8aToU8a } from '@pezkuwi/util';
|
|
3
|
+
import { applyOnEvent } from '../util.js';
|
|
4
|
+
import { Base } from './Base.js';
|
|
5
|
+
import { Blueprint } from './Blueprint.js';
|
|
6
|
+
import { Contract } from './Contract.js';
|
|
7
|
+
import { convertWeight, createBluePrintTx, encodeSalt } from './util.js';
|
|
8
|
+
export class CodeSubmittableResult extends SubmittableResult {
|
|
9
|
+
blueprint;
|
|
10
|
+
contract;
|
|
11
|
+
constructor(result, blueprint, contract) {
|
|
12
|
+
super(result);
|
|
13
|
+
this.blueprint = blueprint;
|
|
14
|
+
this.contract = contract;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
function isValidCode(code) {
|
|
18
|
+
return isWasm(code) || isRiscV(code);
|
|
19
|
+
}
|
|
20
|
+
export class Code extends Base {
|
|
21
|
+
code;
|
|
22
|
+
#tx = {};
|
|
23
|
+
constructor(api, abi, wasm, decorateMethod) {
|
|
24
|
+
super(api, abi, decorateMethod);
|
|
25
|
+
this.code = isValidCode(this.abi.info.source.wasm)
|
|
26
|
+
? this.abi.info.source.wasm
|
|
27
|
+
: u8aToU8a(wasm);
|
|
28
|
+
if (!isValidCode(this.code)) {
|
|
29
|
+
throw new Error('Invalid code provided');
|
|
30
|
+
}
|
|
31
|
+
this.abi.constructors.forEach((c) => {
|
|
32
|
+
if (isUndefined(this.#tx[c.method])) {
|
|
33
|
+
this.#tx[c.method] = createBluePrintTx(c, (o, p) => this.#instantiate(c, o, p));
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
get tx() {
|
|
38
|
+
return this.#tx;
|
|
39
|
+
}
|
|
40
|
+
#instantiate = (constructorOrId, { gasLimit = BN_ZERO, salt, storageDepositLimit = null, value = BN_ZERO }, params) => {
|
|
41
|
+
const palletTx = this._isRevive ? this.api.tx.revive : this.api.tx.contracts;
|
|
42
|
+
if (this._isRevive) {
|
|
43
|
+
return palletTx.instantiateWithCode(value,
|
|
44
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
45
|
+
// @ts-ignore jiggle v1 weights, metadata points to latest
|
|
46
|
+
this._isWeightV1
|
|
47
|
+
? convertWeight(gasLimit).v1Weight
|
|
48
|
+
: convertWeight(gasLimit).v2Weight, storageDepositLimit, compactAddLength(this.code), this.abi.findConstructor(constructorOrId).toU8a(params), encodeSalt(salt)).withResultTransform((result) => new CodeSubmittableResult(result, ...(applyOnEvent(result, ['Instantiated'], (records) => records.reduce(([blueprint, contract], { event }) => this.api.events.revive['Instantiated'].is(event)
|
|
49
|
+
? [
|
|
50
|
+
blueprint,
|
|
51
|
+
new Contract(this.api, this.abi, event.data[1], this._decorateMethod)
|
|
52
|
+
]
|
|
53
|
+
: [blueprint, contract], [undefined, undefined]), this._isRevive) || [undefined, undefined])));
|
|
54
|
+
}
|
|
55
|
+
return palletTx.instantiateWithCode(value,
|
|
56
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
57
|
+
// @ts-ignore jiggle v1 weights, metadata points to latest
|
|
58
|
+
this._isWeightV1
|
|
59
|
+
? convertWeight(gasLimit).v1Weight
|
|
60
|
+
: convertWeight(gasLimit).v2Weight, storageDepositLimit, compactAddLength(this.code), this.abi.findConstructor(constructorOrId).toU8a(params), encodeSalt(salt)).withResultTransform((result) => new CodeSubmittableResult(result, ...(applyOnEvent(result, ['CodeStored', 'Instantiated'], (records) => records.reduce(([blueprint, contract], { event }) => this.api.events.contracts.Instantiated.is(event)
|
|
61
|
+
? [blueprint, new Contract(this.api, this.abi, event.data[1], this._decorateMethod)]
|
|
62
|
+
: this.api.events.contracts.CodeStored.is(event)
|
|
63
|
+
? [new Blueprint(this.api, this.abi, event.data[0], this._decorateMethod), contract]
|
|
64
|
+
: [blueprint, contract], [undefined, undefined]), this._isRevive) || [undefined, undefined])));
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
export function extendCode(type, decorateMethod) {
|
|
68
|
+
return class extends Code {
|
|
69
|
+
static __CodeType = type;
|
|
70
|
+
constructor(api, abi, wasm) {
|
|
71
|
+
super(api, abi, wasm, decorateMethod);
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
}
|
package/base/Contract.js
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { map } from 'rxjs';
|
|
2
|
+
import { SubmittableResult } from '@pezkuwi/api';
|
|
3
|
+
import { BN, BN_HUNDRED, BN_ONE, BN_ZERO, isUndefined, logger } from '@pezkuwi/util';
|
|
4
|
+
import { applyOnEvent } from '../util.js';
|
|
5
|
+
import { Base } from './Base.js';
|
|
6
|
+
import { convertWeight, withMeta } from './util.js';
|
|
7
|
+
const MAX_CALL_GAS = new BN(5_000_000_000_000).isub(BN_ONE);
|
|
8
|
+
const l = logger('Contract');
|
|
9
|
+
function createQuery(meta, fn) {
|
|
10
|
+
return withMeta(meta, (origin, options, ...params) => fn(origin, options, params));
|
|
11
|
+
}
|
|
12
|
+
function createTx(meta, fn) {
|
|
13
|
+
return withMeta(meta, (options, ...params) => fn(options, params));
|
|
14
|
+
}
|
|
15
|
+
export class ContractSubmittableResult extends SubmittableResult {
|
|
16
|
+
contractEvents;
|
|
17
|
+
constructor(result, contractEvents) {
|
|
18
|
+
super(result);
|
|
19
|
+
this.contractEvents = contractEvents;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export class Contract extends Base {
|
|
23
|
+
/**
|
|
24
|
+
* @description The on-chain address for this contract
|
|
25
|
+
*/
|
|
26
|
+
address;
|
|
27
|
+
#query = {};
|
|
28
|
+
#tx = {};
|
|
29
|
+
constructor(api, abi, address, decorateMethod) {
|
|
30
|
+
super(api, abi, decorateMethod);
|
|
31
|
+
this.address = this.registry.createType(this._isRevive ? 'AccountId20' : 'AccountId', address);
|
|
32
|
+
this.abi.messages.forEach((m) => {
|
|
33
|
+
if (isUndefined(this.#tx[m.method])) {
|
|
34
|
+
this.#tx[m.method] = createTx(m, (o, p) => this.#exec(m, o, p));
|
|
35
|
+
}
|
|
36
|
+
if (isUndefined(this.#query[m.method])) {
|
|
37
|
+
this.#query[m.method] = createQuery(m, (f, o, p) => this.#read(m, o, p).send(f));
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
get query() {
|
|
42
|
+
return this.#query;
|
|
43
|
+
}
|
|
44
|
+
get tx() {
|
|
45
|
+
return this.#tx;
|
|
46
|
+
}
|
|
47
|
+
#getGas = (_gasLimit, isCall = false) => {
|
|
48
|
+
const weight = convertWeight(_gasLimit);
|
|
49
|
+
if (weight.v1Weight.gt(BN_ZERO)) {
|
|
50
|
+
return weight;
|
|
51
|
+
}
|
|
52
|
+
return convertWeight(isCall
|
|
53
|
+
? MAX_CALL_GAS
|
|
54
|
+
: convertWeight(this.api.consts.system.blockWeights
|
|
55
|
+
? this.api.consts.system.blockWeights.maxBlock
|
|
56
|
+
: this.api.consts.system['maximumBlockWeight']).v1Weight.muln(64).div(BN_HUNDRED));
|
|
57
|
+
};
|
|
58
|
+
#exec = (messageOrId, { gasLimit = BN_ZERO, storageDepositLimit = null, value = BN_ZERO }, params) => {
|
|
59
|
+
const palletTx = this._isRevive ? this.api.tx.revive : this.api.tx.contracts;
|
|
60
|
+
return palletTx.call(this.address, value,
|
|
61
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
62
|
+
// @ts-ignore jiggle v1 weights, metadata points to latest
|
|
63
|
+
this._isWeightV1
|
|
64
|
+
? convertWeight(gasLimit).v1Weight
|
|
65
|
+
: convertWeight(gasLimit).v2Weight, storageDepositLimit, this.abi.findMessage(messageOrId).toU8a(params)).withResultTransform((result) =>
|
|
66
|
+
// ContractEmitted is the current generation, ContractExecution is the previous generation
|
|
67
|
+
new ContractSubmittableResult(result, applyOnEvent(result, ['ContractEmitted', 'ContractExecution'], (records) => records
|
|
68
|
+
// Filter to only decode events emitted by this specific contract instance.
|
|
69
|
+
.filter((record) => {
|
|
70
|
+
try {
|
|
71
|
+
const contractAddress = record.event.data[0];
|
|
72
|
+
if (this.address.eq(contractAddress)) {
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
l.debug(`Skipping event from different contract ${contractAddress.toString()} (this contract: ${this.address.toString()})`);
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
l.warn(`Unable to extract contract address from event: ${error.message}`);
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
})
|
|
83
|
+
.map((record) => {
|
|
84
|
+
try {
|
|
85
|
+
return this.abi.decodeEvent(record);
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
l.error(`Unable to decode contract event: ${error.message}`);
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
})
|
|
92
|
+
.filter((decoded) => !!decoded), this._isRevive)));
|
|
93
|
+
};
|
|
94
|
+
#read = (messageOrId, { gasLimit = BN_ZERO, storageDepositLimit = null, value = BN_ZERO }, params) => {
|
|
95
|
+
const message = this.abi.findMessage(messageOrId);
|
|
96
|
+
return {
|
|
97
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
98
|
+
send: this._decorateMethod((origin) => (this._isRevive
|
|
99
|
+
? this.api.rx.call.reviveApi.call
|
|
100
|
+
: this.api.rx.call.contractsApi.call)(origin, this.address, value,
|
|
101
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
102
|
+
// @ts-ignore jiggle v1 weights, metadata points to latest
|
|
103
|
+
this._isWeightV1
|
|
104
|
+
? this.#getGas(gasLimit, true).v1Weight
|
|
105
|
+
: this.#getGas(gasLimit, true).v2Weight, storageDepositLimit, message.toU8a(params)).pipe(map(({ debugMessage, gasConsumed, gasRequired, result, storageDeposit }) => ({
|
|
106
|
+
debugMessage,
|
|
107
|
+
gasConsumed,
|
|
108
|
+
gasRequired: gasRequired && !convertWeight(gasRequired).v1Weight.isZero()
|
|
109
|
+
? gasRequired
|
|
110
|
+
: gasConsumed,
|
|
111
|
+
output: result.isOk && message.returnType
|
|
112
|
+
? this.abi.registry.createTypeUnsafe(message.returnType.lookupName || message.returnType.type, [result.asOk.data.toU8a(true)], { isPedantic: true })
|
|
113
|
+
: null,
|
|
114
|
+
result,
|
|
115
|
+
storageDeposit
|
|
116
|
+
}))))
|
|
117
|
+
};
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
export function extendContract(type, decorateMethod) {
|
|
121
|
+
return class extends Contract {
|
|
122
|
+
static __ContractType = type;
|
|
123
|
+
constructor(api, abi, address) {
|
|
124
|
+
super(api, abi, address, decorateMethod);
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
}
|
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
// Copyright 2017-2025 @polkadot/api authors & contributors
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
|
|
4
1
|
export { Blueprint, BlueprintSubmittableResult, extendBlueprint } from './Blueprint.js';
|
|
5
2
|
export { Code, CodeSubmittableResult, extendCode } from './Code.js';
|
|
6
3
|
export { Contract, extendContract } from './Contract.js';
|
package/base/mock.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { TypeRegistry } from '@pezkuwi/types';
|
|
2
|
+
const registry = new TypeRegistry();
|
|
3
|
+
const instantiateWithCode = () => {
|
|
4
|
+
throw new Error('mock');
|
|
5
|
+
};
|
|
6
|
+
instantiateWithCode.meta = { args: new Array(6) };
|
|
7
|
+
export const mockApi = {
|
|
8
|
+
call: {
|
|
9
|
+
contractsApi: {
|
|
10
|
+
call: () => {
|
|
11
|
+
throw new Error('mock');
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
isConnected: true,
|
|
16
|
+
registry,
|
|
17
|
+
tx: {
|
|
18
|
+
contracts: {
|
|
19
|
+
instantiateWithCode
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
export const mockReviveApi = {
|
|
24
|
+
call: {
|
|
25
|
+
reviveApi: {
|
|
26
|
+
call: () => {
|
|
27
|
+
throw new Error('mock');
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
isConnected: true,
|
|
32
|
+
registry,
|
|
33
|
+
tx: {
|
|
34
|
+
revive: {
|
|
35
|
+
instantiateWithCode
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
};
|
|
@@ -5,7 +5,7 @@ import type { WeightV1, WeightV2 } from '@pezkuwi/types/interfaces';
|
|
|
5
5
|
import type { BN } from '@pezkuwi/util';
|
|
6
6
|
import type { AbiConstructor, AbiMessage, BlueprintOptions, WeightAll } from '../types.js';
|
|
7
7
|
import type { BlueprintDeploy, ContractGeneric } from './types.js';
|
|
8
|
-
export declare const EMPTY_SALT: Uint8Array
|
|
8
|
+
export declare const EMPTY_SALT: Uint8Array;
|
|
9
9
|
export declare function withMeta<T extends {
|
|
10
10
|
meta: AbiMessage;
|
|
11
11
|
}>(meta: AbiMessage, creator: Omit<T, 'meta'>): T;
|
package/base/util.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Bytes } from '@pezkuwi/types';
|
|
2
|
+
import { bnToBn, compactAddLength, u8aToU8a } from '@pezkuwi/util';
|
|
3
|
+
import { randomAsU8a } from '@pezkuwi/util-crypto';
|
|
4
|
+
export const EMPTY_SALT = new Uint8Array();
|
|
5
|
+
export function withMeta(meta, creator) {
|
|
6
|
+
creator.meta = meta;
|
|
7
|
+
return creator;
|
|
8
|
+
}
|
|
9
|
+
export function createBluePrintTx(meta, fn) {
|
|
10
|
+
return withMeta(meta, (options, ...params) => fn(options, params));
|
|
11
|
+
}
|
|
12
|
+
export function createBluePrintWithId(fn) {
|
|
13
|
+
return (constructorOrId, options, ...params) => fn(constructorOrId, options, params);
|
|
14
|
+
}
|
|
15
|
+
export function encodeSalt(salt = randomAsU8a()) {
|
|
16
|
+
return salt instanceof Bytes
|
|
17
|
+
? salt
|
|
18
|
+
: salt?.length
|
|
19
|
+
? compactAddLength(u8aToU8a(salt))
|
|
20
|
+
: EMPTY_SALT;
|
|
21
|
+
}
|
|
22
|
+
export function convertWeight(weight) {
|
|
23
|
+
const [refTime, proofSize] = isWeightV2(weight)
|
|
24
|
+
? [weight.refTime.toBn(), weight.proofSize.toBn()]
|
|
25
|
+
: [bnToBn(weight), undefined];
|
|
26
|
+
return {
|
|
27
|
+
v1Weight: refTime,
|
|
28
|
+
v2Weight: { proofSize, refTime }
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
export function isWeightV2(weight) {
|
|
32
|
+
return !!weight.proofSize;
|
|
33
|
+
}
|