-
Notifications
You must be signed in to change notification settings - Fork 1
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
10 changed files
with
188 additions
and
53 deletions.
There are no files selected for viewing
18 changes: 8 additions & 10 deletions
18
packages/huma-shared/src/solana/utils/tokenAssetsSharesUtils.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 |
---|---|---|
@@ -1,16 +1,14 @@ | ||
import { BN } from '@coral-xyz/anchor' | ||
|
||
export function convertToShares( | ||
totalAssets: BN, | ||
totalSupply: BN, | ||
assets: BN, | ||
): BN { | ||
if (!totalSupply.isZero() && totalAssets.isZero()) { | ||
return new BN(0) | ||
totalAssets: bigint, | ||
totalSupply: bigint, | ||
assets: bigint, | ||
): bigint { | ||
if (totalSupply !== BigInt(0) && totalAssets === BigInt(0)) { | ||
return BigInt(0) | ||
} | ||
if (totalSupply.isZero()) { | ||
if (totalSupply === BigInt(0)) { | ||
return assets | ||
} | ||
|
||
return assets.mul(totalSupply).div(totalAssets) | ||
return (assets * totalSupply) / totalAssets | ||
} |
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
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
66 changes: 66 additions & 0 deletions
66
packages/huma-web-shared/src/stellar/utils/fetchStellarDepositRecord.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,66 @@ | ||
import { | ||
STELLAR_CHAINS_INFO, | ||
StellarPoolInfo, | ||
TrancheType, | ||
} from '@huma-finance/shared' | ||
import { SorobanRpc, Address, xdr, scValToNative } from '@stellar/stellar-sdk' | ||
|
||
const getDepositRecordKey = (contractId: string, address: string) => { | ||
const addressScVal = new Address(address).toScVal() | ||
return xdr.LedgerKey.contractData( | ||
new xdr.LedgerKeyContractData({ | ||
contract: new Address(contractId).toScAddress(), | ||
key: xdr.ScVal.scvVec([ | ||
xdr.ScVal.scvSymbol('DepositRecord'), | ||
addressScVal, | ||
]), | ||
durability: xdr.ContractDataDurability.persistent(), | ||
}), | ||
) | ||
} | ||
|
||
export type DepositRecord = { | ||
lastDepositTime: number | ||
principal: bigint | ||
} | ||
|
||
export async function fetchStellarDepositRecord( | ||
poolInfo: StellarPoolInfo, | ||
tranche: TrancheType, | ||
account: string, | ||
): Promise<DepositRecord> { | ||
try { | ||
const chainMetadata = STELLAR_CHAINS_INFO[poolInfo.chainId] | ||
const server = new SorobanRpc.Server(chainMetadata.rpc) | ||
|
||
const key = getDepositRecordKey( | ||
tranche === 'senior' ? poolInfo.seniorTranche! : poolInfo.juniorTranche, | ||
account, | ||
) | ||
// Get the contract data with proper durability | ||
const response = await server.getLedgerEntries(key) | ||
|
||
const contractData = response.entries[0].val | ||
|
||
if (contractData.switch() === xdr.LedgerEntryType.contractData()) { | ||
const data = scValToNative(contractData.contractData().val()) | ||
|
||
if ( | ||
data.last_deposit_time === undefined || | ||
data.principal === undefined | ||
) { | ||
throw new Error('Failed to fetch deposit record') | ||
} | ||
|
||
return { | ||
lastDepositTime: Number(data.last_deposit_time), | ||
principal: BigInt(data.principal), | ||
} | ||
} | ||
|
||
throw new Error('Failed to fetch deposit record') | ||
} catch (error) { | ||
console.error('Error fetching deposit record:', error) | ||
throw error | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './fetchStellarTokenBalance' | ||
export * from './stellarTryFn' | ||
export * from './getClientCommonParams' | ||
export * from './fetchStellarDepositRecord' |
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