Skip to content

Commit

Permalink
Fix after review.
Browse files Browse the repository at this point in the history
  • Loading branch information
andreibancioiu committed Feb 21, 2023
1 parent a080240 commit afa902a
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 22 deletions.
77 changes: 71 additions & 6 deletions src/tokenOperations/tokenOperationsFactory.test.net.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ describe("test factory on testnet", function () {

// Pause
const tx2 = factory.pause({
pause: true,
manager: frank.address,
tokenIdentifier: tokenIdentifier,
transactionNonce: frank.account.nonce
Expand All @@ -143,8 +142,7 @@ describe("test factory on testnet", function () {
const _tx2Outcome = parser.parsePause(tx2OnNetwork);

// Unpause
const tx3 = factory.pause({
unpause: true,
const tx3 = factory.unpause({
manager: frank.address,
tokenIdentifier: tokenIdentifier,
transactionNonce: frank.account.nonce
Expand Down Expand Up @@ -205,7 +203,6 @@ describe("test factory on testnet", function () {

// Freeze
const tx3 = factory.freeze({
freeze: true,
manager: frank.address,
user: grace.address,
tokenIdentifier: tokenIdentifier,
Expand All @@ -220,8 +217,7 @@ describe("test factory on testnet", function () {
assert.equal(tx3Outcome.balance, "10");

// Unfreeze
const tx4 = factory.freeze({
unfreeze: true,
const tx4 = factory.unfreeze({
manager: frank.address,
user: grace.address,
tokenIdentifier: tokenIdentifier,
Expand All @@ -236,6 +232,75 @@ describe("test factory on testnet", function () {
assert.equal(tx4Outcome.balance, "10");
});

it("should issue fungible, freeze, wipe", 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,
transactionNonce: 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 tx3 = factory.freeze({
manager: frank.address,
user: grace.address,
tokenIdentifier: tokenIdentifier,
transactionNonce: 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");

// Wipe
const tx4 = factory.wipe({
manager: frank.address,
user: grace.address,
tokenIdentifier: tokenIdentifier,
transactionNonce: frank.account.nonce
});

const tx4OnNetwork = await processTransaction(frank, tx4, "tx4");
const tx4Outcome = parser.parseWipe(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, then update attributes", async function () {
this.timeout(180000);
await frank.sync(provider);
Expand Down
73 changes: 60 additions & 13 deletions src/tokenOperations/tokenOperationsFactory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import BigNumber from "bignumber.js";
import { ARGUMENTS_SEPARATOR, TRANSACTION_OPTIONS_DEFAULT, TRANSACTION_VERSION_DEFAULT } from "../constants";
import { Err } from "../errors";
import { IAddress, IChainID, IGasLimit, IGasPrice, INonce, ITransactionValue } from "../interface";
import { TransactionOptions, TransactionVersion } from "../networkParams";
import { Transaction } from "../transaction";
Expand All @@ -18,6 +17,7 @@ interface IConfig {
gasLimitSetSpecialRole: IGasLimit;
gasLimitPausing: IGasLimit;
gasLimitFreezing: IGasLimit;
gasLimitWiping: IGasLimit;
gasLimitESDTNFTCreate: IGasLimit;
gasLimitESDTNFTUpdateAttributes: IGasLimit;
gasLimitESDTNFTAddQuantity: IGasLimit;
Expand Down Expand Up @@ -113,16 +113,18 @@ interface INFTCreateArgs extends IBaseArgs {
interface IPausingArgs extends IBaseArgs {
manager: IAddress;
tokenIdentifier: string;
pause?: boolean;
unpause?: boolean;
}

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

interface IWipingArgs extends IBaseArgs {
manager: IAddress;
user: IAddress;
tokenIdentifier: string;
}

interface ILocalMintArgs extends IBaseArgs {
Expand Down Expand Up @@ -374,12 +376,25 @@ export class TokenOperationsFactory {
}

pause(args: IPausingArgs): Transaction {
if (args.pause == args.unpause) {
throw new Err("Must set either 'pause' or 'unpause' (one and only one should be set).")
}
const parts = [
"pause",
utf8ToHex(args.tokenIdentifier)
];

return this.createTransaction({
sender: args.manager,
receiver: this.config.esdtContractAddress,
nonce: args.transactionNonce,
gasPrice: args.gasPrice,
gasLimitHint: args.gasLimit,
executionGasLimit: this.config.gasLimitPausing,
dataParts: parts
});
}

unpause(args: IPausingArgs): Transaction {
const parts = [
args.pause ? "pause" : "unPause",
"unPause",
utf8ToHex(args.tokenIdentifier)
];

Expand All @@ -395,12 +410,26 @@ export class TokenOperationsFactory {
}

freeze(args: IFreezingArgs): Transaction {
if (args.freeze == args.unfreeze) {
throw new Err("Must set either 'freeze' or 'unfreeze' (one and only one should be set).")
}
const parts = [
"freeze",
utf8ToHex(args.tokenIdentifier),
addressToHex(args.user)
];

return this.createTransaction({
sender: args.manager,
receiver: this.config.esdtContractAddress,
nonce: args.transactionNonce,
gasPrice: args.gasPrice,
gasLimitHint: args.gasLimit,
executionGasLimit: this.config.gasLimitFreezing,
dataParts: parts
});
}

unfreeze(args: IFreezingArgs): Transaction {
const parts = [
args.freeze ? "freeze" : "unFreeze",
"unFreeze",
utf8ToHex(args.tokenIdentifier),
addressToHex(args.user)
];
Expand All @@ -416,6 +445,24 @@ export class TokenOperationsFactory {
});
}

wipe(args: IWipingArgs): Transaction {
const parts = [
"wipe",
utf8ToHex(args.tokenIdentifier),
addressToHex(args.user)
];

return this.createTransaction({
sender: args.manager,
receiver: this.config.esdtContractAddress,
nonce: args.transactionNonce,
gasPrice: args.gasPrice,
gasLimitHint: args.gasLimit,
executionGasLimit: this.config.gasLimitWiping,
dataParts: parts
});
}

localMint(args: ILocalMintArgs): Transaction {
const parts = [
"ESDTLocalMint",
Expand Down
1 change: 1 addition & 0 deletions src/tokenOperations/tokenOperationsFactoryConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export class TokenOperationsFactoryConfig {
gasLimitSetSpecialRole: IGasLimit = 60000000;
gasLimitPausing: IGasLimit = 60000000;
gasLimitFreezing: IGasLimit = 60000000;
gasLimitWiping: IGasLimit = 60000000;
gasLimitESDTNFTCreate: IGasLimit = 3000000;
gasLimitESDTNFTUpdateAttributes: IGasLimit = 1000000;
gasLimitESDTNFTAddQuantity: IGasLimit = 1000000;
Expand Down
18 changes: 18 additions & 0 deletions src/tokenOperations/tokenOperationsOutcomeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ export interface IFreezingOutcome {
balance: string;
}

export interface IWipingOutcome {
userAddress: string;
tokenIdentifier: string;
nonce: string;
balance: string;
}

export interface IUpdateAttributesOutcome {
tokenIdentifier: string;
nonce: string;
Expand Down Expand Up @@ -200,6 +207,17 @@ export class TokenOperationsOutcomeParser {
return { userAddress, tokenIdentifier, nonce, balance };
}

parseWipe(transaction: ITransactionOnNetwork): IWipingOutcome {
this.ensureNoError(transaction);

const event = this.findSingleEventByIdentifier(transaction, "ESDTWipe");
const tokenIdentifier = this.extractTokenIdentifier(event);
const nonce = this.extractNonce(event);
const balance = this.extractAmount(event);
const userAddress = this.extractAddress(event);
return { userAddress, tokenIdentifier, nonce, balance };
}

parseUpdateAttributes(transaction: ITransactionOnNetwork): IUpdateAttributesOutcome {
this.ensureNoError(transaction);

Expand Down
6 changes: 3 additions & 3 deletions src/transactionPayload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,21 @@ export class TransactionPayload {
}

/**
* @deprecated Use {@link SmartContract} to create transactions related to smart contracts.
* @deprecated Use {@link SmartContract} to create smart contracts related transactions.
*/
static contractDeploy(): ContractDeployPayloadBuilder {
return new ContractDeployPayloadBuilder();
}

/**
* @deprecated Use {@link SmartContract} to create transactions related to smart contracts.
* @deprecated Use {@link SmartContract} to create smart contracts related transactions.
*/
static contractUpgrade(): ContractUpgradePayloadBuilder {
return new ContractUpgradePayloadBuilder();
}

/**
* @deprecated Use {@link SmartContract} to create transactions related to smart contracts.
* @deprecated Use {@link SmartContract} to create smart contracts related transactions.
*/
static contractCall(): ContractCallPayloadBuilder {
return new ContractCallPayloadBuilder();
Expand Down

0 comments on commit afa902a

Please sign in to comment.