vault: disallow transfer of flowing tokens

This commit is contained in:
Mark Spanbroek 2025-01-28 15:56:53 +01:00
parent c16673e2d6
commit 7e1e71d25e
2 changed files with 23 additions and 6 deletions

View File

@ -123,12 +123,19 @@ abstract contract VaultBase {
Recipient to,
uint128 amount
) internal {
require(
amount <= _balances[controller][context][from].available,
InsufficientBalance()
);
_balances[controller][context][from].available -= amount;
_balances[controller][context][to].available += amount;
Balance memory senderBalance = _getBalance(controller, context, from);
Balance memory receiverBalance = _getBalance(controller, context, to);
require(amount <= senderBalance.available, InsufficientBalance());
senderBalance.available -= amount;
receiverBalance.available += amount;
Flow memory senderFlow = _flows[controller][context][from];
Lock memory lock = _locks[controller][context];
_checkFlowInvariant(senderBalance, lock, senderFlow);
_balances[controller][context][from] = senderBalance;
_balances[controller][context][to] = receiverBalance;
}
function _designate(

View File

@ -588,6 +588,16 @@ describe("Vault", function () {
vault.flow(context, sender, receiver, 1)
).to.be.revertedWith("InsufficientBalance")
})
it("cannot transfer tokens that are flowing", async function () {
await vault.flow(context, sender, receiver, 5)
await expect(
vault.transfer(context, sender, receiver, 500)
).not.to.be.reverted
await expect(
vault.transfer(context, sender, receiver, 1)
).to.be.revertedWith("InsufficientBalance")
})
})
})
})