Skip to content

Commit

Permalink
Merge pull request #409 from 00labs/account-management-sync-develop
Browse files Browse the repository at this point in the history
Account management sync develop
  • Loading branch information
shan-57blocks authored Jan 3, 2025
2 parents 088f97d + 3bfdc32 commit e8d72c7
Show file tree
Hide file tree
Showing 23 changed files with 1,703 additions and 1,356 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
"useWorkspaces": true,
"version": "0.0.62"
"version": "0.0.65"
}
14 changes: 8 additions & 6 deletions packages/examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"startCreateAlt": "ts-node src/createReceivableAlternateNetwork.ts",
"startPay": "ts-node src/payReceivable.ts",
"startPayReference": "ts-node src/payReceivableWithReferenceId.ts",
"startDeclarePaymentByTokenIdV2": "ts-node src/declarePaymentByTokenIdV2.ts",
"startDrawdownPayback": "ts-node src/drawdownAndPaybackToPool.ts",
"startDrawdownPaybackV2": "ts-node src/drawdownAndPaybackReceivableV2.ts",
"startPoolHelpers": "ts-node src/poolContract.ts",
Expand All @@ -27,6 +28,7 @@
"solanaFetchAvailableCredit": "ts-node src/solana/fetchAvailableCredit.ts",
"solanaFetchBorrowerDetails": "ts-node src/solana/fetchBorrowerDetails.ts",
"solanaDrawdown": "ts-node src/solana/drawdown.ts",
"solanaWithdrawYield": "ts-node src/solana/withdrawYield.ts",
"solanaPayback": "ts-node src/solana/payback.ts",
"lint-staged": "lint-staged"
},
Expand All @@ -43,12 +45,12 @@
"@solana/wallet-adapter-react-ui": "^0.9.35",
"@solana/wallet-adapter-wallets": "^0.19.32",
"@stellar/freighter-api": "^3.0.0",
"@huma-finance/soroban-credit-storage": "0.0.15-beta.50",
"@huma-finance/soroban-huma-config": "0.0.15-beta.50",
"@huma-finance/soroban-pool-storage": "0.0.15-beta.50",
"@huma-finance/soroban-sdk": "^0.0.15-beta.50",
"@huma-finance/soroban-tranche-vault": "0.0.15-beta.50",
"@stellar/stellar-sdk": "^12.3.0",
"@huma-finance/soroban-credit-storage": "0.0.18",
"@huma-finance/soroban-huma-config": "0.0.18",
"@huma-finance/soroban-pool-storage": "0.0.18",
"@huma-finance/soroban-sdk": "^0.0.18",
"@huma-finance/soroban-tranche-vault": "0.0.18",
"@stellar/stellar-sdk": "13.0.0",
"@coral-xyz/borsh": "^0.30.1",
"@mui/icons-material": "^5.3.0",
"@mui/material": "^5.0.6",
Expand Down
37 changes: 37 additions & 0 deletions packages/examples/src/declarePaymentByTokenIdV2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { BigNumber, Wallet, ethers } from 'ethers'
import { ChainEnum, POOL_NAME, POOL_TYPE } from '@huma-finance/shared'
import { HumaReceivableHandler, HumaContext } from '@huma-finance/sdk'
require('dotenv').config()

async function main() {
const TEST_PRIVATE_KEY = process.env.TEST_PRIVATE_KEY
const provider = new ethers.providers.JsonRpcProvider(
`https://rpc-amoy.polygon.technology`,
{
name: 'Amoy',
chainId: ChainEnum.Amoy,
},
)
const wallet = new Wallet(TEST_PRIVATE_KEY, provider)

const humaContext = new HumaContext({
signer: wallet,
provider,
chainId: ChainEnum.Amoy,
poolName: POOL_NAME.ArfCreditPoolV2,
poolType: POOL_TYPE.ReceivableBackedCreditLine,
})
const receivableHandler = new HumaReceivableHandler({
humaContext,
})

// Mint a receivable with metadata uploaded to ARWeave
const tx = await receivableHandler.declarePaymentByTokenId(
BigNumber.from(10), // receivableAmount
BigNumber.from(1), // token ID
)
const txResponse = await tx.wait()
console.log(`Success. Tx hash: ${txResponse.transactionHash}`)
}

