Skip to content
This repository has been archived by the owner on May 16, 2024. It is now read-only.

Commit

Permalink
CORE-2033 Add RegistrationV4 contract (#422)
Browse files Browse the repository at this point in the history
* update OpenAPI generated API client code

* add prepare withdrawal v2 workflow

* fix typo

* update error message

* rename vars

* add v2 complete withdrawal

* update

* move location

* add v4 starkex contracts

* add v2 registration contract bindings

* add readme

* update

* update

* add v4 stark contract

* update

* add stark v4 factory

* rename core factory to stark v3

* update

* update examples

* update

* update

* fix bad refs

* add stark v4 factory

* rename core factory to stark v3

* update

* add v2 complete withdrawal

* update

* add v4 stark contract

* update

* update examples

* update changelog and version

* changelog

* rename from v2 to v4

* bump version

* update readme

* minor change

* update solidity contracts

* update contract

* bump version

* regenerate
  • Loading branch information
kaihirota authored Feb 27, 2024
1 parent 3a12f4c commit 0a60a44
Show file tree
Hide file tree
Showing 36 changed files with 6,126 additions and 4,758 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [3.2.0] - 2024-02-27

### Added

- Add V4 registration contract bindings.

## [3.1.0] - 2024-02-27

### Added

- Remove experimental flag for primary sales functions

## [3.0.0] - 2024-02-26
Expand Down
File renamed without changes.
File renamed without changes.
277 changes: 277 additions & 0 deletions contracts/v4/CoreV4.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,277 @@
// Copyright (c) Immutable Pty Ltd 2018 - 2024
// SPDX-License-Identifier: MIT
//
// This contract is an interface for the StarkEx Core contract v4 version.
// It is used to interact with the StarkEx Core contract from the Registration contract.
// The Core contract is used to register and withdraw users and assets from the StarkEx system.
//
// This file was generated using the abi-to-sol tool.
// the StarkEx contract ABI that was provided by StarkWare via slack.
pragma solidity ^0.8.19;

interface CoreV4 {
fallback() external payable;

function VERSION() external view returns (string memory);

function initialize(bytes memory data) external;

receive() external payable;

event LogFrozen();
event LogNewGovernorAccepted(address acceptedGovernor);
event LogNominatedGovernor(address nominatedGovernor);
event LogNominationCancelled();
event LogRegistered(address entry, string entryId);
event LogRemovalIntent(address entry, string entryId);
event LogRemoved(address entry, string entryId);
event LogRemovedGovernor(address removedGovernor);
event LogUnFrozen();

function DEPOSIT_CANCEL_DELAY() external view returns (uint256);

function FREEZE_GRACE_PERIOD() external view returns (uint256);

function MAIN_GOVERNANCE_INFO_TAG() external view returns (string memory);

function MAX_FORCED_ACTIONS_REQS_PER_BLOCK() external view returns (uint256);

function MAX_VERIFIER_COUNT() external view returns (uint256);

function UNFREEZE_DELAY() external view returns (uint256);

function VERIFIER_REMOVAL_DELAY() external view returns (uint256);

function announceAvailabilityVerifierRemovalIntent(address verifier) external;

function announceVerifierRemovalIntent(address verifier) external;

function getRegisteredAvailabilityVerifiers() external view returns (address[] memory _verifers);

function getRegisteredVerifiers() external view returns (address[] memory _verifers);

function isAvailabilityVerifier(address verifierAddress) external view returns (bool);

function isFrozen() external view returns (bool);

function isVerifier(address verifierAddress) external view returns (bool);

function mainAcceptGovernance() external;

function mainCancelNomination() external;

function mainIsGovernor(address testGovernor) external view returns (bool);

function mainNominateNewGovernor(address newGovernor) external;

function mainRemoveGovernor(address governorForRemoval) external;

function registerAvailabilityVerifier(address verifier, string memory identifier) external;

function registerVerifier(address verifier, string memory identifier) external;

function removeAvailabilityVerifier(address verifier) external;

function removeVerifier(address verifier) external;

function unFreeze() external;

event LogDeposit(
address depositorEthKey,
uint256 starkKey,
uint256 vaultId,
uint256 assetType,
uint256 nonQuantizedAmount,
uint256 quantizedAmount
);
event LogDepositCancel(uint256 starkKey, uint256 vaultId, uint256 assetId);
event LogDepositCancelReclaimed(
uint256 starkKey, uint256 vaultId, uint256 assetType, uint256 nonQuantizedAmount, uint256 quantizedAmount
);
event LogDepositNftCancelReclaimed(
uint256 starkKey, uint256 vaultId, uint256 assetType, uint256 tokenId, uint256 assetId
);
event LogMintWithdrawalPerformed(
uint256 ownerKey, uint256 assetType, uint256 nonQuantizedAmount, uint256 quantizedAmount, uint256 assetId
);
event LogMintableWithdrawalAllowed(uint256 ownerKey, uint256 assetId, uint256 quantizedAmount);
event LogNftDeposit(
address depositorEthKey, uint256 starkKey, uint256 vaultId, uint256 assetType, uint256 tokenId, uint256 assetId
);
event LogNftWithdrawalAllowed(uint256 ownerKey, uint256 assetId);
event LogNftWithdrawalPerformed(
uint256 ownerKey, uint256 assetType, uint256 tokenId, uint256 assetId, address recipient
);
event LogTokenAdminAdded(address tokenAdmin);
event LogTokenAdminRemoved(address tokenAdmin);
event LogTokenRegistered(uint256 assetType, bytes assetInfo, uint256 quantum);
event LogUserRegistered(address ethKey, uint256 starkKey, address sender);
event LogWithdrawalAllowed(
uint256 ownerKey, uint256 assetType, uint256 nonQuantizedAmount, uint256 quantizedAmount
);
event LogWithdrawalPerformed(
uint256 ownerKey, uint256 assetType, uint256 nonQuantizedAmount, uint256 quantizedAmount, address recipient
);

function defaultVaultWithdrawalLock() external view returns (uint256);

function deposit(uint256 starkKey, uint256 assetType, uint256 vaultId) external payable;

function deposit(uint256 starkKey, uint256 assetType, uint256 vaultId, uint256 quantizedAmount) external;

function depositCancel(uint256 starkKey, uint256 assetId, uint256 vaultId) external;

function depositERC20(uint256 starkKey, uint256 assetType, uint256 vaultId, uint256 quantizedAmount) external;

function depositEth(uint256 starkKey, uint256 assetType, uint256 vaultId) external payable;

function depositNft(uint256 starkKey, uint256 assetType, uint256 vaultId, uint256 tokenId) external;

function depositNftReclaim(uint256 starkKey, uint256 assetType, uint256 vaultId, uint256 tokenId) external;

function depositReclaim(uint256 starkKey, uint256 assetId, uint256 vaultId) external;

function getActionCount() external view returns (uint256);

function getActionHashByIndex(uint256 actionIndex) external view returns (bytes32);

function getAssetInfo(uint256 assetType) external view returns (bytes memory assetInfo);

function getCancellationRequest(uint256 starkKey, uint256 assetId, uint256 vaultId)
external
view
returns (uint256 request);

function getDepositBalance(uint256 starkKey, uint256 assetId, uint256 vaultId)
external
view
returns (uint256 balance);

function getEthKey(uint256 ownerKey) external view returns (address);

function getFullWithdrawalRequest(uint256 starkKey, uint256 vaultId) external view returns (uint256 res);

function getQuantizedDepositBalance(uint256 starkKey, uint256 assetId, uint256 vaultId)
external
view
returns (uint256 balance);

function getQuantum(uint256 presumedAssetType) external view returns (uint256 quantum);

function getWithdrawalBalance(uint256 ownerKey, uint256 assetId) external view returns (uint256 balance);

function isAssetRegistered(uint256 assetType) external view returns (bool);

function isTokenAdmin(address testedAdmin) external view returns (bool);

function onERC721Received(address, address, uint256, bytes memory) external returns (bytes4);

function orderRegistryAddress() external view returns (address);

function registerAndDepositERC20(
address ethKey,
uint256 starkKey,
bytes memory signature,
uint256 assetType,
uint256 vaultId,
uint256 quantizedAmount
) external;

function registerAndDepositEth(
address ethKey,
uint256 starkKey,
bytes memory signature,
uint256 assetType,
uint256 vaultId
) external payable;

function registerEthAddress(address ethKey, uint256 starkKey, bytes memory starkSignature) external;

function registerSender(uint256 starkKey, bytes memory starkSignature) external;

function registerToken(uint256 assetType, bytes memory assetInfo) external;

function registerToken(uint256 assetType, bytes memory assetInfo, uint256 quantum) external;

function registerTokenAdmin(address newAdmin) external;

function unregisterTokenAdmin(address oldAdmin) external;

function withdraw(uint256 ownerKey, uint256 assetType) external;

function withdrawAndMint(uint256 ownerKey, uint256 assetType, bytes memory mintingBlob) external;

function withdrawNft(uint256 ownerKey, uint256 assetType, uint256 tokenId) external;

event LogOperatorAdded(address operator);
event LogOperatorRemoved(address operator);
event LogRootUpdate(uint256 sequenceNumber, uint256 batchId, uint256 vaultRoot, uint256 orderRoot);
event LogStateTransitionFact(bytes32 stateTransitionFact);
event LogVaultBalanceChangeApplied(address ethKey, uint256 assetId, uint256 vaultId, int256 quantizedAmountChange);

function STARKEX_MAX_DEFAULT_VAULT_LOCK() external view returns (uint256);

function escape(uint256 starkKey, uint256 vaultId, uint256 assetId, uint256 quantizedAmount) external;

function getLastBatchId() external view returns (uint256 batchId);

function getOrderRoot() external view returns (uint256 root);

function getOrderTreeHeight() external view returns (uint256 height);

function getSequenceNumber() external view returns (uint256 seq);

function getVaultRoot() external view returns (uint256 root);

function getVaultTreeHeight() external view returns (uint256 height);

function isOperator(address testedOperator) external view returns (bool);

function registerOperator(address newOperator) external;

function unregisterOperator(address removedOperator) external;

function updateState(uint256[] memory publicInput, uint256[] memory applicationData) external;

event LogFullWithdrawalRequest(uint256 starkKey, uint256 vaultId);

function freezeRequest(uint256 starkKey, uint256 vaultId) external;

function fullWithdrawalRequest(uint256 starkKey, uint256 vaultId) external;

event LogDefaultVaultWithdrawalLockSet(uint256 newDefaultLockTime);
event LogDepositToVault(
address ethKey, uint256 assetId, uint256 vaultId, uint256 nonQuantizedAmount, uint256 quantizedAmount
);
event LogVaultWithdrawalLockSet(address ethKey, uint256 assetId, uint256 vaultId, uint256 timeRelease);
event LogWithdrawalFromVault(
address ethKey, uint256 assetId, uint256 vaultId, uint256 nonQuantizedAmount, uint256 quantizedAmount
);

function depositERC20ToVault(uint256 assetId, uint256 vaultId, uint256 quantizedAmount) external;

function depositEthToVault(uint256 assetId, uint256 vaultId) external payable;

function getQuantizedVaultBalance(address ethKey, uint256 assetId, uint256 vaultId)
external
view
returns (uint256);

function getVaultBalance(address ethKey, uint256 assetId, uint256 vaultId) external view returns (uint256);

function getVaultWithdrawalLock(address ethKey, uint256 assetId, uint256 vaultId) external view returns (uint256);

function isStrictVaultBalancePolicy() external view returns (bool);

function isVaultLocked(address ethKey, uint256 assetId, uint256 vaultId) external view returns (bool);

function lockVault(uint256 assetId, uint256 vaultId, uint256 lockTime) external;

function setDefaultVaultWithdrawalLock(uint256 newDefaultTime) external;

function withdrawFromVault(uint256 assetId, uint256 vaultId, uint256 quantizedAmount) external;

event ImplementationActivationRescheduled(address indexed implementation, uint256 updatedActivationTime);

function updateImplementationActivationTime(address implementation, bytes memory data, bool finalize) external;
}
6 changes: 6 additions & 0 deletions contracts/v4/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# About

Contracts in this directory are for interacting with StarkEx contracts with version >= 4.0.

- `Core.sol` - wrapper for StarkEx V4 contract.
- `Registration.sol` - Version 2 of the registration wrapper contract, which adds helper functions like `withdrawAll` and `registerAndWithdrawAll`, which can be used in edge cases where a user has prepared withdrawals in StarkEx v3 and v4 contract, and wishes to withdraw both in one transaction. It is named RegistrationV4 because it is designed to interact with StarkEx V4 contract.
81 changes: 81 additions & 0 deletions contracts/v4/RegistrationV4.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright (c) Immutable Pty Ltd 2018 - 2024
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

import {CoreV4} from "./CoreV4.sol";

/**
* Emitted when there are no funds to withdraw when calling `withdrawAll`.
*/
error NoFundsToWithdraw(uint256 ethKey, uint256 starkKey);

/**
* RegistrationV4 is a wrapper around the StarkEx contract to provide a more user-friendly interface for executing multiple transactions on the StarkEx contract at once.
* This contract is not upgradeable. If an issue is found with this contract, a new version will be deployed.
*/
contract RegistrationV4 {
CoreV4 public immutable imx;

constructor(address payable _imx) {
imx = CoreV4(_imx);
}

function registerAndWithdrawAll(address ethKey, uint256 starkKey, bytes calldata signature, uint256 assetType)
external
{
if (!isRegistered(starkKey)) {
imx.registerEthAddress(ethKey, starkKey, signature);
}
withdrawAll(uint160(ethKey), starkKey, assetType);
}

function withdrawAll(uint256 ethKey, uint256 starkKey, uint256 assetType) public {
uint256 ethKeyBalance = imx.getWithdrawalBalance(ethKey, assetType);
uint256 starkKeyBalance = imx.getWithdrawalBalance(starkKey, assetType);
if (ethKeyBalance == 0 && starkKeyBalance == 0) {
revert NoFundsToWithdraw(ethKey, starkKey);
}

if (ethKeyBalance > 0) {
imx.withdraw(ethKey, assetType);
}

if (starkKeyBalance > 0) {
imx.withdraw(starkKey, assetType);
}
}

function registerAndWithdrawNft(
address ethKey,
uint256 starkKey,
bytes calldata signature,
uint256 assetType,
uint256 tokenId
) external {
if (!isRegistered(starkKey)) {
imx.registerEthAddress(ethKey, starkKey, signature);
}
imx.withdrawNft(starkKey, assetType, tokenId);
}

function registerWithdrawAndMint(
address ethKey,
uint256 starkKey,
bytes calldata signature,
uint256 assetType,
bytes calldata mintingBlob
) external {
if (!isRegistered(starkKey)) {
imx.registerEthAddress(ethKey, starkKey, signature);
}
imx.withdrawAndMint(starkKey, assetType, mintingBlob);
}

function getVersion() external view returns (string memory) {
return imx.VERSION();
}

function isRegistered(uint256 starkKey) public view returns (bool) {
return imx.getEthKey(starkKey) != address(0);
}
}
2 changes: 1 addition & 1 deletion hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ require('@typechain/hardhat');
require('@nomiclabs/hardhat-ethers');

const config = {
solidity: '0.8.11',
solidity: '0.8.19',
typechain: {
outDir: 'src/contracts',
target: 'ethers-v5',
Expand Down
Loading

0 comments on commit 0a60a44

Please sign in to comment.