Skip to content

Commit

Permalink
Fix functions response (skip deploy)
Browse files Browse the repository at this point in the history
  • Loading branch information
shanejearley committed Oct 29, 2023
1 parent 089e415 commit 6c55280
Show file tree
Hide file tree
Showing 20 changed files with 1,824 additions and 307 deletions.
1,147 changes: 1,136 additions & 11 deletions contracts/ethereum/.openzeppelin/goerli.json

Large diffs are not rendered by default.

27 changes: 1 addition & 26 deletions contracts/ethereum/arguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,6 @@
* npm run verify --workspace @casimir/ethereum -- --constructor-args arguments.js --network goerli CONTRACT_ADDRESS
*/

const { ETHEREUM_CONTRACTS } = require("@casimir/env");

// const { ETHEREUM_CONTRACTS } = require("@casimir/env");

module.exports = [
// Manager
// ETHEREUM_CONTRACTS['TESTNET'].FUNCTIONS_BILLING_REGISTRY_ADDRESS,
// ETHEREUM_CONTRACTS['TESTNET'].KEEPER_REGISTRAR_ADDRESS,
// ETHEREUM_CONTRACTS['TESTNET'].KEEPER_REGISTRY_ADDRESS,
// ETHEREUM_CONTRACTS['TESTNET'].LINK_TOKEN_ADDRESS,
// ETHEREUM_CONTRACTS['TESTNET'].SSV_NETWORK_ADDRESS,
// ETHEREUM_CONTRACTS['TESTNET'].SSV_TOKEN_ADDRESS,
// ETHEREUM_CONTRACTS['TESTNET'].SWAP_FACTORY_ADDRESS,
// ETHEREUM_CONTRACTS['TESTNET'].SWAP_ROUTER_ADDRESS,
// ETHEREUM_CONTRACTS['TESTNET'].WETH_TOKEN_ADDRESS,

// Functions Billing Registry
// ETHEREUM_CONTRACTS['TESTNET'].LINK_TOKEN_ADDRESS,
// ETHEREUM_CONTRACTS['TESTNET'].LINK_ETH_FEED_ADDRESS,
// ETHEREUM_CONTRACTS['TESTNET'].FUNCTIONS_ORACLE_ADDRESS

// factoryAddress,
// functionsOracleAddress,
// compoundStake
ETHEREUM_CONTRACTS['TESTNET'].FACTORY_ADDRESS,
ETHEREUM_CONTRACTS['TESTNET'].FUNCTIONS_ORACLE_ADDRESS,
true
]
// module.exports = []
93 changes: 48 additions & 45 deletions contracts/ethereum/helpers/upkeep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,64 +46,67 @@ export async function fulfillReport({
const requestIds = (await upkeep.queryFilter(upkeep.filters.RequestSent())).slice(-2).map((event) => event.args.id)

const balancesRequestId = requestIds[0]

const balancesResponse = ethers.utils.defaultAbiCoder.encode(
['uint128', 'uint128'],
[ethers.utils.parseEther(beaconBalance.toString()), ethers.utils.parseEther(sweptBalance.toString())]
)

const fulfillBalances = await upkeep.connect(donTransmitter).fulfillRequestDirect(balancesRequestId, balancesResponse)
await fulfillBalances.wait()
// await fulfillFunctionsRequest({
// donTransmitter,
// functionsBillingRegistry,
// requestId: balancesRequestId,
// response: balancesResponse
// })
await fulfillFunctionsRequest({
donTransmitter,
functionsBillingRegistry,
requestId: balancesRequestId,
response: balancesResponse
})

console.log(ethers.utils.defaultAbiCoder.encode(['uint32'], [activatedDeposits]))
console.log(ethers.utils.defaultAbiCoder.encode(['uint32'], [forcedExits]))
console.log(ethers.utils.defaultAbiCoder.encode(['uint32'], [completedExits]))
console.log(ethers.utils.defaultAbiCoder.encode(['uint32[5]'], [compoundablePoolIds]))

const detailsRequestId = requestIds[1]
const detailsResponse = ethers.utils.defaultAbiCoder.encode(
['uint32', 'uint32', 'uint32', 'uint32[5]'],
[activatedDeposits, forcedExits, completedExits, compoundablePoolIds]
)

const fulfillDetails = await upkeep.connect(donTransmitter).fulfillRequestDirect(detailsRequestId, detailsResponse)
await fulfillDetails.wait()
// await fulfillFunctionsRequest({
// donTransmitter,
// functionsBillingRegistry,
// requestId: detailsRequestId,
// response: detailsResponse
// })
console.log('DETAILS', JSON.stringify({ detailsRequestId, detailsResponse }))
await fulfillFunctionsRequest({
donTransmitter,
functionsBillingRegistry,
requestId: detailsRequestId,
response: detailsResponse
})
}

// export async function fulfillFunctionsRequest({
// donTransmitter,
// functionsBillingRegistry,
// requestId,
// response
// }: {
// donTransmitter: SignerWithAddress,
// functionsBillingRegistry: FunctionsBillingRegistry,
// requestId: string,
// response: string
// }) {
// const dummyTransmitter = donTransmitter.address
// const dummySigners = Array(31).fill(dummyTransmitter)
export async function fulfillFunctionsRequest({
donTransmitter,
functionsBillingRegistry,
requestId,
response
}: {
donTransmitter: SignerWithAddress,
functionsBillingRegistry: FunctionsBillingRegistry,
requestId: string,
response: string
}) {
const dummyTransmitter = donTransmitter.address
const dummySigners = Array(31).fill(dummyTransmitter)

// // const { success, result, resultLog } = await simulateRequest(requestConfig)
// const { success, result, resultLog } = await simulateRequest(requestConfig)

// const fulfillAndBill = await functionsBillingRegistry.connect(donTransmitter).fulfillAndBill(
// requestId,
// response,
// '0x',
// dummyTransmitter,
// dummySigners,
// 4,
// 100_000,
// 500_000,
// {
// gasLimit: 500_000,
// }
// )
// await fulfillAndBill.wait()
// }
const fulfillAndBill = await functionsBillingRegistry.connect(donTransmitter).fulfillAndBill(
requestId,
response,
'0x',
dummyTransmitter,
dummySigners,
4,
100_000,
500_000,
{
gasLimit: 500_000,
}
)
await fulfillAndBill.wait()
}
14 changes: 6 additions & 8 deletions contracts/ethereum/scripts/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ void async function () {
await factory.deployed()
console.log(`CasimirFactory contract deployed at ${factory.address}`)

const defaultStrategy = {
const baseStrategy = {
minCollateral: ethers.utils.parseEther('1.0'),
lockPeriod: 0,
userFee: 5,
Expand All @@ -149,23 +149,21 @@ void async function () {
const deployBaseManager = await factory.deployManager(
daoOracle.address,
functionsOracle.address,
defaultStrategy
baseStrategy
)
await deployBaseManager.wait()
const [managerId] = await factory.getManagerIds()
const [managerAddress, registryAddress, upkeepAddress, viewsAddress] = await factory.getManagerConfig(managerId)
console.log(`Default CasimirManager contract deployed to ${managerAddress}`)
console.log(`Default CasimirRegistry contract deployed to ${registryAddress}`)
console.log(`Default CasimirUpkeep contract deployed to ${upkeepAddress}`)
console.log(`Default CasimirViews contract deployed to ${viewsAddress}`)
console.log(`Base CasimirManager contract deployed to ${managerAddress}`)
console.log(`Base CasimirRegistry contract deployed to ${registryAddress}`)
console.log(`Base CasimirUpkeep contract deployed to ${upkeepAddress}`)
console.log(`Base CasimirViews contract deployed to ${viewsAddress}`)
const upkeep = await ethers.getContractAt('CasimirUpkeep', upkeepAddress) as CasimirUpkeep

requestConfig.args[1] = viewsAddress
const fulfillGasLimit = 300000
const setRequest = await upkeep.setFunctionsRequest(requestConfig.source, requestConfig.args, fulfillGasLimit)
await setRequest.wait()
const setTransmitter = await upkeep.setTransmitter(donTransmitter.address)
await setTransmitter.wait()

await functionsBillingRegistry.setAuthorizedSenders([donTransmitter.address, functionsOracle.address])
await functionsOracle.setRegistry(functionsBillingRegistry.address)
Expand Down
15 changes: 7 additions & 8 deletions contracts/ethereum/scripts/report.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import { ethers } from 'hardhat'
import { CasimirFactory, CasimirUpkeep, FunctionsBillingRegistry } from '../build/@types'
import { CasimirFactory, CasimirUpkeep } from '../build/@types'
import ICasimirFactoryAbi from '../build/abi/ICasimirFactory.json'
import ICasimirUpkeepAbi from '../build/abi/ICasimirUpkeep.json'
import FunctionsBillingRegistryAbi from '../build/abi/FunctionsBillingRegistry.json'
import FunctionsOracleAbi from '../build/abi/FunctionsOracle.json'
import { ETHEREUM_CONTRACTS, ETHEREUM_RPC_URL } from '@casimir/env'

void async function() {
if (!process.env.ETHEREUM_RPC_URL) throw new Error('No ethereum rpc url provided')
const [owner, , donTransmitter] = await ethers.getSigners()
process.env.ETHEREUM_RPC_URL = ETHEREUM_RPC_URL['TESTNET']
process.env.FUNCTIONS_BILLING_REGISTRY_ADDRESS = ETHEREUM_CONTRACTS['TESTNET'].FUNCTIONS_BILLING_REGISTRY_ADDRESS
process.env.FUNCTIONS_ORACLE_ADDRESS = ETHEREUM_CONTRACTS['TESTNET'].FUNCTIONS_ORACLE_ADDRESS
const [owner] = await ethers.getSigners()
const provider = new ethers.providers.JsonRpcProvider(ETHEREUM_RPC_URL['TESTNET'])

const factory = new ethers.Contract(ETHEREUM_CONTRACTS['TESTNET'].FACTORY_ADDRESS, ICasimirFactoryAbi, provider) as CasimirFactory
const [managerId] = await factory.getManagerIds()
const managerConfig = await factory.getManagerConfig(managerId)
const upkeep = new ethers.Contract(managerConfig.upkeepAddress, ICasimirUpkeepAbi, provider) as CasimirUpkeep
// const resetReport = await upkeep.connect(owner).resetReport(0, 0, 0, 0, 0)
// await resetReport.wait()
// const requestReport = await upkeep.connect(owner).requestReport()
// await requestReport.wait()
const requestReport = await upkeep.connect(owner).requestReport()
await requestReport.wait()
}()
1 change: 0 additions & 1 deletion contracts/ethereum/scripts/upgrade-proxies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { ethers, upgrades } from 'hardhat'
* Upgrade ethereum contracts
*/
void async function () {
// Redeploying functions billing registry for updates
if (!process.env.FUNCTIONS_BILLING_REGISTRY_ADDRESS) throw new Error('No functions billing registry address provided')
if (!process.env.BEACON_LIBRARY_ADDRESS) throw new Error('No beacon library address provided')
if (!process.env.MANAGER_BEACON_ADDRESS) throw new Error('No manager beacon address provided')
Expand Down
36 changes: 8 additions & 28 deletions contracts/ethereum/src/v1/CasimirUpkeep.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ contract CasimirUpkeep is
uint256 private reportCompletedExits;
/// @dev Current report compoundable pools
uint32[5] private reportCompoundablePoolIds;
/// @dev Finalizable compoundable pools
/// @dev Finalizable compoundable pools (not used, will be removed in future version)
uint32[5] private finalizableCompoundablePoolIds;
/// @dev Current report request
mapping(bytes32 => RequestType) private reportRequests;
Expand All @@ -68,7 +68,7 @@ contract CasimirUpkeep is
bool private reportRequested;
/// @dev Previous report block
uint256 private previousReportBlock;
/// @dev DON transmitter address
/// @dev DON transmitter address (not used, will be removed in future version)
address private transmitterAddress;
/// @dev Storage gap
uint256[48] private __gap;
Expand Down Expand Up @@ -158,17 +158,6 @@ contract CasimirUpkeep is
emit UpkeepPerformed(reportStatus);
}

/// @inheritdoc ICasimirUpkeep
function fulfillRequestDirect(bytes32 requestId, bytes memory response) external {
onlyTransmitter();
RequestType requestType = reportRequests[requestId];
if (requestType == RequestType.NONE) {
revert InvalidRequest();
}
handleResponse(requestId, requestType, response);
emit DirectResponseReceived(requestId, requestType, response);
}

/// @inheritdoc ICasimirUpkeep
function requestReport() external {
onlyFactoryOwner();
Expand All @@ -193,7 +182,12 @@ contract CasimirUpkeep is
previousReportBlock = resetPreviousReportBlock;
previousReportTimestamp = resetPreviousReportTimestamp;
reportRemainingRequests = 0;
reportRequestBlock = 0;
reportBeaconBalance = 0;
reportSweptBalance = 0;
reportActivatedDeposits = 0;
reportForcedExits = 0;
reportCompletedExits = 0;
reportCompoundablePoolIds = [0, 0, 0, 0, 0];
}

/// @inheritdoc ICasimirUpkeep
Expand All @@ -216,12 +210,6 @@ contract CasimirUpkeep is
emit FunctionsRequestSet(newRequestSource, newRequestArgs, newFulfillGasLimit);
}

/// @inheritdoc ICasimirUpkeep
function setTransmitter(address newTransmitterAddress) external {
onlyFactoryOwner();
transmitterAddress = newTransmitterAddress;
}

/// @inheritdoc ICasimirUpkeep
function checkUpkeep(bytes memory) public view override returns (bool upkeepNeeded, bytes memory checkData) {
if (reportStatus == ReportStatus.FINALIZED) {
Expand Down Expand Up @@ -278,7 +266,6 @@ contract CasimirUpkeep is
reportForcedExits = forcedExits;
reportCompletedExits = completedExits;
reportCompoundablePoolIds = compoundablePoolIds;
finalizableCompoundablePoolIds = compoundablePoolIds;
if (reportActivatedDeposits > 0) {
emit ActivationsRequested(activatedDeposits);
}
Expand Down Expand Up @@ -318,11 +305,4 @@ contract CasimirUpkeep is
revert Unauthorized();
}
}

/// @dev Validate the caller is the transmitter
function onlyTransmitter() private view {
if (msg.sender != transmitterAddress) {
revert Unauthorized();
}
}
}
Loading

0 comments on commit 6c55280

Please sign in to comment.