Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delegation transactions outcome parser #402

Merged
merged 3 commits into from
Mar 14, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { assert } from "chai";
import { DelegationTransactionsOutcomeParser } from "./delegationTransactionsOutcomeParser";
import { SmartContractResult, TransactionEvent, TransactionLogs, TransactionOutcome } from "./resources";
import { Address } from "../address";

describe("test token management transactions outcome parser", () => {
const parser = new DelegationTransactionsOutcomeParser();

it("should test parseCreateNewDelegationContract ", () => {
const contractAddress = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqy8lllls62y8s5");

const delegateEvent = new TransactionEvent({
address: "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2",
identifier: "delegate",
topics: [
"Q8M8GTdWSAAA",
"Q8M8GTdWSAAA",
"AQ==",
"Q8M8GTdWSAAA",
"AAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAABD///8=",
],
});

const scDeployEvent = new TransactionEvent({
address: "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqy8lllls62y8s5",
identifier: "SCDeploy",
topics: ["AAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAABD///8=", "PDXX6ssamaSgzKpTfvDMCuEJ9B9sK0AiA+Yzv7sHH1w="],
});

const logs = new TransactionLogs({ events: [delegateEvent, scDeployEvent] });

const scResultEvent = new TransactionEvent({
address: "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2",
identifier: "completedTxEvent",
topics: ["b2g6sUl6beG17FCUIkFwCOTGJjoJJi5SjkP2077e6xA="],
});

const scResultLog = new TransactionLogs({
address: "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2",
events: [scResultEvent],
});

const scResult = new SmartContractResult({
sender: "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqylllslmq6y6",
receiver: "erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2",
data: Buffer.from(
"QDZmNmJAMDAwMDAwMDAwMDAwMDAwMDAwMDEwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAxMGZmZmZmZg==",
"base64",
),
logs: scResultLog,
});

const txOutcome = new TransactionOutcome({ smartContractResults: [scResult], transactionLogs: logs });

const outcome = parser.parseCreateNewDelegationContract(txOutcome);

assert.lengthOf(outcome, 1);
assert.equal(outcome[0].contractAddress, contractAddress.toBech32());
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { ErrParseTransactionOutcome } from "../errors";
import { TransactionEvent, TransactionOutcome } from "./resources";
import { Address } from "../address";

export class DelegationTransactionsOutcomeParser {
constructor() {}

parseCreateNewDelegationContract(transactionOutcome: TransactionOutcome): { contractAddress: string }[] {
this.ensureNoError(transactionOutcome.transactionLogs.events);

const events = this.findEventsByIdentifier(transactionOutcome, "SCDeploy");

return events.map((event) => ({ contractAddress: this.extractContractAddress(event) }));
}

private ensureNoError(transactionEvents: TransactionEvent[]) {
for (const event of transactionEvents) {
if (event.identifier == "signalError") {
const data = Buffer.from(event.data.toString().slice(1)).toString();
const message = this.decodeTopicAsString(event.topics[1]);

throw new ErrParseTransactionOutcome(
`encountered signalError: ${message} (${Buffer.from(data, "hex").toString()})`,
);
}
}
}

private findEventsByIdentifier(transactionOutcome: TransactionOutcome, identifier: string): TransactionEvent[] {
const events = this.gatherAllEvents(transactionOutcome).filter((event) => event.identifier == identifier);

if (events.length == 0) {
throw new ErrParseTransactionOutcome(`cannot find event of type ${identifier}`);
}

return events;
}

private gatherAllEvents(transactionOutcome: TransactionOutcome): TransactionEvent[] {
const allEvents = [];

allEvents.push(...transactionOutcome.transactionLogs.events);

for (const item of transactionOutcome.smartContractResults) {
allEvents.push(...item.logs.events);
}

return allEvents;
}

private extractContractAddress(event: TransactionEvent): string {
if (!event.topics[0]) {
return "";
}
const address = Buffer.from(event.topics[0], "base64");
return Address.fromBuffer(address).bech32();
}

private decodeTopicAsString(topic: string): string {
return Buffer.from(topic, "base64").toString();
}
}
1 change: 1 addition & 0 deletions src/transactionsOutcomeParsers/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./resources";
export * from "./tokenManagementTransactionsOutcomeParser";
export * from "./delegationTransactionsOutcomeParser";
Loading