From c2f714aa2fe0742c4ffd014e29d73cb7577b1386 Mon Sep 17 00:00:00 2001 From: LayNath242 Date: Thu, 8 Sep 2022 08:50:37 +0700 Subject: [PATCH 1/7] add bond state --- api/src/staking/index.ts | 94 +++++++++++++++-------------- api/src/staking/overview/balance.ts | 56 ++++++++++++++++- 2 files changed, 103 insertions(+), 47 deletions(-) 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..c1e1f83 100644 --- a/api/src/staking/overview/balance.ts +++ b/api/src/staking/overview/balance.ts @@ -15,7 +15,59 @@ Sentry.init({ ], }); -const accountState = async (nodeProvider: NodeProviderType, address: string) => { +export const bondState = async ( + nodeProvider: NodeProviderType, + address: string, + activeEra: BN, + freeAfterReserve: BN +) => { + 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, + }; + +} + +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 +99,3 @@ const accountState = async (nodeProvider: NodeProviderType, address: string) => Sentry.captureException(error); } }; - -export default accountState; From 70e129857068f027af1e03095b6007fb5ebc5cfb Mon Sep 17 00:00:00 2001 From: LayNath242 Date: Thu, 8 Sep 2022 08:51:26 +0700 Subject: [PATCH 2/7] remove --- api/src/index.ts | 0 api/src/staking/payouts/index.ts | 0 api/src/staking/pools/index.ts | 0 api/src/staking/stake/index.ts | 0 api/src/staking/validators/index.ts | 0 5 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 api/src/index.ts delete mode 100644 api/src/staking/payouts/index.ts delete mode 100644 api/src/staking/pools/index.ts delete mode 100644 api/src/staking/stake/index.ts delete mode 100644 api/src/staking/validators/index.ts diff --git a/api/src/index.ts b/api/src/index.ts deleted file mode 100644 index e69de29..0000000 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 From d7a5395835f32576565cb5feee5352b7c1944865 Mon Sep 17 00:00:00 2001 From: LayNath242 Date: Thu, 8 Sep 2022 09:10:03 +0700 Subject: [PATCH 3/7] fix import error --- api/src/staking/overview/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From a1a4c726dab062927a2d6c7aaf6d3d9b8be8942f Mon Sep 17 00:00:00 2001 From: LayNath242 Date: Thu, 8 Sep 2022 14:40:01 +0700 Subject: [PATCH 4/7] update --- api/src/staking/overview/balance.ts | 77 +++++++++++++++-------------- 1 file changed, 40 insertions(+), 37 deletions(-) diff --git a/api/src/staking/overview/balance.ts b/api/src/staking/overview/balance.ts index c1e1f83..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" */ @@ -21,50 +20,54 @@ export const bondState = async ( activeEra: BN, freeAfterReserve: BN ) => { - const staking = await nodeProvider.getProvider().api.derive.staking.account(address); - const stakingLedger = JSON.parse(staking.stakingLedger.toString()); + 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']; + const active = new BN(parseInt(stakingLedger['active'].toString())) + const unlocking = stakingLedger['unlocking']; - // free to unbond balance - const freeToUnbond = active; + // free to unbond balance + const freeToUnbond = active; - // total amount actively unlocking - let totalUnlocking = new BN(0); - let totalUnlocked = new BN(0); + // total amount actively unlocking + let totalUnlocking = new BN(0); + let totalUnlocked = new BN(0); - for (const u of unlocking) { - const { value, era } = u; + 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())); + 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, - }; + // 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) => { From 3812c6c6b1fd282530702e3ee3279db1c05f393f Mon Sep 17 00:00:00 2001 From: LayNath242 Date: Thu, 8 Sep 2022 16:14:23 +0700 Subject: [PATCH 5/7] feed price --- api/src/oracal/example.test.ts | 32 ++++++++++++++++++++++++++++++++ api/src/oracal/feedValues.ts | 33 +++++++++++++++++++++++++++++++++ api/src/oracal/getValues.ts | 24 ++++++++++++++++++++++++ api/src/oracal/index.ts | 1 + 4 files changed, 90 insertions(+) create mode 100644 api/src/oracal/example.test.ts create mode 100644 api/src/oracal/feedValues.ts create mode 100644 api/src/oracal/getValues.ts create mode 100644 api/src/oracal/index.ts diff --git a/api/src/oracal/example.test.ts b/api/src/oracal/example.test.ts new file mode 100644 index 0000000..999bb88 --- /dev/null +++ b/api/src/oracal/example.test.ts @@ -0,0 +1,32 @@ +import { createTestPairs } from '@polkadot/keyring/testingPairs'; +import { config, logger } from '../utils'; +import { nodeProvider } from '../utils'; +import { feedValues } from './feedValues'; + + +/// 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); +}; + +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..3e9e839 --- /dev/null +++ b/api/src/oracal/getValues.ts @@ -0,0 +1,24 @@ +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 getPrice = async (nodeProvider: NodeProviderType, token: string, price: number) => { + try { + const provider = nodeProvider.getProvider(); + const value = await provider.api.query.rawValues.account(address); + } 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'; From 6447b8a8fd1b107cb37c7d3063b116f19b912f21 Mon Sep 17 00:00:00 2001 From: LayNath242 Date: Thu, 8 Sep 2022 16:15:23 +0700 Subject: [PATCH 6/7] fix --- api/src/oracal/getValues.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/api/src/oracal/getValues.ts b/api/src/oracal/getValues.ts index 3e9e839..70636d4 100644 --- a/api/src/oracal/getValues.ts +++ b/api/src/oracal/getValues.ts @@ -13,12 +13,12 @@ Sentry.init({ }); -export const getPrice = async (nodeProvider: NodeProviderType, token: string, price: number) => { - try { - const provider = nodeProvider.getProvider(); - const value = await provider.api.query.rawValues.account(address); - } catch (error) { - logger.error(error); - Sentry.captureException(error); - } -} \ No newline at end of file +// export const getPrice = async (nodeProvider: NodeProviderType, token: string, price: number) => { +// try { +// const provider = nodeProvider.getProvider(); +// const value = await provider.api.query.rawValues.account(address); +// } catch (error) { +// logger.error(error); +// Sentry.captureException(error); +// } +// } \ No newline at end of file From 93415422762994e0f7124186540c2cd0ff01f5c3 Mon Sep 17 00:00:00 2001 From: LayNath242 Date: Fri, 9 Sep 2022 10:05:43 +0700 Subject: [PATCH 7/7] get oracle value --- api/src/oracal/example.test.ts | 5 ++++- api/src/oracal/getValues.ts | 19 ++++++++++--------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/api/src/oracal/example.test.ts b/api/src/oracal/example.test.ts index 999bb88..d61d82d 100644 --- a/api/src/oracal/example.test.ts +++ b/api/src/oracal/example.test.ts @@ -2,6 +2,7 @@ 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 @@ -17,7 +18,9 @@ const testPairs = createTestPairs(); const aclice = testPairs.alice; const testOracle = async () => { - await feedValues(nodeProvider, aclice, 'SEL', 240_000_000_000); +// await feedValues(nodeProvider, aclice, 'SEL', 240_000_000_000); + const data = await getValue(nodeProvider, 'KUSD'); + console.log(data) }; Promise.resolve() diff --git a/api/src/oracal/getValues.ts b/api/src/oracal/getValues.ts index 70636d4..c201921 100644 --- a/api/src/oracal/getValues.ts +++ b/api/src/oracal/getValues.ts @@ -13,12 +13,13 @@ Sentry.init({ }); -// export const getPrice = async (nodeProvider: NodeProviderType, token: string, price: number) => { -// try { -// const provider = nodeProvider.getProvider(); -// const value = await provider.api.query.rawValues.account(address); -// } catch (error) { -// logger.error(error); -// Sentry.captureException(error); -// } -// } \ No newline at end of file +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