Skip to content

Commit 9e42836

Browse files
author
Usn Zorro
committed
Ability to withdraw 5% by default and up to 100% shares via DAO
1 parent 20b69a7 commit 9e42836

File tree

2 files changed

+104
-21
lines changed

2 files changed

+104
-21
lines changed

src/treasury/withdraw_stable_pool.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl Contract {
2121
/// collateralization on the static accounts of USDT and USN contracts
2222
///
2323
/// It fails if 'usn' is the only liquidity provider in the stable pool.
24-
pub fn withdraw_stable_pool(&mut self) -> Promise {
24+
pub fn withdraw_stable_pool(&mut self, percent: Option<u8>) -> Promise {
2525
self.assert_owner();
2626

2727
let pool = Pool::stable_pool();
@@ -40,6 +40,7 @@ impl Contract {
4040
GAS_FOR_GET_SHARES,
4141
)
4242
.then(ext_self::handle_start_removing(
43+
percent,
4344
env::current_account_id(),
4445
env::attached_deposit(),
4546
GAS_SURPLUS * 6
@@ -54,7 +55,7 @@ impl Contract {
5455
trait RefFinanceHandler {
5556
#[private]
5657
#[payable]
57-
fn handle_start_removing(&mut self, #[callback] shares: U128) -> Promise;
58+
fn handle_start_removing(&mut self, percent: Option<u8>, #[callback] shares: U128) -> Promise;
5859

5960
#[private]
6061
#[payable]
@@ -65,7 +66,7 @@ trait RefFinanceHandler {
6566
}
6667

6768
trait RefFinanceHandler {
68-
fn handle_start_removing(&mut self, shares: U128) -> Promise;
69+
fn handle_start_removing(&mut self, percent: Option<u8>, shares: U128) -> Promise;
6970

7071
fn handle_remove_deposit(&mut self, amounts: Vec<U128>) -> Promise;
7172

@@ -76,7 +77,7 @@ trait RefFinanceHandler {
7677
impl RefFinanceHandler for Contract {
7778
#[private]
7879
#[payable]
79-
fn handle_start_removing(&mut self, #[callback] shares: U128) -> Promise {
80+
fn handle_start_removing(&mut self, percent: Option<u8>, #[callback] shares: U128) -> Promise {
8081
let pool = Pool::stable_pool();
8182
let min_amounts = vec![U128(0), U128(0)];
8283

@@ -86,8 +87,12 @@ impl RefFinanceHandler for Contract {
8687
"Requires exactly 3 yoctoNEAR of attached deposit"
8788
);
8889

89-
// 5% of shares
90-
let shares_amount = U256::from(u128::from(shares)) * U256::from(5u128) / 100u128;
90+
// 5% of shares by default, but 100% is maximum.
91+
let percent = percent.unwrap_or(5);
92+
93+
require!(percent <= 100, "Maximum 100% of shares can be withdrawn");
94+
95+
let shares_amount = U256::from(u128::from(shares)) * U256::from(percent) / 100u128;
9196

9297
ext_ref_finance::remove_liquidity(
9398
pool.id,

tests/tests.js

+93-15
Original file line numberDiff line numberDiff line change
@@ -1450,24 +1450,107 @@ describe('Withdraw Stable Pool', async function () {
14501450
);
14511451
});
14521452

1453-
it('should remove stable liquidity', async () => {
1454-
// Clear wNEAR 'usn' account.
1455-
await global.wnearContract.burn({
1456-
args: {
1457-
account_id: config.usnId,
1458-
amount: await global.wnearContract.ft_balance_of({
1459-
account_id: config.usnId,
1460-
}),
1453+
it('should fail trying to withdraw 100% because there is only 1 participant', async () => {
1454+
await assert.rejects(
1455+
async () => {
1456+
await global.usnContract.withdraw_stable_pool({
1457+
args: { percent: 100 },
1458+
amount: 3 * ONE_YOCTO,
1459+
gas: GAS_FOR_CALL,
1460+
});
1461+
},
1462+
(err) => {
1463+
assert.match(err.message, /Callback computation 0 was not successful/);
1464+
return true;
1465+
}
1466+
);
1467+
});
1468+
1469+
it('should fail trying to withdraw 101% of liquidity', async () => {
1470+
await assert.rejects(
1471+
async () => {
1472+
await global.usnContract.withdraw_stable_pool({
1473+
args: { percent: 101 },
1474+
amount: 3 * ONE_YOCTO,
1475+
gas: GAS_FOR_CALL,
1476+
});
14611477
},
1478+
(err) => {
1479+
assert.match(err.message, /Maximum 100%/);
1480+
return true;
1481+
}
1482+
);
1483+
});
1484+
1485+
it('should withdraw 99% of shares', async () => {
1486+
const poolInfoBefore = await global.refContract.get_stable_pool({
1487+
pool_id: 0,
1488+
});
1489+
1490+
await global.usnContract.withdraw_stable_pool({
1491+
args: { percent: 99 },
1492+
amount: 3 * ONE_YOCTO,
14621493
gas: GAS_FOR_CALL,
14631494
});
14641495

1465-
const wrapAmountBefore = await global.wnearContract.ft_balance_of({
1496+
const poolInfoAfter = await global.refContract.get_stable_pool({
1497+
pool_id: 0,
1498+
});
1499+
1500+
const usnDeposit = await global.refContract.get_deposit({
14661501
account_id: config.usnId,
1502+
token_id: config.usnId,
1503+
});
1504+
const usdtDeposit = await global.refContract.get_deposit({
1505+
account_id: config.usnId,
1506+
token_id: config.usdtId,
14671507
});
1508+
const wrapDeposit = await global.refContract.get_deposit({
1509+
account_id: config.usnId,
1510+
token_id: config.wnearId,
1511+
});
1512+
assert.equal(usnDeposit, '0');
1513+
assert.equal(usdtDeposit, '0');
1514+
assert.equal(wrapDeposit, '0');
1515+
1516+
assert(
1517+
new BN(poolInfoBefore.amounts[0]).gt(new BN(poolInfoAfter.amounts[0]))
1518+
);
14681519

1469-
assert.equal(wrapAmountBefore, '0');
1520+
assert(
1521+
new BN(poolInfoBefore.amounts[1]).gt(new BN(poolInfoAfter.amounts[1]))
1522+
);
14701523

1524+
const poolUsn99Percent = new BN(poolInfoBefore.amounts[0])
1525+
.mul(new BN(991))
1526+
.div(new BN(1000));
1527+
const poolUsn98Percent = new BN(poolInfoBefore.amounts[0])
1528+
.mul(new BN(98))
1529+
.div(new BN(100));
1530+
const usnAmountDiff = new BN(poolInfoBefore.amounts[0]).sub(
1531+
new BN(poolInfoAfter.amounts[0])
1532+
);
1533+
1534+
// Withdrawn 98% < USN < 99%
1535+
assert(usnAmountDiff.gt(new BN(poolUsn98Percent)));
1536+
assert(usnAmountDiff.lt(new BN(poolUsn99Percent)));
1537+
1538+
const poolUsdt99Percent = new BN(poolInfoBefore.amounts[1])
1539+
.mul(new BN(991))
1540+
.div(new BN(1000));
1541+
const poolUsdt98Percent = new BN(poolInfoBefore.amounts[1])
1542+
.mul(new BN(98))
1543+
.div(new BN(100));
1544+
const usdtAmountDiff = new BN(poolInfoBefore.amounts[1]).sub(
1545+
new BN(poolInfoAfter.amounts[1])
1546+
);
1547+
1548+
// Withdrawn 98% < USDT < 99%
1549+
assert(usdtAmountDiff.gt(new BN(poolUsdt98Percent)));
1550+
assert(usdtAmountDiff.lt(new BN(poolUsdt99Percent)));
1551+
});
1552+
1553+
it('should withdraw 5% of shares', async () => {
14711554
const poolInfoBefore = await global.refContract.get_stable_pool({
14721555
pool_id: 0,
14731556
});
@@ -1494,14 +1577,9 @@ describe('Withdraw Stable Pool', async function () {
14941577
account_id: config.usnId,
14951578
token_id: config.wnearId,
14961579
});
1497-
const wrapAmount = await global.wnearContract.ft_balance_of({
1498-
account_id: config.usnId,
1499-
});
1500-
15011580
assert.equal(usnDeposit, '0');
15021581
assert.equal(usdtDeposit, '0');
15031582
assert.equal(wrapDeposit, '0');
1504-
assert.equal(wrapAmount, '0');
15051583

15061584
assert(
15071585
new BN(poolInfoBefore.amounts[0]).gt(new BN(poolInfoAfter.amounts[0]))

0 commit comments

Comments
 (0)