diff --git a/src/transaction.ts b/src/transaction.ts index 9518e31a..746482b6 100644 --- a/src/transaction.ts +++ b/src/transaction.ts @@ -560,51 +560,25 @@ export class TransactionNext { /** * Creates a new Transaction object. */ - public constructor({ - sender, - receiver, - gasLimit, - chainID, - nonce, - value, - senderUsername, - receiverUsername, - gasPrice, - data, - version, - options, - guardian, - }: { - nonce?: BigNumber.Value; - value?: BigNumber.Value; - sender: string; - receiver: string; - senderUsername?: string; - receiverUsername?: string; - gasPrice?: BigNumber.Value; - gasLimit: BigNumber.Value; - data?: Uint8Array; - chainID: string; - version?: number; - options?: number; - guardian?: string; - }) { - this.nonce = nonce || 0; - this.value = value || new BigNumber(0); - this.sender = sender; - this.receiver = receiver; - this.senderUsername = senderUsername || ""; - this.receiverUsername = receiverUsername || ""; - this.gasPrice = gasPrice || new BigNumber(TRANSACTION_MIN_GAS_PRICE); - this.gasLimit = gasLimit; - this.data = data || new Uint8Array(); - this.chainID = chainID; - this.version = version || TRANSACTION_VERSION_DEFAULT; - this.options = options || TRANSACTION_OPTIONS_DEFAULT; - this.guardian = guardian || ""; + public constructor(init: Partial) { + this.nonce = 0; + this.value = new BigNumber(0); + this.sender = ""; + this.receiver = ""; + this.senderUsername = ""; + this.receiverUsername = ""; + this.gasPrice = new BigNumber(TRANSACTION_MIN_GAS_PRICE); + this.gasLimit = 0; + this.data = new Uint8Array(); + this.chainID = ""; + this.version = TRANSACTION_VERSION_DEFAULT; + this.options = TRANSACTION_OPTIONS_DEFAULT; + this.guardian = ""; this.signature = new Uint8Array(); this.guardianSignature = new Uint8Array(); + + Object.assign(this, init); } } diff --git a/src/transactionsOutcomeParsers/tokenManagementTransactionsOutcomeParser.spec.ts b/src/transactionsOutcomeParsers/tokenManagementTransactionsOutcomeParser.spec.ts index 976ea5a9..a95813f8 100644 --- a/src/transactionsOutcomeParsers/tokenManagementTransactionsOutcomeParser.spec.ts +++ b/src/transactionsOutcomeParsers/tokenManagementTransactionsOutcomeParser.spec.ts @@ -15,8 +15,7 @@ describe("test token management transactions outcome parser", () => { }); const logs = new TransactionLogs({ events: [event] }); - const scResult = new SmartContractResult({}); - const txOutcome = new TransactionOutcome({ smartContractResults: [scResult], transactionLogs: logs }); + const txOutcome = new TransactionOutcome({ transactionLogs: logs }); assert.throws( () => { @@ -28,8 +27,8 @@ describe("test token management transactions outcome parser", () => { }); it("should test parse issue fungible", () => { - const identifer = "ZZZ-9ee87d"; - const base64Identifier = Buffer.from(identifer).toString("base64"); + const identifier = "ZZZ-9ee87d"; + const base64Identifier = Buffer.from(identifier).toString("base64"); const event = new TransactionEvent({ address: "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2", @@ -42,16 +41,15 @@ describe("test token management transactions outcome parser", () => { events: [event], }); - const scResult = new SmartContractResult({}); - const txOutcome = new TransactionOutcome({ smartContractResults: [scResult], transactionLogs: logs }); + const txOutcome = new TransactionOutcome({ transactionLogs: logs }); const outcome = parser.parseIssueFungible(txOutcome); - assert.equal(outcome.tokenIdentifier, identifer); + assert.equal(outcome.tokenIdentifier, identifier); }); it("should test parse issue non fungible", () => { - const identifer = "NFT-f01d1e"; - const base64Identifier = Buffer.from(identifer).toString("base64"); + const identifier = "NFT-f01d1e"; + const base64Identifier = Buffer.from(identifier).toString("base64"); const firstEvent = new TransactionEvent({ address: "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2", @@ -76,16 +74,15 @@ describe("test token management transactions outcome parser", () => { events: [firstEvent, secondEvent, thirdEvent], }); - const scResult = new SmartContractResult({}); - const txOutcome = new TransactionOutcome({ smartContractResults: [scResult], transactionLogs: logs }); + const txOutcome = new TransactionOutcome({ transactionLogs: logs }); const outcome = parser.parseIssueNonFungible(txOutcome); - assert.equal(outcome.tokenIdentifier, identifer); + assert.equal(outcome.tokenIdentifier, identifier); }); it("should test parse issue semi fungible", () => { - const identifer = "SEMIFNG-2c6d9f"; - const base64Identifier = Buffer.from(identifer).toString("base64"); + const identifier = "SEMIFNG-2c6d9f"; + const base64Identifier = Buffer.from(identifier).toString("base64"); const event = new TransactionEvent({ address: "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2", @@ -98,16 +95,15 @@ describe("test token management transactions outcome parser", () => { events: [event], }); - const scResult = new SmartContractResult({}); - const txOutcome = new TransactionOutcome({ smartContractResults: [scResult], transactionLogs: logs }); + const txOutcome = new TransactionOutcome({ transactionLogs: logs }); const outcome = parser.parseIssueSemiFungible(txOutcome); - assert.equal(outcome.tokenIdentifier, identifer); + assert.equal(outcome.tokenIdentifier, identifier); }); it("should test parse register meta esdt", () => { - const identifer = "METATEST-e05d11"; - const base64Identifier = Buffer.from(identifer).toString("base64"); + const identifier = "METATEST-e05d11"; + const base64Identifier = Buffer.from(identifier).toString("base64"); const event = new TransactionEvent({ address: "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2", @@ -120,16 +116,15 @@ describe("test token management transactions outcome parser", () => { events: [event], }); - const scResult = new SmartContractResult({}); - const txOutcome = new TransactionOutcome({ smartContractResults: [scResult], transactionLogs: logs }); + const txOutcome = new TransactionOutcome({ transactionLogs: logs }); const outcome = parser.parseRegisterMetaEsdt(txOutcome); - assert.equal(outcome.tokenIdentifier, identifer); + assert.equal(outcome.tokenIdentifier, identifier); }); it("should test parse register and set all roles", () => { - const identifer = "LMAO-d9f892"; - const base64Identifier = Buffer.from(identifer).toString("base64"); + const identifier = "LMAO-d9f892"; + const base64Identifier = Buffer.from(identifier).toString("base64"); const roles = ["ESDTRoleLocalMint", "ESDTRoleLocalBurn"]; const event = new TransactionEvent({ @@ -170,13 +165,13 @@ describe("test token management transactions outcome parser", () => { }); const outcome = parser.parseRegisterAndSetAllRoles(txOutcome); - assert.equal(outcome.tokenIdentifier, identifer); + assert.equal(outcome.tokenIdentifier, identifier); assert.deepEqual(outcome.roles, roles); }); it("should test parse register set special role", () => { - const identifer = "METATEST-e05d11"; - const base64Identifier = Buffer.from(identifer).toString("base64"); + const identifier = "METATEST-e05d11"; + const base64Identifier = Buffer.from(identifier).toString("base64"); const roles = ["ESDTRoleNFTCreate", "ESDTRoleNFTAddQuantity", "ESDTRoleNFTBurn"]; const event = new TransactionEvent({ @@ -197,22 +192,19 @@ describe("test token management transactions outcome parser", () => { events: [event], }); - const scResult = new SmartContractResult({}); - const txOutcome = new TransactionOutcome({ - smartContractResults: [scResult], transactionLogs: transactionLogs, }); const outcome = parser.parseSetSpecialRole(txOutcome); assert.equal(outcome.userAddress, "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"); - assert.equal(outcome.tokenIdentifier, identifer); + assert.equal(outcome.tokenIdentifier, identifier); assert.deepEqual(outcome.roles, roles); }); it("should test parse nft create", () => { - const identifer = "NFT-f01d1e"; - const base64Identifier = Buffer.from(identifer).toString("base64"); + const identifier = "NFT-f01d1e"; + const base64Identifier = Buffer.from(identifier).toString("base64"); const nonce = BigInt(1); const initialQuantity = BigInt(1); @@ -232,22 +224,19 @@ describe("test token management transactions outcome parser", () => { events: [event], }); - const scResult = new SmartContractResult({}); - const txOutcome = new TransactionOutcome({ - smartContractResults: [scResult], transactionLogs: transactionLogs, }); const outcome = parser.parseNftCreate(txOutcome); - assert.equal(outcome.tokenIdentifier, identifer); + assert.equal(outcome.tokenIdentifier, identifier); assert.equal(outcome.nonce, nonce); assert.equal(outcome.initialQuantity, initialQuantity); }); it("should test parse local mint", () => { - const identifer = "AAA-29c4c9"; - const base64Identifier = Buffer.from(identifer).toString("base64"); + const identifier = "AAA-29c4c9"; + const base64Identifier = Buffer.from(identifier).toString("base64"); const nonce = BigInt(0); const mintedSupply = BigInt(100000); @@ -262,23 +251,20 @@ describe("test token management transactions outcome parser", () => { events: [event], }); - const scResult = new SmartContractResult({}); - const txOutcome = new TransactionOutcome({ - smartContractResults: [scResult], transactionLogs: transactionLogs, }); const outcome = parser.parseLocalMint(txOutcome); assert.equal(outcome.userAddress, event.address); - assert.equal(outcome.tokenIdentifier, identifer); + assert.equal(outcome.tokenIdentifier, identifier); assert.equal(outcome.nonce, nonce); assert.equal(outcome.mintedSupply, mintedSupply); }); it("should test parse local burn", () => { - const identifer = "AAA-29c4c9"; - const base64Identifier = Buffer.from(identifer).toString("base64"); + const identifier = "AAA-29c4c9"; + const base64Identifier = Buffer.from(identifier).toString("base64"); const nonce = BigInt(0); const burntSupply = BigInt(100000); @@ -293,23 +279,20 @@ describe("test token management transactions outcome parser", () => { events: [event], }); - const scResult = new SmartContractResult({}); - const txOutcome = new TransactionOutcome({ - smartContractResults: [scResult], transactionLogs: transactionLogs, }); const outcome = parser.parseLocalBurn(txOutcome); assert.equal(outcome.userAddress, event.address); - assert.equal(outcome.tokenIdentifier, identifer); + assert.equal(outcome.tokenIdentifier, identifier); assert.equal(outcome.nonce, nonce); assert.equal(outcome.burntSupply, burntSupply); }); it("should test parse pause", () => { - const identifer = "AAA-29c4c9"; - const base64Identifier = Buffer.from(identifer).toString("base64"); + const identifier = "AAA-29c4c9"; + const base64Identifier = Buffer.from(identifier).toString("base64"); const event = new TransactionEvent({ address: "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2", @@ -322,20 +305,17 @@ describe("test token management transactions outcome parser", () => { events: [event], }); - const scResult = new SmartContractResult({}); - const txOutcome = new TransactionOutcome({ - smartContractResults: [scResult], transactionLogs: transactionLogs, }); const outcome = parser.parsePause(txOutcome); - assert.equal(outcome.tokenIdentifier, identifer); + assert.equal(outcome.tokenIdentifier, identifier); }); it("should test parse unpause", () => { - const identifer = "AAA-29c4c9"; - const base64Identifier = Buffer.from(identifer).toString("base64"); + const identifier = "AAA-29c4c9"; + const base64Identifier = Buffer.from(identifier).toString("base64"); const event = new TransactionEvent({ address: "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2", @@ -348,20 +328,17 @@ describe("test token management transactions outcome parser", () => { events: [event], }); - const scResult = new SmartContractResult({}); - const txOutcome = new TransactionOutcome({ - smartContractResults: [scResult], transactionLogs: transactionLogs, }); const outcome = parser.parseUnpause(txOutcome); - assert.equal(outcome.tokenIdentifier, identifer); + assert.equal(outcome.tokenIdentifier, identifier); }); it("should test parse freeze", () => { - const identifer = "AAA-29c4c9"; - const base64Identifier = Buffer.from(identifer).toString("base64"); + const identifier = "AAA-29c4c9"; + const base64Identifier = Buffer.from(identifier).toString("base64"); const nonce = BigInt(0); const balance = BigInt(10000000); const address = "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"; @@ -390,14 +367,14 @@ describe("test token management transactions outcome parser", () => { const outcome = parser.parseFreeze(txOutcome); assert.equal(outcome.userAddress, address); - assert.equal(outcome.tokenIdentifier, identifer); + assert.equal(outcome.tokenIdentifier, identifier); assert.equal(outcome.nonce, nonce); assert.equal(outcome.balance, balance); }); it("should test parse unfreeze", () => { - const identifer = "AAA-29c4c9"; - const base64Identifier = Buffer.from(identifer).toString("base64"); + const identifier = "AAA-29c4c9"; + const base64Identifier = Buffer.from(identifier).toString("base64"); const nonce = BigInt(0); const balance = BigInt(10000000); const address = "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"; @@ -426,14 +403,14 @@ describe("test token management transactions outcome parser", () => { const outcome = parser.parseUnfreeze(txOutcome); assert.equal(outcome.userAddress, address); - assert.equal(outcome.tokenIdentifier, identifer); + assert.equal(outcome.tokenIdentifier, identifier); assert.equal(outcome.nonce, nonce); assert.equal(outcome.balance, balance); }); it("should test parse wipe", () => { - const identifer = "AAA-29c4c9"; - const base64Identifier = Buffer.from(identifer).toString("base64"); + const identifier = "AAA-29c4c9"; + const base64Identifier = Buffer.from(identifier).toString("base64"); const nonce = BigInt(0); const balance = BigInt(10000000); const address = "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"; @@ -462,14 +439,14 @@ describe("test token management transactions outcome parser", () => { const outcome = parser.parseWipe(txOutcome); assert.equal(outcome.userAddress, address); - assert.equal(outcome.tokenIdentifier, identifer); + assert.equal(outcome.tokenIdentifier, identifier); assert.equal(outcome.nonce, nonce); assert.equal(outcome.balance, balance); }); it("should test parse update attributes", () => { - const identifer = "NFT-f01d1e"; - const base64Identifier = Buffer.from(identifer).toString("base64"); + const identifier = "NFT-f01d1e"; + const base64Identifier = Buffer.from(identifier).toString("base64"); const nonce = BigInt(1); const attributes = "metadata:ipfsCID/test.json;tags:tag1,tag2"; const base64Attributes = Buffer.from(attributes).toString("base64"); @@ -485,22 +462,19 @@ describe("test token management transactions outcome parser", () => { events: [event], }); - const scResult = new SmartContractResult({}); - const txOutcome = new TransactionOutcome({ - smartContractResults: [scResult], transactionLogs: transactionLogs, }); const outcome = parser.parseUpdateAttributes(txOutcome); - assert.equal(outcome.tokenIdentifier, identifer); + assert.equal(outcome.tokenIdentifier, identifier); assert.equal(outcome.nonce, nonce); assert.equal(Buffer.from(outcome.attributes).toString(), attributes); }); it("should test parse add quantity", () => { - const identifer = "NFT-f01d1e"; - const base64Identifier = Buffer.from(identifer).toString("base64"); + const identifier = "NFT-f01d1e"; + const base64Identifier = Buffer.from(identifier).toString("base64"); const nonce = BigInt(1); const addedQuantity = BigInt(10); @@ -515,22 +489,19 @@ describe("test token management transactions outcome parser", () => { events: [event], }); - const scResult = new SmartContractResult({}); - const txOutcome = new TransactionOutcome({ - smartContractResults: [scResult], transactionLogs: transactionLogs, }); const outcome = parser.parseAddQuantity(txOutcome); - assert.equal(outcome.tokenIdentifier, identifer); + assert.equal(outcome.tokenIdentifier, identifier); assert.equal(outcome.nonce, nonce); assert.equal(outcome.addedQuantity, addedQuantity); }); it("should test parse burn quantity", () => { - const identifer = "NFT-f01d1e"; - const base64Identifier = Buffer.from(identifer).toString("base64"); + const identifier = "NFT-f01d1e"; + const base64Identifier = Buffer.from(identifier).toString("base64"); const nonce = BigInt(1); const burntQuantity = BigInt(16); @@ -545,15 +516,12 @@ describe("test token management transactions outcome parser", () => { events: [event], }); - const scResult = new SmartContractResult({}); - const txOutcome = new TransactionOutcome({ - smartContractResults: [scResult], transactionLogs: transactionLogs, }); const outcome = parser.parseBurnQuantity(txOutcome); - assert.equal(outcome.tokenIdentifier, identifer); + assert.equal(outcome.tokenIdentifier, identifier); assert.equal(outcome.nonce, nonce); assert.equal(outcome.burntQuantity, burntQuantity); }); diff --git a/src/transactionsOutcomeParsers/tokenManagementTransactionsOutcomeParser.ts b/src/transactionsOutcomeParsers/tokenManagementTransactionsOutcomeParser.ts index 0c7ad943..f9b389eb 100644 --- a/src/transactionsOutcomeParsers/tokenManagementTransactionsOutcomeParser.ts +++ b/src/transactionsOutcomeParsers/tokenManagementTransactionsOutcomeParser.ts @@ -10,36 +10,36 @@ export class TokenManagementTransactionsOutcomeParser { this.ensureNoError(transactionOutcome.transactionLogs.events); const event = this.findSingleEventByIdentifier(transactionOutcome, "issue"); - const identifer = this.extractTokenIdentifier(event); + const identifier = this.extractTokenIdentifier(event); - return { tokenIdentifier: identifer }; + return { tokenIdentifier: identifier }; } parseIssueNonFungible(transactionOutcome: TransactionOutcome): { tokenIdentifier: string } { this.ensureNoError(transactionOutcome.transactionLogs.events); const event = this.findSingleEventByIdentifier(transactionOutcome, "issueNonFungible"); - const identifer = this.extractTokenIdentifier(event); + const identifier = this.extractTokenIdentifier(event); - return { tokenIdentifier: identifer }; + return { tokenIdentifier: identifier }; } parseIssueSemiFungible(transactionOutcome: TransactionOutcome): { tokenIdentifier: string } { this.ensureNoError(transactionOutcome.transactionLogs.events); const event = this.findSingleEventByIdentifier(transactionOutcome, "issueSemiFungible"); - const identifer = this.extractTokenIdentifier(event); + const identifier = this.extractTokenIdentifier(event); - return { tokenIdentifier: identifer }; + return { tokenIdentifier: identifier }; } parseRegisterMetaEsdt(transactionOutcome: TransactionOutcome): { tokenIdentifier: string } { this.ensureNoError(transactionOutcome.transactionLogs.events); const event = this.findSingleEventByIdentifier(transactionOutcome, "registerMetaESDT"); - const identifer = this.extractTokenIdentifier(event); + const identifier = this.extractTokenIdentifier(event); - return { tokenIdentifier: identifer }; + return { tokenIdentifier: identifier }; } parseRegisterAndSetAllRoles(transactionOutcome: TransactionOutcome): { tokenIdentifier: string; roles: string[] } { @@ -53,8 +53,7 @@ export class TokenManagementTransactionsOutcomeParser { let roles: string[] = []; for (const role of encodedRoles) { - const decodedRole = Buffer.from(role, "base64"); - roles = roles.concat(decodedRole.toString()); + roles = roles.concat(this.decodeTopicAsString(role)); } return { tokenIdentifier: tokenIdentifier, roles: roles }; @@ -83,8 +82,7 @@ export class TokenManagementTransactionsOutcomeParser { let roles: string[] = []; for (const role of encodedRoles) { - const decodedRole = Buffer.from(role, "base64"); - roles = roles.concat(decodedRole.toString()); + roles = roles.concat(this.decodeTopicAsString(role)); } return { userAddress: userAddress, tokenIdentifier: tokenIdentifier, roles: roles }; @@ -294,7 +292,7 @@ export class TokenManagementTransactionsOutcomeParser { for (const event of transactionEvents) { if (event.identifier == "signalError") { const data = Buffer.from(event.data.toString().slice(1)).toString(); - const message = Buffer.from(event.topics[1], "base64").toString(); + const message = this.decodeTopicAsString(event.topics[1]); throw new ErrParseTransactionOutcome( `encountered signalError: ${message} (${Buffer.from(data, "hex").toString()})`, @@ -332,7 +330,7 @@ export class TokenManagementTransactionsOutcomeParser { if (!event.topics[0]) { return ""; } - return Buffer.from(event.topics[0], "base64").toString(); + return this.decodeTopicAsString(event.topics[0]); } private extractNonce(event: TransactionEvent): bigint { @@ -358,4 +356,8 @@ export class TokenManagementTransactionsOutcomeParser { const address = Buffer.from(event.topics[3], "base64"); return Address.fromBuffer(address).bech32(); } + + private decodeTopicAsString(topic: string): string { + return Buffer.from(topic, "base64").toString(); + } }