Skip to content

Commit

Permalink
Merge pull request #406 from 00labs/declare-payment-tokenid
Browse files Browse the repository at this point in the history
SDK declare payment by token id
  • Loading branch information
mliu authored Dec 31, 2024
2 parents 84e8fa2 + 221f72d commit 78f40ea
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 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 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()
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

0 comments on commit 78f40ea

Please sign in to comment.