-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntentSafePlugin.sol
126 lines (108 loc) · 4.21 KB
/
IntentSafePlugin.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
// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity ^0.8.18;
import {BasePluginWithEventMetadata, PluginMetadata} from "./Base.sol";
import {ISafe} from "@safe-global/safe-core-protocol/contracts/interfaces/Accounts.sol";
import {ISafeProtocolManager} from "@safe-global/safe-core-protocol/contracts/interfaces/Manager.sol";
import {SafeTransaction, SafeProtocolAction} from "@safe-global/safe-core-protocol/contracts/DataTypes.sol";
import {ISafeProtocolPlugin} from "./interfaces/Modules.sol";
import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";
import "./common/Enum.sol";
import "./common/ReentrancyGuard.sol";
import "./interfaces/IIntentSafePlugin.sol";
/**
* Module functionalities:
* - pay fees to the settlement contract for ATO execution
* - get quote for ATO payment
* - emit fee payment event
*/
contract IntentPlugin is
BasePluginWithEventMetadata,
IDSNIntentModule,
ReentrancyGuard
{
/// @dev address of settlement contract to pay fees for ATO
address public SETTLEMENT_ENTITY;
/// @dev nonce manager for ATO comming from user
mapping(address => uint256) public userIntentNonceManager;
SafeProtocolAction[] public protocolAction;
constructor(
address _trustedSettlementEntity
)
BasePluginWithEventMetadata(
PluginMetadata({
name: "Intent Plugin",
version: "1.0.0",
requiresRootAccess: false,
iconUrl: "",
appUrl: "https://bananahq.io"
})
)
{
SETTLEMENT_ENTITY = _trustedSettlementEntity;
}
/// @dev return hash for a particular ATO
/// @param _intent - intent to be solved
/// @return hash of ATO
function getATOHash(ATO[] calldata _intent, address _sender) public view returns (bytes32) {
return
keccak256(
abi.encodePacked(
abi.encode(_intent),
userIntentNonceManager[_sender]
)
);
}
/// @dev calculates and returns fees required for solving an ATO
/// @param _intent - Intent to be solved
/// @return fee - fee required for solving the ATO
function getFeeQuote(ATO[] calldata _intent) public view returns (uint256) {
return 0.1 ether; //! need to implement logic for fee calculation
}
/// @dev pay fees and broadcasts an ATO to the network
/// @param _userSafeAccount - account of the user, ato - ATO to be solved
/// @return success - true if fess paid and ATO broadcasted successfully
function payFeesAndExecuteIntent(
ISafeProtocolManager _manager,
ISafe _userSafeAccount,
UserIntent calldata _userIntent
) external override returns (bool) {
require(address(_userSafeAccount) != address(0), "Invalid safe account");
require(address(_userSafeAccount) == _userIntent.sender, "Sender is not the owner");
require(
userIntentNonceManager[_userIntent.sender] == _userIntent.nonce,
"Invalid nonce"
);
require(
address(_userSafeAccount).balance >= getFeeQuote(_userIntent.intent),
"Insufficient fee"
); // check does user wallet has sufficient balance
userIntentNonceManager[_userIntent.sender] += 1;
bytes32 atoHash = getATOHash(_userIntent.intent, _userIntent.sender);
SafeProtocolAction memory action = SafeProtocolAction(
payable(SETTLEMENT_ENTITY),
getFeeQuote(_userIntent.intent),
"0x"
);
protocolAction.push(action);
SafeTransaction memory safeTx = SafeTransaction({
actions: protocolAction,
nonce: 0,
metadataHash: atoHash
});
bytes[] memory response = _manager.executeTransaction(
_userSafeAccount,
safeTx
);
if (keccak256(response[0]) == keccak256(bytes("Ok"))) {
emit FeePaid(atoHash, getFeeQuote(_userIntent.intent));
return true;
}
return false;
}
function supportsInterface(
bytes4 interfaceId
) external pure returns (bool) {
return
interfaceId == type(ISafeProtocolPlugin).interfaceId;
}
}