Skip to content

Commit

Permalink
chore: prep for precompile integration
Browse files Browse the repository at this point in the history
  • Loading branch information
drewstone committed Nov 15, 2024
1 parent ed54259 commit 4631c52
Show file tree
Hide file tree
Showing 4 changed files with 315 additions and 163 deletions.
23 changes: 12 additions & 11 deletions src/assets/AssetDelegator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,45 +33,46 @@ abstract contract AssetDelegator {
/// @param _operator The operator (if applicable) for the operation
/// @param _operation The type of operation performed
/// @param _amount The amount involved in the operation
event OperationExecuted(address indexed _asset, bytes32 indexed _operator, Operation indexed _operation, uint256 _amount);
event OperationExecuted(uint128 indexed _asset, bytes32 indexed _operator, Operation indexed _operation, uint256 _amount);

/// @notice Execute a delegation operation
/// @param _operator The operator address (if applicable)
/// @param _asset The asset to operate on
/// @param _assetId The asset to operate on
/// @param _amount The amount to operate with
/// @param _operation The operation to perform
/// @return success Whether the operation was successful
function op(bytes32 _operator, address _asset, uint256 _amount, Operation _operation) public virtual returns (bool) {
function op(bytes32 _operator, uint128 _assetId, uint256 _amount, Operation _operation) public virtual returns (bool) {
uint8 result;
uint256 assetId = uint256(uint160(_asset));

// TODO: Get the asset ID from the asset address

if (_operation == Operation.Deposit) {
result = DELEGATION.deposit(assetId, _amount);
result = DELEGATION.deposit(_assetId, _amount);
if (result != 0) revert DelegationFailed();
} else if (_operation == Operation.Delegate) {
result = DELEGATION.delegate(_operator, assetId, _amount);
result = DELEGATION.delegate(_operator, _assetId, _amount);
if (result != 0) revert DelegationFailed();
} else if (_operation == Operation.ScheduleUnstake) {
result = DELEGATION.scheduleDelegatorUnstake(_operator, assetId, _amount);
result = DELEGATION.scheduleDelegatorUnstake(_operator, _assetId, _amount);
if (result != 0) revert UnstakeFailed();
} else if (_operation == Operation.CancelUnstake) {
result = DELEGATION.cancelDelegatorUnstake(_operator, assetId, _amount);
result = DELEGATION.cancelDelegatorUnstake(_operator, _assetId, _amount);
if (result != 0) revert UnstakeFailed();
} else if (_operation == Operation.ExecuteUnstake) {
result = DELEGATION.executeDelegatorUnstake();
if (result != 0) revert UnstakeFailed();
} else if (_operation == Operation.ScheduleWithdraw) {
result = DELEGATION.scheduleWithdraw(assetId, _amount);
result = DELEGATION.scheduleWithdraw(_assetId, _amount);
if (result != 0) revert WithdrawalFailed();
} else if (_operation == Operation.CancelWithdraw) {
result = DELEGATION.cancelWithdraw(assetId, _amount);
result = DELEGATION.cancelWithdraw(_assetId, _amount);
if (result != 0) revert WithdrawalFailed();
} else if (_operation == Operation.ExecuteWithdraw) {
result = DELEGATION.executeWithdraw();
if (result != 0) revert WithdrawalFailed();
}

emit OperationExecuted(_asset, _operator, _operation, _amount);
emit OperationExecuted(_assetId, _operator, _operation, _amount);
return true;
}
}
51 changes: 27 additions & 24 deletions src/assets/MasterVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ contract MasterVault is IMasterVault, ICrossChainReceiver {

ICrossChainBridgeManager public immutable bridgeManager;

mapping(uint32 => mapping(uint256 => address)) public syntheticAssets;
mapping(uint32 => mapping(uint256 => uint128)) public syntheticAssets;
mapping(address => bool) public authorizedAdapters;
mapping(bytes32 => address) public userVaults;

Expand All @@ -29,7 +29,7 @@ contract MasterVault is IMasterVault, ICrossChainReceiver {
error BridgeDispatchFailed();

event SyntheticAssetCreated(
address indexed syntheticAsset, uint32 indexed originChainId, uint256 indexed originAsset, uint256 bridgeId
uint128 indexed syntheticAsset, uint32 indexed originChainId, uint256 indexed originAsset, uint256 bridgeId
);

modifier onlyAuthorizedAdapter() {
Expand Down Expand Up @@ -88,11 +88,12 @@ contract MasterVault is IMasterVault, ICrossChainReceiver {
function _handleDepositMessage(uint32 originChainId, bytes32 sender, bytes calldata payload) internal returns (bytes memory) {
ICrossChainDelegatorMessage.DepositMessage memory message = CrossChainDelegatorMessage.decodeDepositMessage(payload);

address syntheticAsset = getOrCreateSyntheticAsset(originChainId, message.originAsset, message.bridgeId);
uint128 syntheticAsset = getOrCreateSyntheticAsset(originChainId, message.originAsset, message.bridgeId);
address userVault = _getOrCreateUserVault(message.sender);

// Mint directly to user vault
SyntheticRestakeAsset(syntheticAsset).mint(userVault, message.amount);
// TODO: Use the assets precompile to mint the userVault directly
// SyntheticRestakeAsset(syntheticAsset).mint(userVault, message.amount);
UserVault(userVault).restakingDeposit(syntheticAsset, message.amount);

return abi.encode(true);
Expand All @@ -108,8 +109,8 @@ contract MasterVault is IMasterVault, ICrossChainReceiver {
{
ICrossChainDelegatorMessage.DelegationMessage memory message = CrossChainDelegatorMessage.decodeDelegationMessage(payload);

address syntheticAsset = syntheticAssets[originChainId][message.originAsset];
if (syntheticAsset == address(0)) revert InvalidAsset(address(0));
uint128 syntheticAsset = syntheticAssets[originChainId][message.originAsset];
if (syntheticAsset == uint128(0)) revert InvalidAsset(address(0));

address userVault = _getOrCreateUserVault(message.sender);
UserVault(userVault).restakingDelegate(syntheticAsset, message.amount, message.operator);
Expand All @@ -128,8 +129,8 @@ contract MasterVault is IMasterVault, ICrossChainReceiver {
ICrossChainDelegatorMessage.ScheduleUnstakeMessage memory message =
CrossChainDelegatorMessage.decodeScheduleUnstakeMessage(payload);

address syntheticAsset = syntheticAssets[originChainId][message.originAsset];
if (syntheticAsset == address(0)) revert InvalidAsset(address(0));
uint128 syntheticAsset = syntheticAssets[originChainId][message.originAsset];
if (syntheticAsset == uint128(0)) revert InvalidAsset(address(0));

address userVault = _getOrCreateUserVault(message.sender);
UserVault(userVault).restakingScheduleUnstake(syntheticAsset, message.amount, message.operator);
Expand All @@ -148,8 +149,8 @@ contract MasterVault is IMasterVault, ICrossChainReceiver {
ICrossChainDelegatorMessage.CancelUnstakeMessage memory message =
CrossChainDelegatorMessage.decodeCancelUnstakeMessage(payload);

address syntheticAsset = syntheticAssets[originChainId][message.originAsset];
if (syntheticAsset == address(0)) revert InvalidAsset(address(0));
uint128 syntheticAsset = syntheticAssets[originChainId][message.originAsset];
if (syntheticAsset == uint128(0)) revert InvalidAsset(address(0));

address userVault = _getOrCreateUserVault(message.sender);
UserVault(userVault).restakingCancelUnstake(syntheticAsset, message.amount, message.operator);
Expand All @@ -168,8 +169,8 @@ contract MasterVault is IMasterVault, ICrossChainReceiver {
ICrossChainDelegatorMessage.ExecuteUnstakeMessage memory message =
CrossChainDelegatorMessage.decodeExecuteUnstakeMessage(payload);

address syntheticAsset = syntheticAssets[originChainId][message.originAsset];
if (syntheticAsset == address(0)) revert InvalidAsset(address(0));
uint128 syntheticAsset = syntheticAssets[originChainId][message.originAsset];
if (syntheticAsset == uint128(0)) revert InvalidAsset(address(0));

address userVault = _getOrCreateUserVault(message.sender);
// Just execute unstake, keep assets in vault
Expand All @@ -189,8 +190,8 @@ contract MasterVault is IMasterVault, ICrossChainReceiver {
ICrossChainDelegatorMessage.ScheduleWithdrawalMessage memory message =
CrossChainDelegatorMessage.decodeScheduleWithdrawalMessage(payload);

address syntheticAsset = syntheticAssets[originChainId][message.originAsset];
if (syntheticAsset == address(0)) revert InvalidAsset(address(0));
uint128 syntheticAsset = syntheticAssets[originChainId][message.originAsset];
if (syntheticAsset == uint128(0)) revert InvalidAsset(address(0));

address userVault = _getOrCreateUserVault(message.sender);
UserVault(userVault).restakingScheduleWithdraw(syntheticAsset, message.amount);
Expand All @@ -209,8 +210,8 @@ contract MasterVault is IMasterVault, ICrossChainReceiver {
ICrossChainDelegatorMessage.CancelWithdrawalMessage memory message =
CrossChainDelegatorMessage.decodeCancelWithdrawalMessage(payload);

address syntheticAsset = syntheticAssets[originChainId][message.originAsset];
if (syntheticAsset == address(0)) revert InvalidAsset(address(0));
uint128 syntheticAsset = syntheticAssets[originChainId][message.originAsset];
if (syntheticAsset == uint128(0)) revert InvalidAsset(address(0));

address userVault = _getOrCreateUserVault(message.sender);
UserVault(userVault).restakingCancelWithdraw(syntheticAsset, message.amount);
Expand All @@ -229,8 +230,8 @@ contract MasterVault is IMasterVault, ICrossChainReceiver {
ICrossChainDelegatorMessage.ExecuteWithdrawalMessage memory message =
CrossChainDelegatorMessage.decodeExecuteWithdrawalMessage(payload);

address syntheticAsset = syntheticAssets[originChainId][message.originAsset];
if (syntheticAsset == address(0)) revert InvalidAsset(address(0));
uint128 syntheticAsset = syntheticAssets[originChainId][message.originAsset];
if (syntheticAsset == uint128(0)) revert InvalidAsset(address(0));

address userVault = _getOrCreateUserVault(message.sender);

Expand All @@ -250,28 +251,30 @@ contract MasterVault is IMasterVault, ICrossChainReceiver {
// Only burn after successful message dispatch
try bridgeManager.dispatchMessage(withdrawalMessage.encode()) {
// If message dispatch succeeds, burn the synthetic asset
SyntheticRestakeAsset(syntheticAsset).burn(userVault, message.amount);
// TODO: Use the assets precompile to burn the userVault directly
// SyntheticRestakeAsset(syntheticAsset).burn(userVault, message.amount);
} catch {
revert BridgeDispatchFailed();
}

return abi.encode(true);
}

function getOrCreateSyntheticAsset(uint32 originChainId, uint256 originAsset, uint256 bridgeId) internal returns (address) {
address synthetic = syntheticAssets[originChainId][originAsset];
function getOrCreateSyntheticAsset(uint32 originChainId, uint256 originAsset, uint256 bridgeId) internal returns (uint128) {
uint128 synthetic = syntheticAssets[originChainId][originAsset];

if (synthetic == address(0)) {
if (synthetic == uint128(0)) {
string memory name = string(abi.encodePacked("Synthetic Restake ", originAsset));
string memory symbol = string(abi.encodePacked("sr", originAsset));

synthetic = address(new SyntheticRestakeAsset(name, symbol, originChainId, originAsset, bridgeId));
address syntheticAddr = address(new SyntheticRestakeAsset(name, symbol, originChainId, originAsset, bridgeId));
synthetic = uint128(uint160(syntheticAddr));
syntheticAssets[originChainId][originAsset] = synthetic;

emit SyntheticAssetCreated(synthetic, originChainId, originAsset, bridgeId);
}

return synthetic;
return uint128(uint160(synthetic));
}

function authorizeAdapter(address adapter) external {
Expand Down
Loading

0 comments on commit 4631c52

Please sign in to comment.