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

Update sendTransaction() and getTransaction() to match Soroban-RPC API #52

Merged
merged 1 commit into from
Feb 24, 2023
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
28 changes: 13 additions & 15 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,34 +189,31 @@ export class Server {
}

/**
* Fetch the status, result, and/or error of a submitted transaction.
* Fetch the details of a submitted transaction.
*
* When submitting a transaction, clients should poll this to tell when the
* transaction has completed.
*
* @example
* const transactionHash = "c4515e3bdc0897f21cc5dbec8c82cf0a936d4741cb74a8e158eb51b9fb00411a";
* server.getTransactionStatus(transactionHash).then(transaction => {
* server.getTransaction(transactionHash).then(transaction => {
* console.log("status:", transaction.status);
* console.log("envelopeXdr:", transaction.envelopeXdr);
* console.log("resultMetaXdr:", transaction.resultMetaXdr);
* console.log("resultXdr:", transaction.resultXdr);
* console.log("error:", transaction.error);
* });
*
* @param {string} hash - The hash of the transaction to check. Encoded as a
* hex string.
*
* @returns {Promise<SorobanRpc.GetTransactionStatusResponse>} Returns a
* promise to the {@link SorobanRpc.GetTransactionStatusResponse} object
* with the status, results, and error of the transaction.
* @returns {Promise<SorobanRpc.GetTransactionResponse>} Returns a
* promise to the {@link SorobanRpc.GetTransactionResponse} object
* with the status, result, and other details about the transaction.
*/
public async getTransactionStatus(
public async getTransaction(
hash: string,
): Promise<SorobanRpc.GetTransactionStatusResponse> {
): Promise<SorobanRpc.GetTransactionResponse> {
return await jsonrpc.post(
this.serverURL.toString(),
"getTransactionStatus",
"getTransaction",
hash,
);
}
Expand Down Expand Up @@ -389,9 +386,9 @@ export class Server {
* preparedTransaction.sign(sourceKeypair);
*
* server.sendTransaction(transaction).then(result => {
* console.log("id:", result.id);
* console.log("transactionHash:", result.transactionHash);
* console.log("status:", result.status);
* console.log("error:", result.error);
* console.log("errorResultXdr:", result.errorResultXdr);
* });
*
* @param {Transaction | FeeBumpTransaction} transaction - The transaction to
Expand Down Expand Up @@ -461,8 +458,9 @@ export class Server {
* transaction.sign(sourceKeypair);
*
* server.sendTransaction(transaction).then(result => {
* console.log("id:", result.id);
* console.log("error:", result.error);
* console.log("transactionHash:", result.transactionHash);
* console.log("status:", result.status);
* console.log("errorResultXdr:", result.errorResultXdr);
* });
*
* @param {Transaction | FeeBumpTransaction} transaction - The transaction to
Expand Down
37 changes: 26 additions & 11 deletions src/soroban_rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ export namespace SorobanRpc {
memBytes: string;
}

export type TransactionStatus = "pending" | "success" | "error";

export interface GetHealthResponse {
status: "healthy";
}
Expand All @@ -43,14 +41,21 @@ export namespace SorobanRpc {
protocolVersion: string;
}

export interface GetTransactionStatusResponse {
id: string;
status: TransactionStatus;
envelopeXdr?: string;
export type GetTransactionStatus = "SUCCESS" | "NOT_FOUND" | "FAILED";

export interface GetTransactionResponse {
status: GetTransactionStatus;
latestLedger: number;
latestLedgerCloseTime: number;
oldestLedger: number;
oldestLedgerCloseTime: number;

// the fields below are set if status is SUCCESS
applicationOrder?: number;
feeBump?: boolean;
resultXdr?: string;
resultMetaXdr?: string;
results?: Array<{ xdr: string }>;
error?: jsonrpc.Error;
ledger?: number;
createdAt?: number;
}

export interface EventFilter {
Expand Down Expand Up @@ -79,9 +84,19 @@ export namespace SorobanRpc {
transaction_id: string;
}

export type SendTransactionStatus =
| "PENDING"
| "DUPLICATE"
| "TRY_AGAIN_LATER"
| "ERROR";

export interface SendTransactionResponse {
id: string;
error?: jsonrpc.Error;
status: SendTransactionStatus;
// errorResultXdr is only set when status is ERROR
errorResultXdr?: string;
transactionHash: string;
latestLedger: number;
latestLedgerCloseTime: number;
}

export interface SimulateTransactionResponse {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
describe("Server#getTransactionStatus", function() {
describe("Server#getTransaction", function() {
let keypair = SorobanClient.Keypair.random();
let account = new SorobanClient.Account(
keypair.publicKey(),
Expand Down Expand Up @@ -39,28 +39,27 @@ describe("Server#getTransactionStatus", function() {
});

it("transaction not found", function(done) {
const result = {
status: "NOT_FOUND",
latestLedger: 100,
latestLedgerCloseTime: 12345,
oldestLedger: 50,
oldestLedgerCloseTime: 500,
};
this.axiosMock
.expects("post")
.withArgs(serverUrl, {
jsonrpc: "2.0",
id: 1,
method: "getTransactionStatus",
method: "getTransaction",
params: [this.hash],
})
.returns(Promise.resolve({ data: { id: 1, error: { code: 404 } } }));
.returns(Promise.resolve({ data: { id: 1, result } }));

this.server
.getTransactionStatus(this.hash)
.then(function(_response) {
done(new Error("Expected error"));
})
.catch(function(err) {
done(
err.code == 404
? null
: new Error("Expected error code 404, got: " + err.code),
);
});
this.server.getTransaction(this.hash).then(function(response) {
expect(response).to.be.deep.equal(result);
done();
});
});

xit("transaction pending", function(done) {});
Expand Down
2 changes: 1 addition & 1 deletion test/unit/server/send_transaction_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe("Server#sendTransaction", function() {
})
.returns(
Promise.resolve({
data: { id: 1, result: { id: this.hash, status: "pending" } },
data: { id: 1, result: { id: this.hash, status: "PENDING" } },
}),
);

Expand Down