Skip to content

Commit 0956a51

Browse files
authored
Merge branch 'main' into package
2 parents 5ecc167 + 4bb15ba commit 0956a51

File tree

20 files changed

+4372
-18
lines changed

20 files changed

+4372
-18
lines changed

.github/actions/setup/action.yml

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
name: Setup environment
2+
3+
inputs:
4+
cargo-cache-key:
5+
description: The key to cache cargo dependencies. Skips cargo caching if not provided.
6+
required: false
7+
cargo-cache-fallback-key:
8+
description: The fallback key to use when caching cargo dependencies. Default to not using a fallback key.
9+
required: false
10+
cargo-cache-local-key:
11+
description: The key to cache local cargo dependencies. Skips local cargo caching if not provided.
12+
required: false
13+
clippy:
14+
description: Install Clippy if `true`. Defaults to `false`.
15+
required: false
16+
rustfmt:
17+
description: Install Rustfmt if `true`. Defaults to `false`.
18+
required: false
19+
js:
20+
description: Install pnpm and node if `true`. Defaults to `false`.
21+
required: false
22+
rust:
23+
description: Install Rust if `true`. Defaults to `false`.
24+
required: false
25+
26+
runs:
27+
using: 'composite'
28+
steps:
29+
- name: Setup pnpm
30+
if: ${{ inputs.js == 'true' }}
31+
uses: pnpm/action-setup@v3
32+
with:
33+
package_json_file: 'js/package.json'
34+
35+
- name: Setup Node.js
36+
if: ${{ inputs.js == 'true' }}
37+
uses: actions/setup-node@v4
38+
with:
39+
node-version: 20
40+
cache: 'pnpm'
41+
cache-dependency-path: 'js/pnpm-lock.yaml'
42+
43+
- name: Install Rustfmt
44+
if: ${{ inputs.rustfmt == 'true' }}
45+
uses: dtolnay/rust-toolchain@master
46+
with:
47+
toolchain: "1.78.0"
48+
components: rustfmt
49+
50+
- name: Install Clippy
51+
if: ${{ inputs.clippy == 'true' }}
52+
uses: dtolnay/rust-toolchain@master
53+
with:
54+
toolchain: "1.78.0"
55+
components: clippy
56+
57+
- name: Install Rust
58+
if: ${{ inputs.rust == 'true' }}
59+
uses: dtolnay/rust-toolchain@master
60+
with:
61+
toolchain: "1.78.0"
62+
63+
- name: Cache Cargo Dependencies
64+
if: ${{ inputs.cargo-cache-key && !inputs.cargo-cache-fallback-key }}
65+
uses: actions/cache@v4
66+
with:
67+
path: |
68+
~/.cargo/bin/
69+
~/.cargo/registry/index/
70+
~/.cargo/registry/cache/
71+
~/.cargo/git/db/
72+
target/
73+
key: ${{ runner.os }}-${{ inputs.cargo-cache-key }}-${{ hashFiles('**/Cargo.lock') }}
74+
restore-keys: ${{ runner.os }}-${{ inputs.cargo-cache-key }}
75+
76+
- name: Cache Cargo Dependencies With Fallback
77+
if: ${{ inputs.cargo-cache-key && inputs.cargo-cache-fallback-key }}
78+
uses: actions/cache@v4
79+
with:
80+
path: |
81+
~/.cargo/bin/
82+
~/.cargo/registry/index/
83+
~/.cargo/registry/cache/
84+
~/.cargo/git/db/
85+
target/
86+
key: ${{ runner.os }}-${{ inputs.cargo-cache-key }}-${{ hashFiles('**/Cargo.lock') }}
87+
restore-keys: |
88+
${{ runner.os }}-${{ inputs.cargo-cache-key }}
89+
${{ runner.os }}-${{ inputs.cargo-cache-fallback-key }}-${{ hashFiles('**/Cargo.lock') }}
90+
${{ runner.os }}-${{ inputs.cargo-cache-fallback-key }}
91+
92+
- name: Cache Local Cargo Dependencies
93+
if: ${{ inputs.cargo-cache-local-key }}
94+
uses: actions/cache@v4
95+
with:
96+
path: |
97+
.cargo/bin/
98+
.cargo/registry/index/
99+
.cargo/registry/cache/
100+
.cargo/git/db/
101+
key: ${{ runner.os }}-${{ inputs.cargo-cache-local-key }}-${{ hashFiles('**/Cargo.lock') }}
102+
restore-keys: ${{ runner.os }}-${{ inputs.cargo-cache-local-key }}
103+

