From 221f72d1bfd44a253c00dea504722c006e2d2f57 Mon Sep 17 00:00:00 2001 From: Michael Liu Date: Tue, 31 Dec 2024 12:01:03 -0500 Subject: [PATCH] Declare payment by token id --- packages/examples/package.json | 1 + .../examples/src/declarePaymentByTokenIdV2.ts | 37 +++++++++++++++++++ .../src/services/v2/HumaReceivableHandler.ts | 18 +++++++++ 3 files changed, 56 insertions(+) create mode 100644 packages/examples/src/declarePaymentByTokenIdV2.ts diff --git a/packages/examples/package.json b/packages/examples/package.json index 97a17e4..3e52403 100644 --- a/packages/examples/package.json +++ b/packages/examples/package.json @@ -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", diff --git a/packages/examples/src/declarePaymentByTokenIdV2.ts b/packages/examples/src/declarePaymentByTokenIdV2.ts new file mode 100644 index 0000000..8d8802d --- /dev/null +++ b/packages/examples/src/declarePaymentByTokenIdV2.ts @@ -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() diff --git a/packages/huma-sdk/src/services/v2/HumaReceivableHandler.ts b/packages/huma-sdk/src/services/v2/HumaReceivableHandler.ts index a225aa6..7b67add 100644 --- a/packages/huma-sdk/src/services/v2/HumaReceivableHandler.ts +++ b/packages/huma-sdk/src/services/v2/HumaReceivableHandler.ts @@ -48,6 +48,24 @@ export class HumaReceivableHandler { return contract.declarePayment(tokenId, paymentAmount, gasOpts) } + async declarePaymentByTokenId( + paymentAmount: BigNumber, + tokenId: BigNumber, + gasOpts: Overrides = {}, + ): Promise { + 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 = {},