diff --git a/api/src/index.ts b/api/src/index.ts deleted file mode 100644 index e69de29..0000000 diff --git a/api/src/oracal/example.test.ts b/api/src/oracal/example.test.ts new file mode 100644 index 0000000..d61d82d --- /dev/null +++ b/api/src/oracal/example.test.ts @@ -0,0 +1,35 @@ +import { createTestPairs } from '@polkadot/keyring/testingPairs'; +import { config, logger } from '../utils'; +import { nodeProvider } from '../utils'; +import { feedValues } from './feedValues'; +import { getValue } from './getValues'; + + +/// import keypart from menonic +// import { Keyring } from '@polkadot/api'; +// const keyring = new Keyring({ type: 'sr25519' }); +// // Some mnemonic phrase +// const PHRASE = 'entire material egg meadow latin bargain dutch coral blood melt acoustic thought'; +// // Add an account, straight mnemonic +// const newPair = keyring.addFromUri(PHRASE); +// test keypair alice/bob/ + +const testPairs = createTestPairs(); +const aclice = testPairs.alice; + +const testOracle = async () => { +// await feedValues(nodeProvider, aclice, 'SEL', 240_000_000_000); + const data = await getValue(nodeProvider, 'KUSD'); + console.log(data) +}; + +Promise.resolve() + .then(async () => { + await nodeProvider.initializeProviders(); + }) + .then(testOracle) + .then(async () => { + await nodeProvider.closeProviders(); + logger.info('Finished'); + process.exit(); + }) diff --git a/api/src/oracal/feedValues.ts b/api/src/oracal/feedValues.ts new file mode 100644 index 0000000..d336f78 --- /dev/null +++ b/api/src/oracal/feedValues.ts @@ -0,0 +1,33 @@ +import type { KeyringPair } from '@polkadot/keyring/types'; +import { RewriteFrames } from '@sentry/integrations'; +import * as Sentry from '@sentry/node'; +import { config, logger, NodeProviderType } from '../utils'; + +Sentry.init({ + dsn: config.sentryDns, + tracesSampleRate: 1.0, + integrations: [ + new RewriteFrames({ + root: global.__dirname, + }), + ], +}); + +export const feedValues = async (nodeProvider: NodeProviderType, pairs: KeyringPair, token: string, price: number) => { + try { + const provider = nodeProvider.getProvider(); + return new Promise((resolve) => { + provider.api.tx.selendraOracle + .feedValues([[{ Token: token }, price]]) + .signAndSend(pairs, (result: any) => { + if (result.status.isFinalized || result.status.isInBlock) { + resolve(undefined); + } + }); + }); + } catch (error) { + logger.error(error); + Sentry.captureException(error); + } +} + diff --git a/api/src/oracal/getValues.ts b/api/src/oracal/getValues.ts new file mode 100644 index 0000000..c201921 --- /dev/null +++ b/api/src/oracal/getValues.ts @@ -0,0 +1,25 @@ +import { RewriteFrames } from '@sentry/integrations'; +import * as Sentry from '@sentry/node'; +import { config, logger, NodeProviderType } from '../utils'; + +Sentry.init({ + dsn: config.sentryDns, + tracesSampleRate: 1.0, + integrations: [ + new RewriteFrames({ + root: global.__dirname, + }), + ], +}); + + +export const getValue = async (nodeProvider: NodeProviderType, token: string,) => { + try { + const provider = nodeProvider.getProvider(); + const value = await provider.api.query.selendraOracle.values({ Token: token }); + return value.toString() ? value.toString() : '{"value": 0, "timestamp": 0}' + } catch (error) { + logger.error(error); + Sentry.captureException(error); + } +} \ No newline at end of file diff --git a/api/src/oracal/index.ts b/api/src/oracal/index.ts new file mode 100644 index 0000000..7d552a4 --- /dev/null +++ b/api/src/oracal/index.ts @@ -0,0 +1 @@ +export * from './feedValues'; diff --git a/api/src/staking/index.ts b/api/src/staking/index.ts index 4dabf9e..58c5b7b 100644 --- a/api/src/staking/index.ts +++ b/api/src/staking/index.ts @@ -1,51 +1,57 @@ -import { config, logger } from '../utils'; -import { activeEraState, nominationsState, inflationState, accountState } from './overview'; -import { nodeProvider, promiseWithTimeout } from '../utils'; -import { RewriteFrames } from '@sentry/integrations'; -import * as Sentry from '@sentry/node'; +// import { config, logger } from '../utils'; +// import { activeEraState, nominationsState, inflationState, accountState, bondState } from './overview'; +// import { nodeProvider, promiseWithTimeout } from '../utils'; +// import { RewriteFrames } from '@sentry/integrations'; +// import * as Sentry from '@sentry/node'; +// import BN from 'bn.js'; -Sentry.init({ - dsn: config.sentryDns, - tracesSampleRate: 1.0, - integrations: [ - new RewriteFrames({ - root: global.__dirname, - }), - ], -}); +// Sentry.init({ +// dsn: config.sentryDns, +// tracesSampleRate: 1.0, +// integrations: [ +// new RewriteFrames({ +// root: global.__dirname, +// }), +// ], +// }); -const StakingApi = async () => { - // const eraState = await activeEraState(nodeProvider); - // const totalNomState = await nominationsState(nodeProvider, eraState?.stat.activeEra ? eraState?.stat.activeEra : 0); - // const inflation = await inflationState(nodeProvider); - // console.log(inflation?.idealInterest) +// const StakingApi = async () => { +// const eraState = await activeEraState(nodeProvider); +// const totalNomState = await nominationsState(nodeProvider, eraState?.stat.activeEra ? eraState?.stat.activeEra : 0); +// const inflation = await inflationState(nodeProvider); +// // console.log(inflation?.idealInterest) - await accountState(nodeProvider, "5DM7PJEFPbcYViEzFXu5GjF96JgoSJ3rb6jfXLsmXqrPVG2o") -}; +// const balance = await accountState(nodeProvider, "seaUDeFGmiMvh1L8dwUF3vAN2e1orExcofwxQHdH9qKzPzeLt") +// await bondState( +// nodeProvider, +// "seaUDeFGmiMvh1L8dwUF3vAN2e1orExcofwxQHdH9qKzPzeLt", +// eraState?.stat.activeEra ? eraState?.stat.activeEra : new BN(0), +// balance?.freeAfterReserve ? balance?.freeAfterReserve : new BN(0)) +// }; -Promise.resolve() - .then(async () => { - await nodeProvider.initializeProviders(); - }) - .then(StakingApi) - .then(async () => { - await nodeProvider.closeProviders(); - logger.info('Finished'); - process.exit(); - }) - .catch(async (error) => { - logger.error(error); - Sentry.captureException(error); +// Promise.resolve() +// .then(async () => { +// await nodeProvider.initializeProviders(); +// }) +// .then(StakingApi) +// .then(async () => { +// await nodeProvider.closeProviders(); +// logger.info('Finished'); +// process.exit(); +// }) +// .catch(async (error) => { +// logger.error(error); +// Sentry.captureException(error); - try { - await promiseWithTimeout(nodeProvider.closeProviders(), 200, Error('Failed to close proivders!')); - } catch (err) { - Sentry.captureException(err); - } +// try { +// await promiseWithTimeout(nodeProvider.closeProviders(), 200, Error('Failed to close proivders!')); +// } catch (err) { +// Sentry.captureException(err); +// } - logger.error('Finished'); - Sentry.close(2000).then(() => { - process.exit(-1); - }); - }); +// logger.error('Finished'); +// Sentry.close(2000).then(() => { +// process.exit(-1); +// }); +// }); \ No newline at end of file diff --git a/api/src/staking/overview/balance.ts b/api/src/staking/overview/balance.ts index e46e03c..fd89ed2 100644 --- a/api/src/staking/overview/balance.ts +++ b/api/src/staking/overview/balance.ts @@ -1,7 +1,6 @@ import { RewriteFrames } from '@sentry/integrations'; import * as Sentry from '@sentry/node'; import { config, logger, NodeProviderType } from '../../utils'; -import { getEraTimeLeft } from '../support'; import BN from 'bn.js'; /* eslint "no-underscore-dangle": "off" */ @@ -15,7 +14,63 @@ Sentry.init({ ], }); -const accountState = async (nodeProvider: NodeProviderType, address: string) => { +export const bondState = async ( + nodeProvider: NodeProviderType, + address: string, + activeEra: BN, + freeAfterReserve: BN +) => { + try { + const staking = await nodeProvider.getProvider().api.derive.staking.account(address); + const stakingLedger = JSON.parse(staking.stakingLedger.toString()); + + const active = new BN(parseInt(stakingLedger['active'].toString())) + const unlocking = stakingLedger['unlocking']; + + // free to unbond balance + const freeToUnbond = active; + + // total amount actively unlocking + let totalUnlocking = new BN(0); + let totalUnlocked = new BN(0); + + for (const u of unlocking) { + const { value, era } = u; + + if (activeEra > era) { + totalUnlocked = totalUnlocked.add(new BN(parseInt(value).toString())); + } else { + totalUnlocking = totalUnlocking.add(new BN(parseInt(value).toString())); + } + } + + // free to bond balance + const freeToBond = BN.max( + freeAfterReserve.sub(active).sub(totalUnlocking).sub(totalUnlocked), + new BN(0) + ); + + // total possible balance that can be bonded + const totalPossibleBond = BN.max( + freeAfterReserve.sub(totalUnlocking).sub(totalUnlocked), + new BN(0) + ); + + return { + freeToBond, + freeToUnbond, + totalUnlocking, + totalUnlocked, + totalPossibleBond, + totalUnlockChuncks: unlocking.length, + }; + } catch (error) { + logger.error(error); + Sentry.captureException(error); + } +} + +export const accountState = async (nodeProvider: NodeProviderType, address: string) => { try { const { data: { free, reserved, miscFrozen, feeFrozen } } = await nodeProvider.getProvider().api.query.system.account(address); const existentialAmount = nodeProvider.getProvider().api.consts.balances.existentialDeposit; @@ -47,5 +102,3 @@ const accountState = async (nodeProvider: NodeProviderType, address: string) => Sentry.captureException(error); } }; - -export default accountState; diff --git a/api/src/staking/overview/index.ts b/api/src/staking/overview/index.ts index 8b6776d..74b637d 100644 --- a/api/src/staking/overview/index.ts +++ b/api/src/staking/overview/index.ts @@ -1,4 +1,4 @@ export { default as activeEraState } from './stats/activeEra'; export { default as nominationsState } from './stats/TotalNominations'; export { default as inflationState } from './networkSats/inflation' -export { default as accountState } from './balance' \ No newline at end of file +export * from './balance' \ No newline at end of file diff --git a/api/src/staking/payouts/index.ts b/api/src/staking/payouts/index.ts deleted file mode 100644 index e69de29..0000000 diff --git a/api/src/staking/pools/index.ts b/api/src/staking/pools/index.ts deleted file mode 100644 index e69de29..0000000 diff --git a/api/src/staking/stake/index.ts b/api/src/staking/stake/index.ts deleted file mode 100644 index e69de29..0000000 diff --git a/api/src/staking/validators/index.ts b/api/src/staking/validators/index.ts deleted file mode 100644 index e69de29..0000000