Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
- [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: cleanup and minor improvements prior release (step 1)](https://github.com/ElrondNetwork/elrond-sdk-erdjs/pull/190)
- [Breaking changes: cleanup and minor improvements prior release (step 2)](https://github.com/ElrondNetwork/elrond-sdk-erdjs/pull/191)

**Breaking changes**
- Removed utility functions: `transaction.awaitExecuted()`, `transaction.awaitPending()`. `TransactionWatcher` should be used directly, instead.
Expand Down Expand Up @@ -49,6 +51,7 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
- Removed `transaction.awaitSigned()` (it isn't useful in relation with any of our signing providers).
- Removed not used interfaces (e.g. `ISignable` is not needed in erdjs, but in walletcore).
- Removed `interaction.getContract()`. Add `interaction.getContractAddress()`.
- Remove `interaction.withGasLimitComponents()`. Not so helpful anymore (since `NetworkConfig` isn't a singleton anymore).

## [10.0.0-beta.3]
- [Extract dapp / signing providers to separate repositories](https://github.com/ElrondNetwork/elrond-sdk-erdjs/pull/170)
Expand Down
14 changes: 7 additions & 7 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
Expand Up @@ -36,7 +36,7 @@
"protobufjs": "6.10.2"
},
"devDependencies": {
"@elrondnetwork/erdjs-network-providers": "0.1.2",
"@elrondnetwork/erdjs-network-providers": "0.1.3",
"@elrondnetwork/erdjs-walletcore": "1.0.0",
"@types/assert": "1.4.6",
"@types/chai": "4.2.11",
Expand Down
6 changes: 3 additions & 3 deletions src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Address } from "./address";
import { Nonce } from "./nonce";
import { Balance } from "./balance";
import { Egld } from "./balanceBuilder";
import { IAccountBalance, INonce } from "./interface";
import { IAccountBalance, IAddress, INonce } from "./interface";

/**
* An abstraction representing an account (user or Smart Contract) on the Network.
Expand All @@ -11,7 +11,7 @@ export class Account {
/**
* The address of the account.
*/
readonly address: Address = new Address();
readonly address: IAddress = new Address();

/**
* The nonce of the account (the account sequence number).
Expand All @@ -26,7 +26,7 @@ export class Account {
/**
* Creates an account object from an address
*/
constructor(address: Address) {
constructor(address: IAddress) {
this.address = address;
}

Expand Down
4 changes: 1 addition & 3 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ export interface ITransactionFetcher {
/**
* Fetches the state of a {@link Transaction}.
*/
getTransaction(txHash: IHash, hintSender?: IAddress, withResults?: boolean): Promise<ITransactionOnNetwork>;
getTransaction(txHash: string): Promise<ITransactionOnNetwork>;
}

export interface ISignature { hex(): string; }
export interface IHash { hex(): string; }
export interface IAddress { bech32(): string; }
export interface ITransactionValue { toString(): string; }
export interface IAccountBalance { toString(): string; }
export interface ITransactionPayload { encoded(): string; }
export interface INonce { valueOf(): number; }
export interface IChainID { valueOf(): string; }
export interface IGasLimit { valueOf(): number; }
Expand Down
18 changes: 9 additions & 9 deletions src/interfaceOfNetwork.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import { IAccountBalance, IAddress, IChainID, IGasLimit, IHash, INonce, ITransactionPayload, ITransactionValue } from "./interface";
import { IAccountBalance, IAddress } from "./interface";

export interface IAccountOnNetwork {
nonce: INonce;
nonce: number;
balance: IAccountBalance;
}

export interface INetworkConfig {
MinGasLimit: IGasLimit;
MinGasLimit: number;
GasPerDataByte: number;
GasPriceModifier: number;
ChainID: IChainID;
ChainID: string;
}

export interface ITransactionOnNetwork {
isCompleted: boolean;

hash: IHash;
hash: string;
type: string;
value: ITransactionValue;
value: string;
receiver: IAddress;
sender: IAddress;
data: ITransactionPayload;
data: Buffer;
status: ITransactionStatus;
receipt: ITransactionReceipt;
contractResults: IContractResults;
Expand All @@ -43,8 +43,8 @@ export interface IContractResults {
}

export interface IContractResultItem {
hash: IHash;
nonce: INonce;
hash: string;
nonce: number;
receiver: IAddress;
sender: IAddress;
data: string;
Expand Down
12 changes: 4 additions & 8 deletions src/smartcontracts/interaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,13 @@ describe("test smart contract interactor", function() {
let transaction = interaction
.withNonce(new Nonce(7))
.withValue(Balance.egld(1))
.withGasLimitComponents({ minGasLimit: 50000, gasPerDataByte: 1500, estimatedExecutionComponent: 20000000 })
.withGasLimit(new GasLimit(20000000))
.buildTransaction();

let expectedGasLimit = new GasLimit(50000)
.add(new GasLimit("dummy".length * 1500))
.add(new GasLimit(20000000));

assert.deepEqual(transaction.getReceiver(), dummyAddress);
assert.deepEqual(transaction.getValue(), Balance.egld(1));
assert.deepEqual(transaction.getNonce(), new Nonce(7));
assert.deepEqual(transaction.getGasLimit(), expectedGasLimit);
assert.equal(transaction.getGasLimit().valueOf(), 20000000);
});

it("should set transfers (payments) on contract calls (transfer and execute)", async function () {
Expand Down Expand Up @@ -126,7 +122,7 @@ describe("test smart contract interactor", function() {

provider.mockQueryContractOnFunction(
"getUltimateAnswer",
new ContractQueryResponse({ returnData: [Buffer.from([42]).toString("base64")], returnCode: ReturnCode.Ok })
new ContractQueryResponse({ returnData: [Buffer.from([42]).toString("base64")], returnCode: "ok" })
);

// Query
Expand Down Expand Up @@ -183,7 +179,7 @@ describe("test smart contract interactor", function() {
// For "get()", return fake 7
provider.mockQueryContractOnFunction(
"get",
new ContractQueryResponse({ returnData: [Buffer.from([7]).toString("base64")], returnCode: ReturnCode.Ok })
new ContractQueryResponse({ returnData: [Buffer.from([7]).toString("base64")], returnCode: "ok" })
);

// Query "get()"
Expand Down
31 changes: 9 additions & 22 deletions src/smartcontracts/interaction.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Balance } from "../balance";
import { ChainID, GasLimit, GasPrice } from "../networkParams";
import { ChainID, GasLimit } from "../networkParams";
import { Transaction } from "../transaction";
import { Query } from "./query";
import { ContractFunction } from "./function";
Expand All @@ -9,7 +9,7 @@ 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 { IAddress, IChainID, IGasLimit, IGasPrice } from "../interface";
import { IAddress, IChainID, IGasLimit, IGasPrice, INonce } from "../interface";
import { InteractionChecker } from "./interactionChecker";

/**
Expand All @@ -32,7 +32,7 @@ export class Interaction {
private readonly function: ContractFunction;
private readonly args: TypedValue[];

private nonce: Nonce = new Nonce(0);
private nonce: INonce = new Nonce(0);
private value: Balance = Balance.Zero();
private gasLimit: IGasLimit = new GasLimit(0);
private gasPrice: IGasPrice | undefined = undefined;
Expand Down Expand Up @@ -142,44 +142,31 @@ export class Interaction {
return this;
}

withSingleESDTNFTTransfer(transfer: Balance, sender: Address) {
withSingleESDTNFTTransfer(transfer: Balance, sender: IAddress) {
this.isWithSingleESDTNFTTransfer = true;
this.tokenTransfers = new TokenTransfersWithinInteraction([transfer], this);
this.tokenTransfersSender = sender;
return this;
}

withMultiESDTNFTTransfer(transfers: Balance[], sender: Address) {
withMultiESDTNFTTransfer(transfers: Balance[], sender: IAddress) {
this.isWithMultiESDTNFTTransfer = true;
this.tokenTransfers = new TokenTransfersWithinInteraction(transfers, this);
this.tokenTransfersSender = sender;
return this;
}

withGasLimit(gasLimit: GasLimit): Interaction {
withGasLimit(gasLimit: IGasLimit): Interaction {
this.gasLimit = gasLimit;
return this;
}

withGasLimitComponents(args: { minGasLimit: number, gasPerDataByte: number, estimatedExecutionComponent: number }): Interaction {
let minGasLimit = args.minGasLimit;
let gasPerDataByte = args.gasPerDataByte;

let transaction = this.buildTransaction();
let dataLength = transaction.getData().length();
let movementComponent = new GasLimit(minGasLimit + gasPerDataByte * dataLength);
let executionComponent = new GasLimit(args.estimatedExecutionComponent);
let gasLimit = movementComponent.add(executionComponent);

return this.withGasLimit(gasLimit);
}

withGasPrice(gasPrice: GasPrice): Interaction {
withGasPrice(gasPrice: IGasPrice): Interaction {
this.gasPrice = gasPrice;
return this;
}

withNonce(nonce: Nonce): Interaction {
withNonce(nonce: INonce): Interaction {
this.nonce = nonce;
return this;
}
Expand All @@ -196,7 +183,7 @@ export class Interaction {
/**
* Sets the "caller" field on contract queries.
*/
withQuerent(querent: Address): Interaction {
withQuerent(querent: IAddress): Interaction {
this.querent = querent;
return this;
}
Expand Down
3 changes: 1 addition & 2 deletions src/smartcontracts/interface.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Balance } from "../balance";
import { IAddress, IChainID, IGasLimit, IGasPrice } from "../interface";
import { IContractQueryResponse, ITransactionOnNetwork } from "../interfaceOfNetwork";
import { Transaction } from "../transaction";
import { Code } from "./code";
import { CodeMetadata } from "./codeMetadata";
import { ContractFunction } from "./function";
import { ReturnCode } from "./returnCode";
import { EndpointDefinition, TypedValue } from "./typesystem";
import { TypedValue } from "./typesystem";

/**
* ISmartContract defines a general interface for operating with {@link SmartContract} objects.
Expand Down
11 changes: 7 additions & 4 deletions src/smartcontracts/nativeSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import { BalanceBuilder } from "../balanceBuilder";
import { Address } from "../address";
import { Code } from "./code";
import { ErrInvalidArgument, ErrTypeInferenceSystemRequiresRegularJavascriptObjects, ErrTypingSystem } from "../errors";
import { IAddress } from "../interface";

export namespace NativeTypes {
export type NativeBuffer = Buffer | string | BalanceBuilder;
export type NativeBytes = Code | Buffer | string | BalanceBuilder;
export type NativeAddress = Address | string | Buffer | { getAddress(): Address };
export type NativeAddress = string | Buffer | IAddress | { getAddress(): IAddress };
}

export namespace NativeSerializer {
Expand Down Expand Up @@ -241,16 +242,18 @@ export namespace NativeSerializer {
errorContext.convertError(native, "Buffer");
}

export function convertNativeToAddress(native: NativeTypes.NativeAddress, errorContext: ArgumentErrorContext): Address {
export function convertNativeToAddress(native: NativeTypes.NativeAddress, errorContext: ArgumentErrorContext): IAddress {
if ((<any>native).bech32) {
return <IAddress>native;
}
if ((<any>native).getAddress) {
return (<any>native).getAddress();
}

switch (native.constructor) {
case Address:
case Buffer:
case String:
return new Address(<Address | Buffer | string>native);
return new Address(<Buffer | string>native);
default:
errorContext.convertError(native, "Address");
}
Expand Down
30 changes: 7 additions & 23 deletions src/smartcontracts/query.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,12 @@ import { assert } from "chai";
import { Address } from "../address";
import { ContractFunction } from "./function";
import { Query } from "./query";
import { Balance } from "../balance";
import * as errors from "../errors";
import { BigUIntValue, U32Value } from "./typesystem";
import BigNumber from "bignumber.js";
import { BytesValue } from "./typesystem/bytes";

describe("test smart contract queries", () => {
it("should prepare query", async () => {
let query = new Query({
func: new ContractFunction("foo"),
address: new Address("erd1qqqqqqqqqqqqqpgq3ytm9m8dpeud35v3us20vsafp77smqghd8ss4jtm0q"),
value: Balance.egld(42)
});

let request = query.toHttpRequest();
assert.equal(request["scAddress"], "erd1qqqqqqqqqqqqqpgq3ytm9m8dpeud35v3us20vsafp77smqghd8ss4jtm0q");
assert.equal(request["funcName"], "foo");
assert.equal(request["value"], "42000000000000000000");
assert.lengthOf(request["args"], 0);
});

it("should prepare query with arguments", async () => {
it("should getEncodedArguments()", async () => {
let query = new Query({
func: new ContractFunction("foo"),
address: new Address("erd1qqqqqqqqqqqqqpgq3ytm9m8dpeud35v3us20vsafp77smqghd8ss4jtm0q"),
Expand All @@ -35,11 +19,11 @@ describe("test smart contract queries", () => {
]
});

let request = query.toHttpRequest();
assert.lengthOf(request["args"], 4);
assert.equal(request["args"][0], "64");
assert.equal(request["args"][1], "21");
assert.equal(request["args"][2], "abba");
assert.equal(request["args"][3], "314dc6448d9338c15b0a00000000");
let args = query.getEncodedArguments();
assert.lengthOf(args, 4);
assert.equal(args[0], "64");
assert.equal(args[1], "21");
assert.equal(args[2], "abba");
assert.equal(args[3], "314dc6448d9338c15b0a00000000");
});
});
Loading