main()
46 changes: 46 additions & 0 deletions packages/examples/src/solana/withdrawYield.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Connection, Keypair, sendAndConfirmTransaction } from '@solana/web3.js'
import {
AnchorProvider,
BN,
setProvider,
Wallet,
web3,
} from '@coral-xyz/anchor'
import { POOL_NAME, SolanaChainEnum } from '@huma-finance/shared'
import { HumaSolanaContext, HumaSolanaProgramHelper } from '@huma-finance/sdk'

require('dotenv').config()

async function main() {
const TEST_PRIVATE_KEY = process.env.TEST_PRIVATE_KEY
const connection = new Connection(
'https://api.devnet.solana.com',
'confirmed',
)

const keypair = web3.Keypair.fromSecretKey(
Buffer.from(JSON.parse(TEST_PRIVATE_KEY)),
)
const wallet = new Wallet(keypair)
setProvider(new AnchorProvider(connection, wallet))

const solanaHumaContext = new HumaSolanaContext({
publicKey: wallet.publicKey,
connection: connection,
chainId: SolanaChainEnum.SolanaDevnet,
poolName: POOL_NAME.ArfCreditPool3Months,
})

const humaSolanaProgramHelper = new HumaSolanaProgramHelper({
solanaContext: solanaHumaContext,
})

const tx = await humaSolanaProgramHelper.buildWithdrawYieldsTransaction()

console.log(tx)

const txResult = await sendAndConfirmTransaction(connection, tx, [keypair])
console.log(txResult)
}

