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
/
ActionTransfer.sol
151 lines (137 loc) · 5.27 KB
/
ActionTransfer.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.10;
pragma experimental ABIEncoderV2;
import {GelatoActionsStandardFull} from "../GelatoActionsStandardFull.sol";
import {IERC20} from "../../external/IERC20.sol";
import {Address} from "../../external/Address.sol";
import {GelatoBytes} from "../../libraries/GelatoBytes.sol";
import {SafeERC20} from "../../external/SafeERC20.sol";
import {DataFlow} from "../../gelato_core/interfaces/IGelatoCore.sol";
/// @dev This action is for user proxies that store funds.
contract ActionTransfer is GelatoActionsStandardFull {
// using SafeERC20 for IERC20; <- internal library methods vs. try/catch
using Address for address payable;
using SafeERC20 for IERC20;
// ======= DEV HELPERS =========
/// @dev use this function to encode the data off-chain for the action data field
function getActionData(address _sendToken, uint256 _sendAmount, address _destination)
public
pure
virtual
returns(bytes memory)
{
return abi.encodeWithSelector(
this.action.selector,
_sendToken,
_sendAmount,
_destination
);
}
/// @dev Used by GelatoActionPipeline.isValid()
function DATA_FLOW_IN_TYPE() public pure virtual override returns (bytes32) {
return keccak256("TOKEN,UINT256");
}
/// @dev Used by GelatoActionPipeline.isValid()
function DATA_FLOW_OUT_TYPE() public pure virtual override returns (bytes32) {
return keccak256("TOKEN,UINT256");
}
// ======= ACTION IMPLEMENTATION DETAILS =========
/// @dev Always use this function for encoding _actionData off-chain
/// Will be called by GelatoActionPipeline if Action.dataFlow.None
function action(address sendToken, uint256 sendAmount, address destination)
public
virtual
delegatecallOnly("ActionTransfer.action")
{
if (sendToken != ETH_ADDRESS) {
IERC20 sendERC20 = IERC20(sendToken);
sendERC20.safeTransfer(destination, sendAmount, "ActionTransfer.action:");
emit LogOneWay(address(this), sendToken, sendAmount, destination);
} else {
payable(destination).sendValue(sendAmount);
}
}
/// @dev Will be called by GelatoActionPipeline if Action.dataFlow.In
// => do not use for _actionData encoding
function execWithDataFlowIn(bytes calldata _actionData, bytes calldata _inFlowData)
external
payable
virtual
override
{
(address sendToken, uint256 sendAmount) = abi.decode(_inFlowData, (address,uint256));
address destination = abi.decode(_actionData[68:100], (address));
action(sendToken, sendAmount, destination);
}
/// @dev Will be called by GelatoActionPipeline if Action.dataFlow.Out
// => do not use for _actionData encoding
function execWithDataFlowOut(bytes calldata _actionData)
external
payable
virtual
override
returns (bytes memory)
{
(address sendToken, uint256 sendAmount, address destination) = abi.decode(
_actionData[4:],
(address,uint256,address)
);
action(sendToken, sendAmount, destination);
return abi.encode(sendToken, sendAmount);
}
/// @dev Will be called by GelatoActionPipeline if Action.dataFlow.InAndOut
// => do not use for _actionData encoding
function execWithDataFlowInAndOut(
bytes calldata _actionData,
bytes calldata _inFlowData
)
external
payable
virtual
override
returns (bytes memory)
{
(address sendToken, uint256 sendAmount) = abi.decode(_inFlowData, (address,uint256));
address destination = abi.decode(_actionData[68:100], (address));
action(sendToken, sendAmount, destination);
return abi.encode(sendToken, sendAmount);
}
// ===== ACTION TERMS CHECK ========
// Overriding and extending GelatoActionsStandard's function (optional)
function termsOk(
uint256, // taskReceipId
address _userProxy,
bytes calldata _actionData,
DataFlow _dataFlow,
uint256, // value
uint256 // cycleId
)
public
view
virtual
override
returns(string memory)
{
if (this.action.selector != GelatoBytes.calldataSliceSelector(_actionData))
return "ActionTransfer: invalid action selector";
if (_dataFlow == DataFlow.In || _dataFlow == DataFlow.InAndOut)
return "ActionTransfer: termsOk check invalidated by inbound DataFlow";
(address sendToken, uint256 sendAmount) = abi.decode(
_actionData[4:68],
(address,uint256)
);
if (sendToken == ETH_ADDRESS) {
if (_userProxy.balance < sendAmount)
return "ActionTransfer: NotOkUserProxyETHBalance";
} else {
try IERC20(sendToken).balanceOf(_userProxy) returns(uint256 sendTokenBalance) {
if (sendTokenBalance < sendAmount)
return "ActionTransfer: NotOkUserProxyERC20Balance";
} catch {
return "ActionTransfer: ErrorBalanceOf";
}
}
// STANDARD return string to signal actionConditions Ok
return OK;
}
}