Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 175 additions & 0 deletions examples/staking/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore

# Logs

logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Caches

.cache

# Diagnostic reports (https://nodejs.org/api/report.html)

report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json

# Runtime data

pids
_.pid
_.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover

lib-cov

# Coverage directory used by tools like istanbul

coverage
*.lcov

# nyc test coverage

.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)

.grunt

# Bower dependency directory (https://bower.io/)

bower_components

# node-waf configuration

.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)

build/Release

# Dependency directories

node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)

web_modules/

# TypeScript cache

*.tsbuildinfo

# Optional npm cache directory

.npm

# Optional eslint cache

.eslintcache

# Optional stylelint cache

.stylelintcache

# Microbundle cache

.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history

.node_repl_history

# Output of 'npm pack'

*.tgz

# Yarn Integrity file

.yarn-integrity

# dotenv environment variable files

.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)

.parcel-cache

# Next.js build output

.next
out

# Nuxt.js build / generate output

.nuxt
dist

# Gatsby files

# Comment in the public line in if your project uses Gatsby and not Next.js

# https://nextjs.org/blog/next-9-1#public-directory-support

# public

# vuepress build output

.vuepress/dist

# vuepress v2.x temp and cache directory

.temp

# Docusaurus cache and generated files

.docusaurus

# Serverless directories

.serverless/

# FuseBox cache

.fusebox/

# DynamoDB Local files

.dynamodb/

# TernJS port file

.tern-port

# Stores VSCode versions used for testing VSCode extensions

.vscode-test

# yarn v2

.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# IntelliJ based IDEs
.idea

# Finder (MacOS) folder config
.DS_Store
3 changes: 3 additions & 0 deletions examples/staking/.papi/descriptors/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*
!.gitignore
!package.json
24 changes: 24 additions & 0 deletions examples/staking/.papi/descriptors/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"version": "0.1.0-autogenerated.7015110913010675152",
"name": "@polkadot-api/descriptors",
"files": [
"dist"
],
"exports": {
".": {
"types": "./dist/index.d.ts",
"module": "./dist/index.mjs",
"import": "./dist/index.mjs",
"require": "./dist/index.js"
},
"./package.json": "./package.json"
},
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"browser": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"sideEffects": false,
"peerDependencies": {
"polkadot-api": ">=1.11.2"
}
}
Binary file added examples/staking/.papi/metadata/dot.scale
Binary file not shown.
12 changes: 12 additions & 0 deletions examples/staking/.papi/polkadot-api.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": 0,
"descriptorPath": ".papi/descriptors",
"entries": {
"dot": {
"chain": "polkadot",
"metadata": ".papi/metadata/dot.scale",
"genesis": "0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3",
"codeHash": "0x716bb701ec19452fa522cdaa83ef101d368ba2bcfd89fde9acb6f793a420a616"
}
}
}
15 changes: 15 additions & 0 deletions examples/staking/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# ink

To install dependencies:

```bash
bun install
```

To run:

```bash
bun run index.ts
```

This project was created using `bun init` in bun v1.1.18. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
47 changes: 47 additions & 0 deletions examples/staking/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { dot } from "@polkadot-api/descriptors"
import { createStakingSdk } from "@polkadot-api/sdk-staking"
import { createClient } from "polkadot-api"
import { withPolkadotSdkCompat } from "polkadot-api/polkadot-sdk-compat"
import { getWsProvider } from "polkadot-api/ws-provider/web"

const TARGET = "13UVJyLnbVp8c4FQeiGRMVBP7xph2wHCuf2RzvyxJomXJ7RL"

const client = createClient(
withPolkadotSdkCompat(getWsProvider("wss://rpc.ibp.network/polkadot")),
)
const api = client.getTypedApi(dot)
// console.log(await api.query.System.ParentHash.getValue())

const sdk = createStakingSdk(api)
// const info = await api.query.Staking.Bonded.getEntries()
// console.log(info.filter(({ keyArgs: [stash], value }) => stash !== value))

