Skip to content

Commit

Permalink
minor improvements on contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
shileiwill committed Apr 25, 2024
1 parent 274a988 commit 78b5db2
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 161 deletions.
5 changes: 5 additions & 0 deletions .changeset/tricky-bats-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"chainlink": patch
---

minor fixes #bugfix
5 changes: 5 additions & 0 deletions contracts/.changeset/new-guests-accept.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@chainlink/contracts": patch
---

minor fixes #bugfix
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ contract AutomationRegistrar2_3 is TypeAndVersionInterface, ConfirmedOwner, IERC
}
/**
* @member upkeepContract address to perform upkeep on
* @member amount quantity of LINK upkeep is funded with (specified in wei)
* @member amount quantity of billing token upkeep is funded with (specified in the billing token's decimals)
* @member adminAddress address to cancel upkeep and withdraw remaining funds
* @member gasLimit amount of gas to provide the target contract when performing upkeep
* @member triggerType the type of trigger for the upkeep
Expand Down Expand Up @@ -204,19 +204,17 @@ contract AutomationRegistrar2_3 is TypeAndVersionInterface, ConfirmedOwner, IERC
/**
* @dev register upkeep on AutomationRegistry contract and emit RegistrationApproved event
* @param requestParams struct of all possible registration parameters
* @param hash the committment of the registration request
*/
function approve(RegistrationParams calldata requestParams, bytes32 hash) external onlyOwner {
function approve(RegistrationParams calldata requestParams) external onlyOwner {
bytes32 hash = keccak256(abi.encode(requestParams));

PendingRequest memory request = s_pendingRequests[hash];
if (request.admin == address(0)) {
revert RequestNotFound();
}
bytes32 expectedHash = keccak256(abi.encode(requestParams));
if (hash != expectedHash) {
revert HashMismatch();
}

delete s_pendingRequests[hash];
_approve(requestParams, expectedHash);
_approve(requestParams, hash);
}

/**
Expand Down
18 changes: 9 additions & 9 deletions contracts/src/v0.8/automation/dev/v2_3/AutomationRegistry2_3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,13 @@ contract AutomationRegistry2_3 is AutomationRegistryBase2_3, OCR2Abstract, Chain
report.upkeepIds[i],
upkeepTransmitInfo[i].upkeep
);
transmitVars.totalPremium += receipt.premiumJuels;
transmitVars.totalReimbursement += receipt.gasReimbursementJuels;
transmitVars.totalPremium += receipt.premiumInJuels;
transmitVars.totalReimbursement += receipt.gasReimbursementInJuels;

emit UpkeepPerformed(
report.upkeepIds[i],
upkeepTransmitInfo[i].performSuccess,
receipt.gasReimbursementJuels + receipt.premiumJuels, // TODO - this is currently the LINK amount, but may change to billing token
receipt.gasReimbursementInJuels + receipt.premiumInJuels, // TODO - this is currently the LINK amount, but may change to billing token
upkeepTransmitInfo[i].gasUsed,
gasOverhead,
report.triggers[i]
Expand Down Expand Up @@ -320,6 +320,10 @@ contract AutomationRegistry2_3 is AutomationRegistryBase2_3, OCR2Abstract, Chain
chainModule: onchainConfig.chainModule
});

uint32 previousConfigBlockNumber = s_storage.latestConfigBlockNumber;
uint32 newLatestConfigBlockNumber = uint32(onchainConfig.chainModule.blockNumber());
uint32 newConfigCount = s_storage.configCount + 1;

s_storage = Storage({
checkGasLimit: onchainConfig.checkGasLimit,
maxPerformGas: onchainConfig.maxPerformGas,
Expand All @@ -330,17 +334,13 @@ contract AutomationRegistry2_3 is AutomationRegistryBase2_3, OCR2Abstract, Chain
upkeepPrivilegeManager: onchainConfig.upkeepPrivilegeManager,
financeAdmin: onchainConfig.financeAdmin,
nonce: s_storage.nonce,
configCount: s_storage.configCount,
latestConfigBlockNumber: s_storage.latestConfigBlockNumber
configCount: newConfigCount,
latestConfigBlockNumber: newLatestConfigBlockNumber
});
s_fallbackGasPrice = onchainConfig.fallbackGasPrice;
s_fallbackLinkPrice = onchainConfig.fallbackLinkPrice;
s_fallbackNativePrice = onchainConfig.fallbackNativePrice;

uint32 previousConfigBlockNumber = s_storage.latestConfigBlockNumber;
s_storage.latestConfigBlockNumber = uint32(onchainConfig.chainModule.blockNumber());
s_storage.configCount += 1;

bytes memory onchainConfigBytes = abi.encode(onchainConfig);

s_latestConfigDigest = _configDigestFromConfigData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ abstract contract AutomationRegistryBase2_3 is ConfirmedOwner {
uint8 internal constant UPKEEP_VERSION_BASE = 4;

// Next block of constants are only used in maxPayment estimation during checkUpkeep simulation
// These values are calibrated using hardhat tests which simulates various cases and verifies that
// These values are calibrated using hardhat tests which simulate various cases and verify that
// the variables result in accurate estimation
uint256 internal constant REGISTRY_CONDITIONAL_OVERHEAD = 93_000; // Fixed gas overhead for conditional upkeeps
uint256 internal constant REGISTRY_LOG_OVERHEAD = 118_000; // Fixed gas overhead for log upkeeps
Expand Down Expand Up @@ -433,16 +433,16 @@ abstract contract AutomationRegistryBase2_3 is ConfirmedOwner {

/**
* @notice struct containing receipt information about a payment or cost estimation
* @member gasCharge the amount to charge a user for gas spent using the billing token's native decimals
* @member premium the premium charged to the user, shared between all nodes, using the billing token's native decimals
* @member gasReimbursementJuels the amount to reimburse a node for gas spent
* @member premiumJuels the premium paid to NOPs, shared between all nodes
* @member gasChargeInBillingToken the amount to charge a user for gas spent using the billing token's native decimals
* @member premiumInBillingToken the premium charged to the user, shared between all nodes, using the billing token's native decimals
* @member gasReimbursementInJuels the amount to reimburse a node for gas spent
* @member premiumInJuels the premium paid to NOPs, shared between all nodes
*/
struct PaymentReceipt {
uint96 gasCharge;
uint96 premium;
uint96 gasReimbursementJuels;
uint96 premiumJuels;
uint96 gasChargeInBillingToken;
uint96 premiumInBillingToken;
uint96 gasReimbursementInJuels;
uint96 premiumInJuels;
}

event AdminPrivilegeConfigSet(address indexed admin, bytes privilegeConfig);
Expand Down Expand Up @@ -683,24 +683,24 @@ abstract contract AutomationRegistryBase2_3 is ConfirmedOwner {
uint256 gasPaymentHexaicosaUSD = (gasWei *
(paymentParams.gasLimit + paymentParams.gasOverhead) +
paymentParams.l1CostWei) * paymentParams.nativeUSD; // gasPaymentHexaicosaUSD has an extra 8 zeros because of decimals on nativeUSD feed
// gasCharge is scaled by the billing token's decimals
receipt.gasCharge = SafeCast.toUint96(
// gasChargeInBillingToken is scaled by the billing token's decimals
receipt.gasChargeInBillingToken = SafeCast.toUint96(
(gasPaymentHexaicosaUSD * numeratorScalingFactor) /
(paymentParams.billingTokenParams.priceUSD * denominatorScalingFactor)
);
receipt.gasReimbursementJuels = SafeCast.toUint96(gasPaymentHexaicosaUSD / paymentParams.linkUSD);
receipt.gasReimbursementInJuels = SafeCast.toUint96(gasPaymentHexaicosaUSD / paymentParams.linkUSD);

// premium calculation
uint256 flatFeeHexaicosaUSD = uint256(paymentParams.billingTokenParams.flatFeeMilliCents) * 1e21; // 1e13 for milliCents to attoUSD and 1e8 for attoUSD to hexaicosaUSD
uint256 premiumHexaicosaUSD = ((((gasWei * paymentParams.gasLimit) + paymentParams.l1CostWei) *
paymentParams.billingTokenParams.gasFeePPB *
paymentParams.nativeUSD) / 1e9) + flatFeeHexaicosaUSD;
// premium is scaled by the billing token's decimals
receipt.premium = SafeCast.toUint96(
receipt.premiumInBillingToken = SafeCast.toUint96(
(premiumHexaicosaUSD * numeratorScalingFactor) /
(paymentParams.billingTokenParams.priceUSD * denominatorScalingFactor)
);
receipt.premiumJuels = SafeCast.toUint96(premiumHexaicosaUSD / paymentParams.linkUSD);
receipt.premiumInJuels = SafeCast.toUint96(premiumHexaicosaUSD / paymentParams.linkUSD);

return receipt;
}
Expand Down Expand Up @@ -764,7 +764,7 @@ abstract contract AutomationRegistryBase2_3 is ConfirmedOwner {
})
);

return receipt.gasCharge + receipt.premium;
return receipt.gasChargeInBillingToken + receipt.premiumInBillingToken;
}

