Skip to content

Commit 3f6becb

Browse files
KogaroshimiguelmtzinfCheyenneAtapour
authored
feat: return asset values in Spoke actions (#935)
Co-authored-by: miguelmtzinf <[email protected]> Co-authored-by: Cheyenne Atapour <[email protected]>
1 parent 01affbf commit 3f6becb

24 files changed

+734
-279
lines changed
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
2-
"borrowNative": "225746",
3-
"repayNative": "241101",
4-
"supplyAsCollateralNative": "154967",
5-
"supplyNative": "131221",
6-
"withdrawNative: full": "125615",
7-
"withdrawNative: partial": "136818"
2+
"borrowNative": "225910",
3+
"repayNative": "241350",
4+
"supplyAsCollateralNative": "155117",
5+
"supplyNative": "131351",
6+
"withdrawNative: full": "121184",
7+
"withdrawNative: partial": "131280"
88
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"borrowWithSig": "213934",
3-
"repayWithSig": "242162",
2+
"borrowWithSig": "214090",
3+
"repayWithSig": "242385",
44
"setSelfAsUserPositionManagerWithSig": "75492",
55
"setUsingAsCollateralWithSig": "85259",
6-
"supplyWithSig": "147831",
6+
"supplyWithSig": "147957",
77
"updateUserDynamicConfigWithSig": "62980",
88
"updateUserRiskPremiumWithSig": "61661",
9-
"withdrawWithSig": "131657"
9+
"withdrawWithSig": "127209"
1010
}
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
{
2-
"borrow: first": "197562",
3-
"borrow: second action, same reserve": "177800",
2+
"borrow: first": "197591",
3+
"borrow: second action, same reserve": "177829",
44
"liquidationCall (receiveShares): full": "261371",
55
"liquidationCall (receiveShares): partial": "301812",
66
"liquidationCall: full": "285384",
77
"liquidationCall: partial": "308159",
8-
"permitReserve + repay (multicall)": "234630",
9-
"permitReserve + supply (multicall)": "140132",
10-
"permitReserve + supply + enable collateral (multicall)": "175435",
11-
"repay: full": "151412",
12-
"repay: partial": "175831",
8+
"permitReserve + repay (multicall)": "234757",
9+
"permitReserve + supply (multicall)": "140176",
10+
"permitReserve + supply + enable collateral (multicall)": "175479",
11+
"repay: full": "151524",
12+
"repay: partial": "175943",
1313
"setUserPositionManagerWithSig: disable": "44896",
1414
"setUserPositionManagerWithSig: enable": "68925",
15-
"supply + enable collateral (multicall)": "153376",
16-
"supply: 0 borrows, collateral disabled": "114887",
17-
"supply: 0 borrows, collateral enabled": "119284",
18-
"supply: 1 borrow": "119276",
19-
"supply: second action, same reserve": "102184",
15+
"supply + enable collateral (multicall)": "153420",
16+
"supply: 0 borrows, collateral disabled": "114916",
17+
"supply: 0 borrows, collateral enabled": "119313",
18+
"supply: 1 borrow": "119305",
19+
"supply: second action, same reserve": "102213",
2020
"updateUserDynamicConfig: 1 collateral": "73827",
2121
"updateUserDynamicConfig: 2 collaterals": "88687",
2222
"updateUserRiskPremium: 1 borrow": "94457",
@@ -26,9 +26,9 @@
2626
"usingAsCollateral: 1 borrow, enable": "32364",
2727
"usingAsCollateral: 2 borrows, disable": "127213",
2828
"usingAsCollateral: 2 borrows, enable": "41942",
29-
"withdraw: 0 borrows, full": "122593",
30-
"withdraw: 0 borrows, partial": "127447",
31-
"withdraw: 1 borrow, partial": "156102",
32-
"withdraw: 2 borrows, partial": "178511",
33-
"withdraw: non collateral": "119987"
29+
"withdraw: 0 borrows, full": "122628",
30+
"withdraw: 0 borrows, partial": "127479",
31+
"withdraw: 1 borrow, partial": "156134",
32+
"withdraw: 2 borrows, partial": "178543",
33+
"withdraw: non collateral": "120019"
3434
}