.github/workflows/main.yml

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
name: Main
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
jobs:
10+
format_and_lint_js:
11+
name: Format & Lint JS
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Git Checkout
15+
uses: actions/checkout@v4
16+
17+
- name: Setup Environment
18+
uses: ./.github/actions/setup
19+
with:
20+
js: true
21+
22+
- name: Install Dependencies
23+
run: cd js && pnpm install --frozen-lockfile
24+
25+
- name: Format
26+
run: cd js && pnpm format
27+
28+
- name: Lint
29+
run: cd js && pnpm lint
30+
31+
test_js:
32+
name: Test JS
33+
runs-on: ubuntu-latest
34+
needs: format_and_lint_js
35+
steps:
36+
- name: Git Checkout
37+
uses: actions/checkout@v4
38+
39+
- name: Setup Environment
40+
uses: ./.github/actions/setup
41+
with:
42+
js: true
43+
44+
- name: Install Dependencies
45+
run: cd js && pnpm install --frozen-lockfile
46+
47+
- name: Build code
48+
run: cd js && pnpm build
49+
50+
- name: Run tests
51+
run: cd js && pnpm test
52+
53+
format_and_lint_rust:
54+
name: Format & Lint Rust
55+
runs-on: ubuntu-latest
56+
steps:
57+
- name: Git Checkout
58+
uses: actions/checkout@v4
59+
60+
- name: Setup Environment
61+
uses: ./.github/actions/setup
62+
with:
63+
clippy: true
64+
rustfmt: true
65+
cargo-cache-key: cargo-lint-tests
66+
cargo-cache-fallback-key: cargo-lint
67+
68+
- name: Format Rust
69+
run: cd rust && cargo fmt --check
70+
71+
- name: Lint Rust
72+
run: cd rust && cargo clippy
73+
74+
test_rust:
75+
name: Test Rust
76+
runs-on: ubuntu-latest
77+
needs: format_and_lint_rust
78+
steps:
79+
- name: Git Checkout
80+
uses: actions/checkout@v4
81+
82+
- name: Setup Environment
83+
uses: ./.github/actions/setup
84+
with:
85+
rust: true
86+
cargo-cache-key: cargo-rust-tests
87+
cargo-cache-fallback-key: cargo-rust
88+
89+
- name: Run tests
90+
run: cd rust && cargo test

js/examples/rpc.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ import { getStakeActivation } from 'solana-rpc-get-stake-activation';
55
const rpc = createSolanaRpc('https://api.testnet.solana.com');
66
let stake = '25R5p1Qoe4BWW4ru7MQSNxxAzdiPN7zAunpCuF8q5iTz';
77
let status = await getStakeActivation(rpc, stake as Address);
8+
console.log(status);