const previousEra = await api.query.Staking.ActiveEra.getValue().then(
(val) => val!.index - 1,
)

console.log("consulting era", previousEra)

console.log(
"nominator status",
await sdk.getNominatorActiveValidators(TARGET, previousEra),
)

const nominatorRewards = await sdk.getNominatorRewards(TARGET, previousEra)
console.log("nominator rewards", nominatorRewards)

const [blockTime, epochDuration, sessionsPerEra] = await Promise.all([
api.constants.Babe.ExpectedBlockTime(),
api.constants.Babe.EpochDuration(),
api.constants.Staking.SessionsPerEra(),
])
const eraDurationInMs = BigInt(sessionsPerEra) * epochDuration * blockTime
const erasInAYear = (365.25 * 24 * 60 * 60 * 1000) / Number(eraDurationInMs)

const rewardPct =
Number(nominatorRewards.total) / Number(nominatorRewards.activeBond)
const apy = Math.pow(1 + rewardPct, erasInAYear) - 1

console.log("APY", (apy * 100).toLocaleString() + "%")

process.exit(0)
17 changes: 17 additions & 0 deletions examples/staking/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "governance-example",
"module": "index.ts",
"type": "module",
"devDependencies": {
"@types/bun": "^1.2.15"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"dependencies": {
"@polkadot-api/descriptors": "file:.papi/descriptors",
"@polkadot-api/sdk-staking": "workspace:*",
"polkadot-api": "^1.13.1",
"rxjs": "7.8.2"
}
}
27 changes: 27 additions & 0 deletions examples/staking/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"compilerOptions": {
// Enable latest features
"lib": ["ESNext", "DOM"],
"target": "ESNext",
"module": "ESNext",
"moduleDetection": "force",
"jsx": "react-jsx",
"allowJs": true,

// Bundler mode
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,

// Best practices
"strict": true,
"skipLibCheck": true,
"noFallthroughCasesInSwitch": true,

// Some stricter flags (disabled by default)
"noUnusedLocals": false,
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false
}
}
7 changes: 7 additions & 0 deletions packages/sdk-accounts/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

## Unreleased

## 0.5.1 2025-10-10

### Fixed

- Identity SDK
- `getIdentities` doesn't resolve sub-identities.

## 0.5.0 2025-09-29

### Added
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk-accounts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@polkadot-api/sdk-accounts",
"version": "0.5.0",
"version": "0.5.1",
"sideEffects": false,
"author": "Victor Oliva (https://github.com/voliva)",
"repository": {
Expand Down
11 changes: 7 additions & 4 deletions packages/sdk-accounts/src/identity/identity-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,19 @@ export function createIdentitySdk(typedApi: IdentitySdkTypedApi): IdentitySdk {
])

const pendingSuperIdentities = Object.entries(identities)
.filter(([, identity], i) => !identity && supersOf[i])
.map(([key]): [SS58String] => [key])
.filter(([key, identity]) => !identity && supersOf[key])
.map(([child]) => ({
child,
super: supersOf[child]![0],
}))

if (pendingSuperIdentities.length) {
const superIdentities =
await typedApi.query.Identity.IdentityOf.getValues(
pendingSuperIdentities,
pendingSuperIdentities.map((v) => [v.super]),
)
superIdentities.forEach((v, i) => {
identities[pendingSuperIdentities[i][0]] = v
identities[pendingSuperIdentities[i].child] = v
? normalizeIdentityValue(v)
: v
})
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk-ink/src/sdk-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
type SS58String,
type TypedApi,
} from "polkadot-api"
import type { Passet, WndAh } from "../.papi/descriptors"
import type { Passet, WndAh } from "../.papi/descriptors/dist"
import type {
Gas,
GenericInkDescriptors,
Expand Down
3 changes: 3 additions & 0 deletions packages/sdk-staking/.papi/descriptors/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*
!.gitignore
!package.json
Loading