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

Add StateChanges in SimulateTransaction API response #963

Merged
merged 11 commits into from
May 30, 2024
23 changes: 21 additions & 2 deletions src/soroban/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,21 @@ export namespace Api {
value: string;
}

export interface RequestAirdropResponse {
transaction_id: string;
interface RawLedgerEntryChange {
type: number;
// This is LedgerKey in base64
key: string;
// these are xdr.LedgerEntry in base64
before?: string;
// these are xdr.LedgerEntry in base64
after?: string;
psheth9 marked this conversation as resolved.
Show resolved Hide resolved
}

interface LedgerEntryChange{
psheth9 marked this conversation as resolved.
Show resolved Hide resolved
type: number;
key: xdr.LedgerKey;
before?: xdr.LedgerEntry;
after?: xdr.LedgerEntry;
}

export type SendTransactionStatus =
Expand Down Expand Up @@ -264,6 +277,9 @@ export namespace Api {

/** present only for invocation simulation */
result?: SimulateHostFunctionResult;

// State Difference information
psheth9 marked this conversation as resolved.
Show resolved Hide resolved
stateChanges?: LedgerEntryChange[];
}

/** Includes details about why the simulation failed */
Expand Down Expand Up @@ -347,5 +363,8 @@ export namespace Api {
minResourceFee: string;
transactionData: string;
};

// State Difference information
stateChanges?: RawLedgerEntryChange[];
}
}
13 changes: 12 additions & 1 deletion src/soroban/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,18 @@ function parseSuccessful(
: xdr.ScVal.scvVoid()
};
})[0]
})
}),

stateChanges: sim.stateChanges?.length ?? 0 > 0
? sim.stateChanges!.map((entryChange) => {
return {
type: entryChange.type,
key: xdr.LedgerKey.fromXDR(entryChange.key, 'base64'),
before: !!entryChange.before ? xdr.LedgerEntry.fromXDR(entryChange.before, 'base64') : undefined,
after: !!entryChange.after ? xdr.LedgerEntry.fromXDR(entryChange.after, 'base64') : undefined,
psheth9 marked this conversation as resolved.
Show resolved Hide resolved
};
})
: undefined,
};

if (!sim.restorePreamble || sim.restorePreamble.transactionData === '') {
Expand Down
4 changes: 2 additions & 2 deletions test/unit/server/soroban/get_account_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe("Server#getAccount", function () {
jsonrpc: "2.0",
id: 1,
method: "getLedgerEntries",
params: { keys: [ key.toXDR("base64") ] },
params: { keys: [key.toXDR("base64")] },
})
.returns(
Promise.resolve({
Expand Down Expand Up @@ -62,7 +62,7 @@ describe("Server#getAccount", function () {
jsonrpc: "2.0",
id: 1,
method: "getLedgerEntries",
params: { keys: [ key.toXDR("base64") ] },
params: { keys: [key.toXDR("base64")] },
})
.returns(
Promise.resolve({
Expand Down
8 changes: 4 additions & 4 deletions test/unit/server/soroban/get_events_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ let getEventsResponseFixture = [
inSuccessfulContractCall: true,
topic: topicVals.slice(0, 2),
value: eventVal,
txHash: "d7d09af2ca4f2929ee701cf86d05e4ca5f849a726d0db344785a8f9894e79e6c"
txHash: "d7d09af2ca4f2929ee701cf86d05e4ca5f849a726d0db344785a8f9894e79e6c",
},
{
type: "contract",
Expand All @@ -258,7 +258,7 @@ let getEventsResponseFixture = [
inSuccessfulContractCall: true,
topic: topicVals.slice(0, 2),
value: eventVal,
txHash: "d7d09af2ca4f2929ee701cf86d05e4ca5f849a726d0db344785a8f9894e79e6c"
txHash: "d7d09af2ca4f2929ee701cf86d05e4ca5f849a726d0db344785a8f9894e79e6c",
},
{
type: "diagnostic",
Expand All @@ -270,7 +270,7 @@ let getEventsResponseFixture = [
inSuccessfulContractCall: true,
topic: [topicVals[0]],
value: eventVal,
txHash: "d7d09af2ca4f2929ee701cf86d05e4ca5f849a726d0db344785a8f9894e79e6c"
txHash: "d7d09af2ca4f2929ee701cf86d05e4ca5f849a726d0db344785a8f9894e79e6c",
},
{
type: "contract",
Expand All @@ -282,6 +282,6 @@ let getEventsResponseFixture = [
inSuccessfulContractCall: true,
topic: topicVals,
value: eventVal,
txHash: "d7d09af2ca4f2929ee701cf86d05e4ca5f849a726d0db344785a8f9894e79e6c"
txHash: "d7d09af2ca4f2929ee701cf86d05e4ca5f849a726d0db344785a8f9894e79e6c",
},
];
2 changes: 1 addition & 1 deletion test/unit/server/soroban/get_ledger_entries_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe("Server#getLedgerEntries", function () {
jsonrpc: "2.0",
id: 1,
method: "getLedgerEntries",
params: {keys: requests},
params: { keys: requests },
})
.returns(
Promise.resolve({
Expand Down
30 changes: 30 additions & 0 deletions test/unit/server/soroban/simulate_transaction_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ describe("Server#simulateTransaction", async function (done) {
retval: xdr.ScVal.fromXDR(simulationResponse.results[0].xdr, "base64"),
},
cost: simulationResponse.cost,
stateChanges: [
psheth9 marked this conversation as resolved.
Show resolved Hide resolved
{
type: 1,
key: "example-key",
before: new xdr.LedgerEntry({
lastModifiedLedgerSeq: 0,
ext: new xdr.LedgerEntryExt(0),
}),
after: new xdr.LedgerEntry({
lastModifiedLedgerSeq: 0,
ext: new xdr.LedgerEntryExt(0),
}),
},
],
_parsed: true,
};

Expand Down Expand Up @@ -175,6 +189,7 @@ describe("Server#simulateTransaction", async function (done) {
delete expected.cost;
delete expected.transactionData;
delete expected.minResourceFee;
delete expected.stateChanges;
expected.error = "This is an error";
expected.events = [];

Expand All @@ -200,6 +215,7 @@ function cloneSimulation(sim) {
retval: xdr.ScVal.fromXDR(sim.result.retval.toXDR()),
},
cost: sim.cost,
stateChanges: sim.stateChanges,
_parsed: sim._parsed,
};
}
Expand Down Expand Up @@ -262,6 +278,20 @@ function baseSimulationResponse(results) {
cpuInsns: "1",
memBytes: "2",
},
stateChanges: [
{
type: 1,
key: "example-key",
before: new xdr.LedgerEntry({
lastModifiedLedgerSeq: 0,
ext: new xdr.LedgerEntryExt(0),
}).toXDR("base64"),
after: new xdr.LedgerEntry({
lastModifiedLedgerSeq: 0,
ext: new xdr.LedgerEntryExt(0),
}).toXDR("base64"),
},
],
};
}

Expand Down
Loading