Skip to content

Commit

Permalink
Pause / unpause, work in progress.
Browse files Browse the repository at this point in the history
  • Loading branch information
andreibancioiu committed Feb 12, 2023
1 parent 9db861b commit 5e894f9
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 99 deletions.
145 changes: 84 additions & 61 deletions src/tokenOperations/tokenOperationsFactory.test.net.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { assert } from "chai";
import { AsyncTimer } from "../asyncTimer";
import { INetworkConfig } from "../interfaceOfNetwork";
import { INetworkConfig, ITransactionOnNetwork } from "../interfaceOfNetwork";
import { loadTestWallets, TestWallet } from "../testutils";
import { createTestnetProvider, INetworkProvider } from "../testutils/networkProviders";
import { Transaction } from "../transaction";
import { TransactionWatcher } from "../transactionWatcher";
import { TokenOperationsFactory } from "./tokenOperationsFactory";
import { TokenOperationsFactoryConfig } from "./tokenOperationsFactoryConfig";
Expand Down Expand Up @@ -51,12 +52,7 @@ describe("test factory on testnet", function () {
nonce: frank.account.nonce
});

frank.account.incrementNonce();
await frank.signer.sign(tx1);
await provider.sendTransaction(tx1);
console.log("tx1", tx1.getHash().hex());

const tx1OnNetwork = await watcher.awaitCompleted(tx1);
const tx1OnNetwork = await processTransaction(frank, tx1, "tx1");
const tx1Outcome = parser.parseIssueFungible(tx1OnNetwork);
const tokenIdentifier = tx1Outcome.tokenIdentifier;
assert.isTrue(tokenIdentifier.includes("FRANK"));
Expand All @@ -71,12 +67,7 @@ describe("test factory on testnet", function () {
nonce: frank.account.nonce
});

frank.account.incrementNonce();
await frank.signer.sign(tx2);
await provider.sendTransaction(tx2);
console.log("tx2", tx2.getHash().hex());

const tx2OnNetwork = await watcher.awaitCompleted(tx2);
const tx2OnNetwork = await processTransaction(frank, tx2, "tx2");
const tx2Outcome = parser.parseSetSpecialRole(tx2OnNetwork);
assert.include(tx2Outcome.roles, "ESDTRoleLocalMint");
assert.include(tx2Outcome.roles, "ESDTRoleLocalBurn");
Expand All @@ -90,12 +81,7 @@ describe("test factory on testnet", function () {
nonce: grace.account.nonce
});

grace.account.incrementNonce();
await grace.signer.sign(tx3);
await provider.sendTransaction(tx3);
console.log("tx3", tx3.getHash().hex());

const tx3OnNetwork = await watcher.awaitCompleted(tx3);
const tx3OnNetwork = await processTransaction(grace, tx3, "tx3");
const tx3Outcome = parser.parseLocalMint(tx3OnNetwork);
assert.equal(tx3Outcome.mintedSupply, "200");

Expand All @@ -108,16 +94,73 @@ describe("test factory on testnet", function () {
nonce: grace.account.nonce
});

grace.account.incrementNonce();
await grace.signer.sign(tx4);
await provider.sendTransaction(tx4);
console.log("tx4", tx4.getHash().hex());

const tx4OnNetwork = await watcher.awaitCompleted(tx4);
const tx4OnNetwork = await processTransaction(grace, tx4, "tx4");
const tx4Outcome = parser.parseLocalBurn(tx4OnNetwork);
assert.equal(tx4Outcome.burntSupply, "50");
});

