Skip to content

Commit

Permalink
Fetch metadata via RPC instead of relying on the Friendbot response (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaptic authored Nov 27, 2024
1 parent bb2e5ba commit 3304995
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ A breaking change will get clearly marked in this log.

## Unreleased

### Fixed
* When using a friendbot that points to a Horizon instance that has ledger metadata disabled, you can no longer extract the account sequence from the response. Instead, we hit RPC directly ([#1107](https://github.com/stellar/js-stellar-sdk/pull/1107/)).


## [v13.0.0](https://github.com/stellar/js-stellar-sdk/compare/v12.3.0...v13.0.0)
This is a direct re-tag of rc.2 with the only change being an upgrade to the `stellar-base` library to incorporate a patch release. Nonetheless, the entire changelog from the prior major version here is replicated for a comprehensive view on what's broken, added, and fixed.
Expand Down
1 change: 1 addition & 0 deletions src/friendbot/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export namespace Api {
// Just the fields we are interested in
export interface Response {
hash: string;
result_meta_xdr: string;
}
}
15 changes: 11 additions & 4 deletions src/rpc/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -977,10 +977,17 @@ export class RpcServer {
`${friendbotUrl}?addr=${encodeURIComponent(account)}`
);

const meta = xdr.TransactionMeta.fromXDR(
response.data.result_meta_xdr,
'base64'
);
let meta: xdr.TransactionMeta;
if (!response.data.result_meta_xdr) {
const txMeta = await this.getTransaction(response.data.hash)
if (txMeta.status !== Api.GetTransactionStatus.SUCCESS) {
throw new Error(`Funding account ${address} failed`);
}
meta = txMeta.resultMetaXdr;
} else {
meta = xdr.TransactionMeta.fromXDR(response.data.result_meta_xdr, 'base64');
}

const sequence = findCreatedAccountSequenceInTransactionMeta(meta);
return new Account(account, sequence);
} catch (error: any) {
Expand Down
92 changes: 91 additions & 1 deletion test/unit/server/soroban/request_airdrop_test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { Account, Keypair, StrKey, Networks, xdr, hash } = StellarSdk;
const { Account, Keypair, StrKey, Networks, xdr, hash, nativeToScVal } =
StellarSdk;
const { Server, AxiosClient } = StellarSdk.rpc;

describe("Server#requestAirdrop", function () {
Expand Down Expand Up @@ -253,4 +254,93 @@ describe("Server#requestAirdrop", function () {
done();
});
});

it("falls back to RPC if no meta is present", function (done) {
const friendbotUrl = "https://friendbot.stellar.org";
const accountId =
"GBZXN7PIRZGNMHGA7MUUUF4GWPY5AYPV6LY4UV2GL6VJGIQRXFDNMADI";
mockGetNetwork.call(this, friendbotUrl);

this.axiosMock
.expects("post")
.withArgs(`${friendbotUrl}?addr=${accountId}`)
.returns(Promise.resolve({ data: { hash: "deadbeef" } }));

const txResult = makeTxResult("SUCCESS");
txResult.txHash = "deadbeef";
txResult.resultMetaXdr = transactionMetaFor(accountId, "1234").toXDR(
"base64",
);

this.axiosMock
.expects("post")
.withArgs(serverUrl, {
jsonrpc: "2.0",
id: 1,
method: "getTransaction",
params: {
hash: "deadbeef",
},
})
.returns(Promise.resolve({ data: { result: txResult } }));

this.server
.requestAirdrop(accountId)
.then((response) => {
expect(response).to.be.deep.equal(new Account(accountId, "1234"));
done();
})
.catch((err) => done(err));
});
});

// ripped out of ./get_transaction_test.js
function makeTxResult(status, addSoroban = true) {
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(1234),
}),
}),
);

// only injected in the success case
//
// this data was picked from a random transaction in horizon:
// aa6a8e198abe53c7e852e4870413b29fe9ef04da1415a97a5de1a4ae489e11e2
const successInfo = {
ledger: 1234,
createdAt: 123456789010,
applicationOrder: 2,
feeBump: false,
envelopeXdr:
"AAAAAgAAAAAT/LQZdYz0FcQ4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9oAAAAApmc7UgUBInrDvij8HMSridx2n1w3I8TVEn4sLr1LSpmAAAAAlBBTExBRElVTQAAAAAAAABQ7rVrrukulDyQa6LTUoH5l2tuzPvutzk+02J9ao4MvgAAIUz88EqYAAAAAVNHWAAAAAAAUO61a67pLpQ8kGui01KB+Zdrbsz77rc5PtNifWqODL4AABpRpyCa1wAAAAKYUsaaCZ233xB1p+lG7YksShJWfrjsmItbokiR3ifa0gAAAAJTSUxWRVIAAAAAAAAAAAAAUO61a67pLpQ8kGui01KB+Zdrbsz77rc5PtNifWqODL4AABv52PPa5wAAAAJQQUxMQURJVU0AAAAAAAAAUO61a67pLpQ8kGui01KB+Zdrbsz77rc5PtNifWqODL4AACFM/PBKmAAAAAJnhbp+Y3re6FWphFwcRP4PY0S0m+EIhh+UWH0aq1j4DAAAAAAAAAAAAAA9pAAAAAJTSUxWRVIAAAAAAAAAAAAAUO61a67pLpQ8kGui01KB+Zdrbsz77rc5PtNifWqODL4AABv52PPa5wAAAAAv/RPIQ4Z5Lcrn1QUubagO9+GM+V6y19lzYAYB/2BJIgAAAAAAAAAAAAA9pAAAAAA=",
resultMetaXdr: metaV3.toXDR("base64"),
};

if (!addSoroban) {
// replace the V3 Soroban meta with a "classic" V2 version
successInfo.resultMetaXdr =
"AAAAAgAAAAIAAAADAtL5awAAAAAAAAAAS0CFMhOtWUKJWerx66zxkxORaiH6/3RUq7L8zspD5RoAAAAAAcm9QAKVkpMAAHpMAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAACAAAAAAAAAAAAAAAAAAAAAwAAAAAC0vi5AAAAAGTB02oAAAAAAAAAAQLS+WsAAAAAAAAAAEtAhTITrVlCiVnq8eus8ZMTkWoh+v90VKuy/M7KQ+UaAAAAAAHJvUAClZKTAAB6TQAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAM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EmQAAAKSAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAABAAAAWwVyiIoAAAAYWVsojgAAAAIAAAAAAAAAAAAAAAAAAAADAAAAAALS9GsAAAAAZMG6qwAAAAAAAAAA";
}

return {
status,
txHash: "ae9f315c048d87a5f853bc15bf284a2c3c89eb0e1cb38c10409b77a877b830a8",
latestLedger: 100,
latestLedgerCloseTime: 12345,
oldestLedger: 50,
oldestLedgerCloseTime: 500,
...(status === "SUCCESS" && successInfo),
};
}

0 comments on commit 3304995

Please sign in to comment.