snapshots/Spoke.Operations.json

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
{
2-
"borrow: first": "268070",
3-
"borrow: second action, same reserve": "211308",
2+
"borrow: first": "268099",
3+
"borrow: second action, same reserve": "211337",
44
"liquidationCall (receiveShares): full": "295096",
55
"liquidationCall (receiveShares): partial": "335537",
66
"liquidationCall: full": "319109",
77
"liquidationCall: partial": "341884",
8-
"permitReserve + repay (multicall)": "268348",
9-
"permitReserve + supply (multicall)": "140132",
10-
"permitReserve + supply + enable collateral (multicall)": "175435",
11-
"repay: full": "146053",
12-
"repay: partial": "209549",
8+
"permitReserve + repay (multicall)": "268475",
9+
"permitReserve + supply (multicall)": "140176",
10+
"permitReserve + supply + enable collateral (multicall)": "175479",
11+
"repay: full": "146165",
12+
"repay: partial": "209661",
1313
"setUserPositionManagerWithSig: disable": "44896",
1414
"setUserPositionManagerWithSig: enable": "68925",
15-
"supply + enable collateral (multicall)": "153376",
16-
"supply: 0 borrows, collateral disabled": "114887",
17-
"supply: 0 borrows, collateral enabled": "119284",
18-
"supply: 1 borrow": "119276",
19-
"supply: second action, same reserve": "102184",
15+
"supply + enable collateral (multicall)": "153420",
16+
"supply: 0 borrows, collateral disabled": "114916",
17+
"supply: 0 borrows, collateral enabled": "119313",
18+
"supply: 1 borrow": "119305",
19+
"supply: second action, same reserve": "102213",
2020
"updateUserDynamicConfig: 1 collateral": "73827",
2121
"updateUserDynamicConfig: 2 collaterals": "88687",
2222
"updateUserRiskPremium: 1 borrow": "176413",
@@ -26,9 +26,9 @@
2626
"usingAsCollateral: 1 borrow, enable": "32364",
2727
"usingAsCollateral: 2 borrows, disable": "286310",
2828
"usingAsCollateral: 2 borrows, enable": "41942",
29-
"withdraw: 0 borrows, full": "122593",
30-
"withdraw: 0 borrows, partial": "127447",
31-
"withdraw: 1 borrow, partial": "235558",
32-
"withdraw: 2 borrows, partial": "335109",
33-
"withdraw: non collateral": "119987"
29+
"withdraw: 0 borrows, full": "122628",
30+
"withdraw: 0 borrows, partial": "127479",
31+
"withdraw: 1 borrow, partial": "235590",
32+
"withdraw: 2 borrows, partial": "335141",
33+
"withdraw: non collateral": "120019"
3434
}

src/position-manager/NativeTokenGateway.sol

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ pragma solidity 0.8.28;
44

55
import {ReentrancyGuardTransient} from 'src/dependencies/openzeppelin/ReentrancyGuardTransient.sol';
66
import {SafeTransferLib} from 'src/dependencies/solady/SafeTransferLib.sol';
7-
import {MathUtils} from 'src/libraries/math/MathUtils.sol';
7+
import {GatewayBase} from 'src/position-manager/GatewayBase.sol';
88
import {ISpoke} from 'src/spoke/interfaces/ISpoke.sol';
99
import {INativeWrapper} from 'src/position-manager/interfaces/INativeWrapper.sol';
10-
import {GatewayBase} from 'src/position-manager/GatewayBase.sol';
1110
import {INativeTokenGateway} from 'src/position-manager/interfaces/INativeTokenGateway.sol';
1211

