Skip to content

Commit

Permalink
Freeze / unfreeze etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
andreibancioiu committed Feb 13, 2023
1 parent 0f0ff4a commit 767e738
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 51 deletions.
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": "@multiversx/sdk-core",
"version": "11.3.0",
"version": "11.4.0-alpha.0",
"description": "MultiversX SDK for JavaScript and TypeScript",
"main": "out/index.js",
"types": "out/index.d.js",
Expand Down
24 changes: 13 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
require('./globals');

export * from "./interface";
export * from "./interfaceOfNetwork";
export * from "./errors";
export * from "./account";
export * from "./address";
export * from "./asyncTimer";
export * from "./errors";
export * from "./gasEstimator";
export * from "./transaction";
export * from "./transactionFactory";
export * from "./transactionPayload";
export * from "./transactionWatcher";
export * from "./interface";
export * from "./interfaceOfNetwork";
export * from "./logger";
export * from "./networkParams";
export * from "./relayedTransactionV1Builder";
export * from "./relayedTransactionV2Builder";
export * from "./signableMessage";
export * from "./utils";
export * from "./smartcontracts";
export * from "./tokenPayment";
export * from "./tokenTransferBuilders";
export * from "./smartcontracts";
export * from "./relayedTransactionV1Builder";
export * from "./relayedTransactionV2Builder";
export * from "./transaction";
export * from "./transactionFactory";
export * from "./transactionPayload";
export * from "./transactionWatcher";
export * from "./transfersFactory";
export * from "./utils";

