Skip to content

Commit

Permalink
update api setup
Browse files Browse the repository at this point in the history
  • Loading branch information
nohaapav committed Dec 22, 2024
1 parent cbddc18 commit f2dcc85
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.0",
"description": "",
"main": "src/bot.js",
"type": "module",
"scripts": {
"test": "node --experimental-vm-modules ./node_modules/.bin/jest --detectOpenHandles",
"start": "node src/bot.js",
Expand All @@ -21,7 +22,6 @@
"url": "https://github.com/galacticcouncil/snakewatch/issues"
},
"homepage": "https://github.com/galacticcouncil/snakewatch#readme",
"type": "module",
"jest": {
"transform": {}
},
Expand Down
20 changes: 14 additions & 6 deletions src/api.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {ApiPromise, WsProvider} from "@polkadot/api";
import {PoolService, TradeRouter} from "@galacticcouncil/sdk";


let initialized = false;
let synced = false;
let ready = false;
let _api;
let _poolService;
let _router;
let provider;

export async function initApi(rpc) {
Expand All @@ -21,14 +21,19 @@ export async function initApi(rpc) {
export async function initSdk(api) {
_poolService = new PoolService(api);
await _poolService.syncRegistry();
synced = true;
_router = new TradeRouter(_poolService);
ready = true;
}

export async function disconnect() {
await _api.disconnect();
await _poolService.disconnect();
provider = null;
_api = null;
_poolService = null;
_router = null;
initialized = false;
ready = false;
}

export function api() {
Expand All @@ -39,8 +44,11 @@ export function api() {
}

export function sdk() {
if (!synced || !_poolService) {
throw new Error('router not synced');
if (!ready || !_router) {
throw new Error('router not ready');
}
return new TradeRouter(_poolService);
return {
router: _router,
poolService: _poolService
};
}
2 changes: 1 addition & 1 deletion src/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ async function main() {
}
}

const pools = await sdk().getPools();
const pools = await sdk().router.getPools();
console.log(pools);

console.log('watching for new blocks');
Expand Down

0 comments on commit f2dcc85

Please sign in to comment.