-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
38 changed files
with
4,256 additions
and
1,628 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
200 changes: 200 additions & 0 deletions
200
packages/adapters-library/src/adapters/jito/products/jitosol/jitoJitosolAdapter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
import { Metaplex } from '@metaplex-foundation/js' | ||
import { getStakePoolAccount } from '@solana/spl-stake-pool' | ||
import { AccountLayout, getMint } from '@solana/spl-token' | ||
import { Connection, PublicKey } from '@solana/web3.js' | ||
import { AdaptersController } from '../../../../core/adaptersController' | ||
import { Chain } from '../../../../core/constants/chains' | ||
import { CacheToDb } from '../../../../core/decorators/cacheToDb' | ||
import { NotImplementedError } from '../../../../core/errors/errors' | ||
import { buildTrustAssetIconUrl } from '../../../../core/utils/buildIconUrl' | ||
import { nativeToken } from '../../../../core/utils/nativeTokens' | ||
import { Helpers, SolanaHelpers } from '../../../../scripts/helpers' | ||
import { | ||
IProtocolAdapter, | ||
ProtocolToken, | ||
} from '../../../../types/IProtocolAdapter' | ||
import { | ||
GetEventsInput, | ||
GetPositionsInput, | ||
GetTotalValueLockedInput, | ||
MovementsByBlock, | ||
PositionType, | ||
ProtocolDetails, | ||
ProtocolPosition, | ||
ProtocolTokenTvl, | ||
SolanaProtocolAdapterParams, | ||
TokenType, | ||
UnwrapExchangeRate, | ||
UnwrapInput, | ||
} from '../../../../types/adapter' | ||
import { Protocol } from '../../../protocols' | ||
|
||
const JITO_STAKE_POOL = new PublicKey( | ||
'Jito4APyf642JPZPx3hGc6WWJ8zPKtRbRs4P815Awbb', | ||
) | ||
|
||
// TODO Can be extracted from stake pool address | ||
const JITOSOL_TOKEN_ADDRESS = new PublicKey( | ||
'J1toso1uCk3RLmjorhTtrVwY9HJ7X8V9yYac6Y7kGCPn', | ||
) | ||
|
||
type AdditionalMetadata = { | ||
stakePool: string | ||
} | ||
|
||
export class JitoJitosolAdapter implements IProtocolAdapter { | ||
productId = 'jitosol' | ||
protocolId: Protocol | ||
chainId = Chain.Solana | ||
helpers: SolanaHelpers | ||
|
||
adapterSettings = { | ||
enablePositionDetectionByProtocolTokenTransfer: false, | ||
includeInUnwrap: true, | ||
} | ||
|
||
private provider: Connection | ||
|
||
adaptersController: AdaptersController | ||
|
||
constructor({ | ||
provider, | ||
protocolId, | ||
adaptersController, | ||
helpers, | ||
}: SolanaProtocolAdapterParams) { | ||
this.provider = provider | ||
this.protocolId = protocolId | ||
this.adaptersController = adaptersController | ||
this.helpers = helpers | ||
} | ||
|
||
getProtocolDetails(): ProtocolDetails { | ||
return { | ||
protocolId: this.protocolId, | ||
name: 'Jito', | ||
description: 'Jito defi adapter', | ||
siteUrl: 'https://www.jito.network/staking/', | ||
iconUrl: buildTrustAssetIconUrl( | ||
Chain.Solana, | ||
JITOSOL_TOKEN_ADDRESS.toString(), | ||
), | ||
positionType: PositionType.Staked, | ||
chainId: this.chainId, | ||
productId: this.productId, | ||
} | ||
} | ||
|
||
@CacheToDb | ||
async getProtocolTokens(): Promise<ProtocolToken<AdditionalMetadata>[]> { | ||
const metaplex = Metaplex.make(this.provider) | ||
|
||
const token = await metaplex | ||
.nfts() | ||
.findByMint({ mintAddress: JITOSOL_TOKEN_ADDRESS }) | ||
|
||
const mintInfo = await getMint(this.provider, JITOSOL_TOKEN_ADDRESS) | ||
|
||
return [ | ||
{ | ||
address: JITOSOL_TOKEN_ADDRESS.toString(), | ||
name: token.name, | ||
symbol: token.symbol, | ||
decimals: mintInfo.decimals, | ||
stakePool: JITO_STAKE_POOL.toString(), | ||
underlyingTokens: [nativeToken[Chain.Solana]], | ||
}, | ||
] | ||
} | ||
|
||
async getPositions({ | ||
userAddress, | ||
blockNumber, | ||
}: GetPositionsInput): Promise<ProtocolPosition[]> { | ||
const { stakePool, underlyingTokens, ...protocolToken } = ( | ||
await this.getProtocolTokens() | ||
)[0]! | ||
|
||
const tokenAccounts = await this.provider.getTokenAccountsByOwner( | ||
new PublicKey(userAddress), | ||
{ | ||
mint: new PublicKey(protocolToken.address), | ||
}, | ||
) | ||
|
||
const userBalance = tokenAccounts.value.reduce((total, accountInfo) => { | ||
const accountData = AccountLayout.decode( | ||
Uint8Array.from(accountInfo.account.data), | ||
) | ||
|
||
return total + accountData.amount | ||
}, 0n) | ||
|
||
return [ | ||
{ | ||
...protocolToken, | ||
type: TokenType.Protocol, | ||
balanceRaw: userBalance, | ||
}, | ||
] | ||
} | ||
|
||
async getWithdrawals({ | ||
protocolTokenAddress, | ||
fromBlock, | ||
toBlock, | ||
userAddress, | ||
}: GetEventsInput): Promise<MovementsByBlock[]> { | ||
throw new NotImplementedError() | ||
} | ||
|
||
async getDeposits({ | ||
protocolTokenAddress, | ||
fromBlock, | ||
toBlock, | ||
userAddress, | ||
}: GetEventsInput): Promise<MovementsByBlock[]> { | ||
throw new NotImplementedError() | ||
} | ||
|
||
async getTotalValueLocked({ | ||
protocolTokenAddresses, | ||
blockNumber, | ||
}: GetTotalValueLockedInput): Promise<ProtocolTokenTvl[]> { | ||
throw new NotImplementedError() | ||
} | ||
|
||
async unwrap(input: UnwrapInput): Promise<UnwrapExchangeRate> { | ||
const { | ||
stakePool, | ||
underlyingTokens: [underlyingToken], | ||
...protocolToken | ||
} = (await this.getProtocolTokens())[0]! | ||
|
||
// totalLamports is the total amount of SOL units in the pool | ||
// poolTokenSupply is the total amount of JitoSOL tokens in circulation | ||
const { | ||
account: { | ||
data: { totalLamports, poolTokenSupply }, | ||
}, | ||
} = await getStakePoolAccount(this.provider, new PublicKey(stakePool)) | ||
|
||
const underlyingRateRaw = | ||
(BigInt(totalLamports.toString()) * | ||
10n ** BigInt(protocolToken.decimals)) / | ||
BigInt(poolTokenSupply.toString()) | ||
|
||
return { | ||
...protocolToken, | ||
baseRate: 1, | ||
type: TokenType.Protocol, | ||
tokens: [ | ||
{ | ||
...underlyingToken!, | ||
type: TokenType.Underlying, | ||
underlyingRateRaw, | ||
}, | ||
], | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
packages/adapters-library/src/adapters/jito/products/jitosol/tests/testCases.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import type { TestCase } from '../../../../../types/testCase' | ||
|
||
export const testCases: TestCase[] = [ | ||
// { | ||
// chainId: Chain.Ethereum, | ||
// method: 'positions', | ||
// input: { | ||
// userAddress: '0x6b8Be925ED8277fE4D27820aE4677e76Ebf4c255', | ||
// }, | ||
// }, | ||
// { | ||
// chainId: Chain.Ethereum, | ||
// method: 'profits', | ||
// input: { | ||
// userAddress: '0xCEadFdCCd0E8E370D985c49Ed3117b2572243A4a', | ||
// timePeriod: TimePeriod.oneDay, | ||
// }, | ||
// }, | ||
] |
45 changes: 45 additions & 0 deletions
45
packages/adapters-library/src/adapters/prices-solana/products/usd/pricesSolanaUsdAdapter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { Connection } from '@solana/web3.js' | ||
import { AdaptersController } from '../../../../core/adaptersController' | ||
import { Chain } from '../../../../core/constants/chains' | ||
import { Helpers } from '../../../../scripts/helpers' | ||
import { UnwrapExchangeRate } from '../../../../types/adapter' | ||
import { Erc20Metadata } from '../../../../types/erc20Metadata' | ||
import { IPricesAdapter } from '../../../prices-v2/products/usd/pricesV2UsdAdapter' | ||
|
||
export class PricesSolanaUsdAdapter implements IPricesAdapter { | ||
productId = 'usd' | ||
chainId = Chain.Solana | ||
helpers: Helpers | ||
|
||
adapterSettings = { | ||
enablePositionDetectionByProtocolTokenTransfer: false, | ||
includeInUnwrap: false, | ||
} | ||
|
||
private provider: Connection | ||
|
||
adaptersController: AdaptersController | ||
|
||
constructor({ | ||
provider, | ||
adaptersController, | ||
}: { | ||
provider: Connection | ||
adaptersController: AdaptersController | ||
}) { | ||
this.provider = provider | ||
this.adaptersController = adaptersController | ||
this.helpers = {} as Helpers | ||
} | ||
|
||
async getPrice({ | ||
blockNumber, | ||
tokenMetadata, | ||
}: { | ||
blockNumber: number | ||
tokenMetadata: Erc20Metadata | ||
}): Promise<UnwrapExchangeRate> { | ||
// TODO Implement price fetching | ||
throw new Error('Error fetching price') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.