Skip to content

Commit

Permalink
incorporate some more changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ShivaanshK committed Sep 11, 2024
1 parent 3611b12 commit 70d2ad2
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 143 deletions.
2 changes: 1 addition & 1 deletion contracts/common/BiconomySponsorshipPaymasterErrors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ contract BiconomySponsorshipPaymasterErrors {
/**
* @notice Throws when invalid signature length in paymasterAndData
*/
error InvalidDynamicAdjustment();
error InvalidPriceMarkup();

/**
* @notice Throws when insufficient funds for paymasterid
Expand Down
2 changes: 1 addition & 1 deletion contracts/common/BiconomyTokenPaymasterErrors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ contract BiconomyTokenPaymasterErrors {
/**
* @notice Throws when invalid signature length in paymasterAndData
*/
error InvalidDynamicAdjustment();
error InvalidPriceMarkup();

/**
* @notice Throws when each token doesnt have a corresponding oracle
Expand Down
8 changes: 4 additions & 4 deletions contracts/interfaces/IBiconomySponsorshipPaymaster.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import { PackedUserOperation } from "@account-abstraction/contracts/core/UserOpe

interface IBiconomySponsorshipPaymaster{
event UnaccountedGasChanged(uint256 indexed oldValue, uint256 indexed newValue);
event FixedDynamicAdjustmentChanged(uint256 indexed oldValue, uint256 indexed newValue);
event FixedPriceMarkupChanged(uint256 indexed oldValue, uint256 indexed newValue);
event VerifyingSignerChanged(address indexed oldSigner, address indexed newSigner, address indexed actor);
event FeeCollectorChanged(address indexed oldFeeCollector, address indexed newFeeCollector, address indexed actor);
event GasDeposited(address indexed paymasterId, uint256 indexed value);
event GasWithdrawn(address indexed paymasterId, address indexed to, uint256 indexed value);
event GasBalanceDeducted(address indexed paymasterId, uint256 indexed charge, bytes32 indexed userOpHash);
event DynamicAdjustmentCollected(address indexed paymasterId, uint256 indexed dynamicAdjustment);
event PriceMarkupCollected(address indexed paymasterId, uint256 indexed priceMarkup);
event Received(address indexed sender, uint256 value);
event TokensWithdrawn(address indexed token, address indexed to, uint256 indexed amount, address actor);

Expand All @@ -35,7 +35,7 @@ interface IBiconomySponsorshipPaymaster{
address paymasterId,
uint48 validUntil,
uint48 validAfter,
uint32 dynamicAdjustment
uint32 priceMarkup
)
external
view
Expand All @@ -48,7 +48,7 @@ interface IBiconomySponsorshipPaymaster{
address paymasterId,
uint48 validUntil,
uint48 validAfter,
uint32 dynamicAdjustment,
uint32 priceMarkup,
bytes calldata signature
);
}
6 changes: 3 additions & 3 deletions contracts/interfaces/IBiconomyTokenPaymaster.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface IBiconomyTokenPaymaster {
}

event UpdatedUnaccountedGas(uint256 indexed oldValue, uint256 indexed newValue);
event UpdatedFixedDynamicAdjustment(uint256 indexed oldValue, uint256 indexed newValue);
event UpdatedFixedPriceMarkup(uint256 indexed oldValue, uint256 indexed newValue);
event UpdatedVerifyingSigner(address indexed oldSigner, address indexed newSigner, address indexed actor);
event UpdatedFeeCollector(address indexed oldFeeCollector, address indexed newFeeCollector, address indexed actor);
event UpdatedPriceExpiryDuration(uint256 indexed oldValue, uint256 indexed newValue);
Expand All @@ -30,7 +30,7 @@ interface IBiconomyTokenPaymaster {
address indexed token,
uint256 nativeCharge,
uint256 tokenCharge,
uint256 dynamicAdjustment,
uint256 priceMarkup,
bytes32 indexed userOpHash
);
event Received(address indexed sender, uint256 value);
Expand All @@ -42,7 +42,7 @@ interface IBiconomyTokenPaymaster {

function setUnaccountedGas(uint256 value) external payable;

function setDynamicAdjustment(uint256 _newUnaccountedGas) external payable;
function setPriceMarkup(uint256 _newUnaccountedGas) external payable;

function setPriceExpiryDuration(uint256 _newPriceExpiryDuration) external payable;

Expand Down
4 changes: 2 additions & 2 deletions contracts/libraries/TokenPaymasterParserLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ library TokenPaymasterParserLib {
uint48 validAfter,
address tokenAddress,
uint128 tokenPrice,
uint32 externalDynamicAdjustment,
uint32 externalPriceMarkup,
bytes memory signature
)
{
validUntil = uint48(bytes6(modeSpecificData[:6]));
validAfter = uint48(bytes6(modeSpecificData[6:12]));
tokenAddress = address(bytes20(modeSpecificData[12:32]));
tokenPrice = uint128(bytes16(modeSpecificData[32:48]));
externalDynamicAdjustment = uint32(bytes4(modeSpecificData[48:52]));
externalPriceMarkup = uint32(bytes4(modeSpecificData[48:52]));
signature = modeSpecificData[52:];
}

Expand Down
34 changes: 17 additions & 17 deletions contracts/sponsorship/BiconomySponsorshipPaymaster.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ contract BiconomySponsorshipPaymaster is
address public feeCollector;
uint256 public unaccountedGas;

// Denominator to prevent precision errors when applying dynamic adjustment
// Denominator to prevent precision errors when applying price markup
uint256 private constant PRICE_DENOMINATOR = 1e6;
// Offset in PaymasterAndData to get to PAYMASTER_ID_OFFSET
uint256 private constant PAYMASTER_ID_OFFSET = PAYMASTER_DATA_OFFSET;
Expand Down Expand Up @@ -194,7 +194,7 @@ contract BiconomySponsorshipPaymaster is
address paymasterId,
uint48 validUntil,
uint48 validAfter,
uint32 dynamicAdjustment
uint32 priceMarkup
)
public
view
Expand All @@ -217,7 +217,7 @@ contract BiconomySponsorshipPaymaster is
paymasterId,
validUntil,
validAfter,
dynamicAdjustment
priceMarkup
)
);
}
Expand All @@ -229,15 +229,15 @@ contract BiconomySponsorshipPaymaster is
address paymasterId,
uint48 validUntil,
uint48 validAfter,
uint32 dynamicAdjustment,
uint32 priceMarkup,
bytes calldata signature
)
{
unchecked {
paymasterId = address(bytes20(paymasterAndData[PAYMASTER_ID_OFFSET:PAYMASTER_ID_OFFSET + 20]));
validUntil = uint48(bytes6(paymasterAndData[PAYMASTER_ID_OFFSET + 20:PAYMASTER_ID_OFFSET + 26]));
validAfter = uint48(bytes6(paymasterAndData[PAYMASTER_ID_OFFSET + 26:PAYMASTER_ID_OFFSET + 32]));
dynamicAdjustment = uint32(bytes4(paymasterAndData[PAYMASTER_ID_OFFSET + 32:PAYMASTER_ID_OFFSET + 36]));
priceMarkup = uint32(bytes4(paymasterAndData[PAYMASTER_ID_OFFSET + 32:PAYMASTER_ID_OFFSET + 36]));
signature = paymasterAndData[PAYMASTER_ID_OFFSET + 36:];
}
}
Expand All @@ -256,24 +256,24 @@ contract BiconomySponsorshipPaymaster is
override
{
unchecked {
(address paymasterId, uint32 dynamicAdjustment, bytes32 userOpHash) =
(address paymasterId, uint32 priceMarkup, bytes32 userOpHash) =
abi.decode(context, (address, uint32, bytes32));

// Include unaccountedGas since EP doesn't include this in actualGasCost
// unaccountedGas = postOpGas + EP overhead gas + estimated penalty
actualGasCost = actualGasCost + (unaccountedGas * actualUserOpFeePerGas);
// Apply the dynamic adjustment
uint256 adjustedGasCost = (actualGasCost * dynamicAdjustment) / PRICE_DENOMINATOR;
// Apply the price markup
uint256 adjustedGasCost = (actualGasCost * priceMarkup) / PRICE_DENOMINATOR;

// Deduct the adjusted cost
paymasterIdBalances[paymasterId] -= adjustedGasCost;

if (adjustedGasCost > actualGasCost) {
// Apply dynamicAdjustment to fee collector balance
// Apply priceMarkup to fee collector balance
uint256 premium = adjustedGasCost - actualGasCost;
paymasterIdBalances[feeCollector] += premium;
// Review if we should emit adjustedGasCost as well
emit DynamicAdjustmentCollected(paymasterId, premium);
emit PriceMarkupCollected(paymasterId, premium);
}

emit GasBalanceDeducted(paymasterId, adjustedGasCost, userOpHash);
Expand All @@ -287,7 +287,7 @@ contract BiconomySponsorshipPaymaster is
* paymasterAndData[52:72] : paymasterId (dappDepositor)
* paymasterAndData[72:78] : validUntil
* paymasterAndData[78:84] : validAfter
* paymasterAndData[84:88] : dynamicAdjustment
* paymasterAndData[84:88] : priceMarkup
* paymasterAndData[88:] : signature
*/
function _validatePaymasterUserOp(
Expand All @@ -300,7 +300,7 @@ contract BiconomySponsorshipPaymaster is
override
returns (bytes memory context, uint256 validationData)
{
(address paymasterId, uint48 validUntil, uint48 validAfter, uint32 dynamicAdjustment, bytes calldata signature)
(address paymasterId, uint48 validUntil, uint48 validAfter, uint32 priceMarkup, bytes calldata signature)
= parsePaymasterAndData(userOp.paymasterAndData);
//ECDSA library supports both 64 and 65-byte long signatures.
// we only "require" it here so that the revert reason on invalid signature will be of "VerifyingPaymaster", and
Expand All @@ -310,7 +310,7 @@ contract BiconomySponsorshipPaymaster is
}

bool validSig = verifyingSigner.isValidSignatureNow(
ECDSA_solady.toEthSignedMessageHash(getHash(userOp, paymasterId, validUntil, validAfter, dynamicAdjustment)),
ECDSA_solady.toEthSignedMessageHash(getHash(userOp, paymasterId, validUntil, validAfter, priceMarkup)),
signature
);

Expand All @@ -319,19 +319,19 @@ contract BiconomySponsorshipPaymaster is
return ("", _packValidationData(true, validUntil, validAfter));
}

if (dynamicAdjustment > 2e6 || dynamicAdjustment == 0) {
revert InvalidDynamicAdjustment();
if (priceMarkup > 2e6 || priceMarkup == 0) {
revert InvalidPriceMarkup();
}

// Send 1e6 for No markup
// Send between 0 and 1e6 for discount
uint256 effectiveCost = (requiredPreFund * dynamicAdjustment) / PRICE_DENOMINATOR;
uint256 effectiveCost = (requiredPreFund * priceMarkup) / PRICE_DENOMINATOR;

if (effectiveCost > paymasterIdBalances[paymasterId]) {
revert InsufficientFundsForPaymasterId();
}

context = abi.encode(paymasterId, dynamicAdjustment, userOpHash);
context = abi.encode(paymasterId, priceMarkup, userOpHash);

//no need for other on-chain validation: entire UserOp should have been checked
// by the external service prior to signing it.
Expand Down
Loading

0 comments on commit 70d2ad2

Please sign in to comment.