Skip to content

Commit

Permalink
Merge pull request #6 from subspace/3-setup-basic-function-to-connect…
Browse files Browse the repository at this point in the history
…-to-the-rpc-consensus

Setup basic function to connect to the rpc consensus
  • Loading branch information
marc-aurele-besner authored Jun 4, 2024
2 parents b716d0a + 806c0c3 commit 5207b22
Show file tree
Hide file tree
Showing 25 changed files with 5,373 additions and 725 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Build

on: [push, pull_request]

jobs:
build-and-test:
runs-on: ubuntu-latest
name: Build all packages and run tests
steps:
- name: Checkout 🛎️
uses: actions/checkout@v3

- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '18'

- name: Enable Corepack
run: corepack enable

- name: Set Yarn version to Berry
run: corepack prepare [email protected] --activate

- name: Install dependencies
run: yarn install

- name: Build auto-utils package 🔧
run: yarn workspace @autonomys/auto-utils build

- name: Build all packages 🔧
run: yarn build

- name: Run tests 🧪
run: yarn test
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
node_modules/
packages/*/node_modules/

# Yarn cache
.yarn/*
.yarnrc.yml

# TypeScript build outputs
dist/
packages/*/dist/
Expand Down
19 changes: 10 additions & 9 deletions .vscode/auto.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@
"folders": [
{
"name": "root",
"path": ".."
"path": "..",
},
{
"name": "auto-utils",
"path": "../packages/auto-utils"
"path": "../packages/auto-utils",
},
{
"name": "auto-consensus",
"path": "../packages/auto-consensus"
"path": "../packages/auto-consensus",
},
{
"name": "auto-id",
"path": "../packages/auto-id"
}
"path": "../packages/auto-id",
},
],
"settings": {
"editor.codeActionsOnSave": {
"source.sortImports": "explicit",
"source.fixAll.eslint": "explicit",
"source.fixAll.tslint": "explicit"
"source.fixAll.tslint": "explicit",
},
"editor.defaultFormatter": "esbenp.prettier-vscode",
"javascript.updateImportsOnFileMove.enabled": "always",
Expand All @@ -31,6 +31,7 @@
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": true,
"editor.formatOnSave": true,
"cSpell.words": [
"autonomys",
"extrinsics",
"gemini",
"graphiql",
Expand All @@ -39,7 +40,7 @@
"subscan",
"subspace",
"subsquid",
"typegen"
]
}
"typegen",
],
},
}
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": true,
"editor.formatOnSave": true,
"cSpell.words": [
"autonomys",
"extrinsics",
"gemini",
"graphiql",
Expand Down
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Autonomys Auto SDK Monorepo

This monorepo contains multiple packages for the Autonomys Auto SDK, including utility, consensus and identity functions.

## Structure

The repository is organized as follows:

- `packages/auto-utils`: Utility functions for the SDK.
- `packages/auto-consensus`: Consensus functions.
- `packages/auto-id`: Identity functions.

## Requirements

- Node.js
- Yarn 2 (Berry) or later

## Setup

1. **Clone the repository:**

`git clone https://github.com/subspace/auto-sdk.git`

2. **Navigate to the project directory:**

`cd auto-sdk`

3. **Set Yarn to use the Berry version:**

`yarn set version berry`

4. **Install dependencies:**

`yarn install`

## Scripts

### Build

To build all packages:

`yarn build`

### Test

To run tests for all packages:

`yarn test`

## Workspaces

This project uses Yarn workspaces. Packages are located in the `packages` directory. Each package can have its own dependencies and build scripts.

## License

This project is licensed under the MIT License. See the LICENSE file for more details.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
"packages/*"
],
"scripts": {
"build": "yarn workspaces run build",
"test": "yarn workspaces run test"
"build": "yarn workspaces foreach --all run build",
"test": "yarn workspaces foreach --all run test"
},
"devDependencies": {
"eslint": "^8.57.0",
"prettier": "^3.2.5",
"typescript": "^5.4.5"
}
},
"packageManager": "[email protected]"
}
21 changes: 21 additions & 0 deletions packages/auto-consensus/__test__/balances.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { activate, disconnect } from '@autonomys/auto-utils'
import { totalIssuance } from '../src/balances'

describe('Verify balances functions', () => {
beforeAll(async () => {
await activate()
})

afterAll(async () => {
await disconnect()
})

test('Check totalIssuance return a number greater than zero', async () => {
// totalIssuance is an async function that returns a hex number as a string
const rawIssuance = await totalIssuance()
// Convert the hex number to a BigInt
const issuance = BigInt(rawIssuance.toString())
// Check if the issuance is greater than zero
expect(issuance).toBeGreaterThan(BigInt(0))
})
})
21 changes: 21 additions & 0 deletions packages/auto-consensus/__test__/info.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { activate, disconnect } from '@autonomys/auto-utils'
import { currentTimestamp } from '../src/info'

