Skip to content

Commit

Permalink
Merge branch 'master' into dependabot/npm_and_yarn/babel/plugin-trans…
Browse files Browse the repository at this point in the history
…form-private-property-in-object-7.24.1
  • Loading branch information
steveluscher committed Apr 17, 2024
2 parents b4637ba + 0bcc0ca commit 876e12a
Show file tree
Hide file tree
Showing 199 changed files with 7,973 additions and 6,087 deletions.
11 changes: 11 additions & 0 deletions .changeset/calm-crabs-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'@solana/codecs-data-structures': patch
'@solana/codecs-strings': patch
'@solana/transactions': patch
'@solana/codecs-core': patch
'@solana/addresses': patch
'@solana/rpc-types': patch
'@solana/options': patch
---

Renamed `mapCodec` to `transformCodec`
4 changes: 2 additions & 2 deletions .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
}
],
"commit": false,
"fixed": [["@solana/!({*-impl,build-scripts,test-*,tsconfig,web3.js-legacy-sham})"]],
"fixed": [["@solana/!({*-impl,build-scripts,test-*,tsconfig})"]],
"ignore": ["@solana/web3.js"],
"linked": [],
"snapshot": {
"prereleaseTemplate": "{tag}.1.{datetime}.{commit}",
"prereleaseTemplate": "{tag}.2.{datetime}.{commit}",
"useCalculatedVersion": true
},
"privatePackages": false,
Expand Down
16 changes: 16 additions & 0 deletions .changeset/gorgeous-gorillas-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
'@solana/codecs-core': patch
'@solana/errors': patch
---

Added new `addCodecSentinel` primitive

The `addCodecSentinel` function provides a new way of delimiting the size of a codec. It allows us to add a sentinel to the end of the encoded data and to read until that sentinel is found when decoding. It accepts any codec and a `Uint8Array` sentinel responsible for delimiting the encoded data.

```ts
const codec = addCodecSentinel(getUtf8Codec(), new Uint8Array([255, 255]));
codec.encode('hello');
// 0x68656c6c6fffff
// | └-- Our sentinel.
// └-- Our encoded string.
```
5 changes: 5 additions & 0 deletions .changeset/little-berries-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solana/programs": patch
---

Remove program types and `resolveTransactionError` helper
5 changes: 5 additions & 0 deletions .changeset/lovely-mice-promise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solana/programs": patch
---

Add `isProgramError` helper function to `@solana/programs`
33 changes: 33 additions & 0 deletions .changeset/mean-vans-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
'@solana/codecs-data-structures': patch
'@solana/errors': patch
---

Added `useValuesAsDiscriminators` option to `getEnumCodec`

When dealing with numerical enums that have explicit values, you may now use the `useValuesAsDiscriminators` option to encode the value of the enum variant instead of its index.

```ts
enum Numbers {
One,
Five = 5,
Six,
Nine = 9,
}

const codec = getEnumCodec(Numbers, { useValuesAsDiscriminators: true });
codec.encode(Direction.One); // 0x00
codec.encode(Direction.Five); // 0x05
codec.encode(Direction.Six); // 0x06
codec.encode(Direction.Nine); // 0x09
```

Note that when using the `useValuesAsDiscriminators` option on an enum that contains a lexical value, an error will be thrown.