1312
/// @title NativeTokenGateway
@@ -42,61 +41,75 @@ contract NativeTokenGateway is INativeTokenGateway, GatewayBase, ReentrancyGuard
4241
address spoke,
4342
uint256 reserveId,
4443
uint256 amount
45-
) external payable nonReentrant onlyRegisteredSpoke(spoke) {
44+
) external payable nonReentrant onlyRegisteredSpoke(spoke) returns (uint256, uint256) {
4645
require(msg.value == amount, NativeAmountMismatch());
47-
_supplyNative(spoke, reserveId, msg.sender, amount);
46+
return _supplyNative(spoke, reserveId, msg.sender, amount);
4847
}
4948

5049
/// @inheritdoc INativeTokenGateway
5150
function supplyAsCollateralNative(
5251
address spoke,
5352
uint256 reserveId,
5453
uint256 amount
55-
) external payable nonReentrant onlyRegisteredSpoke(spoke) {
54+
) external payable nonReentrant onlyRegisteredSpoke(spoke) returns (uint256, uint256) {
5655
require(msg.value == amount, NativeAmountMismatch());
57-
_supplyNative(spoke, reserveId, msg.sender, amount);
56+
(uint256 suppliedShares, uint256 suppliedAmount) = _supplyNative(
57+
spoke,
58+
reserveId,
59+
msg.sender,
60+
amount
61+
);
5862
ISpoke(spoke).setUsingAsCollateral(reserveId, true, msg.sender);
63+
64+
return (suppliedShares, suppliedAmount);
5965
}
6066

6167
/// @inheritdoc INativeTokenGateway
6268
function withdrawNative(
6369
address spoke,
6470
uint256 reserveId,
6571
uint256 amount
66-
) external onlyRegisteredSpoke(spoke) {
72+
) external onlyRegisteredSpoke(spoke) returns (uint256, uint256) {
6773
(address underlying, ) = _getReserveData(spoke, reserveId);
6874
_validateParams(underlying, amount);
6975

70-
uint256 withdrawAmount = MathUtils.min(
76+
(uint256 withdrawnShares, uint256 withdrawnAmount) = ISpoke(spoke).withdraw(
77+
reserveId,
7178
amount,
72-
ISpoke(spoke).getUserSuppliedAssets(reserveId, msg.sender)
79+
msg.sender
7380
);
81+
_nativeWrapper.withdraw(withdrawnAmount);
82+
msg.sender.safeTransferETH(withdrawnAmount);
7483

75-
ISpoke(spoke).withdraw(reserveId, withdrawAmount, msg.sender);
76-
_nativeWrapper.withdraw(withdrawAmount);
77-
msg.sender.safeTransferETH(withdrawAmount);
84+
return (withdrawnShares, withdrawnAmount);
7885
}
7986

8087
/// @inheritdoc INativeTokenGateway
8188
function borrowNative(
8289
address spoke,
8390
uint256 reserveId,
8491
uint256 amount
85-
) external onlyRegisteredSpoke(spoke) {
92+
) external onlyRegisteredSpoke(spoke) returns (uint256, uint256) {
8693
(address underlying, ) = _getReserveData(spoke, reserveId);
8794
_validateParams(underlying, amount);
8895

89-
ISpoke(spoke).borrow(reserveId, amount, msg.sender);
90-
_nativeWrapper.withdraw(amount);
91-
msg.sender.safeTransferETH(amount);
96+
(uint256 borrowedShares, uint256 borrowedAmount) = ISpoke(spoke).borrow(
97+
reserveId,
98+
amount,
99+
msg.sender
100+
);
101+
_nativeWrapper.withdraw(borrowedAmount);
102+
msg.sender.safeTransferETH(borrowedAmount);
103+
104+
return (borrowedShares, borrowedAmount);
92105
}
93106

94107
/// @inheritdoc INativeTokenGateway
95108
function repayNative(
96109
address spoke,
97110
uint256 reserveId,
98111
uint256 amount
99-
) external payable nonReentrant onlyRegisteredSpoke(spoke) {
112+
) external payable nonReentrant onlyRegisteredSpoke(spoke) returns (uint256, uint256) {
100113
require(msg.value == amount, NativeAmountMismatch());
101114
(address underlying, address hub) = _getReserveData(spoke, reserveId);
102115
_validateParams(underlying, amount);
@@ -111,11 +124,17 @@ contract NativeTokenGateway is INativeTokenGateway, GatewayBase, ReentrancyGuard
111124

112125
_nativeWrapper.deposit{value: repayAmount}();
113126
address(_nativeWrapper).safeApproveWithRetry(hub, repayAmount);
114-
ISpoke(spoke).repay(reserveId, repayAmount, msg.sender);
127+
(uint256 repaidShares, uint256 repaidAmount) = ISpoke(spoke).repay(
128+
reserveId,
129+
repayAmount,
130+
msg.sender
131+
);
115132

116133
if (leftovers > 0) {
117134
msg.sender.safeTransferETH(leftovers);
118135
}
136+
137+
return (repaidShares, repaidAmount);
119138
}
120139

121140
/// @inheritdoc INativeTokenGateway
@@ -124,13 +143,18 @@ contract NativeTokenGateway is INativeTokenGateway, GatewayBase, ReentrancyGuard
124143
}
125144