it("should issue fungible, pause, unpause, freeze, unfreeze", async function () {
this.timeout(120000);
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"));

// Pause
const tx2 = factory.pause({
pause: true,
manager: frank.address,
tokenIdentifier: tokenIdentifier,
nonce: frank.account.nonce
});

const tx2OnNetwork = await processTransaction(frank, tx2, "tx2");
const _tx2Outcome = parser.parsePause(tx2OnNetwork);

// Unpause
const tx3 = factory.pause({
unpause: true,
manager: frank.address,
tokenIdentifier: tokenIdentifier,
nonce: frank.account.nonce
});

const tx3OnNetwork = await processTransaction(frank, tx3, "tx3");
const _tx3Outcome = parser.parseUnpause(tx3OnNetwork);

// Freeze
const tx4 = factory.freeze({
freeze: 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);
});

it("should issue and create NFT", async function () {
this.timeout(180000);
await frank.sync(provider);
Expand All @@ -138,12 +181,7 @@ describe("test factory on testnet", function () {
nonce: frank.account.nonce
});

frank.account.incrementNonce();
await frank.signer.sign(tx1);
await provider.sendTransaction(tx1);
console.log("tx1", tx1.getHash().hex());

const tx1OnNetwork = await watcher.awaitCompleted(tx1);
const tx1OnNetwork = await processTransaction(frank, tx1, "tx1");
const tx1Outcome = parser.parseIssueNonFungible(tx1OnNetwork);
const tokenIdentifier = tx1Outcome.tokenIdentifier;
assert.isTrue(tokenIdentifier.includes("FRANK"));
Expand All @@ -161,12 +199,7 @@ describe("test factory on testnet", function () {
nonce: frank.account.nonce
});

frank.account.incrementNonce();
await frank.signer.sign(tx2);
await provider.sendTransaction(tx2);
console.log("tx2", tx2.getHash().hex());

let tx2OnNetwork = await watcher.awaitCompleted(tx2);
let tx2OnNetwork = await processTransaction(frank, tx2, "tx2");

// For such transactions, the "isCompleted" field is somehow incorrect (false positive).
// Let's wait a bit more to have the outcome.
Expand All @@ -191,12 +224,7 @@ describe("test factory on testnet", function () {
nonce: grace.account.nonce
});

grace.account.incrementNonce();
await grace.signer.sign(tx);
await provider.sendTransaction(tx);
console.log("tx", tx.getHash().hex());

const txOnNetwork = await watcher.awaitCompleted(tx);
const txOnNetwork = await processTransaction(grace, tx, "tx");
const txOutcome = parser.parseNFTCreate(txOnNetwork);

assert.equal(txOutcome.tokenIdentifier, tokenIdentifier);
Expand Down Expand Up @@ -225,12 +253,7 @@ describe("test factory on testnet", function () {
nonce: frank.account.nonce
});

frank.account.incrementNonce();
await frank.signer.sign(tx1);
await provider.sendTransaction(tx1);
console.log("tx1", tx1.getHash().hex());

const tx1OnNetwork = await watcher.awaitCompleted(tx1);
const tx1OnNetwork = await processTransaction(frank, tx1, "tx1");
const tx1Outcome = parser.parseIssueSemiFungible(tx1OnNetwork);
const tokenIdentifier = tx1Outcome.tokenIdentifier;
assert.isTrue(tokenIdentifier.includes("FRANK"));
Expand All @@ -247,12 +270,7 @@ describe("test factory on testnet", function () {
nonce: frank.account.nonce
});

frank.account.incrementNonce();
await frank.signer.sign(tx2);
await provider.sendTransaction(tx2);
console.log("tx2", tx2.getHash().hex());

let tx2OnNetwork = await watcher.awaitCompleted(tx2);
let tx2OnNetwork = await processTransaction(frank, tx2, "tx2");

// For such transactions, the "isCompleted" field is somehow incorrect (false positive).
// Let's wait a bit more to have the outcome.
Expand All @@ -277,17 +295,22 @@ describe("test factory on testnet", function () {
nonce: grace.account.nonce
});

grace.account.incrementNonce();
await grace.signer.sign(tx);
await provider.sendTransaction(tx);
console.log("tx", tx.getHash().hex());

const txOnNetwork = await watcher.awaitCompleted(tx);
const txOnNetwork = await processTransaction(grace, tx, "tx");
const txOutcome = parser.parseNFTCreate(txOnNetwork);

assert.equal(txOutcome.tokenIdentifier, tokenIdentifier);
assert.equal(txOutcome.nonce, i);
assert.equal(txOutcome.initialQuantity, i * 10);
}
});

async function processTransaction(wallet: TestWallet, transaction: Transaction, tag: string): Promise<ITransactionOnNetwork> {
wallet.account.incrementNonce();
await wallet.signer.sign(transaction);
await provider.sendTransaction(transaction);
console.log(`Sent transaction [${tag}]: ${transaction.getHash().hex()}`);

const transactionOnNetwork = await watcher.awaitCompleted(transaction);
return transactionOnNetwork;
}
});
8 changes: 4 additions & 4 deletions src/tokenOperations/tokenOperationsFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,16 @@ interface IESDTNFTCreateArgs extends IBaseArgs {
interface IESDTPausingArgs extends IBaseArgs {
manager: IAddress;
tokenIdentifier: string;
pause: boolean;
unpause: boolean;
pause?: boolean;
unpause?: boolean;
}

interface IESDTFreezingArgs extends IBaseArgs {
manager: IAddress;
user: IAddress;
tokenIdentifier: string;
freeze: boolean;
unfreeze: boolean;
freeze?: boolean;
unfreeze?: boolean;
}

interface IESDTLocalMintArgs extends IBaseArgs {
Expand Down
72 changes: 38 additions & 34 deletions src/tokenOperations/tokenOperationsOutcomeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,8 @@ export interface IBurnOutcome {
burntSupply: string;
}

// export interface IPausingOutcome {
// tokenIdentifier: string;
// paused: boolean;
// }
export interface IPausingOutcome {
}

// export interface IFreezingOutcome {
// userAddress: IAddress;
Expand Down Expand Up @@ -144,6 +142,42 @@ export class TokenOperationsOutcomeParser {
return { userAddress, tokenIdentifier, nonce, burntSupply };
}

parsePause(transaction: ITransactionOnNetwork): IPausingOutcome {
this.ensureNoError(transaction);
const _ = this.findSingleEventByIdentifier(transaction, "ESDTPause");
return {};
}

parseUnpause(transaction: ITransactionOnNetwork): IPausingOutcome {
this.ensureNoError(transaction);
const _ = this.findSingleEventByIdentifier(transaction, "ESDTUnPause");
return {};
}


// 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;
// }
// }


private ensureNoError(transaction: ITransactionOnNetwork) {
for (const event of transaction.logs.events) {
if (event.identifier == "signalError") {
Expand Down Expand Up @@ -181,33 +215,3 @@ export class TokenOperationsOutcomeParser {
}
}


// export class ESDTPausingParser extends BaseParser<IEmptyOutcome> {
// protected parseSuccessfulOutcome(_events: ITransactionEvent[]): IEmptyOutcome | null {
// return {};
// }
// }



// 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;
// }
// }

0 comments on commit 5e894f9

Please sign in to comment.