Skip to content

Commit 5207b22

Browse files
Merge pull request #6 from subspace/3-setup-basic-function-to-connect-to-the-rpc-consensus
Setup basic function to connect to the rpc consensus
2 parents b716d0a + 806c0c3 commit 5207b22

25 files changed

+5373
-725
lines changed

.github/workflows/build.yaml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Build
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
build-and-test:
7+
runs-on: ubuntu-latest
8+
name: Build all packages and run tests
9+
steps:
10+
- name: Checkout 🛎️
11+
uses: actions/checkout@v3
12+
13+
- name: Set up Node.js
14+
uses: actions/setup-node@v2
15+
with:
16+
node-version: '18'
17+
18+
- name: Enable Corepack
19+
run: corepack enable
20+
21+
- name: Set Yarn version to Berry
22+
run: corepack prepare yarn@4.2.2 --activate
23+
24+
- name: Install dependencies
25+
run: yarn install
26+
27+
- name: Build auto-utils package 🔧
28+
run: yarn workspace @autonomys/auto-utils build
29+
30+
- name: Build all packages 🔧
31+
run: yarn build
32+
33+
- name: Run tests 🧪
34+
run: yarn test

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
node_modules/
33
packages/*/node_modules/
44

5+
# Yarn cache
6+
.yarn/*
7+
.yarnrc.yml
8+
59
# TypeScript build outputs
610
dist/
711
packages/*/dist/

.vscode/auto.code-workspace

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@
22
"folders": [
33
{
44
"name": "root",
5-
"path": ".."
5+
"path": "..",
66
},
77
{
88
"name": "auto-utils",
9-
"path": "../packages/auto-utils"
9+
"path": "../packages/auto-utils",
1010
},
1111
{
1212
"name": "auto-consensus",
13-
"path": "../packages/auto-consensus"
13+
"path": "../packages/auto-consensus",
1414
},
1515
{
1616
"name": "auto-id",
17-
"path": "../packages/auto-id"
18-
}
17+
"path": "../packages/auto-id",
18+
},
1919
],
2020
"settings": {
2121
"editor.codeActionsOnSave": {
2222
"source.sortImports": "explicit",
2323
"source.fixAll.eslint": "explicit",
24-
"source.fixAll.tslint": "explicit"
24+
"source.fixAll.tslint": "explicit",
2525
},
2626
"editor.defaultFormatter": "esbenp.prettier-vscode",
2727
"javascript.updateImportsOnFileMove.enabled": "always",
@@ -31,6 +31,7 @@
3131
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": true,
3232
"editor.formatOnSave": true,
3333
"cSpell.words": [
34+
"autonomys",
3435
"extrinsics",
3536
"gemini",
3637
"graphiql",
@@ -39,7 +40,7 @@
3940
"subscan",
4041
"subspace",
4142
"subsquid",
42-
"typegen"
43-
]
44-
}
43+
"typegen",
44+
],
45+
},
4546
}

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": true,
1313
"editor.formatOnSave": true,
1414
"cSpell.words": [
15+
"autonomys",
1516
"extrinsics",
1617
"gemini",
1718
"graphiql",

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Autonomys Auto SDK Monorepo
2+
3+
This monorepo contains multiple packages for the Autonomys Auto SDK, including utility, consensus and identity functions.
4+
5+
## Structure
6+
7+
The repository is organized as follows:
8+
9+
- `packages/auto-utils`: Utility functions for the SDK.
10+
- `packages/auto-consensus`: Consensus functions.
11+
- `packages/auto-id`: Identity functions.
12+
13+
## Requirements
14+
15+
- Node.js
16+
- Yarn 2 (Berry) or later
17+
18+
## Setup
19+
20+
1. **Clone the repository:**
21+
22+
`git clone https://github.com/subspace/auto-sdk.git`
23+
24+
2. **Navigate to the project directory:**
25+
26+
`cd auto-sdk`
27+
28+
3. **Set Yarn to use the Berry version:**
29+
30+
`yarn set version berry`
31+
32+
4. **Install dependencies:**
33+
34+
`yarn install`
35+
36+
## Scripts
37+
38+
### Build
39+
40+
To build all packages:
41+
42+
`yarn build`
43+
44+
### Test
45+
46+
To run tests for all packages:
47+
48+
`yarn test`
49+
50+
## Workspaces
51+
52+
This project uses Yarn workspaces. Packages are located in the `packages` directory. Each package can have its own dependencies and build scripts.
53+
54+
## License
55+
56+
This project is licensed under the MIT License. See the LICENSE file for more details.

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
"packages/*"
88
],
99
"scripts": {
10-
"build": "yarn workspaces run build",
11-
"test": "yarn workspaces run test"
10+
"build": "yarn workspaces foreach --all run build",
11+
"test": "yarn workspaces foreach --all run test"
1212
},
1313
"devDependencies": {
1414
"eslint": "^8.57.0",
1515
"prettier": "^3.2.5",
1616
"typescript": "^5.4.5"
17-
}
17+
},
18+
"packageManager": "yarn@4.2.2"
1819
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { activate, disconnect } from '@autonomys/auto-utils'
2+
import { totalIssuance } from '../src/balances'
3+
4+
describe('Verify balances functions', () => {
5+
beforeAll(async () => {
6+
await activate()
7+
})
8+
9+
afterAll(async () => {
10+
await disconnect()
11+
})
12+
13+
test('Check totalIssuance return a number greater than zero', async () => {
14+
// totalIssuance is an async function that returns a hex number as a string
15+
const rawIssuance = await totalIssuance()
16+
// Convert the hex number to a BigInt
17+
const issuance = BigInt(rawIssuance.toString())
18+
// Check if the issuance is greater than zero
19+
expect(issuance).toBeGreaterThan(BigInt(0))
20+
})
21+
})
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { activate, disconnect } from '@autonomys/auto-utils'
2+
import { currentTimestamp } from '../src/info'
3+
4+
describe('Verify info functions', () => {
5+
beforeAll(async () => {
6+
await activate()
7+
})
8+
9+
afterAll(async () => {
10+
await disconnect()
11+
})
12+
13+
test('Check timestamp return a number greater than zero', async () => {
14+
// totalIssuance is an async function that returns a hex number as a string
15+
const rawTimestamp = await currentTimestamp()
16+
// Convert the hex number to a BigInt
17+
const timestamp = BigInt(rawTimestamp.toString())
18+
// Check if the issuance is greater than zero
19+
expect(timestamp).toBeGreaterThan(BigInt(0))
20+
})
21+
})
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/** @type {import('ts-jest').JestConfigWithTsJest} */
2+
module.exports = {
3+
preset: 'ts-jest',
4+
testEnvironment: 'node',
5+
};