main()
4 changes: 2 additions & 2 deletions packages/huma-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@huma-finance/sdk",
"version": "0.0.62",
"version": "0.0.65",
"types": "./dist/index.d.ts",
"main": "./dist/cjs/index.js",
"module": "./dist/index.js",
Expand Down Expand Up @@ -29,7 +29,7 @@
"@ethersproject/constants": "^5.7.0",
"@ethersproject/contracts": "^5.7.0",
"@ethersproject/providers": "^5.6.0",
"@huma-finance/shared": "^0.0.62",
"@huma-finance/shared": "^0.0.65",
"@irys/sdk": "^0.2.11",
"axios": "^1.4.0",
"bignumber.js": "^9.1.1",
Expand Down
97 changes: 96 additions & 1 deletion packages/huma-sdk/src/helpers/solana/HumaSolanaProgramHelper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BN } from '@coral-xyz/anchor'
import { BN, utils } from '@coral-xyz/anchor'
import {
getCreditAccounts,
getHumaProgram,
Expand All @@ -11,6 +11,7 @@ import {
createApproveCheckedInstruction,
createAssociatedTokenAccountInstruction,
getAccount,
TOKEN_2022_PROGRAM_ID,
TOKEN_PROGRAM_ID,
TokenAccountNotFoundError,
} from '@solana/spl-token'
Expand Down Expand Up @@ -127,6 +128,100 @@ export class HumaSolanaProgramHelper {
return tx
}

async buildWithdrawYieldsTransaction(): Promise<Transaction> {
const { publicKey, connection, chainId, poolName } = this.#solanaContext
const program = getHumaProgram(chainId, connection)
const poolInfo = getSolanaPoolInfo(chainId, poolName)

if (!poolInfo) {
throw new Error('Could not find pool')
}

const tx: Transaction = new Transaction()

const { underlyingTokenATA, seniorTrancheATA, juniorTrancheATA } =
getTokenAccounts(poolInfo, publicKey)
const [juniorYieldDistributingLenderAccount] =
PublicKey.findProgramAddressSync(
[
utils.bytes.utf8.encode('yield_distributing_lender'),
new PublicKey(poolInfo.juniorTrancheMint).toBuffer(),
publicKey.toBuffer(),
],
program.programId,
)
const programTx = await program.methods
.withdrawYields()
.accountsPartial({
lender: publicKey,
humaConfig: poolInfo.humaConfig,
poolConfig: poolInfo.poolConfig,
yieldDistributingLender: juniorYieldDistributingLenderAccount,
underlyingMint: poolInfo.underlyingMint.address,
poolUnderlyingToken: poolInfo.poolUnderlyingTokenAccount,
lenderUnderlyingToken: underlyingTokenATA,
trancheMint: poolInfo.juniorTrancheMint,
lenderTrancheToken: juniorTrancheATA,
underlyingTokenProgram: TOKEN_PROGRAM_ID,
trancheTokenProgram: TOKEN_2022_PROGRAM_ID,
})
.transaction()
const txAccounts: PublicKey[] = [
publicKey,
new PublicKey(poolInfo.humaConfig),
new PublicKey(poolInfo.poolConfig),
juniorYieldDistributingLenderAccount,
new PublicKey(poolInfo.underlyingMint.address),
new PublicKey(poolInfo.poolUnderlyingTokenAccount),
underlyingTokenATA,
new PublicKey(poolInfo.juniorTrancheMint),
juniorTrancheATA,
TOKEN_PROGRAM_ID,
TOKEN_2022_PROGRAM_ID,
]
tx.add(programTx)

if (poolInfo.seniorTrancheMint) {
const [seniorYieldDistributingLenderAccount] =
PublicKey.findProgramAddressSync(
[
utils.bytes.utf8.encode('yield_distributing_lender'),
new PublicKey(poolInfo.seniorTrancheMint).toBuffer(),
publicKey.toBuffer(),
],
program.programId,
)
const programTx = await program.methods
.withdrawYields()
.accountsPartial({
lender: publicKey,
humaConfig: poolInfo.humaConfig,
poolConfig: poolInfo.poolConfig,
yieldDistributingLender: seniorYieldDistributingLenderAccount,
underlyingMint: poolInfo.underlyingMint.address,
poolUnderlyingToken: poolInfo.poolUnderlyingTokenAccount,
lenderUnderlyingToken: underlyingTokenATA,
trancheMint: poolInfo.seniorTrancheMint,
lenderTrancheToken: seniorTrancheATA,
underlyingTokenProgram: TOKEN_PROGRAM_ID,
trancheTokenProgram: TOKEN_2022_PROGRAM_ID,
})
.transaction()
txAccounts.push(
...[
seniorYieldDistributingLenderAccount,
new PublicKey(poolInfo.seniorTrancheMint),
seniorTrancheATA,
],
)
tx.add(programTx)
}

await buildOptimalTransaction(tx, txAccounts, this.#solanaContext)

return tx
}

async buildDrawdownTransaction(amount: BN): Promise<Transaction> {
const { publicKey, connection, chainId, poolName } = this.#solanaContext
const program = getHumaProgram(chainId, connection)
Expand Down
11 changes: 8 additions & 3 deletions packages/huma-sdk/src/services/SubgraphService.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import {
ARF_3_MONTH_POOL_SCROLL_MAP,
CHAIN_POOLS_INFO_V2,
ChainEnum,
CreditEvent,
isV2Pool,
POOL_NAME,
POOL_TYPE,
PoolContractMap,
PoolSubgraphMap,
RealWorldReceivableInfoBase,
CHAIN_POOLS_INFO_V2,
ChainEnum,
isV2Pool,
requestPost,
} from '@huma-finance/shared'
import { gql } from 'graphql-request'
Expand Down Expand Up @@ -258,6 +259,10 @@ function getReceivableV2Info(
poolAddress = CHAIN_POOLS_INFO_V2[chainId as ChainEnum]?.[poolName]?.pool
}

if (poolAddress === ARF_3_MONTH_POOL_SCROLL_MAP.poolNew) {
poolAddress = ARF_3_MONTH_POOL_SCROLL_MAP.poolOld
}

const ReceivablesV2Query = `
query {
rwreceivables(
Expand Down
18 changes: 18 additions & 0 deletions packages/huma-sdk/src/services/v2/HumaReceivableHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ export class HumaReceivableHandler {
return contract.declarePayment(tokenId, paymentAmount, gasOpts)
}

async declarePaymentByTokenId(
paymentAmount: BigNumber,
tokenId: BigNumber,
gasOpts: Overrides = {},
): Promise<TransactionResponse> {
const contract = await getReceivableContractV2(
this.#humaContext.poolName,
this.#humaContext.signer,
)
if (!contract) {
throw new Error('Could not find Receivable contract')
}

gasOpts = await getDefaultGasOptions(gasOpts, this.#humaContext.chainId)

return contract.declarePayment(tokenId, paymentAmount, gasOpts)
}

async burnReceivable(
tokenId: BigNumberish,
gasOpts: Overrides = {},
Expand Down
2 changes: 1 addition & 1 deletion packages/huma-shared/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@huma-finance/shared",
"version": "0.0.62",
"version": "0.0.65",
"types": "./dist/index.d.ts",
"main": "./dist/cjs/index.js",
"module": "./dist/index.js",
Expand Down
Loading

0 comments on commit e8d72c7

Please sign in to comment.