Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
karneges committed May 1, 2023
1 parent a991646 commit eba34b4
Show file tree
Hide file tree
Showing 30 changed files with 2,767 additions and 532 deletions.
51 changes: 41 additions & 10 deletions contracts/StEverAccount.tsol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 9,8 @@ import "./utils/Constants.tsol";

import "@broxus/contracts/contracts/libraries/MsgFlag.tsol";

import {IStEverAccount as OldIStEverAccount} from "./test/v4/interfaces/IStEverAccount.tsol";



contract StEverAccount is IStEverAccount {
Expand All @@ -17,7 19,7 @@ contract StEverAccount is IStEverAccount {
uint32 currentVersion; //setup from _init

// mappings
mapping(uint64 => WithdrawRequest) public withdrawRequests;
mapping(uint64 => IStEverAccount.WithdrawRequest) public withdrawRequests;

//constant
uint128 constant MAX_PENDING_COUNT = 50;
Expand Down Expand Up @@ -46,7 48,7 @@ contract StEverAccount is IStEverAccount {
view
responsible
override
returns (AccountDetails)
returns (IStEverAccount.AccountDetails)
{
return
{
Expand Down Expand Up @@ -78,10 80,10 @@ contract StEverAccount is IStEverAccount {
IStEverVault(vault).onPendingWithdrawRejected{value: 0, flag:MsgFlag.ALL_NOT_RESERVED, bounce: false}(_nonce, user, _amount, _remainingGasTo);
}

function resetPendingValues(mapping(uint64 => WithdrawRequest) rejectedWithdrawals, address _sendGasTo) override external onlyVault {
function resetPendingValues(mapping(uint64 => IStEverAccount.WithdrawRequest) rejectedWithdrawals, address _sendGasTo) override external onlyVault {
tvm.rawReserve(_reserve(), 0);

for ((uint64 nonce, WithdrawRequest rejectedWithdrawRequest) : rejectedWithdrawals) {
for ((uint64 nonce, IStEverAccount.WithdrawRequest rejectedWithdrawRequest) : rejectedWithdrawals) {
withdrawRequests[nonce] = rejectedWithdrawRequest;
}

Expand All @@ -91,7 93,7 @@ contract StEverAccount is IStEverAccount {
function removePendingWithdraw(uint64 _nonce) override external onlyVault {
tvm.rawReserve(_reserve(), 0);
if (withdrawRequests.exists(_nonce)) {
WithdrawRequest withdrawRequest = withdrawRequests[_nonce];
IStEverAccount.WithdrawRequest withdrawRequest = withdrawRequests[_nonce];
delete withdrawRequests[_nonce];
IStEverVault(vault).onPendingWithdrawRemoved{
value: 0,
Expand All @@ -106,12 108,12 @@ contract StEverAccount is IStEverAccount {
function createAndSendWithdrawToUserRequest(uint64[] _satisfiedWithdrawRequests) internal {

uint128 totalAmount = 0;
mapping(uint64 => WithdrawRequest) withdrawals;
mapping(uint64 => IStEverAccount.WithdrawRequest) withdrawals;

for (uint256 i = 0; i < _satisfiedWithdrawRequests.length; i ) {
uint64 withdrawRequestKey = _satisfiedWithdrawRequests[i];
if (withdrawRequests.exists(withdrawRequestKey)) {
WithdrawRequest withdrawRequest = withdrawRequests[withdrawRequestKey];
IStEverAccount.WithdrawRequest withdrawRequest = withdrawRequests[withdrawRequestKey];
withdrawals[withdrawRequestKey] = withdrawRequest;
delete withdrawRequests[withdrawRequestKey];
totalAmount = withdrawRequest.amount;
Expand All @@ -138,6 140,10 @@ contract StEverAccount is IStEverAccount {
for((uint64 nonce,) : withdrawRequests) {
satisfiedWithdrawRequests.push(nonce);
}
if (satisfiedWithdrawRequests.length == 0) {
user.transfer({value: 0, flag: MsgFlag.ALL_NOT_RESERVED, bounce: false});
return;
}
createAndSendWithdrawToUserRequest(satisfiedWithdrawRequests);
}

Expand All @@ -148,7 154,7 @@ contract StEverAccount is IStEverAccount {
return;
}

WithdrawRequest withdrawRequest = withdrawRequests[_proofNonce];
IStEverAccount.WithdrawRequest withdrawRequest = withdrawRequests[_proofNonce];
if ((withdrawRequest.timestamp Constants.TIME_AFTER_EMERGENCY_CAN_BE_ACTIVATED) > now) {
IStEverVault(vault).startEmergencyRejected{value: 0, flag: MsgFlag.ALL_NOT_RESERVED, bounce: false}(user, ErrorCodes.EMERGENCY_CANT_BE_ACTIVATED);
return;
Expand Down Expand Up @@ -212,8 218,33 @@ contract StEverAccount is IStEverAccount {
(user) = abi.decode(initialData, (address));

TvmCell constructorParams = s.loadRef();
(uint32 _current_version,) = abi.decode(constructorParams, (uint32, uint32));
_init(_current_version);
(uint32 _newVersion, uint32 _currentVersion) = abi.decode(constructorParams, (uint32, uint32));

if (_newVersion == _currentVersion) {
_init(_newVersion);
}

if (_newVersion != _currentVersion) {
(
address vault,
address user,
uint32 currentVersion,
mapping(uint64 => OldIStEverAccount.WithdrawRequest) oldWithdrawRequests
) = abi.decode(
s.loadRef(),
(
address,
address,
uint32,
mapping(uint64 => OldIStEverAccount.WithdrawRequest)
)
);

for ((uint64 nonce, OldIStEverAccount.WithdrawRequest oldWithdrawRequest) : oldWithdrawRequests) {
withdrawRequests[nonce] = IStEverAccount.WithdrawRequest(oldWithdrawRequest.amount, oldWithdrawRequest.timestamp, 0);
}
}


send_gas_to.transfer({value: 0, bounce: false, flag: MsgFlag.ALL_NOT_RESERVED});
}
Expand Down
73 changes: 68 additions & 5 deletions contracts/StEverVault.tsol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 9,7 @@ import "./base/StEverVaultEmergency.tsol";
import "./base/StEverStrategiesManager.tsol";
import "./base/StEverVaultStrategiesController.tsol";
import "./base/StEverVaultValidators.tsol";

import { IStEverVault as OldIStEverVault} from "./test/v4/interfaces/IStEverVault.tsol";


import "./utils/ErrorCodes.tsol";
Expand Down Expand Up @@ -270,7 270,6 @@ contract StEverVault is
address _user,
mapping(uint64 => IStEverAccount.WithdrawRequest) _withdrawals
) override external onlyAccount(_user) {

if(_withdrawals.empty()) {
return;
}
Expand Down Expand Up @@ -417,7 416,8 @@ contract StEverVault is
minStrategyDepositValue, // uint128
minStrategyWithdrawValue, // uint128
isPaused, // bool
strategyFactory, // address
strategyFactory, // address,
withdrawHoldTime, // uint64
owner, // address
accountVersion, // uint32
stEverVaultVersion, // uint32
Expand All @@ -438,9 438,72 @@ contract StEverVault is

}

// upgrade to v3.1
// upgrade to v5
function onCodeUpgrade(TvmCell _upgradeData) private {
tvm.rawReserve(_reserve(), 0);
owner.transfer({value: 0, flag: MsgFlag.ALL_NOT_RESERVED, bounce: false});
tvm.resetStorage();
address sendGasTo;
mapping(uint64 => OldIStEverVault.PendingWithdraw) oldPendingWithdrawals;
(
stEverVaultVersion,
sendGasTo,
governance,
platformCode,
accountCode,
stEverSupply,
totalAssets,
availableAssets,
totalStEverFee,
stEverWallet,
stTokenRoot,
gainFee,
stEverFeePercent,
minStrategyDepositValue,
minStrategyWithdrawValue,
isPaused,
owner,
accountVersion,
,
strategies,
oldPendingWithdrawals,
emergencyState
) = abi.decode(
_upgradeData,
(
uint32,
address,
uint256,
TvmCell,
TvmCell,
uint128,
uint128,
uint128,
uint128,
address,
address,
uint128,
uint32,
uint128,
uint128,
bool,
address,
uint32,
uint32,
mapping(address => StrategyParams),
mapping(uint64 => OldIStEverVault.PendingWithdraw),
EmergencyState
)
);

for ((uint64 oldPendingWithdraw, OldIStEverVault.PendingWithdraw oldPendingParams) : oldPendingWithdrawals) {
pendingWithdrawals[oldPendingWithdraw] = PendingWithdraw({
user: oldPendingParams.user,
amount: oldPendingParams.amount,
remainingGasTo: oldPendingParams.remainingGasTo,
unlockTime: 0
});
}

sendGasTo.transfer({value: 0, flag: MsgFlag.ALL_NOT_RESERVED, bounce: false});
}
}
4 changes: 2 additions & 2 deletions contracts/base/StEverVaultBase.tsol
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 149,10 @@ abstract contract StEverVaultBase is StEverVaultStorage {
owner.transfer({value: 0, flag: MsgFlag.ALL_NOT_RESERVED, bounce: false});
}

function setWithdrawHoldTime(uint64 _hold_time) override external onlyOwner minCallValue {
function setWithdrawHoldTimeInSeconds(uint64 _holdTime) override external onlyOwner minCallValue {
tvm.rawReserve(_reserve(), 0);

withdrawHoldTime = _hold_time;
withdrawHoldTime = _holdTime;
emit WithdrawHoldTimeUpdated(withdrawHoldTime);

owner.transfer({value: 0, flag: MsgFlag.ALL_NOT_RESERVED, bounce: false});
Expand Down
2 changes: 1 addition & 1 deletion contracts/base/StEverVaultStorage.tsol
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 28,7 @@ abstract contract StEverVaultStorage is IStEverVault {
uint128 minStrategyWithdrawValue = 100 ever;
bool isPaused;
address strategyFactory;
uint64 withdrawHoldTime;
uint64 withdrawHoldTime; // seconds

address owner;
uint32 accountVersion;
Expand Down
1 change: 0 additions & 1 deletion contracts/interfaces/IStEverAccount.tsol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 10,6 @@ interface IStEverAccount {
address vault;
uint32 version;
}

struct WithdrawRequest {
uint128 amount;
uint64 timestamp;
Expand Down
5 changes: 3 additions & 2 deletions contracts/interfaces/IStEverVault.tsol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 7,7 @@ interface IStEverVault {
// common
event PausedStateChanged(bool pauseState);
event StrategyFactoryAddressUpdated(address _strategyFactory);
event WithdrawHoldTimeUpdated(uint64 withdrawHoldTime);
event WithdrawHoldTimeUpdated(uint64 withdrawHoldTimeSeconds);
// Strategy
event StrategiesAdded(address[] strategy);
event StrategyRemoved(address strategy);
Expand Down Expand Up @@ -124,6 124,7 @@ interface IStEverVault {
StrategyState state;
}


struct PendingWithdraw {
uint128 amount;
address user;
Expand Down Expand Up @@ -248,7 249,7 @@ interface IStEverVault {
function setMinStrategyWithdrawValue(uint128 minStrategyWithdrawValue) external;
function setStEverFeePercent(uint32 _stEverFeePercent) external;
function setIsPaused(bool isPaused) external;
function setWithdrawHoldTime(uint64 _hold_time) external;
function setWithdrawHoldTimeInSeconds(uint64 _hold_time) external;

// ownership
function transferOwnership(address _newOwner, address _sendGasTo) external;
Expand Down
Loading

0 comments on commit eba34b4

Please sign in to comment.