Skip to content

Commit 5d6c2dd

Browse files
authored
DRY cleanups for contracts (#4395)
1 parent ade5dc4 commit 5d6c2dd

File tree

6 files changed

+34
-29
lines changed

6 files changed

+34
-29
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"@babel/core": "^7.16.5",
3535
"@babel/register": "^7.16.5",
3636
"@babel/runtime": "^7.16.5",
37-
"@polkadot/dev": "^0.64.21",
37+
"@polkadot/dev": "^0.64.22",
3838
"@polkadot/typegen": "workspace:packages/typegen",
3939
"@types/jest": "^27.0.3",
4040
"copyfiles": "^2.4.1"

packages/api-contract/src/base/Blueprint.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,19 @@ export class Blueprint<ApiType extends ApiTypes> extends Base<ApiType> {
5858

5959
#deploy = (constructorOrId: AbiConstructor | string | number, { gasLimit = BN_ZERO, salt, storageDepositLimit = null, value = BN_ZERO }: BlueprintOptions, params: unknown[]): SubmittableExtrinsic<ApiType, BlueprintSubmittableResult<ApiType>> => {
6060
const hasStorageDeposit = this.api.tx.contracts.instantiate.meta.args.length === 6;
61-
61+
const encParams = this.abi.findConstructor(constructorOrId).toU8a(params);
62+
const encSalt = encodeSalt(salt);
6263
const tx = hasStorageDeposit
63-
? this.api.tx.contracts.instantiate(value, gasLimit, storageDepositLimit, this.codeHash, this.abi.findConstructor(constructorOrId).toU8a(params), encodeSalt(salt))
64+
? this.api.tx.contracts.instantiate(value, gasLimit, storageDepositLimit, this.codeHash, encParams, encSalt)
6465
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
6566
// @ts-ignore old style without storage deposit
66-
: this.api.tx.contracts.instantiate(value, gasLimit, this.codeHash, this.abi.findConstructor(constructorOrId).toU8a(params), encodeSalt(salt));
67-
68-
return tx
69-
.withResultTransform((result: ISubmittableResult) =>
70-
new BlueprintSubmittableResult(result, applyOnEvent(result, ['Instantiated'], ([record]: EventRecord[]) =>
71-
new Contract<ApiType>(this.api, this.abi, record.event.data[1] as AccountId, this._decorateMethod)
72-
))
73-
);
67+
: this.api.tx.contracts.instantiate(value, gasLimit, this.codeHash, encParams, encSalt);
68+
69+
return tx.withResultTransform((result: ISubmittableResult) =>
70+
new BlueprintSubmittableResult(result, applyOnEvent(result, ['Instantiated'], ([record]: EventRecord[]) =>
71+
new Contract<ApiType>(this.api, this.abi, record.event.data[1] as AccountId, this._decorateMethod)
72+
))
73+
);
7474
};
7575
}
7676