packages/auto-consensus/package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,16 @@
33
"version": "0.1.0",
44
"main": "dist/index.js",
55
"scripts": {
6-
"build": "tsc"
6+
"build": "tsc",
7+
"test": "jest"
8+
},
9+
"dependencies": {
10+
"@autonomys/auto-utils": "workspace:*"
711
},
812
"devDependencies": {
13+
"@types/jest": "^29.5.12",
14+
"jest": "^29.7.0",
15+
"ts-jest": "^29.1.4",
916
"typescript": "^5.4.5"
1017
}
1118
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { activate } from '@autonomys/auto-utils'
2+
3+
export const totalIssuance = async (networkId?: string) => {
4+
// Get the api instance for the network
5+
const api = await activate({ networkId })
6+
7+
// Get the current total token issuance
8+
const totalIssuance = await api.query.balances.totalIssuance()
9+
10+
return totalIssuance
11+
}

packages/auto-consensus/src/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
export const helloWorld = () => {
2-
console.log('Hello World');
3-
};
1+
export * from './balances'
2+
export * from './info'

packages/auto-consensus/src/info.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { activate } from '@autonomys/auto-utils'
2+
3+
export const currentTimestamp = async (networkId?: string) => {
4+
// Get the api instance for the network
5+
const api = await activate({ networkId })
6+
7+
// Get the current timestamp
8+
const timestamp = await api.query.timestamp.now()
9+
10+
return timestamp
11+
}

packages/auto-consensus/tsconfig.json

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
{
2-
"extends": "../../tsconfig.json",
3-
"compilerOptions": {
4-
"outDir": "./dist",
5-
"rootDir": "./src"
6-
},
7-
"include": ["src/**/*"],
8-
9-
}
10-
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "./dist",
5+
"rootDir": "./src"
6+
},
7+
"include": ["src/**/*"]
8+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { defaultNetwork, networks } from '../src/constants/network'
2+
import { getNetworkDetails, getNetworkRpcUrls } from '../src/network'
3+
4+
describe('Verify network functions', () => {
5+
const TEST_NETWORK = { networkId: networks[0].id }
6+
const TEST_INVALID_NETWORK = { networkId: 'invalid-network' }
7+
8+
test('Check getNetworkDetails return all network detail', async () => {
9+
const network = getNetworkDetails()
10+
expect(network).toEqual(defaultNetwork)
11+
})
12+
13+
test('Check getNetworkRpcUrls return the network urls', async () => {
14+
const rpcUrls = getNetworkRpcUrls()
15+
expect(rpcUrls).toEqual(defaultNetwork.rpcUrls)
16+
})
17+
18+
test('Check getNetworkDetails return the network detail for a specific network', async () => {
19+
const rpcUrls = getNetworkDetails(TEST_NETWORK)
20+
expect(rpcUrls).toEqual(networks[0])
21+
})
22+
23+
test('Check getNetworkRpcUrls return the network urls for a specific network', async () => {
24+
const rpcUrls = getNetworkRpcUrls(TEST_NETWORK)
25+
expect(rpcUrls).toEqual(networks[0].rpcUrls)
26+
})
27+
28+
test('Check getNetworkDetails return the network urls for an invalid network', async () => {
29+
expect(() => getNetworkDetails(TEST_INVALID_NETWORK)).toThrow(
30+
'Network with id invalid-network not found',
31+
)
32+
})
33+
34+
test('Check getNetworkRpcUrls return the network urls for an invalid network', async () => {
35+
expect(() => getNetworkRpcUrls(TEST_INVALID_NETWORK)).toThrow(
36+
'Network with id invalid-network not found',
37+
)
38+
})
39+
})

packages/auto-utils/jest.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/** @type {import('ts-jest').JestConfigWithTsJest} */
2+
module.exports = {
3+
preset: 'ts-jest',
4+
testEnvironment: 'node',
5+
};

packages/auto-utils/package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,16 @@
33
"version": "0.1.0",
44
"main": "dist/index.js",
55
"scripts": {
6-
"build": "tsc"
6+
"build": "tsc",
7+
"test": "jest"
8+
},
9+
"dependencies": {
10+
"@polkadot/api": "^11.2.1"
711
},
812
"devDependencies": {
13+
"@types/jest": "^29.5.12",
14+
"jest": "^29.7.0",
15+
"ts-jest": "^29.1.4",
916
"typescript": "^5.4.5"
1017
}
1118
}

0 commit comments

Comments
 (0)