js/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"bugs": {
2323
"url": "https://github.com/solana-developers/solana-rpc-get-stake-activation/issues"
2424
},
25+
"packageManager": "[email protected]",
2526
"exports": {
2627
".": {
2728
"types": "./dist/types/index.d.ts",

js/src/rpc.ts

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,41 @@ export async function getStakeActivation(
1919
rpc: Rpc<SolanaRpcApi>,
2020
stakeAddress: Address
2121
): Promise<StakeActivation> {
22-
const stakeAccount = await fetchEncodedAccount(rpc, stakeAddress);
23-
assertAccountExists(stakeAccount);
24-
const stake = decodeAccount(stakeAccount, stakeAccountCodec);
25-
if (stake.data.discriminant === 0) {
26-
throw new Error('');
27-
}
28-
const rentExemptReserve = stake.data.meta.rentExemptReserve;
29-
if (stake.data.discriminant === 1) {
22+
const [epochInfo, stakeAccount, stakeHistory] = await Promise.all([
23+
rpc.getEpochInfo().send(),
24+
(async () => {
25+
const stakeAccountEncoded = await fetchEncodedAccount(rpc, stakeAddress);
26+
assertAccountExists(stakeAccountEncoded);
27+
const stakeAccount = decodeAccount(stakeAccountEncoded, stakeAccountCodec);
28+
if (stakeAccount.data.discriminant === 0) {
29+
throw new Error('');
30+
}
31+
return stakeAccount;
32+
})(),
33+
(async () => {
34+
const stakeHistoryAccountEncoded = await fetchEncodedAccount(
35+
rpc,
36+
SYSVAR_STAKE_HISTORY_ADDRESS
37+
);
38+
assertAccountExists(stakeHistoryAccountEncoded);
39+
const stakeHistory = decodeAccount(stakeHistoryAccountEncoded, stakeHistoryCodec);
40+
return stakeHistory;
41+
})(),
42+
]);
43+
44+
const rentExemptReserve = stakeAccount.data.meta.rentExemptReserve;
45+
if (stakeAccount.data.discriminant === 1) {
3046
return {
3147
status: 'inactive',
3248
active: BigInt(0),
33-
inactive: stake.lamports - rentExemptReserve,
49+
inactive: stakeAccount.lamports - rentExemptReserve,
3450
};
3551
}
3652

37-
const stakeHistoryAccount = await fetchEncodedAccount(
38-
rpc,
39-
SYSVAR_STAKE_HISTORY_ADDRESS
40-
);
41-
assertAccountExists(stakeHistoryAccount);
42-
const epochInfo = await rpc.getEpochInfo().send();
43-
const stakeHistory = decodeAccount(stakeHistoryAccount, stakeHistoryCodec);
44-
4553
// THE HARD PART
4654
const { effective, activating, deactivating } =
4755
getStakeActivatingAndDeactivating(
48-
stake.data.stake.delegation,
56+
stakeAccount.data.stake.delegation,
4957
epochInfo.epoch,
5058
stakeHistory.data
5159
);

rust/rust-toolchain.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[toolchain]
2+
channel = "1.78.0"
3+

web3js-1.0/.eslintrc.cjs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module.exports = {
2+
extends: ['@solana/eslint-config-solana'],
3+
ignorePatterns: ['.eslintrc.cjs', 'tsup.config.ts', 'env-shim.ts'],
4+
parserOptions: {
5+
project: 'tsconfig.json',
6+
tsconfigRootDir: __dirname,
7+
sourceType: 'module',
8+
},
9+
rules: {
10+
'@typescript-eslint/ban-types': 'off',
11+
'@typescript-eslint/sort-type-constituents': 'off',
12+
'prefer-destructuring': 'off',
13+
'simple-import-sort/imports': 'off',
14+
'sort-keys-fix/sort-keys-fix': 'off',
15+
'typescript-sort-keys/interface': 'off',
16+
},
17+
};

web3js-1.0/.prettierrc.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"semi": true,
3+
"singleQuote": true,
4+
"trailingComma": "es5",
5+
"useTabs": false,
6+
"tabWidth": 2,
7+
"arrowParens": "always",
8+
"printWidth": 80
9+
}

web3js-1.0/env-shim.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Clever obfuscation to prevent the build system from inlining the value of `NODE_ENV`
2+
export const __DEV__ = /* @__PURE__ */ (() =>
3+
(process as any)['en' + 'v'].NODE_ENV === 'development')();

web3js-1.0/examples/example.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { getStakeActivation } from 'solana-rpc-get-stake-activation';
2+
import { PublicKey } from "@solana/web3.js";
3+
import { Connection } from '@solana/web3.js';
4+
5+
const connection = new Connection('https://api.testnet.solana.com');
6+
let stake = new PublicKey('25R5p1Qoe4BWW4ru7MQSNxxAzdiPN7zAunpCuF8q5iTz');
7+
let status = await getStakeActivation(connection, stake);
8+
console.log(status);

0 commit comments

Comments
 (0)