83 changes: 79 additions & 4 deletions src/tokenOperations/tokenOperationsFactory.test.net.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { assert } from "chai";
import { AsyncTimer } from "../asyncTimer";
import { GasEstimator } from "../gasEstimator";
import { INetworkConfig, ITransactionOnNetwork } from "../interfaceOfNetwork";
import { loadTestWallets, TestWallet } from "../testutils";
import { createTestnetProvider, INetworkProvider } from "../testutils/networkProviders";
import { TokenPayment } from "../tokenPayment";
import { Transaction } from "../transaction";
import { TransactionWatcher } from "../transactionWatcher";
import { TransfersFactory } from "../transfersFactory";
import { TokenOperationsFactory } from "./tokenOperationsFactory";
import { TokenOperationsFactoryConfig } from "./tokenOperationsFactoryConfig";
import { TokenOperationsOutcomeParser } from "./tokenOperationsOutcomeParser";
Expand All @@ -16,6 +19,7 @@ describe("test factory on testnet", function () {
let network: INetworkConfig;
let factory: TokenOperationsFactory;
let parser: TokenOperationsOutcomeParser;
let transfersFactory: TransfersFactory;

before(async function () {
console.log(`> ${this.currentTest?.title} ...`);
Expand All @@ -27,6 +31,7 @@ describe("test factory on testnet", function () {
network = await provider.getNetworkConfig();
factory = new TokenOperationsFactory(new TokenOperationsFactoryConfig(network.ChainID));
parser = new TokenOperationsOutcomeParser();
transfersFactory = new TransfersFactory(new GasEstimator());
});

it("should issue fungible, mint, burn", async function () {
Expand Down Expand Up @@ -99,8 +104,8 @@ describe("test factory on testnet", function () {
assert.equal(tx4Outcome.burntSupply, "50");
});

it("should issue fungible, pause, unpause, freeze, unfreeze", async function () {
this.timeout(120000);
it("should issue fungible, pause, unpause", async function () {
this.timeout(240000);
await frank.sync(provider);

// Issue
Expand Down Expand Up @@ -148,17 +153,87 @@ describe("test factory on testnet", function () {
const tx3OnNetwork = await processTransaction(frank, tx3, "tx3");
const _tx3Outcome = parser.parseUnpause(tx3OnNetwork);

// Send some tokens to Grace
const tx4 = transfersFactory.createESDTTransfer({
payment: TokenPayment.fungibleFromBigInteger(tokenIdentifier, 10),
sender: frank.account.address,
receiver: grace.account.address,
chainID: network.ChainID,
nonce: frank.account.nonce
});

const _tx4OnNetwork = await processTransaction(frank, tx4, "tx4");
});

it("should issue fungible, freeze, unfreeze", async function () {
this.timeout(240000);
await frank.sync(provider);

// Issue
const tx1 = factory.issueFungible({
issuer: frank.address,
tokenName: "FRANK",
tokenTicker: "FRANK",
initialSupply: 100,
numDecimals: 0,
canFreeze: true,
canWipe: true,
canPause: true,
canMint: true,
canBurn: true,
canChangeOwner: true,
canUpgrade: true,
canAddSpecialRoles: true,
nonce: frank.account.nonce
});

const tx1OnNetwork = await processTransaction(frank, tx1, "tx1");
const tx1Outcome = parser.parseIssueFungible(tx1OnNetwork);
const tokenIdentifier = tx1Outcome.tokenIdentifier;
assert.isTrue(tokenIdentifier.includes("FRANK"));

// Send some tokens to Grace
const tx2 = transfersFactory.createESDTTransfer({
payment: TokenPayment.fungibleFromBigInteger(tokenIdentifier, 10),
sender: frank.account.address,
receiver: grace.account.address,
chainID: network.ChainID,
nonce: frank.account.nonce
});

const _tx2OnNetwork = await processTransaction(frank, tx2, "tx2");

// Freeze
const tx4 = factory.freeze({
const tx3 = factory.freeze({
freeze: true,
manager: frank.address,
user: grace.address,
tokenIdentifier: tokenIdentifier,
nonce: frank.account.nonce
});

const tx3OnNetwork = await processTransaction(frank, tx3, "tx3");
const tx3Outcome = parser.parseFreeze(tx3OnNetwork);
assert.equal(tx3Outcome.userAddress, grace.address.bech32());
assert.equal(tx3Outcome.tokenIdentifier, tokenIdentifier);
assert.equal(tx3Outcome.nonce, 0);
assert.equal(tx3Outcome.balance, "10");

// Unfreeze
const tx4 = factory.freeze({
unfreeze: true,
manager: frank.address,
user: grace.address,
tokenIdentifier: tokenIdentifier,
nonce: frank.account.nonce
});

const tx4OnNetwork = await processTransaction(frank, tx4, "tx4");
// const _tx4Outcome = parser.parseFreeze(tx4OnNetwork);
const tx4Outcome = parser.parseUnfreeze(tx4OnNetwork);
assert.equal(tx4Outcome.userAddress, grace.address.bech32());
assert.equal(tx4Outcome.tokenIdentifier, tokenIdentifier);
assert.equal(tx4Outcome.nonce, 0);
assert.equal(tx4Outcome.balance, "10");
});

it("should issue and create NFT", async function () {
Expand Down
56 changes: 26 additions & 30 deletions src/tokenOperations/tokenOperationsOutcomeParser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Address } from "../address";
import { ErrCannotParseTransactionOutcome } from "../errors";
import { IAddress } from "../interface";
import { bufferToBigInt } from "./codec";
Expand Down Expand Up @@ -65,15 +66,12 @@ export interface IBurnOutcome {
export interface IPausingOutcome {
}

// export interface IFreezingOutcome {
// userAddress: IAddress;
// tokenIdentifier: string;
// nonce: INonce;
// balance: string;
// }

// export interface IEmptyOutcome {
// }
export interface IFreezingOutcome {
userAddress: string;
tokenIdentifier: string;
nonce: number;
balance: string;
}

export class TokenOperationsOutcomeParser {
parseIssueFungible(transaction: ITransactionOnNetwork): IESDTIssueOutcome {
Expand Down Expand Up @@ -154,29 +152,27 @@ export class TokenOperationsOutcomeParser {
return {};
}

parseFreeze(transaction: ITransactionOnNetwork): IFreezingOutcome {
this.ensureNoError(transaction);

const event = this.findSingleEventByIdentifier(transaction, "ESDTFreeze");
const tokenIdentifier = event.topics[0]?.valueOf().toString();
const nonce = bufferToBigInt(event.topics[1]?.valueOf()).toNumber() || 0;
const balance = bufferToBigInt(event.topics[2]?.valueOf()).toString();
const userAddress = Address.fromBuffer(event.topics[3]?.valueOf()).toString();
return { userAddress, tokenIdentifier, nonce, balance };
}

// export class ESDTFreezingParser extends BaseParser<IFreezingOutcome> {
// protected parseSuccessfulOutcome(events: ITransactionEvent[]): IFreezingOutcome | null {
// for (const event of events) {
// if (event.identifier == "ESDTFreeze" || event.identifier == "ESDTUnFreeze") {
// let balance = bufferToBigInt(event.topics[2].valueOf());
// if (balance.isNaN()) {
// balance = new BigNumber(0);
// }

// return {
// userAddress: new Address(event.topics[3].valueOf()),
// tokenIdentifier: event.topics[0].valueOf().toString(),
// nonce: bufferToBigInt(event.topics[1].valueOf()).toNumber() || 0,
// balance: balance.toString()
// };
// }
// }

// return null;
// }
// }
parseUnfreeze(transaction: ITransactionOnNetwork): IFreezingOutcome {
this.ensureNoError(transaction);

const event = this.findSingleEventByIdentifier(transaction, "ESDTUnFreeze");
const tokenIdentifier = event.topics[0]?.valueOf().toString();
const nonce = bufferToBigInt(event.topics[1]?.valueOf()).toNumber() || 0;
const balance = bufferToBigInt(event.topics[2]?.valueOf()).toString();
const userAddress = Address.fromBuffer(event.topics[3]?.valueOf()).toString();
return { userAddress, tokenIdentifier, nonce, balance };
}

private ensureNoError(transaction: ITransactionOnNetwork) {
for (const event of transaction.logs.events) {
Expand Down
3 changes: 0 additions & 3 deletions src/transactionFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,3 @@ export class TransactionFactory {
});
}
}

export class TransfersFactory extends TransactionFactory {
}
4 changes: 4 additions & 0 deletions src/transfersFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { TransactionFactory } from "./transactionFactory";

export class TransfersFactory extends TransactionFactory {
}

0 comments on commit 767e738

Please sign in to comment.