packages/api-contract/src/base/Code.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,14 @@ export class Code<ApiType extends ApiTypes> extends Base<ApiType> {
6363

6464
#instantiate = (constructorOrId: AbiConstructor | string | number, { gasLimit = BN_ZERO, salt, storageDepositLimit = null, value = BN_ZERO }: BlueprintOptions, params: unknown[]): SubmittableExtrinsic<ApiType, CodeSubmittableResult<ApiType>> => {
6565
const hasStorageDeposit = this.api.tx.contracts.instantiateWithCode.meta.args.length === 6;
66-
66+
const encCode = compactAddLength(this.code);
67+
const encParams = this.abi.findConstructor(constructorOrId).toU8a(params);
68+
const encSalt = encodeSalt(salt);
6769
const tx = hasStorageDeposit
68-
? this.api.tx.contracts.instantiateWithCode(value, gasLimit, storageDepositLimit, compactAddLength(this.code), this.abi.findConstructor(constructorOrId).toU8a(params), encodeSalt(salt))
70+
? this.api.tx.contracts.instantiateWithCode(value, gasLimit, storageDepositLimit, encCode, encParams, encSalt)
6971
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
7072
// @ts-ignore old style without storage deposit
71-
: this.api.tx.contracts.instantiateWithCode(value, gasLimit, compactAddLength(this.code), this.abi.findConstructor(constructorOrId).toU8a(params), encodeSalt(salt));
73+
: this.api.tx.contracts.instantiateWithCode(value, gasLimit, encCode, encParams, encSalt);
7274

7375
return tx.withResultTransform((result: ISubmittableResult) =>
7476
new CodeSubmittableResult(result, ...(applyOnEvent(result, ['CodeStored', 'Instantiated'], (records: EventRecord[]) =>

packages/api-contract/src/base/Contract.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,13 @@ export class Contract<ApiType extends ApiTypes> extends Base<ApiType> {
108108

109109
#exec = (messageOrId: AbiMessage | string | number, { gasLimit = BN_ZERO, storageDepositLimit = null, value = BN_ZERO }: ContractOptions, params: unknown[]): SubmittableExtrinsic<ApiType> => {
110110
const hasStorageDeposit = this.api.tx.contracts.call.meta.args.length === 5;
111-
111+
const gas = this.#getGas(gasLimit);
112+
const encParams = this.abi.findMessage(messageOrId).toU8a(params);
112113
const tx = hasStorageDeposit
113-
? this.api.tx.contracts.call(this.address, value, this.#getGas(gasLimit), storageDepositLimit, this.abi.findMessage(messageOrId).toU8a(params))
114+
? this.api.tx.contracts.call(this.address, value, gas, storageDepositLimit, encParams)
114115
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
115116
// @ts-ignore old style without storage deposit
116-
: this.api.tx.contracts.call(this.address, value, this.#getGas(gasLimit), this.abi.findMessage(messageOrId).toU8a(params));
117+
: this.api.tx.contracts.call(this.address, value, gas, encParams);
117118

118119
return tx.withResultTransform((result: ISubmittableResult) =>
119120
// ContractEmitted is the current generation, ContractExecution is the previous generation
@@ -142,11 +143,12 @@ export class Contract<ApiType extends ApiTypes> extends Base<ApiType> {
142143
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
143144
send: this._decorateMethod((origin: string | AccountId | Uint8Array) => {
144145
const hasStorageDeposit = this.api.tx.contracts.call.meta.args.length === 5;
146+
const inputData = message.toU8a(params);
145147
const rpc = hasStorageDeposit
146-
? this.api.rx.rpc.contracts.call({ dest: this.address, gasLimit: this.#getGas(gasLimit, true), inputData: message.toU8a(params), origin, storageDepositLimit, value })
147-
: this.api.rx.rpc.contracts.call({ dest: this.address, gasLimit: this.#getGas(gasLimit, true), inputData: message.toU8a(params), origin, value });
148+
? this.api.rx.rpc.contracts.call({ dest: this.address, gasLimit: this.#getGas(gasLimit, true), inputData, origin, storageDepositLimit, value })
149+
: this.api.rx.rpc.contracts.call({ dest: this.address, gasLimit: this.#getGas(gasLimit, true), inputData, origin, value });
148150

149-
const mapFn = ({ debugMessage, gasConsumed, gasRequired, result }: ContractExecResult): ContractCallOutcome => ({
151+
const mapFn = ({ debugMessage, gasConsumed, gasRequired, result, storageDeposit }: ContractExecResult): ContractCallOutcome => ({
150152
debugMessage,
151153
gasConsumed,
152154
gasRequired: gasRequired && !gasRequired.isZero()
@@ -155,12 +157,12 @@ export class Contract<ApiType extends ApiTypes> extends Base<ApiType> {
155157
output: result.isOk && message.returnType
156158
? this.abi.registry.createTypeUnsafe(message.returnType.lookupName || message.returnType.type, [result.asOk.data.toU8a(true)], { isPedantic: true })
157159
: null,
158-
result
160+
result,
161+
storageDeposit
159162
});
160163

161164
return rpc.pipe(map(mapFn));
162-
}
163-
)
165+
})
164166
};
165167
};
166168
}

packages/api-contract/src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import type { ApiBase } from '@polkadot/api/base';
55
import type { ApiTypes } from '@polkadot/api/types';
66
import type { Text, u64 } from '@polkadot/types';
7-
import type { ContractExecResultResult, ContractSelector } from '@polkadot/types/interfaces';
7+
import type { ContractExecResultResult, ContractSelector, StorageDeposit } from '@polkadot/types/interfaces';
88
import type { Codec, TypeDef } from '@polkadot/types/types';
99
import type { BN } from '@polkadot/util';
1010
import type { Abi } from '.';
@@ -58,6 +58,7 @@ export interface ContractCallOutcome {
5858
gasRequired: u64;
5959
output: Codec | null;
6060
result: ContractExecResultResult;
61+
storageDeposit: StorageDeposit;
6162
}
6263

6364
export interface DecodedEvent {

yarn.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1955,9 +1955,9 @@ __metadata:
19551955
languageName: unknown
19561956
linkType: soft
19571957

1958-
"@polkadot/dev@npm:^0.64.21":
1959-
version: 0.64.21
1960-
resolution: "@polkadot/dev@npm:0.64.21"
1958+
"@polkadot/dev@npm:^0.64.22":
1959+
version: 0.64.22
1960+
resolution: "@polkadot/dev@npm:0.64.22"
19611961
dependencies:
19621962
"@babel/cli": ^7.16.0
19631963
"@babel/core": ^7.16.5
@@ -2042,7 +2042,7 @@ __metadata:
20422042
polkadot-exec-rollup: scripts/polkadot-exec-rollup.mjs
20432043
polkadot-exec-tsc: scripts/polkadot-exec-tsc.mjs
20442044
polkadot-exec-webpack: scripts/polkadot-exec-webpack.mjs
2045-
checksum: c2f1c141b00f940b4940df21dae51e39e9f71ea1caa3a33ce819701c7b5e9cf85491cf7315efb69a99c184bdb5c502cc13c906047c5cb49cacf1360bfd2ea89e
2045+
checksum: 4f2e79f7de3777b17ab877819d32a9b88ec136e18cd0fc8002885b3bf0afae0e02acf9e05b16ecc86c20c12c8a5fea89416c2f48555571c2223929ce5b5fa1a5
20462046
languageName: node
20472047
linkType: hard
20482048

@@ -8954,7 +8954,7 @@ resolve@^2.0.0-next.3:
89548954
"@babel/core": ^7.16.5
89558955
"@babel/register": ^7.16.5
89568956
"@babel/runtime": ^7.16.5
8957-
"@polkadot/dev": ^0.64.21
8957+
"@polkadot/dev": ^0.64.22
89588958
"@polkadot/typegen": "workspace:packages/typegen"
89598959
"@types/jest": ^27.0.3
89608960
copyfiles: ^2.4.1

0 commit comments

Comments
 (0)