Skip to content

Commit

Permalink
Merge pull request #209 from ElrondNetwork/decoupling
Browse files Browse the repository at this point in the history
Decoupling, minor refactoring
  • Loading branch information
andreibancioiu authored May 5, 2022
2 parents 8a213ed + ed9a8f8 commit 403944b
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 75 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
40 changes: 20 additions & 20 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
32 changes: 6 additions & 26 deletions src/smartcontracts/abi.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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();
}
}
1 change: 1 addition & 0 deletions src/smartcontracts/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
28 changes: 19 additions & 9 deletions src/smartcontracts/interface.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -32,8 +29,8 @@ export interface ISmartContract {
}

export interface DeployArguments {
code: Code;
codeMetadata?: CodeMetadata;
code: ICode;
codeMetadata?: ICodeMetadata;
initArguments?: TypedValue[];
value?: ITransactionValue;
gasLimit: IGasLimit;
Expand All @@ -42,8 +39,8 @@ export interface DeployArguments {
}

export interface UpgradeArguments {
code: Code;
codeMetadata?: CodeMetadata;
code: ICode;
codeMetadata?: ICodeMetadata;
initArguments?: TypedValue[];
value?: ITransactionValue;
gasLimit: IGasLimit;
Expand All @@ -52,7 +49,7 @@ export interface UpgradeArguments {
}

export interface CallArguments {
func: ContractFunction;
func: IContractFunction;
args?: TypedValue[];
value?: ITransactionValue;
gasLimit: IGasLimit;
Expand All @@ -62,7 +59,7 @@ export interface CallArguments {
}

export interface QueryArguments {
func: ContractFunction;
func: IContractFunction;
args?: TypedValue[];
value?: ITransactionValue;
caller?: IAddress
Expand All @@ -83,3 +80,16 @@ export interface UntypedOutcomeBundle {
returnMessage: string;
values: Buffer[];
}

export interface ICode {
toString(): string;
}

export interface ICodeMetadata {
toString(): string;
}

export interface IContractFunction {
name: string;
toString(): string;
}
6 changes: 3 additions & 3 deletions src/smartcontracts/query.ts
Original file line number Diff line number Diff line change
@@ -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
}) {
Expand Down
24 changes: 11 additions & 13 deletions src/smartcontracts/transactionPayloadBuilders.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@

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 { ICode, ICodeMetadata, IContractFunction } from "./interface";
import { TypedValue } from "./typesystem";

export const ArwenVirtualMachine = "0500";
Expand All @@ -13,22 +11,22 @@ export const ArwenVirtualMachine = "0500";
* 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;
}

/**
* Sets the code metadata of the Smart Contract.
*/
setCodeMetadata(codeMetadata: CodeMetadata): ContractDeployPayloadBuilder {
setCodeMetadata(codeMetadata: ICodeMetadata): ContractDeployPayloadBuilder {
this.codeMetadata = codeMetadata;
return this;
}
Expand Down Expand Up @@ -68,22 +66,22 @@ 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;
}

/**
* Sets the code metadata of the Smart Contract.
*/
setCodeMetadata(codeMetadata: CodeMetadata): ContractUpgradePayloadBuilder {
setCodeMetadata(codeMetadata: ICodeMetadata): ContractUpgradePayloadBuilder {
this.codeMetadata = codeMetadata;
return this;
}
Expand Down Expand Up @@ -123,13 +121,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;
}
Expand Down
4 changes: 3 additions & 1 deletion src/tokenPayment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 403944b

Please sign in to comment.