diff --git a/__test__/unit/debt_order/debt_order.spec.ts b/__test__/unit/debt_order/debt_order.spec.ts index 8b21c139..ced927ed 100644 --- a/__test__/unit/debt_order/debt_order.spec.ts +++ b/__test__/unit/debt_order/debt_order.spec.ts @@ -4,12 +4,14 @@ import { Dharma } from "../../../src/dharma"; jest.unmock("@dharmaprotocol/contracts"); +import { FILL_SCENARIOS } from "./scenarios/fill_scenarios"; import { IS_EXPIRED_SCENARIOS } from "./scenarios/is_expired_scenarios"; import { DEBT_ORDER_PARAMS_ONE } from "./scenarios/valid_debt_order_params"; // Test runners import { testCancel } from "./runners/cancel_as_debtor"; import { testCreate } from "./runners/create"; +import { testFill } from "./runners/fill"; import { testGetTotalExpectedRepaymentAmount } from "./runners/get_total_expected_repayment_amount"; import { testIsCancelled } from "./runners/is_cancelled"; import { testExpired } from "./runners/is_expired"; @@ -25,6 +27,12 @@ describe("Debt Order (Integration)", () => { await testCreate(dharma, DEBT_ORDER_PARAMS_ONE); }); + describe("#fill", async () => { + FILL_SCENARIOS.forEach(async (scenario) => { + await testFill(dharma, web3, scenario); + }); + }); + describe("#isExpired", async () => { IS_EXPIRED_SCENARIOS.forEach(async (scenario) => { await testExpired(dharma, scenario); diff --git a/__test__/unit/debt_order/runners/fill.ts b/__test__/unit/debt_order/runners/fill.ts new file mode 100644 index 00000000..19db7851 --- /dev/null +++ b/__test__/unit/debt_order/runners/fill.ts @@ -0,0 +1,48 @@ +import * as Web3 from "web3"; + +// Types +import { DebtOrder, DebtOrderParams } from "../../../../src/debt_order"; +import { EthereumAddress } from "../../../../src/types"; + +// Import Dharma for typing-checking. +import { Dharma } from "../../../../src"; + +// Test utils +import { ACCOUNTS } from "../../../accounts"; + +const CREDITOR = ACCOUNTS[1]; + +import { FillScenario } from "../scenarios/fill_scenarios"; +import { setBalancesAndAllowances } from "../utils/set_balances_and_allowances"; + +export async function testFill(dharma: Dharma, web3: Web3, scenario: FillScenario) { + await setBalancesAndAllowances(dharma, web3, scenario.debtOrderParams, CREDITOR.address); + + describe(scenario.description, () => { + let debtOrder: DebtOrder; + + beforeAll(async () => { + debtOrder = await DebtOrder.create(dharma, scenario.debtOrderParams); + scenario.setUpDebtOrder(debtOrder); + }); + + if (scenario.shouldSucceed) { + test.only("#fill succeeds", async () => { + const fillTransactionHash = await debtOrder.fillAsCreditor( + scenario.creditorAddress, + ); + + await dharma.blockchain.awaitTransactionMinedAsync(fillTransactionHash); + // test the transaction receipt is correct + }); + } else { + test("#fill fails", async () => { + const fillTransactionHash = await debtOrder.fillAsCreditor( + scenario.creditorAddress, + ); + + // test the transaction fails + }); + } + }); +} diff --git a/__test__/unit/debt_order/scenarios/fill_scenarios.ts b/__test__/unit/debt_order/scenarios/fill_scenarios.ts new file mode 100644 index 00000000..47ee64ec --- /dev/null +++ b/__test__/unit/debt_order/scenarios/fill_scenarios.ts @@ -0,0 +1,49 @@ +// Debt Order +import { DebtOrder, DebtOrderParams } from "../../../../src/debt_order"; + +// Types +import { EthereumAddress, InterestRate, TimeInterval, TokenAmount } from "../../../../src/types"; + +// Test utils +import { ACCOUNTS } from "../../../accounts"; + +const debtor = ACCOUNTS[1]; +const creditor = ACCOUNTS[2]; + +export interface FillScenario { + description: string; + creditorAddress: EthereumAddress; + debtOrderParams: DebtOrderParams; + setUpDebtOrder: (debtOrder: DebtOrder) => void; + shouldSucceed: boolean; +} + +const creditorAddress: EthereumAddress = new EthereumAddress(creditor.address); + +const debtOrderParams: DebtOrderParams = { + principal: new TokenAmount(5, "REP"), + collateral: new TokenAmount(10, "WETH"), + interestRate: new InterestRate(12.3), + termLength: new TimeInterval(6, "months"), + debtorAddress: new EthereumAddress(debtor.address), + expiresIn: new TimeInterval(5, "days"), +}; + +export const FILL_SCENARIOS: FillScenario[] = [ + { + description: "when the debt order is open", + creditorAddress, + debtOrderParams, + setUpDebtOrder: (debtOrder: DebtOrder) => {}, + shouldSucceed: true, + }, + // { + // description: "when the debt order is expired", + // }, + // { + // description: "when the debt order is already filled", + // }, + // { + // description: "when the debt order is canceled", + // }, +]; diff --git a/src/apis/order_api.ts b/src/apis/order_api.ts index b03a2072..28f3653e 100644 --- a/src/apis/order_api.ts +++ b/src/apis/order_api.ts @@ -135,17 +135,6 @@ export class OrderAPI { const debtOrderDataWrapped = new DebtOrderDataWrapper(debtOrderData); - console.log( - debtOrderDataWrapped.getCreditor(), - debtOrderDataWrapped.getOrderAddresses(), - debtOrderDataWrapped.getOrderValues(), - debtOrderDataWrapped.getOrderBytes32(), - debtOrderDataWrapped.getSignaturesV(), - debtOrderDataWrapped.getSignaturesR(), - debtOrderDataWrapped.getSignaturesS(), - txOptions, - ); - return debtKernel.fillDebtOrder.sendTransactionAsync( debtOrderDataWrapped.getCreditor(), debtOrderDataWrapped.getOrderAddresses(),