126145
/// @dev `msg.value` verification must be done before calling this.
127-
function _supplyNative(address spoke, uint256 reserveId, address user, uint256 amount) internal {
146+
function _supplyNative(
147+
address spoke,
148+
uint256 reserveId,
149+
address user,
150+
uint256 amount
151+
) internal returns (uint256, uint256) {
128152
(address underlying, address hub) = _getReserveData(spoke, reserveId);
129153
_validateParams(underlying, amount);
130154

131155
_nativeWrapper.deposit{value: amount}();
132156
address(_nativeWrapper).safeApproveWithRetry(hub, amount);
133-
ISpoke(spoke).supply(reserveId, amount, user);
157+
return ISpoke(spoke).supply(reserveId, amount, user);
134158
}
135159

136160
function _validateParams(address underlying, uint256 amount) internal view {

src/position-manager/SignatureGateway.sol

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import {EIP712} from 'src/dependencies/solady/EIP712.sol';
99
import {MathUtils} from 'src/libraries/math/MathUtils.sol';
1010
import {NoncesKeyed} from 'src/utils/NoncesKeyed.sol';
1111
import {Multicall} from 'src/utils/Multicall.sol';
12-
import {ISpoke} from 'src/spoke/interfaces/ISpoke.sol';
1312
import {EIP712Hash, EIP712Types} from 'src/position-manager/libraries/EIP712Hash.sol';
1413
import {GatewayBase} from 'src/position-manager/GatewayBase.sol';
14+
import {ISpoke} from 'src/spoke/interfaces/ISpoke.sol';
1515
import {ISignatureGateway} from 'src/position-manager/interfaces/ISignatureGateway.sol';
1616

1717
/// @title SignatureGateway
@@ -32,7 +32,7 @@ contract SignatureGateway is ISignatureGateway, GatewayBase, NoncesKeyed, Multic
3232
function supplyWithSig(
3333
EIP712Types.Supply calldata params,
3434
bytes calldata signature
35-
) external onlyRegisteredSpoke(params.spoke) {
35+
) external onlyRegisteredSpoke(params.spoke) returns (uint256, uint256) {
3636
require(block.timestamp <= params.deadline, InvalidSignature());
3737
address spoke = params.spoke;
3838
uint256 reserveId = params.reserveId;
@@ -45,14 +45,14 @@ contract SignatureGateway is ISignatureGateway, GatewayBase, NoncesKeyed, Multic
4545
underlying.safeTransferFrom(user, address(this), params.amount);
4646
underlying.safeApproveWithRetry(hub, params.amount);
4747

48-
ISpoke(spoke).supply(reserveId, params.amount, user);
48+
return ISpoke(spoke).supply(reserveId, params.amount, user);
4949
}
5050

5151
/// @inheritdoc ISignatureGateway
5252
function withdrawWithSig(
5353
EIP712Types.Withdraw calldata params,
5454
bytes calldata signature
55-
) external onlyRegisteredSpoke(params.spoke) {
55+
) external onlyRegisteredSpoke(params.spoke) returns (uint256, uint256) {
5656
require(block.timestamp <= params.deadline, InvalidSignature());
5757
address spoke = params.spoke;
5858
uint256 reserveId = params.reserveId;
@@ -62,20 +62,21 @@ contract SignatureGateway is ISignatureGateway, GatewayBase, NoncesKeyed, Multic
6262
_useCheckedNonce(user, params.nonce);
6363

6464
(address underlying, ) = _getReserveData(spoke, reserveId);
65-
uint256 withdrawAmount = MathUtils.min(
65+
(uint256 withdrawnShares, uint256 withdrawnAmount) = ISpoke(spoke).withdraw(
66+
reserveId,
6667
params.amount,
67-
ISpoke(spoke).getUserSuppliedAssets(reserveId, user)
68+
user
6869
);
70+
underlying.safeTransfer(user, withdrawnAmount);
6971

70-
ISpoke(spoke).withdraw(reserveId, withdrawAmount, user);
71-
underlying.safeTransfer(user, withdrawAmount);
72+
return (withdrawnShares, withdrawnAmount);
7273
}
7374

7475
/// @inheritdoc ISignatureGateway
7576
function borrowWithSig(
7677
EIP712Types.Borrow calldata params,
7778
bytes calldata signature
78-
) external onlyRegisteredSpoke(params.spoke) {
79+
) external onlyRegisteredSpoke(params.spoke) returns (uint256, uint256) {
7980
require(block.timestamp <= params.deadline, InvalidSignature());
8081
address spoke = params.spoke;
8182
uint256 reserveId = params.reserveId;
@@ -85,16 +86,21 @@ contract SignatureGateway is ISignatureGateway, GatewayBase, NoncesKeyed, Multic
8586
_useCheckedNonce(user, params.nonce);
8687

8788
(address underlying, ) = _getReserveData(spoke, reserveId);
89+
(uint256 borrowedShares, uint256 borrowedAmount) = ISpoke(spoke).borrow(
90+
reserveId,
91+
params.amount,
92+
user
93+
);
94+
underlying.safeTransfer(user, borrowedAmount);
8895

89-
ISpoke(spoke).borrow(reserveId, params.amount, user);
90-
underlying.safeTransfer(user, params.amount);
96+
return (borrowedShares, borrowedAmount);
9197
}
9298

9399
/// @inheritdoc ISignatureGateway
94100
function repayWithSig(
95101
EIP712Types.Repay calldata params,
96102
bytes calldata signature
97-
) external onlyRegisteredSpoke(params.spoke) {
103+
) external onlyRegisteredSpoke(params.spoke) returns (uint256, uint256) {
98104
require(block.timestamp <= params.deadline, InvalidSignature());
99105
address spoke = params.spoke;
100106
uint256 reserveId = params.reserveId;
@@ -112,7 +118,7 @@ contract SignatureGateway is ISignatureGateway, GatewayBase, NoncesKeyed, Multic
112118
underlying.safeTransferFrom(user, address(this), repayAmount);
113119
underlying.safeApproveWithRetry(hub, repayAmount);
114120

115-
ISpoke(spoke).repay(reserveId, repayAmount, user);
121+
return ISpoke(spoke).repay(reserveId, repayAmount, user);
116122
}
117123

118124
/// @inheritdoc ISignatureGateway

0 commit comments

Comments
 (0)