Skip to content

Commit

Permalink
Prepare release w/ support for stable Protocol 20 and RPC. (#167)
Browse files Browse the repository at this point in the history
* Update library to latest stellar-base
* Drop expiration workaround in lieu of new TTL in schema (#168)
* Upgrade minimum Node version to 18, add fixups for Node 20
* Update API in accordance with new RPC schemas (#173)
* Mark as stable version
  • Loading branch information
Shaptic authored Dec 6, 2023
1 parent 3fff672 commit 4caf267
Show file tree
Hide file tree
Showing 15 changed files with 997 additions and 1,202 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/gh_pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ jobs:
ref: master
path: js-stellar-base

- name: Install Node 16
- name: Install Node 18
uses: actions/setup-node@v2
with:
node-version: 16
node-version: '18.x'

- name: Install Dependencies
run: yarn install
Expand Down
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# Changelog

- Forked from [stellar/js-stellar-sdk](https://github.com/stellar/js-stellar-sdk).
**This repository has been deprecated** in favor of the [`stellar-sdk`](https://github.com/stellar/js-stellar-sdk) package. Please read the [migration guide](https://gist.github.com/Shaptic/5ce4f16d9cce7118f391fbde398c2f30) for how to upgrade to that package. Future changes will only be made there.

A breaking change should be clearly marked in this log.

## v1.0.0

### Breaking Changes
* See the new deprecation warning at the top of this document and in the [README](./README.md).
* XDR has been upgraded to the latest `stellar-base` release, supporting the current stable XDR for Protocol 20 ([#167](https://github.com/stellar/js-soroban-client/pull/167)).


## Unreleased
Expand Down
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@
<a href="https://coveralls.io/github/stellar/js-soroban-client?branch=master"><img alt="Coverage Status" src="https://coveralls.io/repos/stellar/js-soroban-client/badge.svg?branch=master&service=github" /></a>
</p>

js-soroban-client is a Javascript library for communicating with a
[Soroban RPC server](https://github.com/stellar/go/tree/master/exp/services/soroban-rpc).
It is used for building Stellar apps either on Node.js or in the browser.
# Deprecation Notice

It provides:
**This repository has been deprecated** in favor of the [`stellar-sdk`](https://github.com/stellar/js-stellar-sdk) package. Please read the [migration guide](https://gist.github.com/Shaptic/5ce4f16d9cce7118f391fbde398c2f30) for how to upgrade to that package. Future changes will only be made there.

----------

js-soroban-client is a JavaScript library for communicating with a
[Soroban RPC server](https://soroban.stellar.org/api) and building Stellar apps. It provides:
- a networking layer API for soroban-rpc methods.
- facilities for building and signing transactions, for communicating with a
soroban-rpc instance, and for submitting transactions or querying network
state.

<details>

### soroban-client vs stellar-base

soroban-client is a high-level library that serves as client-side API for Horizon.
Expand Down Expand Up @@ -214,3 +218,5 @@ See [CONTRIBUTING.md](./CONTRIBUTING.md) for the detailed release process. Once
js-soroban-client is licensed under an Apache-2.0 license. See the
[LICENSE](https://github.com/stellar/js-soroban-client/blob/master/LICENSE) file
for details.

</details>
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "soroban-client",
"version": "1.0.0-beta.4",
"version": "1.0.0",
"description": "A library for working with Stellar's Soroban RPC servers.",
"author": "Stellar Development Foundation <[email protected]>",
"homepage": "https://github.com/stellar/js-soroban-client",
Expand Down Expand Up @@ -84,7 +84,7 @@
"@babel/preset-env": "^7.23.2",
"@babel/preset-typescript": "^7.23.2",
"@babel/register": "^7.22.15",
"@definitelytyped/dtslint": "^0.0.189",
"@definitelytyped/dtslint": "^0.0.197",
"@istanbuljs/nyc-config-babel": "3.0.0",
"@stellar/tsconfig": "^1.0.2",
"@types/chai": "^4.3.9",
Expand Down Expand Up @@ -119,7 +119,7 @@
"karma-mocha": "^2.0.0",
"karma-sinon-chai": "^2.0.2",
"karma-webpack": "^5.0.0",
"lint-staged": "^14.0.1",
"lint-staged": "^15.1.0",
"minami": "^1.1.1",
"mocha": "^10.2.0",
"node-polyfill-webpack-plugin": "^2.0.1",
Expand All @@ -139,7 +139,7 @@
"axios": "^1.6.0",
"bignumber.js": "^9.1.1",
"buffer": "^6.0.3",
"stellar-base": "v10.0.0-beta.4",
"stellar-base": "git+https://github.com/stellar/js-stellar-base#af1b94a",
"urijs": "^1.19.1"
}
}
6 changes: 4 additions & 2 deletions src/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function parseRawEvents(
contractId: new Contract(evt.contractId)
}),
topic: evt.topic.map((topic) => xdr.ScVal.fromXDR(topic, 'base64')),
value: xdr.ScVal.fromXDR(evt.value.xdr, 'base64')
value: xdr.ScVal.fromXDR(evt.value, 'base64')
};
})
};
Expand All @@ -55,7 +55,9 @@ export function parseRawLedgerEntries(
lastModifiedLedgerSeq: rawEntry.lastModifiedLedgerSeq,
key: xdr.LedgerKey.fromXDR(rawEntry.key, 'base64'),
val: xdr.LedgerEntryData.fromXDR(rawEntry.xdr, 'base64'),
expirationLedgerSeq: rawEntry.expirationLedgerSeq
...(rawEntry.liveUntilLedgerSeq !== undefined && {
liveUntilLedgerSeq: rawEntry.liveUntilLedgerSeq
})
};
})
};
Expand Down
98 changes: 10 additions & 88 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import {
FeeBumpTransaction,
Keypair,
Transaction,
xdr,
hash
xdr
} from 'stellar-base';

import AxiosClient from './axios';
Expand Down Expand Up @@ -171,7 +170,7 @@ export class Server {
* const key = xdr.ScVal.scvSymbol("counter");
* server.getContractData(contractId, key, Durability.Temporary).then(data => {
* console.log("value:", data.val);
* console.log("expirationLedgerSeq:", data.expirationLedgerSeq);
* console.log("liveUntilLedgerSeq:", data.liveUntilLedgerSeq);
* console.log("lastModified:", data.lastModifiedLedgerSeq);
* console.log("latestLedger:", data.latestLedger);
* });
Expand Down Expand Up @@ -261,7 +260,7 @@ export class Server {
* const ledgerData = response.entries[0];
* console.log("key:", ledgerData.key);
* console.log("value:", ledgerData.val);
* console.log("expirationLedgerSeq:", ledgerData.expirationLedgerSeq);
* console.log("liveUntilLedgerSeq:", ledgerData.liveUntilLedgerSeq);
* console.log("lastModified:", ledgerData.lastModifiedLedgerSeq);
* console.log("latestLedger:", response.latestLedger);
* });
Expand All @@ -273,15 +272,11 @@ export class Server {
}

public async _getLedgerEntries(...keys: xdr.LedgerKey[]) {
return jsonrpc
.post<SorobanRpc.RawGetLedgerEntriesResponse>(
this.serverURL.toString(),
'getLedgerEntries',
expandRequestIncludeExpirationLedgers(keys).map((k) =>
k.toXDR('base64')
)
)
.then((response) => mergeResponseExpirationLedgers(response, keys));
return jsonrpc.post<SorobanRpc.RawGetLedgerEntriesResponse>(
this.serverURL.toString(),
'getLedgerEntries',
keys.map((k) => k.toXDR('base64'))
);
}

/**
Expand Down Expand Up @@ -455,7 +450,7 @@ export class Server {
*
* @param {Transaction | FeeBumpTransaction} transaction the transaction to
* simulate, which should include exactly one operation (one of
* {@link xdr.InvokeHostFunctionOp}, {@link xdr.BumpFootprintExpirationOp},
* {@link xdr.InvokeHostFunctionOp}, {@link xdr.ExtendFootprintTTLOp},
* or {@link xdr.RestoreFootprintOp}). Any provided footprint or auth
* information will be ignored.
*
Expand Down Expand Up @@ -526,7 +521,7 @@ export class Server {
*
* @param {Transaction | FeeBumpTransaction} transaction the transaction to
* prepare. It should include exactly one operation, which must be one of
* {@link xdr.InvokeHostFunctionOp}, {@link xdr.BumpFootprintExpirationOp},
* {@link xdr.InvokeHostFunctionOp}, {@link xdr.ExtendFootprintTTLOp},
* or {@link xdr.RestoreFootprintOp}.
*
* Any provided footprint will be overwritten. However, if your operation
Expand Down Expand Up @@ -752,76 +747,3 @@ function findCreatedAccountSequenceInTransactionMeta(

throw new Error('No account created in transaction');
}

// TODO - remove once rpc updated to
// append expiration entry per data LK requested onto server-side response
// https://github.com/stellar/soroban-tools/issues/1010
function mergeResponseExpirationLedgers(
ledgerEntriesResponse: SorobanRpc.RawGetLedgerEntriesResponse,
requestedKeys: xdr.LedgerKey[]
): SorobanRpc.RawGetLedgerEntriesResponse {
const requestedKeyXdrs = new Set<String>(
requestedKeys.map((requestedKey) => requestedKey.toXDR('base64'))
);
const expirationKeyToRawEntryResult = new Map<
String,
SorobanRpc.RawLedgerEntryResult
>();
(ledgerEntriesResponse.entries ?? []).forEach((rawEntryResult) => {
if (!rawEntryResult.key || !rawEntryResult.xdr) {
throw new TypeError(
`invalid ledger entry: ${JSON.stringify(rawEntryResult)}`
);
}
const parsedKey = xdr.LedgerKey.fromXDR(rawEntryResult.key, 'base64');
const isExpirationMeta =
parsedKey.switch().value === xdr.LedgerEntryType.expiration().value &&
!requestedKeyXdrs.has(rawEntryResult.key);
const keyHash = isExpirationMeta
? parsedKey.expiration().keyHash().toString()
: hash(parsedKey.toXDR()).toString();

const rawEntry =
expirationKeyToRawEntryResult.get(keyHash) ?? rawEntryResult;

if (isExpirationMeta) {
const expirationLedgerSeq = xdr.LedgerEntryData.fromXDR(
rawEntryResult.xdr,
'base64'
)
.expiration()
.expirationLedgerSeq();
expirationKeyToRawEntryResult.set(keyHash, {
...rawEntry,
expirationLedgerSeq
});
} else {
expirationKeyToRawEntryResult.set(keyHash, {
...rawEntry,
...rawEntryResult
});
}
});

ledgerEntriesResponse.entries = [...expirationKeyToRawEntryResult.values()];
return ledgerEntriesResponse;
}

// TODO - remove once rpc updated to
// include expiration entry on responses for any data LK's requested
// https://github.com/stellar/soroban-tools/issues/1010
function expandRequestIncludeExpirationLedgers(
keys: xdr.LedgerKey[]
): xdr.LedgerKey[] {
return keys.concat(
keys
.filter(
(key) => key.switch().value !== xdr.LedgerEntryType.expiration().value
)
.map((key) =>
xdr.LedgerKey.expiration(
new xdr.LedgerKeyExpiration({ keyHash: hash(key.toXDR()) })
)
)
);
}
Loading

0 comments on commit 4caf267

Please sign in to comment.