From ffd0e30cea596ae87500f0f78a4dc6c7a690f3df Mon Sep 17 00:00:00 2001 From: Andrei Bancioiu Date: Wed, 4 May 2022 17:53:19 +0300 Subject: [PATCH 1/7] TokenPayment.numDecimals is now a public field. --- src/tokenPayment.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/tokenPayment.ts b/src/tokenPayment.ts index 05935cff..5552dbd2 100644 --- a/src/tokenPayment.ts +++ b/src/tokenPayment.ts @@ -11,8 +11,9 @@ export class TokenPayment { readonly tokenIdentifier: string; readonly nonce: number; readonly amountAsBigInteger: BigNumber; - private readonly numDecimals: number; + readonly numDecimals: number; + // TODO (breaking, next major version): constructor({ ... }) constructor(tokenIdentifier: string, nonce: number, amountAsBigInteger: BigNumber.Value, numDecimals: number) { let amount = new BigNumber(amountAsBigInteger); if (!amount.isInteger() || amount.isNegative()) { @@ -72,6 +73,7 @@ export class TokenPayment { return `${this.toRationalNumber()} ${this.tokenIdentifier}`; } + // TODO (breaking, next major version): rename to "toAmount()", make it private. toRationalNumber() { return this.amountAsBigInteger.shiftedBy(-this.numDecimals).toFixed(this.numDecimals); } From 0676cf7efc55da068520e2b1890043373805134f Mon Sep 17 00:00:00 2001 From: Andrei Bancioiu Date: Wed, 4 May 2022 17:57:04 +0300 Subject: [PATCH 2/7] ITokenPayment now has BigNumber.Value as output dependency, instead of BigNumber. Theoretically, this isn't a breaking change (interface is used as an input dependency): an [old ITokenPayment] is assignable to a variable of the type [new ITokenPayment]. --- src/interface.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/interface.ts b/src/interface.ts index aa0becbd..b0fcac57 100644 --- a/src/interface.ts +++ b/src/interface.ts @@ -27,6 +27,6 @@ export interface ITransactionPayload { export interface ITokenPayment { readonly tokenIdentifier: string; readonly nonce: number; - readonly amountAsBigInteger: BigNumber; - valueOf(): BigNumber; + readonly amountAsBigInteger: BigNumber.Value; + valueOf(): BigNumber.Value; } From a6bb658b2616d0d7945329324919050dc67d9ee5 Mon Sep 17 00:00:00 2001 From: Andrei Bancioiu Date: Wed, 4 May 2022 17:58:02 +0300 Subject: [PATCH 3/7] Decouple transactionPayloadBuilders from "Code", "CodeMetadata" and "ContractFunction". Decoupling from "TypedValue" is a bit more complex - not done yet. --- .../transactionPayloadBuilders.ts | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/src/smartcontracts/transactionPayloadBuilders.ts b/src/smartcontracts/transactionPayloadBuilders.ts index 7b565354..af96af07 100644 --- a/src/smartcontracts/transactionPayloadBuilders.ts +++ b/src/smartcontracts/transactionPayloadBuilders.ts @@ -1,26 +1,35 @@ import { TransactionPayload } from "../transactionPayload"; import { guardValueIsSet } from "../utils"; -import { Code } from "./code"; -import { CodeMetadata } from "./codeMetadata"; -import { ContractFunction } from "./function"; import { ArgSerializer } from "./argSerializer"; import { TypedValue } from "./typesystem"; export const ArwenVirtualMachine = "0500"; +interface ICode { + toString(): string; +} + +interface ICodeMetadata { + toString(): string; +} + +interface IContractFunction { + name: string; +} + /** * A builder for {@link TransactionPayload} objects, to be used for Smart Contract deployment transactions. */ export class ContractDeployPayloadBuilder { - private code: Code | null = null; - private codeMetadata: CodeMetadata = new CodeMetadata(); + private code: ICode | null = null; + private codeMetadata: ICodeMetadata = ""; private arguments: TypedValue[] = []; /** * Sets the code of the Smart Contract. */ - setCode(code: Code): ContractDeployPayloadBuilder { + setCode(code: ICode): ContractDeployPayloadBuilder { this.code = code; return this; } @@ -28,7 +37,7 @@ export class ContractDeployPayloadBuilder { /** * Sets the code metadata of the Smart Contract. */ - setCodeMetadata(codeMetadata: CodeMetadata): ContractDeployPayloadBuilder { + setCodeMetadata(codeMetadata: ICodeMetadata): ContractDeployPayloadBuilder { this.codeMetadata = codeMetadata; return this; } @@ -68,14 +77,14 @@ export class ContractDeployPayloadBuilder { * A builder for {@link TransactionPayload} objects, to be used for Smart Contract upgrade transactions. */ export class ContractUpgradePayloadBuilder { - private code: Code | null = null; - private codeMetadata: CodeMetadata = new CodeMetadata(); + private code: ICode | null = null; + private codeMetadata: ICodeMetadata = ""; private arguments: TypedValue[] = []; /** * Sets the code of the Smart Contract. */ - setCode(code: Code): ContractUpgradePayloadBuilder { + setCode(code: ICode): ContractUpgradePayloadBuilder { this.code = code; return this; } @@ -83,7 +92,7 @@ export class ContractUpgradePayloadBuilder { /** * Sets the code metadata of the Smart Contract. */ - setCodeMetadata(codeMetadata: CodeMetadata): ContractUpgradePayloadBuilder { + setCodeMetadata(codeMetadata: ICodeMetadata): ContractUpgradePayloadBuilder { this.codeMetadata = codeMetadata; return this; } @@ -123,13 +132,13 @@ export class ContractUpgradePayloadBuilder { * A builder for {@link TransactionPayload} objects, to be used for Smart Contract execution transactions. */ export class ContractCallPayloadBuilder { - private contractFunction: ContractFunction | null = null; + private contractFunction: IContractFunction | null = null; private arguments: TypedValue[] = []; /** * Sets the function to be called (executed). */ - setFunction(contractFunction: ContractFunction): ContractCallPayloadBuilder { + setFunction(contractFunction: IContractFunction): ContractCallPayloadBuilder { this.contractFunction = contractFunction; return this; } From 1b6765afed169208a998c4318bd3096d9833e6b6 Mon Sep 17 00:00:00 2001 From: Andrei Bancioiu Date: Wed, 4 May 2022 18:06:11 +0300 Subject: [PATCH 4/7] Further decoupling from "Code", "CodeMetadata", "ContractFunction". --- src/smartcontracts/function.ts | 1 + src/smartcontracts/interface.ts | 28 +++++++++++++++++++--------- src/smartcontracts/query.ts | 6 +++--- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/smartcontracts/function.ts b/src/smartcontracts/function.ts index df0281ca..2725625c 100644 --- a/src/smartcontracts/function.ts +++ b/src/smartcontracts/function.ts @@ -40,6 +40,7 @@ export class ContractFunction { return this.name; } + // TODO (breaking, next major version): remove function, not used. equals(other: ContractFunction | null): boolean { if (!other) { return false; diff --git a/src/smartcontracts/interface.ts b/src/smartcontracts/interface.ts index 53985cd5..95f4f76e 100644 --- a/src/smartcontracts/interface.ts +++ b/src/smartcontracts/interface.ts @@ -1,8 +1,5 @@ import { IAddress, IChainID, IGasLimit, IGasPrice, ITransactionValue } from "../interface"; import { Transaction } from "../transaction"; -import { Code } from "./code"; -import { CodeMetadata } from "./codeMetadata"; -import { ContractFunction } from "./function"; import { ReturnCode } from "./returnCode"; import { TypedValue } from "./typesystem"; @@ -32,8 +29,8 @@ export interface ISmartContract { } export interface DeployArguments { - code: Code; - codeMetadata?: CodeMetadata; + code: ICode; + codeMetadata?: ICodeMetadata; initArguments?: TypedValue[]; value?: ITransactionValue; gasLimit: IGasLimit; @@ -42,8 +39,8 @@ export interface DeployArguments { } export interface UpgradeArguments { - code: Code; - codeMetadata?: CodeMetadata; + code: ICode; + codeMetadata?: ICodeMetadata; initArguments?: TypedValue[]; value?: ITransactionValue; gasLimit: IGasLimit; @@ -52,7 +49,7 @@ export interface UpgradeArguments { } export interface CallArguments { - func: ContractFunction; + func: IContractFunction; args?: TypedValue[]; value?: ITransactionValue; gasLimit: IGasLimit; @@ -62,7 +59,7 @@ export interface CallArguments { } export interface QueryArguments { - func: ContractFunction; + func: IContractFunction; args?: TypedValue[]; value?: ITransactionValue; caller?: IAddress @@ -83,3 +80,16 @@ export interface UntypedOutcomeBundle { returnMessage: string; values: Buffer[]; } + +interface ICode { + toString(): string; +} + +interface ICodeMetadata { + toString(): string; +} + +export interface IContractFunction { + name: string; + toString(): string; +} diff --git a/src/smartcontracts/query.ts b/src/smartcontracts/query.ts index d53e4053..a298ebf0 100644 --- a/src/smartcontracts/query.ts +++ b/src/smartcontracts/query.ts @@ -1,20 +1,20 @@ -import { ContractFunction } from "./function"; import { Address } from "../address"; import { TypedValue } from "./typesystem"; import { ArgSerializer } from "./argSerializer"; import { IAddress, ITransactionValue } from "../interface"; +import { IContractFunction } from "./interface"; export class Query { caller: IAddress; address: IAddress; - func: ContractFunction; + func: IContractFunction; args: TypedValue[]; value: ITransactionValue; constructor(obj: { caller?: IAddress, address: IAddress, - func: ContractFunction, + func: IContractFunction, args?: TypedValue[], value?: ITransactionValue }) { From 74195800ef573c9fea8e9c53d6a6edab4771a2b3 Mon Sep 17 00:00:00 2001 From: Andrei Bancioiu Date: Thu, 5 May 2022 09:09:32 +0300 Subject: [PATCH 5/7] Simplification: ignore second parameter of new SmartContractAbi(). --- src/smartcontracts/abi.ts | 32 ++++++-------------------------- 1 file changed, 6 insertions(+), 26 deletions(-) diff --git a/src/smartcontracts/abi.ts b/src/smartcontracts/abi.ts index 37ce3b42..440c9502 100644 --- a/src/smartcontracts/abi.ts +++ b/src/smartcontracts/abi.ts @@ -1,24 +1,18 @@ -import { ErrInvariantFailed } from "../errors"; import { guardValueIsSetWithMessage } from "../utils"; import { ContractFunction } from "./function"; import { AbiRegistry, EndpointDefinition } from "./typesystem"; import { ContractInterface } from "./typesystem/contractInterface"; export class SmartContractAbi { - private readonly interfaces: ContractInterface[] = []; + private readonly interface: ContractInterface; - constructor(registry: AbiRegistry, implementsInterfaces: string[]) { - this.interfaces.push(...registry.getInterfaces(implementsInterfaces)); + // TODO (breaking, next major version): remove second parameter (not used anymore). + constructor(registry: AbiRegistry, _implementsInterfaces?: string[]) { + this.interface = registry.interfaces[0]; } getAllEndpoints(): EndpointDefinition[] { - let endpoints = []; - - for (const iface of this.interfaces) { - endpoints.push(...iface.endpoints); - } - - return endpoints; + return this.interface.endpoints; } getEndpoint(name: string | ContractFunction): EndpointDefinition { @@ -31,20 +25,6 @@ export class SmartContractAbi { } getConstructorDefinition(): EndpointDefinition | null { - let constructors = []; - for (const iface of this.interfaces) { - let constructor_definition = iface.getConstructorDefinition(); - if (constructor_definition !== null) { - constructors.push(constructor_definition); - } - } - switch (constructors.length) { - case 0: - return null; - case 1: - return constructors[0]; - default: - throw new ErrInvariantFailed(`Found more than 1 constructor (found ${constructors.length})`); - } + return this.interface.getConstructorDefinition(); } } From 85d61f83bc04c35ed17d114d3c3c9fe552bbac0d Mon Sep 17 00:00:00 2001 From: Andrei Bancioiu Date: Thu, 5 May 2022 09:49:15 +0300 Subject: [PATCH 6/7] Update changelog, bump version. --- CHANGELOG.md | 3 +++ package-lock.json | 40 ++++++++++++++++++++-------------------- package.json | 2 +- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 99f80979..3a8e94a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how ## Unreleased - TBD +## 10.2.2 + - [Decoupling, minor refactoring](https://github.com/ElrondNetwork/elrond-sdk-erdjs/pull/209) + ## 10.2.1 - [When loading the ABI, sort custom types by their type dependencies](https://github.com/ElrondNetwork/elrond-sdk-erdjs/pull/208) diff --git a/package-lock.json b/package-lock.json index ea841909..e240755e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@elrondnetwork/erdjs", - "version": "10.2.1", + "version": "10.2.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@elrondnetwork/erdjs", - "version": "10.2.1", + "version": "10.2.2", "license": "GPL-3.0-or-later", "dependencies": { "@elrondnetwork/transaction-decoder": "0.1.0", @@ -1556,9 +1556,9 @@ } }, "node_modules/caniuse-lite": { - "version": "1.0.30001335", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001335.tgz", - "integrity": "sha512-ddP1Tgm7z2iIxu6QTtbZUv6HJxSaV/PZeSrWFZtbY4JZ69tOeNhBCl3HyRQgeNZKE5AOn1kpV7fhljigy0Ty3w==", + "version": "1.0.30001336", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001336.tgz", + "integrity": "sha512-/YxSlBmL7iKXTbIJ48IQTnAOBk7XmWsxhBF1PZLOko5Dt9qc4Pl+84lfqG3Tc4EuavurRn1QLoVJGxY2iSycfw==", "dev": true, "funding": [ { @@ -2092,9 +2092,9 @@ } }, "node_modules/electron-to-chromium": { - "version": "1.4.131", - "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.131.tgz", - "integrity": "sha512-oi3YPmaP87hiHn0c4ePB67tXaF+ldGhxvZnT19tW9zX6/Ej+pLN0Afja5rQ6S+TND7I9EuwQTT8JYn1k7R7rrw==", + "version": "1.4.134", + "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.134.tgz", + "integrity": "sha512-OdD7M2no4Mi8PopfvoOuNcwYDJ2mNFxaBfurA6okG3fLBaMcFah9S+si84FhX+FIWLKkdaiHfl4A+5ep/gOVrg==", "dev": true }, "node_modules/elliptic": { @@ -2558,9 +2558,9 @@ } }, "node_modules/follow-redirects": { - "version": "1.14.9", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.9.tgz", - "integrity": "sha512-MQDfihBQYMcyy5dhRDJUHcw7lb2Pv/TuE6xP1vyraLukNDHKbDxDNaOE3NbCAdKQApno+GPRyo1YAp89yCjK4w==", + "version": "1.15.0", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.0.tgz", + "integrity": "sha512-aExlJShTV4qOUOL7yF1U5tvLCB0xQuudbf6toyYA0E/acBNw71mvjFTnLaRp50aQaYocMR0a/RMMBIHeZnGyjQ==", "dev": true, "funding": [ { @@ -7006,9 +7006,9 @@ "dev": true }, "caniuse-lite": { - "version": "1.0.30001335", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001335.tgz", - "integrity": "sha512-ddP1Tgm7z2iIxu6QTtbZUv6HJxSaV/PZeSrWFZtbY4JZ69tOeNhBCl3HyRQgeNZKE5AOn1kpV7fhljigy0Ty3w==", + "version": "1.0.30001336", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001336.tgz", + "integrity": "sha512-/YxSlBmL7iKXTbIJ48IQTnAOBk7XmWsxhBF1PZLOko5Dt9qc4Pl+84lfqG3Tc4EuavurRn1QLoVJGxY2iSycfw==", "dev": true }, "chai": { @@ -7464,9 +7464,9 @@ } }, "electron-to-chromium": { - "version": "1.4.131", - "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.131.tgz", - "integrity": "sha512-oi3YPmaP87hiHn0c4ePB67tXaF+ldGhxvZnT19tW9zX6/Ej+pLN0Afja5rQ6S+TND7I9EuwQTT8JYn1k7R7rrw==", + "version": "1.4.134", + "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.134.tgz", + "integrity": "sha512-OdD7M2no4Mi8PopfvoOuNcwYDJ2mNFxaBfurA6okG3fLBaMcFah9S+si84FhX+FIWLKkdaiHfl4A+5ep/gOVrg==", "dev": true }, "elliptic": { @@ -7868,9 +7868,9 @@ "dev": true }, "follow-redirects": { - "version": "1.14.9", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.9.tgz", - "integrity": "sha512-MQDfihBQYMcyy5dhRDJUHcw7lb2Pv/TuE6xP1vyraLukNDHKbDxDNaOE3NbCAdKQApno+GPRyo1YAp89yCjK4w==", + "version": "1.15.0", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.0.tgz", + "integrity": "sha512-aExlJShTV4qOUOL7yF1U5tvLCB0xQuudbf6toyYA0E/acBNw71mvjFTnLaRp50aQaYocMR0a/RMMBIHeZnGyjQ==", "dev": true }, "foreach": { diff --git a/package.json b/package.json index bd3347ce..4a48fd0c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@elrondnetwork/erdjs", - "version": "10.2.1", + "version": "10.2.2", "description": "Smart Contracts interaction framework", "main": "out/index.js", "types": "out/index.d.js", From ed9a8f888d0ac747d41a1a696b99815cf7f511b6 Mon Sep 17 00:00:00 2001 From: Andrei Bancioiu Date: Thu, 5 May 2022 09:51:31 +0300 Subject: [PATCH 7/7] Fix after self-review. --- src/smartcontracts/interface.ts | 4 ++-- src/smartcontracts/transactionPayloadBuilders.ts | 13 +------------ 2 files changed, 3 insertions(+), 14 deletions(-) diff --git a/src/smartcontracts/interface.ts b/src/smartcontracts/interface.ts index 95f4f76e..7082912c 100644 --- a/src/smartcontracts/interface.ts +++ b/src/smartcontracts/interface.ts @@ -81,11 +81,11 @@ export interface UntypedOutcomeBundle { values: Buffer[]; } -interface ICode { +export interface ICode { toString(): string; } -interface ICodeMetadata { +export interface ICodeMetadata { toString(): string; } diff --git a/src/smartcontracts/transactionPayloadBuilders.ts b/src/smartcontracts/transactionPayloadBuilders.ts index af96af07..f1bf9b37 100644 --- a/src/smartcontracts/transactionPayloadBuilders.ts +++ b/src/smartcontracts/transactionPayloadBuilders.ts @@ -2,22 +2,11 @@ import { TransactionPayload } from "../transactionPayload"; import { guardValueIsSet } from "../utils"; import { ArgSerializer } from "./argSerializer"; +import { ICode, ICodeMetadata, IContractFunction } from "./interface"; import { TypedValue } from "./typesystem"; export const ArwenVirtualMachine = "0500"; -interface ICode { - toString(): string; -} - -interface ICodeMetadata { - toString(): string; -} - -interface IContractFunction { - name: string; -} - /** * A builder for {@link TransactionPayload} objects, to be used for Smart Contract deployment transactions. */