Skip to content

Commit

Permalink
Merge branch 'main' of github.com:sonarwatch/portfolio
Browse files Browse the repository at this point in the history
  • Loading branch information
Geninsus committed Nov 7, 2024
2 parents ad393b7 + c8af005 commit 9ee7f0b
Show file tree
Hide file tree
Showing 10 changed files with 1,458 additions and 4 deletions.
4 changes: 4 additions & 0 deletions packages/core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).

## [0.14.25](https://github.com/sonarwatch/portfolio/compare/core-0.14.24...core-0.14.25) (2024-11-07)



## [0.14.24](https://github.com/sonarwatch/portfolio/compare/core-0.14.23...core-0.14.24) (2024-11-07)


Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sonarwatch/portfolio-core",
"version": "0.14.24",
"version": "0.14.25",
"type": "commonjs",
"types": "./src/index.d.ts",
"dependencies": {
Expand Down
4 changes: 4 additions & 0 deletions packages/plugins/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).

## [0.14.25](https://github.com/sonarwatch/portfolio/compare/plugins-0.14.24...plugins-0.14.25) (2024-11-07)



## [0.14.24](https://github.com/sonarwatch/portfolio/compare/plugins-0.14.23...plugins-0.14.24) (2024-11-07)


Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sonarwatch/portfolio-plugins",
"version": "0.14.24",
"version": "0.14.25",
"type": "commonjs",
"types": "./src/index.d.ts",
"peerDependencies": {
Expand Down
11 changes: 11 additions & 0 deletions packages/plugins/src/plugins/nxfinance/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { IdlItem } from '@solanafm/explorer-kit-idls';
import { PublicKey } from '@solana/web3.js';
import { NxfinanceLeverageIDL } from './leverageIdl';
import { NxfinanceLendIdl } from './lendIdl';
import { NxfinanceStakingIdl } from './stakingIdl';

export const platformId = 'nxfinance';
export const platform: Platform = {
Expand Down Expand Up @@ -44,7 +45,17 @@ export const nxfinanceLendIdlItem = {
} as IdlItem;

export const lendingPoolKey = 'lending-pools';
export const stakingPoolKey = 'staking-pool';

export const lendingPools = [
new PublicKey('HVn3F2wq2Fvr8T5yX7VS9yWaNxX5PMgxTyHb4aKAX8z3'),
];

export const stakePool = '2P1eeegdbEhN3bnCroJJPXiH13i4rw1XYk8ftdh9meRY';
export const stakingProgramId = '9un1MopS4NRhgVDLXB1DqoQDTmq1un48YKJuPiMLpSc9';

export const nxfinanceStakingIdlItem = {
programId: new PublicKey(stakingProgramId).toString(),
idl: NxfinanceStakingIdl,
idlType: 'anchor',
} as IdlItem;
10 changes: 8 additions & 2 deletions packages/plugins/src/plugins/nxfinance/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import { platform } from './constants';
import positionsFetcher from './positionsFetcher';
import lendPositionsFetcher from './lendPositionsFetcher';
import lendingPoolsJob from './lendingPoolsJob';
import stakingFetcher from './stakingFetcher';
import stakingPoolJob from './stakingPoolJob';

export const platforms: Platform[] = [platform];
export const jobs: Job[] = [lendingPoolsJob];
export const fetchers: Fetcher[] = [positionsFetcher, lendPositionsFetcher];
export const jobs: Job[] = [lendingPoolsJob, stakingPoolJob];
export const fetchers: Fetcher[] = [
positionsFetcher,
lendPositionsFetcher,
stakingFetcher,
];
105 changes: 105 additions & 0 deletions packages/plugins/src/plugins/nxfinance/stakingFetcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { NetworkId, solanaNativeAddress } from '@sonarwatch/portfolio-core';
import { PublicKey } from '@solana/web3.js';
import BigNumber from 'bignumber.js';
import { Cache } from '../../Cache';
import {
nxfinanceStakingIdlItem,
platformId,
stakePool,
stakingPoolKey,
stakingProgramId,
} from './constants';
import { Fetcher, FetcherExecutor } from '../../Fetcher';
import { getClientSolana } from '../../utils/clients';
import { getAutoParsedMultipleAccountsInfo } from '../../utils/solana';
import { StakingAccount, StakingPoolAccount } from './types';
import { ElementRegistry } from '../../utils/elementbuilder/ElementRegistry';

const executor: FetcherExecutor = async (owner: string, cache: Cache) => {
const connection = getClientSolana();

const stakingAccount = (
await getAutoParsedMultipleAccountsInfo<StakingAccount>(
connection,
nxfinanceStakingIdlItem,
[
PublicKey.findProgramAddressSync(
[
new PublicKey(stakePool).toBuffer(),
Buffer.from('nx_account', 'utf8'),
new PublicKey(owner).toBuffer(),
],
new PublicKey(stakingProgramId)
)[0],
]
)
)[0];

if (!stakingAccount || stakingAccount.stakedTokens === '0') return [];

const stakePoolAccount = await cache.getItem<StakingPoolAccount>(
stakingPoolKey,
{
prefix: platformId,
networkId: NetworkId.solana,
}
);

if (!stakePoolAccount) return [];

const elementRegistry = new ElementRegistry(NetworkId.solana, platformId);
const element = elementRegistry.addElementBorrowlend({
label: 'Staked',
});

element.addSuppliedAsset({
address: stakePoolAccount.stakeTokenMint,
amount: stakingAccount.stakedTokens,
});

/* const m = new Date(
new BigNumber(stakingAccount.lastUpdateNoteTime)
.multipliedBy(new BigNumber(1e3))
.toNumber()
);
const stakedTokens = new BigNumber(stakingAccount.stakedTokens);
const w = new BigNumber(stakePoolAccount.increaseNoteRatePerSecond)
.times(stakedTokens)
.times(new BigNumber(Date.now()).minus(m.getTime()))
.div(1e3)
.dividedBy(10 ** 9);
const stakedNotes = new BigNumber(stakingAccount.stakedNotes);
const A = stakedTokens.multipliedBy(2).minus(stakedNotes);
const M = new BigNumber(stakingAccount.notes).plus(w);
element.addRewardAsset({
address: stakePoolAccount.stakeTokenMint,
amount: (M.isGreaterThan(A) ? A : M).toNumber(),
}); */

element.addRewardAsset({
address: solanaNativeAddress,
amount: stakingAccount.claimableReward,
});

element.addSuppliedAsset({
address: stakePoolAccount.stakeTokenMint,
amount: stakingAccount.withdrawingTokens,
attributes: {
lockedUntil: new BigNumber(stakingAccount.timeOfWithdrawApply)
.plus(14 * 24 * 60 * 60) // 14 days
.multipliedBy(new BigNumber(1e3))
.toNumber(),
},
});

return elementRegistry.getElements(cache);
};

const fetcher: Fetcher = {
id: `${platformId}-staking`,
networkId: NetworkId.solana,
executor,
};

export default fetcher;
Loading

0 comments on commit 9ee7f0b

Please sign in to comment.