/**
Expand Down Expand Up @@ -1002,22 +1002,23 @@ abstract contract AutomationRegistryBase2_3 is ConfirmedOwner {
// balance is in the token's native decimals
uint96 balance = upkeep.balance;
// payment is in the token's native decimals
uint96 payment = receipt.gasCharge + receipt.premium;
uint96 payment = receipt.gasChargeInBillingToken + receipt.premiumInBillingToken;

// this shouldn't happen, but in rare edge cases, we charge the full balance in case the user
// can't cover the amount owed
if (balance < receipt.gasCharge) {
if (balance < receipt.gasChargeInBillingToken) {
// if the user can't cover the gas fee, then direct all of the payment to the transmitter and distribute no premium to the DON
payment = balance;
receipt.gasReimbursementJuels = SafeCast.toUint96(
receipt.gasReimbursementInJuels = SafeCast.toUint96(
(balance * paymentParams.billingTokenParams.priceUSD) / paymentParams.linkUSD
);
receipt.premiumJuels = 0;
receipt.premiumInJuels = 0;
} else if (balance < payment) {
// if the user can cover the gas fee, but not the premium, then reduce the premium
payment = balance;
receipt.premiumJuels = SafeCast.toUint96(
((balance * paymentParams.billingTokenParams.priceUSD) / paymentParams.linkUSD) - receipt.gasReimbursementJuels
receipt.premiumInJuels = SafeCast.toUint96(
((balance * paymentParams.billingTokenParams.priceUSD) / paymentParams.linkUSD) -
receipt.gasReimbursementInJuels
);
}

Expand Down Expand Up @@ -1120,13 +1121,12 @@ abstract contract AutomationRegistryBase2_3 is ConfirmedOwner {
* @notice updates the signers and transmitters lists
*/
function _updateTransmitters(address[] memory signers, address[] memory transmitters) internal {
uint96 transmittersListLength = uint96(s_transmittersList.length);
uint96 totalPremium = s_hotVars.totalPremium;

// move all pooled payments out of the pool to each transmitter's balance
for (uint256 i = 0; i < s_transmittersList.length; i++) {
_updateTransmitterBalanceFromPool(
s_transmittersList[i],
s_hotVars.totalPremium,
uint96(s_transmittersList.length)
);
_updateTransmitterBalanceFromPool(s_transmittersList[i], totalPremium, transmittersListLength);
}

// remove any old signer/transmitter addresses
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ contract AutomationRegistryLogicA2_3 is AutomationRegistryBase2_3, Chainable {
delete s_upkeepOffchainConfig[id];
// nullify existing proposed admin change if an upkeep is being migrated
delete s_proposedAdmin[id];
delete s_upkeepAdmin[id];
s_upkeepIDs.remove(id);
emit UpkeepMigrated(id, upkeep.balance, destination);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ contract AutomationRegistryLogicC2_3 is AutomationRegistryBase2_3 {
_onlyFinanceAdminAllowed();
if (s_payoutMode == PayoutMode.ON_CHAIN) revert MustSettleOnchain();

uint96 totalPremium = s_hotVars.totalPremium;
uint256 activeTransmittersLength = s_transmittersList.length;
uint256 deactivatedTransmittersLength = s_deactivatedTransmitters.length();
uint256 length = activeTransmittersLength + deactivatedTransmittersLength;
Expand All @@ -171,7 +172,7 @@ contract AutomationRegistryLogicC2_3 is AutomationRegistryBase2_3 {
address transmitterAddr = s_transmittersList[i];
uint96 balance = _updateTransmitterBalanceFromPool(
transmitterAddr,
s_hotVars.totalPremium,
totalPremium,
uint96(activeTransmittersLength)
);

Expand Down Expand Up @@ -548,7 +549,7 @@ contract AutomationRegistryLogicC2_3 is AutomationRegistryBase2_3 {
}

/**
* @notice returns the upkeep privilege config
* @notice returns the admin's privilege config
*/
function getAdminPrivilegeConfig(address admin) external view returns (bytes memory) {
return s_adminPrivilegeConfig[admin];
Expand All @@ -562,7 +563,7 @@ contract AutomationRegistryLogicC2_3 is AutomationRegistryBase2_3 {
}

/**
* @notice returns the upkeep's forwarder contract
* @notice returns if the dedupKey exists or not
*/
function hasDedupKey(bytes32 dedupKey) external view returns (bool) {
return s_dedupKeys[dedupKey];
Expand Down
Loading

0 comments on commit 78b5db2

Please sign in to comment.