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: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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
- [Breaking changes: cleanup and minor improvements prior release (step 1)](https://github.com/ElrondNetwork/elrond-sdk-erdjs/pull/190)
- [TokenPayment instead of Balance, where applicable](https://github.com/ElrondNetwork/elrond-sdk-erdjs/pull/193)

## 10.0.0-alpha.5
- [Breaking change: adjustements to transaction awaitening and completion, transaction watcher](https://github.com/ElrondNetwork/elrond-sdk-erdjs/pull/173)
Expand Down Expand Up @@ -52,6 +52,7 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
- 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).
- `TokenPayment` should be used instead of `Balance`, where applicable.

## [10.0.0-beta.3]
- [Extract dapp / signing providers to separate repositories](https://github.com/ElrondNetwork/elrond-sdk-erdjs/pull/170)
Expand Down
52 changes: 26 additions & 26 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 src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class Account {
/**
* The balance of the account.
*/
balance: Balance = Egld("0");
balance: IAccountBalance = Egld("0");

/**
* Creates an account object from an address
Expand Down
7 changes: 7 additions & 0 deletions src/balanceBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Token, TokenType } from "./token";

/**
* Creates balances for ESDTs (Fungible, Semi-Fungible (SFT) or Non-Fungible Tokens).
* @deprecated: when preparing token transfers, one should use the class "TokenPayment" instead.
*/
export interface BalanceBuilder {

Expand Down Expand Up @@ -60,6 +61,9 @@ export interface BalanceBuilder {
one(): Balance;
}

/**
* @deprecated: when preparing token transfers, one should use the class "TokenPayment" instead.
*/
class BalanceBuilderImpl {
readonly token: Token;
nonce_: BigNumber | null;
Expand Down Expand Up @@ -114,6 +118,9 @@ class BalanceBuilderImpl {
}
}

/**
* @deprecated: when preparing token transfers, one should use the class "TokenPayment" instead.
*/
export function createBalanceBuilder(token: Token): BalanceBuilder {
let impl = new BalanceBuilderImpl(token);
let denominated = <BalanceBuilder>impl.value.bind(impl);
Expand Down
17 changes: 2 additions & 15 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,8 @@ export class Err extends Error {
* Signals invalid arguments for a function, for an operation.
*/
export class ErrInvalidArgument extends Err {
public constructor(
name: string,
value?: any,
reason: string = "not specified",
inner?: Error
) {
super(ErrInvalidArgument.getMessage(name, value, reason), inner);
}

static getMessage(name: string, value?: any, reason?: string): string {
if (value) {
return `Invalid argument "${name}": ${value}. Reason: ${reason}`;
}

return `Invalid argument "${name}"`;
public constructor(message: string, inner?: Error) {
super(`Invalid argument: ${message}`, inner);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ export * from "./utils";
export * from "./scArgumentsParser";
export * from "./esdtHelpers";
export * from "./token";
export * from "./tokenPayment";
export * from "./smartcontracts";
7 changes: 7 additions & 0 deletions src/interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import BigNumber from "bignumber.js";
import { ITransactionOnNetwork } from "./interfaceOfNetwork";

export interface ITransactionFetcher {
Expand All @@ -15,3 +16,9 @@ export interface INonce { valueOf(): number; }
export interface IChainID { valueOf(): string; }
export interface IGasLimit { valueOf(): number; }
export interface IGasPrice { valueOf(): number; }

export interface ITokenPayment {
readonly tokenIdentifier: string;
readonly nonce: number;
readonly amountAsBigInteger: BigNumber;
}
9 changes: 5 additions & 4 deletions src/proto/serializer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as errors from "../errors";
import { Balance } from "../balance";
import { bigIntToBuffer } from "../smartcontracts/codec/utils";
import { Transaction } from "../transaction";
import { proto } from "./compiled";
import {TRANSACTION_OPTIONS_DEFAULT} from "../constants";
import { Address } from "../address";
import { ITransactionValue } from "../interface";
import BigNumber from "bignumber.js";

/**
* Hides away the serialization complexity, for each type of object (e.g. transactions).
Expand All @@ -22,7 +23,7 @@ export class ProtoSerializer {
let protoTransaction = new proto.Transaction({
// elrond-go's serializer handles nonce == 0 differently, thus we treat 0 as "undefined".
Nonce: transaction.getNonce().valueOf() ? transaction.getNonce().valueOf() : undefined,
Value: this.serializeBalance(transaction.getValue()),
Value: this.serializeTransactionValue(transaction.getValue()),
RcvAddr: receiverPubkey,
RcvUserName: null,
SndAddr: senderPubkey,
Expand All @@ -47,8 +48,8 @@ export class ProtoSerializer {
/**
* Custom serialization, compatible with elrond-go.
*/
private serializeBalance(balance: Balance): Buffer {
let value = balance.valueOf();
private serializeTransactionValue(transactionValue: ITransactionValue): Buffer {
let value = new BigNumber(transactionValue.toString());
if (value.isZero()) {
return Buffer.from([0, 0]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/smartcontracts/codec/boolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class BooleanBinaryCodec {

decodeTopLevel(buffer: Buffer): BooleanValue {
if (buffer.length > 1) {
throw new errors.ErrInvalidArgument("buffer", buffer, "should be a buffer of size <= 1");
throw new errors.ErrInvalidArgument("buffer should be of size <= 1");
}

let firstByte = buffer[0];
Expand Down
25 changes: 12 additions & 13 deletions src/smartcontracts/interaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ import { ChainID, GasLimit } from "../networkParams";
import { ContractFunction } from "./function";
import { Nonce } from "../nonce";
import { ReturnCode } from "./returnCode";
import { Balance } from "../balance";
import BigNumber from "bignumber.js";
import { BytesValue } from "./typesystem/bytes";
import { Token, TokenType } from "../token";
import { createBalanceBuilder } from "../balanceBuilder";
import { TokenPayment } from "../tokenPayment";
import { ContractQueryResponse } from "@elrondnetwork/erdjs-network-providers";

describe("test smart contract interactor", function() {
Expand All @@ -39,13 +38,13 @@ describe("test smart contract interactor", function() {

let transaction = interaction
.withNonce(new Nonce(7))
.withValue(Balance.egld(1))
.withValue(TokenPayment.egldFromAmount(1))
.withGasLimit(new GasLimit(20000000))
.buildTransaction();

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

Expand All @@ -54,10 +53,10 @@ describe("test smart contract interactor", function() {
let dummyFunction = new ContractFunction("dummy");
let alice = new Address("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th");

const TokenFoo = createBalanceBuilder(new Token({ identifier: "FOO-6ce17b", decimals: 0, type: TokenType.Fungible }));
const TokenBar = createBalanceBuilder(new Token({ identifier: "BAR-5bc08f", decimals: 3, type: TokenType.Fungible }));
const LKMEX = createBalanceBuilder(new Token({ identifier: "LKMEX-aab910", decimals: 18, type: TokenType.Semifungible }));
const Strămoși = createBalanceBuilder(new Token({ identifier: "MOS-b9b4b2", decimals: 0, type: TokenType.Nonfungible }));
const TokenFoo = (amount: BigNumber.Value) => TokenPayment.fungibleFromAmount("FOO-6ce17b", amount, 0);
const TokenBar = (amount: BigNumber.Value) => TokenPayment.fungibleFromAmount("BAR-5bc08f", amount, 3);
const LKMEX = (nonce: number, amount: BigNumber.Value) => TokenPayment.metaEsdtFromAmount("LKMEX-aab910", nonce, amount, 18);
const Strămoși = (nonce: number) => TokenPayment.nonFungible("MOS-b9b4b2", nonce);

const hexFoo = "464f4f2d366365313762";
const hexBar = "4241522d356263303866";
Expand All @@ -68,21 +67,21 @@ describe("test smart contract interactor", function() {

// ESDT, single
let transaction = new Interaction(contract, dummyFunction, [])
.withSingleESDTTransfer(TokenFoo("10"))
.withSingleESDTTransfer(TokenFoo(10))
.buildTransaction();

assert.equal(transaction.getData().toString(), `ESDTTransfer@${hexFoo}@0a@${hexDummyFunction}`);

// Meta ESDT (special SFT), single
transaction = new Interaction(contract, dummyFunction, [])
.withSingleESDTNFTTransfer(LKMEX.nonce(123456).value(123.456), alice)
.withSingleESDTNFTTransfer(LKMEX(123456, 123.456), alice)
.buildTransaction();

assert.equal(transaction.getData().toString(), `ESDTNFTTransfer@${hexLKMEX}@01e240@06b14bd1e6eea00000@${hexContractAddress}@${hexDummyFunction}`);

// NFT, single
transaction = new Interaction(contract, dummyFunction, [])
.withSingleESDTNFTTransfer(Strămoși.nonce(1).one(), alice)
.withSingleESDTNFTTransfer(Strămoși(1), alice)
.buildTransaction();

assert.equal(transaction.getData().toString(), `ESDTNFTTransfer@${hexStrămoși}@01@01@${hexContractAddress}@${hexDummyFunction}`);
Expand All @@ -96,7 +95,7 @@ describe("test smart contract interactor", function() {

// NFT, multiple
transaction = new Interaction(contract, dummyFunction, [])
.withMultiESDTNFTTransfer([Strămoși.nonce(1).one(), Strămoși.nonce(42).one()], alice)
.withMultiESDTNFTTransfer([Strămoși(1), Strămoși(42)], alice)
.buildTransaction();

assert.equal(transaction.getData().toString(), `MultiESDTNFTTransfer@${hexContractAddress}@02@${hexStrămoși}@01@01@${hexStrămoși}@2a@01@${hexDummyFunction}`);
Expand Down
Loading