Skip to content

Commit

Permalink
Merge pull request #188 from ElrondNetwork/simplify-or-remove-controller
Browse files Browse the repository at this point in the history
Remove SmartContractController (downgraded to a mere test utility)
  • Loading branch information
andreibancioiu authored Apr 8, 2022
2 parents 1eb62b0 + 4007dc0 commit b373c59
Show file tree
Hide file tree
Showing 15 changed files with 112 additions and 184 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ All notable changes will be documented in this file.
Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.

## Unreleased
- TBD

## 10.0.0-alpha.5
- [Breaking change: adjustements to transaction awaitening and completion, transaction watcher](https://github.com/ElrondNetwork/elrond-sdk-erdjs/pull/173)
- [Breaking change: simplify network config / improve design - not a singleton anymore](https://github.com/ElrondNetwork/elrond-sdk-erdjs/pull/176)
- [Fix / improve results parser (better heuristics)](https://github.com/ElrondNetwork/elrond-sdk-erdjs/pull/177)
Expand All @@ -18,6 +21,7 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
- [Breaking change: unifying provider interfaces, preparing network providers for extraction - step 5](https://github.com/ElrondNetwork/elrond-sdk-erdjs/pull/185)
- [Breaking change: extractions (network providers and contract wrappers)](https://github.com/ElrondNetwork/elrond-sdk-erdjs/pull/186)
- [Breaking change: rename "methods" to "methodsExplicit". Rename "methodsAuto" to "methods" (default choice)](https://github.com/ElrondNetwork/elrond-sdk-erdjs/pull/187)
- [Remove SmartContractController (downgraded to a mere test utility)](https://github.com/ElrondNetwork/elrond-sdk-erdjs/pull/188)

**Breaking changes**
- Removed utility functions: `transaction.awaitExecuted()`, `transaction.awaitPending()`. `TransactionWatcher` should be used directly, instead.
Expand All @@ -38,6 +42,7 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
- https://github.com/ElrondNetwork/elrond-sdk-erdjs-network-providers
- https://github.com/ElrondNetwork/elrond-sdk-erdjs-contract-wrappers
- Rename `methods` to `methodsExplicit`. Rename `methodsAuto` (only added in erdjs 10 beta) to `methods` (default choice). Therefore, by default, interactions are created without having to pass `TypedValue` objects as parameters. Automatic type inference is applied.
- Remove `SmartContractController` (downgraded to a mere test utility).

## [10.0.0-beta.3]
- [Extract dapp / signing providers to separate repositories](https://github.com/ElrondNetwork/elrond-sdk-erdjs/pull/170)
Expand Down
4 changes: 2 additions & 2 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.0.0-beta.3",
"version": "10.0.0-alpha.5",
"description": "Smart Contracts interaction framework",
"main": "out/index.js",
"types": "out/index.d.js",
Expand Down
7 changes: 1 addition & 6 deletions src/interface.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import { Signature } from "./signature";
import { ITransactionOnNetwork, ITransactionStatus } from "./interfaceOfNetwork";
import { ITransactionOnNetwork } from "./interfaceOfNetwork";

export interface ITransactionFetcher {
/**
* Fetches the state of a {@link Transaction}.
*/
getTransaction(txHash: IHash, hintSender?: IBech32Address, withResults?: boolean): Promise<ITransactionOnNetwork>;

/**
* Queries the status of a {@link Transaction}.
*/
getTransactionStatus(txHash: IHash): Promise<ITransactionStatus>;
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/smartcontracts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export * from "./argSerializer";
export * from "./code";
export * from "./codec";
export * from "./codeMetadata";
export * from "./smartContractController";
export * from "./function";
export * from "./interaction";
export * from "./interactionChecker";
Expand Down
10 changes: 4 additions & 6 deletions src/smartcontracts/interaction.local.net.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { DefaultSmartContractController } from "./smartContractController";
import { ContractController } from "../testutils/contractController";
import { SmartContract } from "./smartContract";
import { BigUIntValue, OptionalValue, OptionValue, TokenIdentifierValue, U32Value } from "./typesystem";
import { loadAbiRegistry, loadContractCode, loadTestWallets, TestWallet } from "../testutils";
import { SmartContractAbi } from "./abi";
import { assert } from "chai";
import { Interaction } from "./interaction";
import { GasLimit } from "../networkParams";
import { ReturnCode } from "./returnCode";
import BigNumber from "bignumber.js";
import { BytesValue } from "./typesystem/bytes";
import { createLocalnetProvider } from "../testutils/networkProviders";


Expand All @@ -26,7 +24,7 @@ describe("test smart contract interactor", function () {
let abiRegistry = await loadAbiRegistry(["src/testdata/answer.abi.json"]);
let abi = new SmartContractAbi(abiRegistry, ["answer"]);
let contract = new SmartContract({ abi: abi });
let controller = new DefaultSmartContractController(abi, provider);
let controller = new ContractController(provider);

let network = await provider.getNetworkConfig();
await alice.sync(provider);
Expand Down Expand Up @@ -74,7 +72,7 @@ describe("test smart contract interactor", function () {
let abiRegistry = await loadAbiRegistry(["src/testdata/counter.abi.json"]);
let abi = new SmartContractAbi(abiRegistry, ["counter"]);
let contract = new SmartContract({ abi: abi });
let controller = new DefaultSmartContractController(abi, provider);
let controller = new ContractController(provider);

let network = await provider.getNetworkConfig();
await alice.sync(provider);
Expand Down Expand Up @@ -127,7 +125,7 @@ describe("test smart contract interactor", function () {
let abiRegistry = await loadAbiRegistry(["src/testdata/lottery-esdt.abi.json"]);
let abi = new SmartContractAbi(abiRegistry, ["Lottery"]);
let contract = new SmartContract({ abi: abi });
let controller = new DefaultSmartContractController(abi, provider);
let controller = new ContractController(provider);

let network = await provider.getNetworkConfig();
await alice.sync(provider);
Expand Down
11 changes: 6 additions & 5 deletions src/smartcontracts/interaction.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DefaultSmartContractController } from "./smartContractController";
import { ContractController } from "../testutils/contractController";
import { SmartContract } from "./smartContract";
import { BigUIntValue, OptionalValue, OptionValue, TokenIdentifierValue, U32Value } from "./typesystem";
import {
Expand Down Expand Up @@ -112,7 +112,7 @@ describe("test smart contract interactor", function() {
let abiRegistry = await loadAbiRegistry(["src/testdata/answer.abi.json"]);
let abi = new SmartContractAbi(abiRegistry, ["answer"]);
let contract = new SmartContract({ address: dummyAddress, abi: abi });
let controller = new DefaultSmartContractController(abi, provider);
let controller = new ContractController(provider);

let interaction = <Interaction>contract.methods
.getUltimateAnswer()
Expand Down Expand Up @@ -174,9 +174,9 @@ describe("test smart contract interactor", function() {
let abiRegistry = await loadAbiRegistry(["src/testdata/counter.abi.json"]);
let abi = new SmartContractAbi(abiRegistry, ["counter"]);
let contract = new SmartContract({ address: dummyAddress, abi: abi });
let controller = new DefaultSmartContractController(abi, provider);
let controller = new ContractController(provider);

let getInteraction = <Interaction>contract.methodsExplicit.get();
let getInteraction = <Interaction>contract.methodsExplicit.get().check();
let incrementInteraction = (<Interaction>contract.methods.increment()).withGasLimit(new GasLimit(543210));
let decrementInteraction = (<Interaction>contract.methods.decrement()).withGasLimit(new GasLimit(987654));

Expand Down Expand Up @@ -221,7 +221,7 @@ describe("test smart contract interactor", function() {
let abiRegistry = await loadAbiRegistry(["src/testdata/lottery-esdt.abi.json"]);
let abi = new SmartContractAbi(abiRegistry, ["Lottery"]);
let contract = new SmartContract({ address: dummyAddress, abi: abi });
let controller = new DefaultSmartContractController(abi, provider);
let controller = new ContractController(provider);

let startInteraction = <Interaction>(
contract.methodsExplicit
Expand All @@ -237,6 +237,7 @@ describe("test smart contract interactor", function() {
OptionalValue.newMissing()
])
.withGasLimit(new GasLimit(5000000))
.check()
);

let statusInteraction = <Interaction>(
Expand Down
16 changes: 15 additions & 1 deletion src/smartcontracts/interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ import { Transaction } from "../transaction";
import { Query } from "./query";
import { ContractFunction } from "./function";
import { Address } from "../address";
import { AddressValue, BigUIntValue, BytesValue, TypedValue, U64Value, U8Value } from "./typesystem";
import { AddressValue, BigUIntValue, BytesValue, EndpointDefinition, TypedValue, U64Value, U8Value } from "./typesystem";
import { Nonce } from "../nonce";
import { ESDTNFT_TRANSFER_FUNCTION_NAME, ESDT_TRANSFER_FUNCTION_NAME, MULTI_ESDTNFT_TRANSFER_FUNCTION_NAME } from "../constants";
import { Account } from "../account";
import { CallArguments } from "./interface";
import { IChainID, IGasLimit, IGasPrice } from "../interface";
import { InteractionChecker } from "./interactionChecker";

/**
* Internal interface: the smart contract, as seen from the perspective of an {@link Interaction}.
*/
interface ISmartContractWithinInteraction {
call({ func, args, value, gasLimit, receiver }: CallArguments): Transaction;
getAddress(): Address;
getEndpoint(name: ContractFunction): EndpointDefinition;
}

/**
Expand Down Expand Up @@ -65,6 +67,10 @@ export class Interaction {
return this.function;
}

getEndpoint(): EndpointDefinition {
return this.contract.getEndpoint(this.function);
}

getArguments(): TypedValue[] {
return this.args;
}
Expand Down Expand Up @@ -198,6 +204,14 @@ export class Interaction {
this.querent = querent;
return this;
}

/**
* To perform custom checking, extend {@link Interaction} and override this method.
*/
check(): Interaction {
new InteractionChecker().checkInteraction(this, this.getEndpoint());
return this;
}
}

class TokenTransfersWithinInteraction {
Expand Down
7 changes: 1 addition & 6 deletions src/smartcontracts/interactionChecker.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as errors from "../errors";
import { EndpointDefinition } from "./typesystem";
import { Interaction } from "./interaction";
import { IInteractionChecker } from "./interface";

/**
* An interaction checker that aims to be as strict as possible.
Expand All @@ -10,7 +9,7 @@ import { IInteractionChecker } from "./interface";
* - errors related to calling "non-payable" functions with some value provided
* - gas estimation errors (not yet implemented)
*/
export class InteractionChecker implements IInteractionChecker {
export class InteractionChecker {
checkInteraction(interaction: Interaction, definition: EndpointDefinition): void {
this.checkPayable(interaction, definition);
this.checkArguments(interaction, definition);
Expand Down Expand Up @@ -50,7 +49,3 @@ export class InteractionChecker implements IInteractionChecker {
}
}
}

export class NullInteractionChecker implements IInteractionChecker {
checkInteraction(_interaction: Interaction, _definition: EndpointDefinition): void { }
}
12 changes: 1 addition & 11 deletions src/smartcontracts/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { Transaction } from "../transaction";
import { Code } from "./code";
import { CodeMetadata } from "./codeMetadata";
import { ContractFunction } from "./function";
import { Interaction } from "./interaction";
import { ReturnCode } from "./returnCode";
import { EndpointDefinition, TypedValue } from "./typesystem";

Expand Down Expand Up @@ -79,6 +78,7 @@ export interface TypedOutcomeBundle {
firstValue?: TypedValue;
secondValue?: TypedValue;
thirdValue?: TypedValue;
lastValue?: TypedValue;
}

export interface UntypedOutcomeBundle {
Expand All @@ -87,16 +87,6 @@ export interface UntypedOutcomeBundle {
values: Buffer[];
}

export interface ISmartContractController {
deploy(transaction: Transaction): Promise<{ transactionOnNetwork: ITransactionOnNetwork, bundle: UntypedOutcomeBundle }>;
execute(interaction: Interaction, transaction: Transaction): Promise<{ transactionOnNetwork: ITransactionOnNetwork, bundle: TypedOutcomeBundle }>;
query(interaction: Interaction): Promise<TypedOutcomeBundle>;
}

export interface IInteractionChecker {
checkInteraction(interaction: Interaction, definition: EndpointDefinition): void;
}

export interface IResultsParser {
parseQueryResponse(queryResponse: IContractQueryResponse, endpoint: EndpointDefinition): TypedOutcomeBundle;
parseUntypedQueryResponse(queryResponse: IContractQueryResponse): UntypedOutcomeBundle;
Expand Down
6 changes: 4 additions & 2 deletions src/smartcontracts/resultsParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export class ResultsParser implements IResultsParser {
values: values,
firstValue: values[0],
secondValue: values[1],
thirdValue: values[2]
thirdValue: values[2],
lastValue: values[values.length - 1]
};
}

Expand All @@ -60,7 +61,8 @@ export class ResultsParser implements IResultsParser {
values: values,
firstValue: values[0],
secondValue: values[1],
thirdValue: values[2]
thirdValue: values[2],
lastValue: values[values.length - 1]
};
}

Expand Down
6 changes: 5 additions & 1 deletion src/smartcontracts/smartContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ContractFunction } from "./function";
import { Query } from "./query";
import { SmartContractAbi } from "./abi";
import { guardValueIsSet } from "../utils";
import { TypedValue } from "./typesystem";
import { EndpointDefinition, TypedValue } from "./typesystem";
import { bigIntToBuffer } from "./codec/utils";
import BigNumber from "bignumber.js";
import { Interaction } from "./interaction";
Expand Down Expand Up @@ -129,6 +129,10 @@ export class SmartContract implements ISmartContract {
return this.abi!;
}

getEndpoint(name: string | ContractFunction): EndpointDefinition {
return this.getAbi().getEndpoint(name);
}

/**
* Creates a {@link Transaction} for deploying the Smart Contract to the Network.
*/
Expand Down
Loading

0 comments on commit b373c59

Please sign in to comment.