Skip to content

Commit

Permalink
Merge branch 'main' into package
Browse files Browse the repository at this point in the history
  • Loading branch information
joncinque authored Aug 28, 2024
2 parents 5ecc167 + 4bb15ba commit 0956a51
Show file tree
Hide file tree
Showing 20 changed files with 4,372 additions and 18 deletions.
103 changes: 103 additions & 0 deletions .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
name: Setup environment

inputs:
cargo-cache-key:
description: The key to cache cargo dependencies. Skips cargo caching if not provided.
required: false
cargo-cache-fallback-key:
description: The fallback key to use when caching cargo dependencies. Default to not using a fallback key.
required: false
cargo-cache-local-key:
description: The key to cache local cargo dependencies. Skips local cargo caching if not provided.
required: false
clippy:
description: Install Clippy if `true`. Defaults to `false`.
required: false
rustfmt:
description: Install Rustfmt if `true`. Defaults to `false`.
required: false
js:
description: Install pnpm and node if `true`. Defaults to `false`.
required: false
rust:
description: Install Rust if `true`. Defaults to `false`.
required: false

runs:
using: 'composite'
steps:
- name: Setup pnpm
if: ${{ inputs.js == 'true' }}
uses: pnpm/action-setup@v3
with:
package_json_file: 'js/package.json'

- name: Setup Node.js
if: ${{ inputs.js == 'true' }}
uses: actions/setup-node@v4
with:
node-version: 20
cache: 'pnpm'
cache-dependency-path: 'js/pnpm-lock.yaml'

- name: Install Rustfmt
if: ${{ inputs.rustfmt == 'true' }}
uses: dtolnay/rust-toolchain@master
with:
toolchain: "1.78.0"
components: rustfmt

- name: Install Clippy
if: ${{ inputs.clippy == 'true' }}
uses: dtolnay/rust-toolchain@master
with:
toolchain: "1.78.0"
components: clippy

- name: Install Rust
if: ${{ inputs.rust == 'true' }}
uses: dtolnay/rust-toolchain@master
with:
toolchain: "1.78.0"

- name: Cache Cargo Dependencies
if: ${{ inputs.cargo-cache-key && !inputs.cargo-cache-fallback-key }}
uses: actions/cache@v4
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-${{ inputs.cargo-cache-key }}-${{ hashFiles('**/Cargo.lock') }}
restore-keys: ${{ runner.os }}-${{ inputs.cargo-cache-key }}

- name: Cache Cargo Dependencies With Fallback
if: ${{ inputs.cargo-cache-key && inputs.cargo-cache-fallback-key }}
uses: actions/cache@v4
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-${{ inputs.cargo-cache-key }}-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-${{ inputs.cargo-cache-key }}
${{ runner.os }}-${{ inputs.cargo-cache-fallback-key }}-${{ hashFiles('**/Cargo.lock') }}
${{ runner.os }}-${{ inputs.cargo-cache-fallback-key }}
- name: Cache Local Cargo Dependencies
if: ${{ inputs.cargo-cache-local-key }}
uses: actions/cache@v4
with:
path: |
.cargo/bin/
.cargo/registry/index/
.cargo/registry/cache/
.cargo/git/db/
key: ${{ runner.os }}-${{ inputs.cargo-cache-local-key }}-${{ hashFiles('**/Cargo.lock') }}
restore-keys: ${{ runner.os }}-${{ inputs.cargo-cache-local-key }}

90 changes: 90 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
name: Main

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
format_and_lint_js:
name: Format & Lint JS
runs-on: ubuntu-latest
steps:
- name: Git Checkout
uses: actions/checkout@v4

- name: Setup Environment
uses: ./.github/actions/setup
with:
js: true

- name: Install Dependencies
run: cd js && pnpm install --frozen-lockfile

- name: Format
run: cd js && pnpm format

- name: Lint
run: cd js && pnpm lint

test_js:
name: Test JS
runs-on: ubuntu-latest
needs: format_and_lint_js
steps:
- name: Git Checkout
uses: actions/checkout@v4

