This repository has been archived by the owner on Sep 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
ConditionBalance.sol
86 lines (81 loc) · 2.63 KB
/
ConditionBalance.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.10;
pragma experimental ABIEncoderV2;
import {GelatoConditionsStandard} from "../GelatoConditionsStandard.sol";
import {IERC20} from "../../external/IERC20.sol";
contract ConditionBalance is GelatoConditionsStandard {
/// @dev use this function to encode the data off-chain for the condition data field
function getConditionData(
address _account,
address _token,
uint256 _refBalance,
bool _greaterElseSmaller
)
public
pure
virtual
returns(bytes memory)
{
return abi.encodeWithSelector(
this.balanceCheck.selector,
_account,
_token,
_refBalance,
_greaterElseSmaller
);
}
/// @param _conditionData The encoded data from getConditionData()
function ok(uint256, bytes calldata _conditionData, uint256)
public
view
virtual
override
returns(string memory)
{
(address _account,
address _token,
uint256 _refBalance,
bool _greaterElseSmaller) = abi.decode(
_conditionData[4:],
(address,address,uint256,bool)
);
return balanceCheck(_account, _token, _refBalance, _greaterElseSmaller);
}
// Specific Implementation
function balanceCheck(
address _account,
address _token,
uint256 _refBalance,
bool _greaterElseSmaller
)
public
view
virtual
returns(string memory)
{
// ETH balances
if (_token == 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) {
if (_greaterElseSmaller) { // greaterThan
if (_account.balance >= _refBalance) return OK;
return "NotOkETHBalanceIsNotGreaterThanRefBalance";
} else { // smallerThan
if (_account.balance <= _refBalance) return OK;
return "NotOkETHBalanceIsNotSmallerThanRefBalance";
}
} else {
// ERC20 balances
IERC20 erc20 = IERC20(_token);
try erc20.balanceOf(_account) returns (uint256 erc20Balance) {
if (_greaterElseSmaller) { // greaterThan
if (erc20Balance >= _refBalance) return OK;
return "NotOkERC20BalanceIsNotGreaterThanRefBalance";
} else { // smallerThan
if (erc20Balance <= _refBalance) return OK;
return "NotOkERC20BalanceIsNotSmallerThanRefBalance";
}
} catch {
return "ERC20Error";
}
}
}
}