```ts
enum Lexical {
One,
Two = 'two',
}
getEnumCodec(Lexical, { useValuesAsDiscriminators: true }); // Throws an error.
```
77 changes: 38 additions & 39 deletions .changeset/pre.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,50 @@
"mode": "pre",
"tag": "preview",
"initialVersions": {
"@solana/accounts": "2.0.0-preview.1",
"@solana/addresses": "2.0.0-preview.1",
"@solana/assertions": "2.0.0-preview.1",
"@solana/codecs-core": "2.0.0-preview.1",
"@solana/codecs-data-structures": "2.0.0-preview.1",
"@solana/codecs-numbers": "2.0.0-preview.1",
"@solana/codecs-strings": "2.0.0-preview.1",
"@solana/codecs": "2.0.0-preview.1",
"@solana/compat": "2.0.0-preview.1",
"@solana/errors": "2.0.0-preview.1",
"@solana/functional": "2.0.0-preview.1",
"@solana/instructions": "2.0.0-preview.1",
"@solana/keys": "2.0.0-preview.1",
"@solana/options": "2.0.0-preview.1",
"@solana/programs": "2.0.0-preview.1",
"@solana/rpc-api": "2.0.0-preview.1",
"@solana/rpc-graphql": "2.0.0-preview.1",
"@solana/rpc-parsed-types": "2.0.0-preview.1",
"@solana/rpc-spec-types": "2.0.0-preview.1",
"@solana/rpc-spec": "2.0.0-preview.1",
"@solana/rpc-subscriptions-api": "2.0.0-preview.1",
"@solana/rpc-subscriptions-spec": "2.0.0-preview.1",
"@solana/rpc-subscriptions-transport-websocket": "2.0.0-preview.1",
"@solana/rpc-subscriptions": "2.0.0-preview.1",
"@solana/rpc-transformers": "2.0.0-preview.1",
"@solana/rpc-transport-http": "2.0.0-preview.1",
"@solana/rpc-types": "2.0.0-preview.1",
"@solana/rpc": "2.0.0-preview.1",
"@solana/signers": "2.0.0-preview.1",
"@solana/transaction-confirmation": "2.0.0-preview.1",
"@solana/transactions": "2.0.0-preview.1",
"@solana/web3.js": "0.0.0-development",
"@solana/web3.js-experimental": "2.0.0-preview.1",
"@solana/web3.js-legacy-sham": "1.0.0-preview.1",
"@solana/webcrypto-ed25519-polyfill": "2.0.0-preview.1",
"@solana/accounts": "2.0.0-preview.2",
"@solana/addresses": "2.0.0-preview.2",
"@solana/assertions": "2.0.0-preview.2",
"@solana/build-scripts": "0.0.0",
"@solana/codecs": "2.0.0-preview.2",
"@solana/codecs-core": "2.0.0-preview.2",
"@solana/codecs-data-structures": "2.0.0-preview.2",
"@solana/codecs-numbers": "2.0.0-preview.2",
"@solana/codecs-strings": "2.0.0-preview.2",
"@solana/compat": "2.0.0-preview.2",
"@solana/crypto-impl": "0.0.0",
"@solana/sysvars": "2.0.0-preview.1",
"@solana/errors": "2.0.0-preview.2",
"@solana/fast-stable-stringify": "2.0.0-preview.2",
"@solana/functional": "2.0.0-preview.2",
"@solana/instructions": "2.0.0-preview.2",
"@solana/keys": "2.0.0-preview.2",
"@solana/web3.js-experimental": "2.0.0-preview.2",
"@solana/web3.js": "0.0.0-development",
"@solana/options": "2.0.0-preview.2",
"@solana/programs": "2.0.0-preview.2",
"@solana/rpc": "2.0.0-preview.2",
"@solana/rpc-api": "2.0.0-preview.2",
"@solana/rpc-graphql": "2.0.0-preview.2",
"@solana/rpc-parsed-types": "2.0.0-preview.2",
"@solana/rpc-spec": "2.0.0-preview.2",
"@solana/rpc-spec-types": "2.0.0-preview.2",
"@solana/rpc-subscriptions": "2.0.0-preview.2",
"@solana/rpc-subscriptions-api": "2.0.0-preview.2",
"@solana/rpc-subscriptions-spec": "2.0.0-preview.2",
"@solana/rpc-subscriptions-transport-websocket": "2.0.0-preview.2",
"@solana/rpc-transformers": "2.0.0-preview.2",
"@solana/rpc-transport-http": "2.0.0-preview.2",
"@solana/rpc-types": "2.0.0-preview.2",
"@solana/signers": "2.0.0-preview.2",
"@solana/sysvars": "2.0.0-preview.2",
"@solana/test-config": "0.0.0",
"@solana/test-matchers": "0.0.0",
"@solana/text-encoding-impl": "0.0.0",
"@solana/transaction-confirmation": "2.0.0-preview.2",
"@solana/transaction-messages": "2.0.0-preview.2",
"@solana/transactions": "2.0.0-preview.2",
"@solana/tsconfig": "0.0.0",
"@solana/webcrypto-ed25519-polyfill": "2.0.0-preview.2",
"@solana/ws-impl": "0.0.0"
},
"changesets": [
"three-guests-study"
]
"changesets": []
}
5 changes: 5 additions & 0 deletions .changeset/short-toes-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@solana/codecs-data-structures': patch
---

The `getBooleanCodec` function now accepts variable-size number codecs
1 change: 0 additions & 1 deletion .changeset/three-guests-study.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
'@solana/instructions': patch
'@solana/keys': patch
'@solana/web3.js-experimental': patch
'@solana/web3.js-legacy-sham': patch
'@solana/options': patch
'@solana/programs': patch
'@solana/rpc': patch
Expand Down
5 changes: 5 additions & 0 deletions .changeset/witty-camels-remember.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solana/ws-impl": patch
---

Use native `WebSocket` for compatibility with server runtimes that support it, like Deno
7 changes: 6 additions & 1 deletion .github/workflows/actions/install-dependencies/action.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
name: Install Dependencies
description: Sets up Node and its package manager, then installs all dependencies

inputs:
version:
default: 'lts/*'
type: string

runs:
using: composite
steps:
Expand All @@ -12,7 +17,7 @@ runs:
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 18
node-version: ${{ inputs.version }}
cache: 'pnpm'

- name: Install dependencies
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/pull-requests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ jobs:

- name: Install Dependencies
uses: ./.github/workflows/actions/install-dependencies
with:
version: ${{ matrix.node }}

- name: Setup Solana Test Validator
id: start-test-validator
Expand Down
6 changes: 4 additions & 2 deletions packages/addresses/src/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
FixedSizeDecoder,
FixedSizeEncoder,
fixEncoderSize,
mapEncoder,
transformEncoder,
} from '@solana/codecs-core';
import { getBase58Decoder, getBase58Encoder } from '@solana/codecs-strings';
import {
Expand Down Expand Up @@ -82,7 +82,9 @@ export function address<TAddress extends string = string>(putativeAddress: TAddr
}

export function getAddressEncoder(): FixedSizeEncoder<Address, 32> {
return mapEncoder(fixEncoderSize(getMemoizedBase58Encoder(), 32), putativeAddress => address(putativeAddress));
return transformEncoder(fixEncoderSize(getMemoizedBase58Encoder(), 32), putativeAddress =>
address(putativeAddress),
);
}

export function getAddressDecoder(): FixedSizeDecoder<Address, 32> {
Expand Down
3 changes: 2 additions & 1 deletion packages/addresses/src/program-derived-address.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assertDigestCapabilityIsAvailable } from '@solana/assertions';
import type { ReadonlyUint8Array } from '@solana/codecs-core';
import {
isSolanaError,
SOLANA_ERROR__ADDRESSES__FAILED_TO_FIND_VIABLE_PDA_BUMP_SEED,
Expand Down Expand Up @@ -77,7 +78,7 @@ type SeedInput = Readonly<{
seed: Seed;
}>;

type Seed = Uint8Array | string;
type Seed = ReadonlyUint8Array | string;

const MAX_SEED_LENGTH = 32;
const MAX_SEEDS = 16;
Expand Down
47 changes: 38 additions & 9 deletions packages/codecs-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ There is a significant library of composable codecs at your disposal, enabling y
- [`@solana/codecs-data-structures`](https://github.com/solana-labs/solana-web3.js/tree/master/packages/codecs-data-structures) for many data structure codecs such as objects, arrays, tuples, sets, maps, enums, discriminated unions, booleans, etc.
- [`@solana/options`](https://github.com/solana-labs/solana-web3.js/tree/master/packages/options) for a Rust-like `Option` type and associated codec.

You may also be interested in some of the helpers of this `@solana/codecs-core` library such as `mapCodec`, `fixCodecSize` or `reverseCodec` that create new codecs from existing ones.
You may also be interested in some of the helpers of this `@solana/codecs-core` library such as `transformCodec`, `fixCodecSize` or `reverseCodec` that create new codecs from existing ones.

Note that all of these libraries are included in the [`@solana/codecs` package](https://github.com/solana-labs/solana-web3.js/tree/master/packages/codecs) as well as the main `@solana/web3.js` package for your convenience.

Expand Down Expand Up @@ -113,14 +113,14 @@ const value = u64Codec.decode(bytes); // BigInt(42)

This relationship between the type we encode “From” and decode “To” can be generalized in TypeScript as `To extends From`.

Here’s another example using an object with default values. You can read more about the `mapEncoder` helper below.
Here’s another example using an object with default values. You can read more about the `transformEncoder` helper below.

```ts
type Person = { name: string, age: number };
type PersonInput = { name: string, age?: number };

const getPersonEncoder = (): Encoder<PersonInput> =>
mapEncoder(
transformEncoder(
getStructEncoder([
['name', addEncoderSizePrefix(getUtf8Encoder(), getU32Encoder())],
['age', getU32Encoder()],
Expand Down Expand Up @@ -285,15 +285,15 @@ const getCipherDecoder = () =>
const getCipherCodec = () => combineCodec(getCipherEncoder(), getCipherDecoder());
```

## Mapping codecs
## Transforming codecs

It is possible to transform a `Codec<T>` to a `Codec<U>` by providing two mapping functions: one that goes from `T` to `U` and one that does the opposite.

For instance, here’s how you would map a `u32` integer into a `string` representation of that number.

```ts
const getStringU32Codec = () =>
mapCodec(
transformCodec(
getU32Codec(),
(integerAsString: string): number => parseInt(integerAsString),
(integer: number): string => integer.toString(),
Expand All @@ -309,7 +309,7 @@ To illustrate that, let’s take our previous `getStringU32Codec` example but ma

```ts
const getStringU64Codec = () =>
mapCodec(
transformCodec(
getU64Codec(),
(integerInput: number | string): number | bigint =>
typeof integerInput === 'string' ? BigInt(integerAsString) : integerInput,
Expand All @@ -327,7 +327,7 @@ const getPersonCodec = (): Codec<Person> => { /*...*/ }

type PersonInput = { name: string; age?: number; }
const getPersonWithDefaultValueCodec = (): Codec<PersonInput, Person> =>
mapCodec(
transformCodec(
getPersonCodec(),
(person: PersonInput): Person => { ...person, age: person.age ?? 42 }
)
Expand All @@ -337,8 +337,8 @@ Similar helpers exist to map `Encoder` and `Decoder` instances allowing you to s

```ts
const getStringU32Encoder = () =>
mapEncoder(getU32Encoder(), (integerAsString: string): number => parseInt(integerAsString));
const getStringU32Decoder = () => mapDecoder(getU32Decoder(), (integer: number): string => integer.toString());
transformEncoder(getU32Encoder(), (integerAsString: string): number => parseInt(integerAsString));
const getStringU32Decoder = () => transformDecoder(getU32Decoder(), (integer: number): string => integer.toString());
const getStringU32Codec = () => combineCodec(getStringU32Encoder(), getStringU32Decoder());
```

Expand Down Expand Up @@ -385,6 +385,35 @@ const getU32Base58Decoder = () => addDecoderSizePrefix(getBase58Decoder(), getU3
const getU32Base58Codec = () => combineCodec(getU32Base58Encoder(), getU32Base58Decoder());
```

## Adding sentinels to codecs

Another way of delimiting the size of a codec is to use sentinels. The `addCodecSentinel` function allows us to add a sentinel to the end of the encoded data and to read until that sentinel is found when decoding. It accepts any codec and a `Uint8Array` sentinel responsible for delimiting the encoded data.

```ts
const codec = addCodecSentinel(getUtf8Codec(), new Uint8Array([255, 255]));
codec.encode('hello');
// 0x68656c6c6fffff
// | └-- Our sentinel.
// └-- Our encoded string.
```

Note that the sentinel _must not_ be present in the encoded data and _must_ be present in the decoded data for this to work. If this is not the case, dedicated errors will be thrown.

```ts
const sentinel = new Uint8Array([108, 108]); // 'll'
const codec = addCodecSentinel(getUtf8Codec(), sentinel);

codec.encode('hello'); // Throws: sentinel is in encoded data.
codec.decode(new Uint8Array([1, 2, 3])); // Throws: sentinel missing in decoded data.
```

Separate `addEncoderSentinel` and `addDecoderSentinel` functions are also available.

```ts
const bytes = addEncoderSentinel(getUtf8Encoder(), sentinel).encode('hello');
const value = addDecoderSentinel(getUtf8Decoder(), sentinel).decode(bytes);
```

## Adjusting the size of codecs

The `resizeCodec` helper re-defines the size of a given codec by accepting a function that takes the current size of the codec and returns a new size. This works for both fixed-size and variable-size codecs.
Expand Down
Loading

0 comments on commit 876e12a

Please sign in to comment.