Balance Guard
Last updated
Last updated
This policy makes sure that a the balance of some address in some token (Native or ERC20) doesn't change over an allowed threshold with any given transaction.
If an attacker gains privileged access to an account (wallet or smart contract) - either by stealing the private keys or by exploiting some vulnerability - and the account has some balance in some token (Native or ERC20) - this policy will block a malicious transaction from going through if the change in balance is above a configured threshold for the token.
When a transaction reaches this policy in the hook, it first looks up the balances of the account in it's configured tokens.
Then, in the hook, the policy compares the difference in balance (per token) against it's configured change-threshold.
If the transaction changed the balance more than is allowed by the threshold, it will be reverted.
Deploy your own version of this policy (see ).
Call the method to configure the balance change thresholds.
Add the newly deployed policy to the (you can do this or , see ).
That's it!
consumerMaxBalanceChange
mapping
(address =>
mapping
(address => uint)
)
a mapping between accounts, tokens, and their balance balance change threshold
for example:
[someAccount][someToken] = 50000
consumerLastBalance
mapping
(address =>
mapping
(address => uint[])
)
a mapping between accounts, tokens, and their balances before executing the transaction
function setConsumerMaxBalanceChange(address consumer, address token, uint maxBalanceChange)
Callable only by the policy owner. Sets the maximum balance change that can take place on the given token in a transaction.
consumer
address
the account holding the balance
token
address
the token to track balance changes for
maxBalanceChange
uint
the maximum balance change for the token
function removeToken(address consumer, address token)
Callable only by the policy owner. Removes the token from the consumer, so that the policy will no longer track balance changes on that token.
consumer
address
the account holding the balance
token
address
the token to track balance changes for
function getConsumerTokens(address consumer)
Gets the tokens and their configured balance change threshold for consumer
.
consumer
address
the account holding the balance
This policy runs during both the Firewall's and the hooks.
On our GitHub repository: