Skip to content

Commit

Permalink
Use env-enc to pass functions urls
Browse files Browse the repository at this point in the history
  • Loading branch information
shanejearley committed Sep 7, 2023
1 parent bdc7b17 commit 1562361
Show file tree
Hide file tree
Showing 15 changed files with 197 additions and 211 deletions.
14 changes: 7 additions & 7 deletions contracts/ethereum/helpers/upkeep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export async function fulfillReport({
const requestIds = (await upkeep.queryFilter(upkeep.filters.RequestSent())).slice(-2).map((event) => event.args.id)

const balancesRequestId = requestIds[0]
const balancesResponseBytes = ethers.utils.defaultAbiCoder.encode(
const balancesResponse = ethers.utils.defaultAbiCoder.encode(
['uint128', 'uint128'],
[ethers.utils.parseEther(activeBalance.toString()), ethers.utils.parseEther(sweptBalance.toString())]
)
Expand All @@ -54,11 +54,11 @@ export async function fulfillReport({
keeper,
functionsBillingRegistry,
requestId: balancesRequestId,
responseBytes: balancesResponseBytes
response: balancesResponse
})

const detailsRequestId = requestIds[1]
const detailsResponseBytes = ethers.utils.defaultAbiCoder.encode(
const detailsResponse = ethers.utils.defaultAbiCoder.encode(
['uint32', 'uint32', 'uint32', 'uint32[5]'],
[activatedDeposits, forcedExits, completedExits, compoundablePoolIds]
)
Expand All @@ -67,20 +67,20 @@ export async function fulfillReport({
keeper,
functionsBillingRegistry,
requestId: detailsRequestId,
responseBytes: detailsResponseBytes
response: detailsResponse
})
}

export async function fulfillFunctionsRequest({
keeper,
functionsBillingRegistry,
requestId,
responseBytes
response
}: {
keeper: SignerWithAddress,
functionsBillingRegistry: FunctionsBillingRegistry,
requestId: string,
responseBytes: string
response: string
}) {
const dummyTransmitter = keeper.address
const dummySigners = Array(31).fill(dummyTransmitter)
Expand All @@ -89,7 +89,7 @@ export async function fulfillFunctionsRequest({

const fulfillAndBill = await functionsBillingRegistry.connect(keeper).fulfillAndBill(
requestId,
responseBytes,
response,
'0x',
dummyTransmitter,
dummySigners,
Expand Down
6 changes: 3 additions & 3 deletions contracts/ethereum/scripts/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ void async function () {
await result.wait()
}

const secrets = '0x' // Parse requestConfig.secrets and encrypt if necessary
const requestCBOR = await upkeep.generateRequest(requestConfig.source, secrets, requestConfig.args)
const requestSource = requestConfig.source
const requestArgs = requestConfig.args
const fulfillGasLimit = 300000
const setRequest = await manager.setFunctionsRequest(requestCBOR, fulfillGasLimit)
const setRequest = await manager.setFunctionsRequest(requestSource, requestArgs, fulfillGasLimit)
await setRequest.wait()

await functionsBillingRegistry.setAuthorizedSenders([keeper.address, manager.address, upkeep.address, functionsOracle.address])
Expand Down
127 changes: 42 additions & 85 deletions contracts/ethereum/src/v1/CasimirManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,6 @@ contract CasimirManager is ICasimirManager, Ownable, ReentrancyGuard {
swapFactory = IUniswapV3Factory(swapFactoryAddress);
swapRouter = ISwapRouter(swapRouterAddress);
tokenAddresses[Token.WETH] = wethTokenAddress;

registry = new CasimirRegistry(ssvNetworkViewsAddress);
upkeep = new CasimirUpkeep(functionsOracleAddress);
}
Expand Down Expand Up @@ -543,7 +542,6 @@ contract CasimirManager is ICasimirManager, Ownable, ReentrancyGuard {

user.stakeRatioSum0 = stakeRatioSum;
user.stake0 -= amount;

if (amount <= getWithdrawableBalance()) {
if (amount <= exitedBalance) {
exitedBalance -= amount;
Expand All @@ -563,7 +561,6 @@ contract CasimirManager is ICasimirManager, Ownable, ReentrancyGuard {
);
requestedWithdrawalBalance += amount;
requestedWithdrawals++;

uint256 coveredExitBalance = requestedExits * POOL_CAPACITY;
if (requestedWithdrawalBalance > coveredExitBalance) {
uint256 exitsRequired = (requestedWithdrawalBalance -
Expand Down Expand Up @@ -601,7 +598,6 @@ contract CasimirManager is ICasimirManager, Ownable, ReentrancyGuard {
requestedWithdrawalQueue.remove(0);
withdrawalAmount += withdrawal.amount;
withdrawalCount++;

fulfillWithdrawal(withdrawal.user, withdrawal.amount);
}
if (withdrawalAmount <= exitedBalance) {
Expand Down Expand Up @@ -644,7 +640,6 @@ contract CasimirManager is ICasimirManager, Ownable, ReentrancyGuard {
uint32 poolId = readyPoolIds[0];
readyPoolIds.remove(0);
pendingPoolIds.push(poolId);

poolAddresses[poolId] = address(
new CasimirPool(
address(registry),
Expand All @@ -653,32 +648,44 @@ contract CasimirManager is ICasimirManager, Ownable, ReentrancyGuard {
operatorIds
)
);

bytes memory computedWithdrawalCredentials = abi.encodePacked(
bytes1(uint8(1)),
bytes11(0),
poolAddresses[poolId]
);

require(
keccak256(computedWithdrawalCredentials) ==
keccak256(withdrawalCredentials),
"Invalid withdrawal credentials"
);

registerPool(
poolId,
depositDataRoot,
publicKey,
signature,
withdrawalCredentials,
operatorIds,
shares,
cluster,
feeAmount,
minimumTokenAmount,
processed
);
{
for (uint256 i = 0; i < operatorIds.length; i++) {
registry.addOperatorPool(operatorIds[i], poolId);
}
beaconDeposit.deposit{value: POOL_CAPACITY}(
publicKey,
withdrawalCredentials,
signature,
depositDataRoot
);
uint256 ssvAmount = retrieveFees(
feeAmount,
minimumTokenAmount,
tokenAddresses[Token.SSV],
processed
);
ssvToken.approve(address(ssvNetwork), ssvAmount);
ssvNetwork.registerValidator(
publicKey,
operatorIds,
shares,
ssvAmount,
cluster
);

emit PoolRegistered(poolId);
}

emit DepositInitiated(poolId);
}
Expand Down Expand Up @@ -781,15 +788,13 @@ contract CasimirManager is ICasimirManager, Ownable, ReentrancyGuard {
);

stakedPoolIds.remove(poolIndex);

if (poolDetails.status == ICasimirPool.PoolStatus.EXITING_REQUESTED) {
requestedExits--;
} else if (
poolDetails.status == ICasimirPool.PoolStatus.EXITING_FORCED
) {
forcedExits--;
}

pool.setStatus(ICasimirPool.PoolStatus.WITHDRAWN);
pool.withdrawBalance(blamePercents);
ssvNetwork.removeValidator(poolDetails.publicKey, poolDetails.operatorIds, cluster);
Expand Down Expand Up @@ -919,16 +924,26 @@ contract CasimirManager is ICasimirManager, Ownable, ReentrancyGuard {

/**
* Set a new Chainlink functions request
* @param newRequestCBOR The new Chainlink functions request CBOR
* @param newRequestSource JavaScript source code
* @param newRequestArgs List of arguments accessible from within the source code
* @param newFulfillGasLimit The new Chainlink functions fulfill gas limit
*/
function setFunctionsRequest(
bytes calldata newRequestCBOR,
string calldata newRequestSource,
string[] calldata newRequestArgs,
uint32 newFulfillGasLimit
) external onlyOwner {
upkeep.setRequest(newRequestCBOR, newFulfillGasLimit);
upkeep.setRequest(
newRequestSource,
newRequestArgs,
newFulfillGasLimit
);

emit FunctionsRequestSet(newRequestCBOR, newFulfillGasLimit);
emit FunctionsRequestSet(
newRequestSource,
newRequestArgs,
newFulfillGasLimit
);
}

/**
Expand Down Expand Up @@ -1112,6 +1127,7 @@ contract CasimirManager is ICasimirManager, Ownable, ReentrancyGuard {
block.timestamp >= user.actionPeriodTimestamp + ACTION_PERIOD,
"Action period maximum reached"
);

if (block.timestamp >= user.actionPeriodTimestamp + ACTION_PERIOD) {
user.actionPeriodTimestamp = block.timestamp;
user.actionCount = 1;
Expand All @@ -1120,63 +1136,6 @@ contract CasimirManager is ICasimirManager, Ownable, ReentrancyGuard {
}
}

/**
* @dev Register a pool with Beacon and SSV
* @param poolId The pool ID
* @param depositDataRoot The deposit data root
* @param publicKey The validator public key
* @param signature The signature
* @param withdrawalCredentials The withdrawal credentials
* @param operatorIds The operator IDs
* @param shares The operator shares
* @param cluster The SSV cluster snapshot
* @param feeAmount The fee amount to deposit
* @param minimumTokenAmount The minimum SSV token amount out after processing fees
* @param processed Whether the fee amount is already processed
*/
function registerPool(
uint32 poolId,
bytes32 depositDataRoot,
bytes memory publicKey,
bytes memory signature,
bytes memory withdrawalCredentials,
uint64[] memory operatorIds,
bytes memory shares,
ISSVNetworkCore.Cluster memory cluster,
uint256 feeAmount,
uint256 minimumTokenAmount,
bool processed
) private {
for (uint256 i = 0; i < operatorIds.length; i++) {
registry.addOperatorPool(operatorIds[i], poolId);
}

beaconDeposit.deposit{value: POOL_CAPACITY}(
publicKey,
withdrawalCredentials,
signature,
depositDataRoot
);

uint256 ssvAmount = retrieveFees(
feeAmount,
minimumTokenAmount,
tokenAddresses[Token.SSV],
processed
);
ssvToken.approve(address(ssvNetwork), ssvAmount);

ssvNetwork.registerValidator(
publicKey,
operatorIds,
shares,
ssvAmount,
cluster
);

emit PoolRegistered(poolId);
}

/**
* @notice Request a given count of staked pool exits
* @param count The number of exits to request
Expand All @@ -1193,7 +1152,6 @@ contract CasimirManager is ICasimirManager, Ownable, ReentrancyGuard {
) {
count--;
index++;

pool.setStatus(ICasimirPool.PoolStatus.EXITING_REQUESTED);
requestedExits++;

Expand Down Expand Up @@ -1241,7 +1199,6 @@ contract CasimirManager is ICasimirManager, Ownable, ReentrancyGuard {
address(swapRouter),
wethToken.balanceOf(address(this))
);

IUniswapV3PoolState swapPool = IUniswapV3PoolState(
swapFactory.getPool(
tokenAddresses[Token.WETH],
Expand Down
4 changes: 2 additions & 2 deletions contracts/ethereum/src/v1/CasimirPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ contract CasimirPool is ICasimirPool, Ownable, ReentrancyGuard {
ICasimirManager private immutable manager;
/** Registry contract */
ICasimirRegistry private immutable registry;
/** Pool ID */
uint32 private immutable id;

/*********/
/* State */
/*********/

/** Pool ID */
uint32 private immutable id;
/** Validator public key */
bytes private publicKey;
/** Operator IDs */
Expand Down
2 changes: 0 additions & 2 deletions contracts/ethereum/src/v1/CasimirRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,10 @@ contract CasimirRegistry is ICasimirRegistry, Ownable {

operatorPools[operatorId][poolId] = false;
operator.poolCount -= 1;

if (operator.poolCount == 0 && operator.resharing) {
operator.active = false;
operator.resharing = false;
}

if (blameAmount > 0) {
operator.collateral -= blameAmount;
manager.depositRecoveredBalance{value: blameAmount}(poolId);
Expand Down
Loading

0 comments on commit 1562361

Please sign in to comment.