Skip to content

Commit

Permalink
Remove transation outcome and fix changes
Browse files Browse the repository at this point in the history
  • Loading branch information
danielailie committed Nov 19, 2024
1 parent f377ac6 commit aa68ee4
Show file tree
Hide file tree
Showing 35 changed files with 464 additions and 884 deletions.
2 changes: 1 addition & 1 deletion src/abi/resultsParser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import {
TransactionEventOnNetwork,
TransactionEventTopic,
TransactionLogsOnNetwork,
TransactionOnNetwork,
} from "../networkProviders";
import { loadAbiRegistry } from "../testutils";
import { TransactionOnNetwork } from "../transactions";
import { ArgSerializer } from "./argSerializer";
import { ResultsParser } from "./resultsParser";
import { ReturnCode } from "./returnCode";
Expand Down
1 change: 1 addition & 0 deletions src/abi/typesystem/abiRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export class AbiRegistry {
}

getEvent(name: string): EventDefinition {
console.log({ name });
const result = this.events.find((e) => e.identifier == name);
guardValueIsSetWithMessage(`event [${name}] not found`, result);
return result!;
Expand Down
63 changes: 12 additions & 51 deletions src/converters/transactionsConverter.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { ResultsParser } from "../abi";
import { IPlainTransactionObject, ITransaction } from "../interface";
import { IContractResultItem, ITransactionEvent, ITransactionOnNetwork } from "../interfaceOfNetwork";
import { IContractResultItem, ITransactionEvent } from "../interfaceOfNetwork";
import { Transaction } from "../transaction";
import {
SmartContractCallOutcome,
SmartContractResult,
TransactionEvent,
TransactionLogs,
TransactionOutcome,
} from "../transactionsOutcomeParsers/resources";
import { TransactionEvent, TransactionEventData, TransactionEventTopic } from "../transactionEvents";
import { TransactionLogs } from "../transactionLogs";
import { SmartContractResult } from "../transactionsOutcomeParsers/resources";

export class TransactionsConverter {
public transactionToPlainObject(transaction: ITransaction): IPlainTransactionObject {
Expand Down Expand Up @@ -71,42 +66,6 @@ export class TransactionsConverter {
return Buffer.from(value || "", "hex");
}

/**
* @deprecated Where {@link TransactionOutcome} was needed (throughout the SDK), pass the {@link ITransactionOnNetwork} object instead.
*
* Summarizes the outcome of a transaction on the network, and maps it to the "standard" resources (according to the sdk-specs).
*
* In the future, this converter function will become obsolete,
* as the impedance mismatch between the network components and the "core" components will be reduced.
*/
public transactionOnNetworkToOutcome(transactionOnNetwork: ITransactionOnNetwork): TransactionOutcome {
// In the future, this will not be needed because the transaction, as returned from the API,
// will hold the data corresponding to the direct smart contract call outcome (in case of smart contract calls).
const legacyResultsParser = new ResultsParser();
const callOutcomeBundle = legacyResultsParser.parseUntypedOutcome(transactionOnNetwork);
const callOutcome = new SmartContractCallOutcome({
function: transactionOnNetwork.function,
returnCode: callOutcomeBundle.returnCode.toString(),
returnMessage: callOutcomeBundle.returnMessage,
returnDataParts: callOutcomeBundle.values,
});

const contractResults = transactionOnNetwork.contractResults.items.map((result) =>
this.smartContractResultOnNetworkToSmartContractResult(result),
);

const logs = new TransactionLogs({
address: transactionOnNetwork.logs.address.bech32(),
events: transactionOnNetwork.logs.events.map((event) => this.eventOnNetworkToEvent(event)),
});

return new TransactionOutcome({
logs: logs,
smartContractResults: contractResults,
directSmartContractCallOutcome: callOutcome,
});
}

private smartContractResultOnNetworkToSmartContractResult(
resultOnNetwork: IContractResultItem,
): SmartContractResult {
Expand All @@ -115,7 +74,7 @@ export class TransactionsConverter {
receiver: resultOnNetwork.receiver.bech32(),
data: Buffer.from(resultOnNetwork.data),
logs: new TransactionLogs({
address: resultOnNetwork.logs.address.bech32(),
address: resultOnNetwork.logs.address,
events: resultOnNetwork.logs.events.map((event) => this.eventOnNetworkToEvent(event)),
}),
});
Expand All @@ -126,19 +85,21 @@ export class TransactionsConverter {
// After Sirius, the "additionalData" field includes the payload of the legacy "data" field, as well (as its first element):
// https://github.com/multiversx/mx-chain-go/blob/v1.6.18/process/transactionLog/process.go#L159
const legacyData = eventOnNetwork.dataPayload?.valueOf() || Buffer.from(eventOnNetwork.data || "");
const dataItems = eventOnNetwork.additionalData?.map((data) => Buffer.from(data.valueOf())) || [];
const dataItems =
eventOnNetwork.additionalData?.map((data) => new TransactionEventData(Buffer.from(data.valueOf()))) || [];

if (dataItems.length === 0) {
if (legacyData.length) {
dataItems.push(Buffer.from(legacyData));
dataItems.push(new TransactionEventData(Buffer.from(legacyData)));
}
}

return new TransactionEvent({
address: eventOnNetwork.address.bech32(),
address: eventOnNetwork.address,
identifier: eventOnNetwork.identifier,
topics: eventOnNetwork.topics.map((topic) => Buffer.from(topic.hex(), "hex")),
dataItems: dataItems,
topics: eventOnNetwork.topics.map((topic) => new TransactionEventTopic(topic.hex())),
dataPayload: new TransactionEventData(Buffer.from(legacyData)),
additionalData: dataItems,
});
}
}
173 changes: 0 additions & 173 deletions src/converters/transactionsConverters.spec.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
import {
ContractResultItem,
ContractResults,
TransactionEventData,
TransactionEventOnNetwork,
TransactionEventTopic,
TransactionLogsOnNetwork,
TransactionOnNetwork,
} from "../networkProviders";
import { assert } from "chai";
import { Address } from "../address";
import { Transaction } from "../transaction";
import {
SmartContractCallOutcome,
SmartContractResult,
TransactionEvent,
TransactionLogs,
TransactionOutcome,
} from "../transactionsOutcomeParsers/resources";
import { TransactionsConverter } from "./transactionsConverter";

describe("test transactions converter", async () => {
Expand Down Expand Up @@ -61,160 +44,4 @@ describe("test transactions converter", async () => {
guardianSignature: undefined,
});
});

it("converts transaction on network to transaction outcome", () => {
const converter = new TransactionsConverter();

const transactionOnNetwork = new TransactionOnNetwork({
nonce: 7,
function: "hello",
logs: new TransactionLogsOnNetwork({
address: Address.fromBech32("erd1k2s324ww2g0yj38qn2ch2jwctdy8mnfxep94q9arncc6xecg3xaq6mjse8"),
events: [
new TransactionEventOnNetwork({
identifier: "foobar",
topics: [],
dataPayload: new TransactionEventData(Buffer.from("foo")),
additionalData: [],
}),
],
}),
contractResults: new ContractResults([
new ContractResultItem({
nonce: 8,
data: "@6f6b@2a",
logs: new TransactionLogsOnNetwork({
address: Address.fromBech32("erd1k2s324ww2g0yj38qn2ch2jwctdy8mnfxep94q9arncc6xecg3xaq6mjse8"),
events: [
new TransactionEventOnNetwork({
identifier: "writeLog",
topics: [
new TransactionEventTopic(
// '@too much gas provided for processing: gas provided = 596384500, gas used = 733010'
"QHRvbyBtdWNoIGdhcyBwcm92aWRlZCBmb3IgcHJvY2Vzc2luZzogZ2FzIHByb3ZpZGVkID0gNTk2Mzg0NTAwLCBnYXMgdXNlZCA9IDczMzAxMA==",
),
],
dataPayload: TransactionEventData.fromBase64("QDZmNmI="),
}),
],
}),
}),
]),
});

const actualTransactionOutcome = converter.transactionOnNetworkToOutcome(transactionOnNetwork);
const expectedTransactionOutcome = new TransactionOutcome({
directSmartContractCallOutcome: new SmartContractCallOutcome({
function: "hello",
returnCode: "ok",
returnMessage: "ok",
returnDataParts: [Buffer.from([42])],
}),
smartContractResults: [
new SmartContractResult({
sender: "",
receiver: "",
data: Buffer.from("@6f6b@2a"),
logs: {
address: "erd1k2s324ww2g0yj38qn2ch2jwctdy8mnfxep94q9arncc6xecg3xaq6mjse8",
events: [
new TransactionEvent({
address: "",
identifier: "writeLog",
topics: [
Buffer.from(
"@too much gas provided for processing: gas provided = 596384500, gas used = 733010",
),
],
dataItems: [Buffer.from("QDZmNmI=", "base64")],
}),
],
},
}),
],
logs: new TransactionLogs({
address: "erd1k2s324ww2g0yj38qn2ch2jwctdy8mnfxep94q9arncc6xecg3xaq6mjse8",
events: [
new TransactionEvent({
address: "",
identifier: "foobar",
topics: [],
dataItems: [Buffer.from("foo")],
}),
],
}),
});

assert.deepEqual(actualTransactionOutcome, expectedTransactionOutcome);
});

it("converts transaction on network to transaction outcome (with signal error)", () => {
const converter = new TransactionsConverter();

const transactionOnNetwork = new TransactionOnNetwork({
nonce: 42,
function: "hello",
contractResults: new ContractResults([
new ContractResultItem({
nonce: 42,
data: "@657865637574696f6e206661696c6564",
logs: new TransactionLogsOnNetwork({
address: Address.fromBech32("erd1qqqqqqqqqqqqqpgqj8k976l59n7fyth8ujl4as5uyn3twn0ha0wsge5r5x"),
events: [
new TransactionEventOnNetwork({
address: Address.fromBech32(
"erd1qqqqqqqqqqqqqpgqj8k976l59n7fyth8ujl4as5uyn3twn0ha0wsge5r5x",
),
identifier: "signalError",
topics: [
new TransactionEventTopic("XmC5/yOF6ie6DD2kaJd5qPc2Ss7h2w7nvuWaxmCiiXQ="),
new TransactionEventTopic("aW5zdWZmaWNpZW50IGZ1bmRz"),
],
dataPayload: new TransactionEventData(Buffer.from("@657865637574696f6e206661696c6564")),
additionalData: [
new TransactionEventData(Buffer.from("@657865637574696f6e206661696c6564")),
new TransactionEventData(Buffer.from("foobar")),
],
}),
],
}),
}),
]),
});

const actualTransactionOutcome = converter.transactionOnNetworkToOutcome(transactionOnNetwork);
const expectedTransactionOutcome = new TransactionOutcome({
directSmartContractCallOutcome: new SmartContractCallOutcome({
function: "hello",
returnCode: "execution failed",
returnMessage: "execution failed",
returnDataParts: [],
}),
smartContractResults: [
new SmartContractResult({
sender: "",
receiver: "",
data: Buffer.from("@657865637574696f6e206661696c6564"),
logs: {
address: "erd1qqqqqqqqqqqqqpgqj8k976l59n7fyth8ujl4as5uyn3twn0ha0wsge5r5x",
events: [
new TransactionEvent({
address: "erd1qqqqqqqqqqqqqpgqj8k976l59n7fyth8ujl4as5uyn3twn0ha0wsge5r5x",
identifier: "signalError",
topics: [
Address.fromBech32(
"erd1testnlersh4z0wsv8kjx39me4rmnvjkwu8dsaea7ukdvvc9z396qykv7z7",
).getPublicKey(),
Buffer.from("insufficient funds"),
],
dataItems: [Buffer.from("@657865637574696f6e206661696c6564"), Buffer.from("foobar")],
}),
],
},
}),
],
});

assert.deepEqual(actualTransactionOutcome, expectedTransactionOutcome);
});
});
4 changes: 2 additions & 2 deletions src/delegation/delegationController.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { IAccount } from "../accounts/interfaces";
import { ITransactionOnNetwork } from "../interfaceOfNetwork";
import { INetworkProvider } from "../networkProviders/interface";
import { Transaction } from "../transaction";
import { TransactionComputer } from "../transactionComputer";
import { TransactionOnNetwork } from "../transactions";
import { TransactionsFactoryConfig } from "../transactionsFactoryConfig";
import { TransactionWatcher } from "../transactionWatcher";
import { DelegationTransactionsFactory } from "./delegationTransactionsFactory";
Expand Down Expand Up @@ -42,7 +42,7 @@ export class DelegationController {
return this.parseCreateNewDelegationContract(transaction);
}

parseCreateNewDelegationContract(transactionOnNetwork: ITransactionOnNetwork): { contractAddress: string }[] {
parseCreateNewDelegationContract(transactionOnNetwork: TransactionOnNetwork): { contractAddress: string }[] {
return this.parser.parseCreateNewDelegationContract(transactionOnNetwork);
}

Expand Down
Loading

0 comments on commit aa68ee4

Please sign in to comment.