Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file removed api/src/index.ts
Empty file.
35 changes: 35 additions & 0 deletions api/src/oracal/example.test.ts
Original file line number Diff line number Diff line change
@@ -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();
})
33 changes: 33 additions & 0 deletions api/src/oracal/feedValues.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}

25 changes: 25 additions & 0 deletions api/src/oracal/getValues.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
1 change: 1 addition & 0 deletions api/src/oracal/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './feedValues';
94 changes: 50 additions & 44 deletions api/src/staking/index.ts
Original file line number Diff line number Diff line change
@@ -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);
// });
// });
61 changes: 57 additions & 4 deletions api/src/staking/overview/balance.ts
Original file line number Diff line number Diff line change
@@ -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" */
Expand All @@ -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;
Expand Down Expand Up @@ -47,5 +102,3 @@ const accountState = async (nodeProvider: NodeProviderType, address: string) =>
Sentry.captureException(error);
}
};

export default accountState;
2 changes: 1 addition & 1 deletion api/src/staking/overview/index.ts
Original file line number Diff line number Diff line change
@@ -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'
export * from './balance'
Empty file removed api/src/staking/payouts/index.ts
Empty file.
Empty file removed api/src/staking/pools/index.ts
Empty file.
Empty file removed api/src/staking/stake/index.ts
Empty file.
Empty file.