-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RPC: Add support for
getTransactions
endpoint (#1037)
* Add getTransactions endpoint - 1 * Add unittest * remove pagination test * Fix linter * Update src/rpc/server.ts Co-authored-by: George <[email protected]> * Fix review comments * fix diagnosticEvents parsing * small change * small change - 2 * add diagnosticEvents to GetTransaction response * Add CHANGELOG --------- Co-authored-by: George <[email protected]>
- Loading branch information
1 parent
581b32a
commit e6cda07
Showing
5 changed files
with
305 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
const { | ||
xdr, | ||
Keypair, | ||
Account, | ||
TransactionBuilder, | ||
nativeToScVal, | ||
XdrLargeInt, | ||
} = StellarSdk; | ||
const { Server, AxiosClient } = StellarSdk.rpc; | ||
|
||
describe("Server#getTransactions", function () { | ||
beforeEach(function () { | ||
this.server = new Server(serverUrl); | ||
this.axiosMock = sinon.mock(AxiosClient); | ||
this.prepareAxios = (result) => { | ||
this.axiosMock | ||
.expects("post") | ||
.withArgs(serverUrl, { | ||
jsonrpc: "2.0", | ||
id: 1, | ||
method: "getTransactions", | ||
params: { | ||
startLedger: 1234, | ||
limit: 10, | ||
}, | ||
}) | ||
.returns(Promise.resolve({ data: { id: 1, result } })); | ||
}; | ||
}); | ||
|
||
afterEach(function () { | ||
this.axiosMock.verify(); | ||
this.axiosMock.restore(); | ||
}); | ||
|
||
it("fetches transactions successfully", function (done) { | ||
const rawResult = makeGetTransactionsResult(); | ||
this.prepareAxios(rawResult); | ||
|
||
let expected = JSON.parse(JSON.stringify(rawResult)); | ||
expected.transactions = expected.transactions.map((tx) => { | ||
let parsedTx = { ...tx }; | ||
[ | ||
["envelopeXdr", xdr.TransactionEnvelope], | ||
["resultXdr", xdr.TransactionResult], | ||
["resultMetaXdr", xdr.TransactionMeta], | ||
].forEach(([field, struct]) => { | ||
parsedTx[field] = struct.fromXDR(tx[field], "base64"); | ||
}); | ||
if (tx.status === "SUCCESS") { | ||
parsedTx.returnValue = parsedTx.resultMetaXdr | ||
.v3() | ||
.sorobanMeta() | ||
.returnValue(); | ||
} | ||
return parsedTx; | ||
}); | ||
|
||
this.server | ||
.getTransactions({ startLedger: 1234, limit: 10 }) | ||
.then((resp) => { | ||
expect(Object.keys(resp)).to.eql(Object.keys(expected)); | ||
expect(resp.transactions.length).to.equal(expected.transactions.length); | ||
expect(resp).to.eql(expected); | ||
expect(resp.transactions[0].returnValue).to.eql( | ||
new XdrLargeInt("u64", 1234).toScVal(), | ||
); | ||
expect(resp.transactions[1].returnValue).to.eql( | ||
new XdrLargeInt("u64", 1235).toScVal(), | ||
); | ||
done(); | ||
}) | ||
.catch((err) => done(err)); | ||
}); | ||
|
||
it("empty transaction list", function (done) { | ||
const result = { | ||
transactions: [], | ||
latestLedger: 100, | ||
oldestLedger: 1, | ||
oldestLedgerCloseTimestamp: 123456789, | ||
latestLedgerCloseTimestamp: 987654321, | ||
cursor: "123456", | ||
}; | ||
this.prepareAxios(result); | ||
|
||
this.server | ||
.getTransactions({ startLedger: 1234, limit: 10 }) | ||
.then((resp) => { | ||
expect(resp).to.deep.equal(result); | ||
expect(resp.transactions).to.be.an("array").that.is.empty; | ||
done(); | ||
}) | ||
.catch((err) => done(err)); | ||
}); | ||
|
||
it("handles errors", function (done) { | ||
const errorResponse = { | ||
code: -32600, | ||
message: "Invalid request", | ||
data: { | ||
extras: { | ||
reason: "Invalid startLedger", | ||
}, | ||
}, | ||
}; | ||
|
||
this.axiosMock | ||
.expects("post") | ||
.withArgs(serverUrl, { | ||
jsonrpc: "2.0", | ||
id: 1, | ||
method: "getTransactions", | ||
params: { startLedger: -1, limit: 10 }, | ||
}) | ||
.returns(Promise.reject({ response: { data: errorResponse } })); | ||
|
||
this.server | ||
.getTransactions({ startLedger: -1, limit: 10 }) | ||
.then(() => { | ||
done(new Error("Expected method to reject.")); | ||
}) | ||
.catch((err) => { | ||
expect(err.response.data).to.eql(errorResponse); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
function makeGetTransactionsResult(count = 2) { | ||
const transactions = []; | ||
for (let i = 0; i < count; i++) { | ||
transactions.push(makeTxResult(1234 + i, i + 1, "SUCCESS")); | ||
} | ||
return { | ||
transactions, | ||
latestLedger: 100, | ||
latestLedgerCloseTimestamp: 987654321, | ||
oldestLedger: 1, | ||
oldestLedgerCloseTimestamp: 123456789, | ||
cursor: "123456", | ||
}; | ||
} | ||
|
||
function makeTxResult(ledger, applicationOrder, status) { | ||
const metaV3 = new xdr.TransactionMeta( | ||
3, | ||
new xdr.TransactionMetaV3({ | ||
ext: new xdr.ExtensionPoint(0), | ||
txChangesBefore: [], | ||
operations: [], | ||
txChangesAfter: [], | ||
sorobanMeta: new xdr.SorobanTransactionMeta({ | ||
ext: new xdr.SorobanTransactionMetaExt(0), | ||
events: [], | ||
diagnosticEvents: [], | ||
returnValue: nativeToScVal(ledger), | ||
}), | ||
}), | ||
); | ||
|
||
return { | ||
status: status, | ||
ledger: ledger, | ||
createdAt: ledger * 25 + 100, | ||
applicationOrder: applicationOrder, | ||
feeBump: false, | ||
envelopeXdr: | ||
"AAAAAgAAAAAT/LQZdYz0FcQ4Xwyg8IM17rkUx3pPCCWL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