Skip to content

Commit f963ba0

Browse files
committed
fix: check liquidity and allow 0 fee
1 parent e6a4248 commit f963ba0

File tree

3 files changed

+105
-9
lines changed

3 files changed

+105
-9
lines changed

contracts/reservoir.clar

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
;; SOFTWARE.
2424

2525
(use-trait sip-010 'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE.sip-010-trait-ft-standard.sip-010-trait)
26-
(use-trait stackflow-token .stackflow-token.stackflow-token)
27-
;; (use-trait stackflow-token 'SP126XFZQ3ZHYM6Q6KAQZMMJSDY91A8BTT6AD08RV.stackflow-token-0-6-0.stackflow-token)
26+
(use-trait stackflow-token 'SP126XFZQ3ZHYM6Q6KAQZMMJSDY91A8BTT6AD08RV.stackflow-token-0-6-0.stackflow-token)
2827

2928
(define-constant OPERATOR tx-sender)
3029
(define-constant RESERVOIR current-contract)
@@ -395,6 +394,7 @@
395394
;;; tap.
396395
;;; Returns:
397396
;;; -`(ok expire-block)` on success
397+
;;; - `ERR_AMOUNT_NOT_AVAILABLE` if reservoir liquidity is below `amount`
398398
;;; - `ERR_BORROW_FEE_PAYMENT_FAILED` if the fee payment failed
399399
;;; - Errors passed through from the StackFlow `deposit` function
400400
(define-public (borrow-liquidity
@@ -411,11 +411,16 @@
411411
(let (
412412
(borrower tx-sender)
413413
(expected-fee (get-borrow-fee amount))
414+
(available-liquidity (unwrap-panic (get-available-liquidity token)))
414415
(until (+ burn-block-height BORROW_TERM_BLOCKS))
415416
)
416417
(try! (check-valid stackflow token))
418+
(asserts! (<= amount available-liquidity) ERR_AMOUNT_NOT_AVAILABLE)
417419
(asserts! (>= fee expected-fee) ERR_INVALID_FEE)
418-
(unwrap! (transfer-to-contract token fee) ERR_BORROW_FEE_PAYMENT_FAILED)
420+
(if (is-eq fee u0)
421+
true
422+
(unwrap! (transfer-to-contract token fee) ERR_BORROW_FEE_PAYMENT_FAILED)
423+
)
419424
(try! (match token
420425
t (as-contract? ((with-ft (contract-of t) "*" amount))
421426
(try! (contract-call? stackflow deposit amount token borrower reservoir-balance

deployments/default.simnet-plan.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,6 @@ plan:
9393
emulated-sender: SP126XFZQ3ZHYM6Q6KAQZMMJSDY91A8BTT6AD08RV
9494
path: ./.cache/requirements/SP126XFZQ3ZHYM6Q6KAQZMMJSDY91A8BTT6AD08RV.stackflow-token-0-6-0.clar
9595
clarity-version: 4
96-
- transaction-type: emulated-contract-publish
97-
contract-name: stackflow-token
98-
emulated-sender: ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM
99-
path: contracts/stackflow-token.clar
100-
clarity-version: 4
10196
- transaction-type: emulated-contract-publish
10297
contract-name: reservoir
10398
emulated-sender: ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM
@@ -113,6 +108,11 @@ plan:
113108
emulated-sender: ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM
114109
path: contracts/stackflow.clar
115110
clarity-version: 4
111+
- transaction-type: emulated-contract-publish
112+
contract-name: stackflow-token
113+
emulated-sender: ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM
114+
path: contracts/stackflow-token.clar
115+
clarity-version: 4
116116
- transaction-type: emulated-contract-publish
117117
contract-name: test-token
118118
emulated-sender: ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM

tests/reservoir.test.ts

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,14 @@ describe("reservoir", () => {
159159
address1
160160
);
161161

162+
// Add liquidity so amount check passes and we reach fee validation
163+
simnet.callPublicFn(
164+
"reservoir",
165+
"add-liquidity",
166+
[Cl.none(), Cl.uint(1000000)],
167+
deployer
168+
);
169+
162170
const mySignature = generateDepositSignature(
163171
address1PK,
164172
null,
@@ -554,6 +562,89 @@ describe("reservoir", () => {
554562
);
555563
});
556564

565+
it("can borrow liquidity with zero fee when borrow rate is zero", () => {
566+
// borrow-rate is u0 from init
567+
const { result: feeResult } = simnet.callReadOnlyFn(
568+
"reservoir",
569+
"get-borrow-fee",
570+
[Cl.uint(50000)],
571+
deployer
572+
);
573+
expect(feeResult).toBeUint(0);
574+
575+
// Add liquidity to reservoir
576+
simnet.callPublicFn(
577+
"reservoir",
578+
"add-liquidity",
579+
[Cl.none(), Cl.uint(5000000000)],
580+
deployer
581+
);
582+
583+
// Fund initial tap
584+
const { result } = simnet.callPublicFn(
585+
"reservoir",
586+
"create-tap",
587+
[
588+
Cl.principal(stackflowContract),
589+
Cl.none(),
590+
Cl.uint(1000000),
591+
Cl.uint(0),
592+
],
593+
address1
594+
);
595+
expect(result.type).toBe(ClarityType.ResponseOk);
596+
597+
const amount = 50000;
598+
const fee = 0;
599+
600+
const mySignature = generateDepositSignature(
601+
address1PK,
602+
null,
603+
address1,
604+
reservoirContract,
605+
1000000,
606+
50000,
607+
1,
608+
reservoirContract
609+
);
610+
611+
const reservoirSignature = generateDepositSignature(
612+
deployerPK,
613+
null,
614+
reservoirContract,
615+
address1,
616+
50000,
617+
1000000,
618+
1,
619+
reservoirContract
620+
);
621+
622+
const borrow = simnet.callPublicFn(
623+
"reservoir",
624+
"borrow-liquidity",
625+
[
626+
Cl.principal(stackflowContract),
627+
Cl.uint(amount),
628+
Cl.uint(fee),
629+
Cl.none(),
630+
Cl.uint(1000000),
631+
Cl.uint(50000),
632+
Cl.buffer(mySignature),
633+
Cl.buffer(reservoirSignature),
634+
Cl.uint(1),
635+
],
636+
address1
637+
);
638+
expect(borrow.result).toBeOk(
639+
Cl.uint(simnet.burnBlockHeight + BORROW_TERM_BLOCKS)
640+
);
641+
642+
// 5000000000 - 50000 (borrowed), with no fee transfer
643+
const stxBalances = simnet.getAssetsMap().get("STX")!;
644+
const reservoirBalance = stxBalances.get(reservoirContract);
645+
expect(reservoirBalance).toBe(4999950000n);
646+
});
647+
557648
it("can borrow additional liquidity before previous term ends", () => {
558649
// Set rate to 10% and fund the reservoir
559650
simnet.callPublicFn(
@@ -764,7 +855,7 @@ describe("reservoir", () => {
764855
],
765856
address1
766857
);
767-
expect(result).toBeErr(Cl.uint(StackflowError.DepositFailed));
858+
expect(result).toBeErr(Cl.uint(ReservoirError.AmountNotAvailable));
768859
});
769860
});
770861

0 commit comments

Comments
 (0)