- name: Setup Environment
uses: ./.github/actions/setup
with:
js: true

- name: Install Dependencies
run: cd js && pnpm install --frozen-lockfile

- name: Build code
run: cd js && pnpm build

- name: Run tests
run: cd js && pnpm test

format_and_lint_rust:
name: Format & Lint Rust
runs-on: ubuntu-latest
steps:
- name: Git Checkout
uses: actions/checkout@v4

- name: Setup Environment
uses: ./.github/actions/setup
with:
clippy: true
rustfmt: true
cargo-cache-key: cargo-lint-tests
cargo-cache-fallback-key: cargo-lint

- name: Format Rust
run: cd rust && cargo fmt --check

- name: Lint Rust
run: cd rust && cargo clippy

test_rust:
name: Test Rust
runs-on: ubuntu-latest
needs: format_and_lint_rust
steps:
- name: Git Checkout
uses: actions/checkout@v4

- name: Setup Environment
uses: ./.github/actions/setup
with:
rust: true
cargo-cache-key: cargo-rust-tests
cargo-cache-fallback-key: cargo-rust

- name: Run tests
run: cd rust && cargo test
1 change: 1 addition & 0 deletions js/examples/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ import { getStakeActivation } from 'solana-rpc-get-stake-activation';
const rpc = createSolanaRpc('https://api.testnet.solana.com');
let stake = '25R5p1Qoe4BWW4ru7MQSNxxAzdiPN7zAunpCuF8q5iTz';
let status = await getStakeActivation(rpc, stake as Address);
console.log(status);
1 change: 1 addition & 0 deletions js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"bugs": {
"url": "https://github.com/solana-developers/solana-rpc-get-stake-activation/issues"
},
"packageManager": "[email protected]",
"exports": {
".": {
"types": "./dist/types/index.d.ts",
Expand Down
44 changes: 26 additions & 18 deletions js/src/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,41 @@ export async function getStakeActivation(
rpc: Rpc<SolanaRpcApi>,
stakeAddress: Address
): Promise<StakeActivation> {
const stakeAccount = await fetchEncodedAccount(rpc, stakeAddress);
assertAccountExists(stakeAccount);
const stake = decodeAccount(stakeAccount, stakeAccountCodec);
if (stake.data.discriminant === 0) {
throw new Error('');
}
const rentExemptReserve = stake.data.meta.rentExemptReserve;
if (stake.data.discriminant === 1) {
const [epochInfo, stakeAccount, stakeHistory] = await Promise.all([
rpc.getEpochInfo().send(),
(async () => {
const stakeAccountEncoded = await fetchEncodedAccount(rpc, stakeAddress);
assertAccountExists(stakeAccountEncoded);
const stakeAccount = decodeAccount(stakeAccountEncoded, stakeAccountCodec);
if (stakeAccount.data.discriminant === 0) {
throw new Error('');
}
return stakeAccount;
})(),
(async () => {
const stakeHistoryAccountEncoded = await fetchEncodedAccount(
rpc,
SYSVAR_STAKE_HISTORY_ADDRESS
);
assertAccountExists(stakeHistoryAccountEncoded);
const stakeHistory = decodeAccount(stakeHistoryAccountEncoded, stakeHistoryCodec);
return stakeHistory;
})(),
]);

const rentExemptReserve = stakeAccount.data.meta.rentExemptReserve;
if (stakeAccount.data.discriminant === 1) {
return {
status: 'inactive',
active: BigInt(0),
inactive: stake.lamports - rentExemptReserve,
inactive: stakeAccount.lamports - rentExemptReserve,
};
}

const stakeHistoryAccount = await fetchEncodedAccount(
rpc,
SYSVAR_STAKE_HISTORY_ADDRESS
);
assertAccountExists(stakeHistoryAccount);
const epochInfo = await rpc.getEpochInfo().send();
const stakeHistory = decodeAccount(stakeHistoryAccount, stakeHistoryCodec);

// THE HARD PART
const { effective, activating, deactivating } =
getStakeActivatingAndDeactivating(
stake.data.stake.delegation,
stakeAccount.data.stake.delegation,
epochInfo.epoch,
stakeHistory.data
);
Expand Down
3 changes: 3 additions & 0 deletions rust/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[toolchain]
channel = "1.78.0"

17 changes: 17 additions & 0 deletions web3js-1.0/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
extends: ['@solana/eslint-config-solana'],
ignorePatterns: ['.eslintrc.cjs', 'tsup.config.ts', 'env-shim.ts'],
parserOptions: {
project: 'tsconfig.json',
tsconfigRootDir: __dirname,
sourceType: 'module',
},
rules: {
'@typescript-eslint/ban-types': 'off',
'@typescript-eslint/sort-type-constituents': 'off',
'prefer-destructuring': 'off',
'simple-import-sort/imports': 'off',
'sort-keys-fix/sort-keys-fix': 'off',
'typescript-sort-keys/interface': 'off',
},
};
9 changes: 9 additions & 0 deletions web3js-1.0/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"semi": true,
"singleQuote": true,
"trailingComma": "es5",
"useTabs": false,
"tabWidth": 2,
"arrowParens": "always",
"printWidth": 80
}
3 changes: 3 additions & 0 deletions web3js-1.0/env-shim.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Clever obfuscation to prevent the build system from inlining the value of `NODE_ENV`
export const __DEV__ = /* @__PURE__ */ (() =>
(process as any)['en' + 'v'].NODE_ENV === 'development')();
8 changes: 8 additions & 0 deletions web3js-1.0/examples/example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { getStakeActivation } from 'solana-rpc-get-stake-activation';
import { PublicKey } from "@solana/web3.js";
import { Connection } from '@solana/web3.js';

const connection = new Connection('https://api.testnet.solana.com');
let stake = new PublicKey('25R5p1Qoe4BWW4ru7MQSNxxAzdiPN7zAunpCuF8q5iTz');
let status = await getStakeActivation(connection, stake);
console.log(status);
62 changes: 62 additions & 0 deletions web3js-1.0/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"name": "solana-rpc-get-stake-activation",
"version": "1.0.0",
"description": "Client-side replica of Solana RPC's getStakeActivation",
"sideEffects": false,
"module": "./dist/src/index.mjs",
"main": "./dist/src/index.js",
"types": "./dist/types/index.d.ts",
"type": "commonjs",
"keywords": [],
"author": "",
"license": "Apache-2.0",
"exports": {
".": {
"types": "./dist/types/index.d.ts",
"import": "./dist/src/index.mjs",
"require": "./dist/src/index.js"
}
},
"files": [
"./dist/src",
"./dist/types"
],
"scripts": {
"build": "rimraf dist && tsup && tsc -p ./tsconfig.declarations.json",
"build:docs": "typedoc",
"test": "ava",
"lint": "eslint --ext js,ts,tsx src",
"lint:fix": "eslint --fix --ext js,ts,tsx src",
"format": "prettier --check src test",
"format:fix": "prettier --write src test",
"prepublishOnly": "pnpm build"
},
"dependencies": {
"@solana/web3.js": "^1.95.3"
},
"devDependencies": {
"@ava/typescript": "^4.1.0",
"@types/node": "^20",
"@typescript-eslint/eslint-plugin": "^7.16.1",
"@typescript-eslint/parser": "^7.16.1",
"ava": "^6.1.3",
"eslint": "^8.57.0",
"prettier": "^3.3.3",
"rimraf": "^5.0.5",
"tsup": "^8.1.2",
"typedoc": "^0.25.12",
"typedoc-plugin-missing-exports": "^2.2.0",
"typescript": "^5.5.3"
},
"ava": {
"nodeArguments": [
"--no-warnings"
],
"typescript": {
"compile": false,
"rewritePaths": {
"test/": "dist/test/"
}
}
}
}
Loading

0 comments on commit 0956a51

Please sign in to comment.