describe('Verify info functions', () => {
beforeAll(async () => {
await activate()
})

afterAll(async () => {
await disconnect()
})

test('Check timestamp return a number greater than zero', async () => {
// totalIssuance is an async function that returns a hex number as a string
const rawTimestamp = await currentTimestamp()
// Convert the hex number to a BigInt
const timestamp = BigInt(rawTimestamp.toString())
// Check if the issuance is greater than zero
expect(timestamp).toBeGreaterThan(BigInt(0))
})
})
5 changes: 5 additions & 0 deletions packages/auto-consensus/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
9 changes: 8 additions & 1 deletion packages/auto-consensus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
"version": "0.1.0",
"main": "dist/index.js",
"scripts": {
"build": "tsc"
"build": "tsc",
"test": "jest"
},
"dependencies": {
"@autonomys/auto-utils": "workspace:*"
},
"devDependencies": {
"@types/jest": "^29.5.12",
"jest": "^29.7.0",
"ts-jest": "^29.1.4",
"typescript": "^5.4.5"
}
}
11 changes: 11 additions & 0 deletions packages/auto-consensus/src/balances.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { activate } from '@autonomys/auto-utils'

export const totalIssuance = async (networkId?: string) => {
// Get the api instance for the network
const api = await activate({ networkId })

// Get the current total token issuance
const totalIssuance = await api.query.balances.totalIssuance()

return totalIssuance
}
5 changes: 2 additions & 3 deletions packages/auto-consensus/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export const helloWorld = () => {
console.log('Hello World');
};
export * from './balances'
export * from './info'
11 changes: 11 additions & 0 deletions packages/auto-consensus/src/info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { activate } from '@autonomys/auto-utils'

export const currentTimestamp = async (networkId?: string) => {
// Get the api instance for the network
const api = await activate({ networkId })

// Get the current timestamp
const timestamp = await api.query.timestamp.now()

return timestamp
}
16 changes: 7 additions & 9 deletions packages/auto-consensus/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"rootDir": "./src"
},
"include": ["src/**/*"],

}

"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"rootDir": "./src"
},
"include": ["src/**/*"]
}
39 changes: 39 additions & 0 deletions packages/auto-utils/__test__/network.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { defaultNetwork, networks } from '../src/constants/network'
import { getNetworkDetails, getNetworkRpcUrls } from '../src/network'

describe('Verify network functions', () => {
const TEST_NETWORK = { networkId: networks[0].id }
const TEST_INVALID_NETWORK = { networkId: 'invalid-network' }

test('Check getNetworkDetails return all network detail', async () => {
const network = getNetworkDetails()
expect(network).toEqual(defaultNetwork)
})

test('Check getNetworkRpcUrls return the network urls', async () => {
const rpcUrls = getNetworkRpcUrls()
expect(rpcUrls).toEqual(defaultNetwork.rpcUrls)
})

test('Check getNetworkDetails return the network detail for a specific network', async () => {
const rpcUrls = getNetworkDetails(TEST_NETWORK)
expect(rpcUrls).toEqual(networks[0])
})

test('Check getNetworkRpcUrls return the network urls for a specific network', async () => {
const rpcUrls = getNetworkRpcUrls(TEST_NETWORK)
expect(rpcUrls).toEqual(networks[0].rpcUrls)
})

test('Check getNetworkDetails return the network urls for an invalid network', async () => {
expect(() => getNetworkDetails(TEST_INVALID_NETWORK)).toThrow(
'Network with id invalid-network not found',
)
})

test('Check getNetworkRpcUrls return the network urls for an invalid network', async () => {
expect(() => getNetworkRpcUrls(TEST_INVALID_NETWORK)).toThrow(
'Network with id invalid-network not found',
)
})
})
5 changes: 5 additions & 0 deletions packages/auto-utils/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
9 changes: 8 additions & 1 deletion packages/auto-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
"version": "0.1.0",
"main": "dist/index.js",
"scripts": {
"build": "tsc"
"build": "tsc",
"test": "jest"
},
"dependencies": {
"@polkadot/api": "^11.2.1"
},
"devDependencies": {
"@types/jest": "^29.5.12",
"jest": "^29.7.0",
"ts-jest": "^29.1.4",
"typescript": "^5.4.5"
}
}
Loading

0 comments on commit 5207b22

Please sign in to comment.