Skip to content

Commit

Permalink
initial changes for packing paymasterAndData more closely (without te…
Browse files Browse the repository at this point in the history
…sts updated)
  • Loading branch information
livingrockrises committed Dec 4, 2023
1 parent e5a9491 commit c523299
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 33 deletions.
6 changes: 3 additions & 3 deletions contracts/test/helpers/MockChainlinkAggregator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ contract MockChainlinkOracleAggregator is Ownable, IOracleAggregator {
*/
function getTokenValueOfOneNativeToken(
address token
) external view virtual returns (uint256 exchangeRate) {
) external view virtual returns (uint128 exchangeRate) {
// we'd actually want eth / token
uint256 tokenPriceUnadjusted = _getTokenPrice(token);
uint8 _tokenOracleDecimals = tokensInfo[token].decimals;
exchangeRate =
((10 ** _tokenOracleDecimals) *
uint128(((10 ** _tokenOracleDecimals) *
(10 ** IERC20Metadata(token).decimals())) /
tokenPriceUnadjusted;
tokenPriceUnadjusted);
}

// Making explicit revert or make use of stale price feed which reverts
Expand Down
31 changes: 13 additions & 18 deletions contracts/token/BiconomyTokenPaymaster.sol
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ contract BiconomyTokenPaymaster is
// receiver of withdrawn fee tokens
address public feeReceiver;

// paymasterAndData: concat of [paymasterAddress(address), priceSource(enum 1 byte), abi.encode(validUntil, validAfter, feeToken, exchangeRate, priceMarkup): makes up 32*5 bytes, signature]
// paymasterAndData: concat of [paymasterAddress(address), priceSource(enum 1 byte), validUntil(6 byte), validAfter(6 byte), feeToken(20 bytes), exchangeRate(16 bytes), priceMarkup(4 bytes), signature]
// PND offset is used to indicate offsets to decode, used along with Signature offset
uint256 private constant VALID_PND_OFFSET = 21;

uint256 private constant SIGNATURE_OFFSET = 181;
uint256 private constant SIGNATURE_OFFSET = 73;

/**
* Designed to enable the community to track change in storage variable UNACCOUNTED_COST which is used
Expand Down Expand Up @@ -116,7 +116,7 @@ contract BiconomyTokenPaymaster is
uint256 indexed totalCharge,
uint32 priceMarkup,
bytes32 userOpHash,
uint256 exchangeRate,
uint128 exchangeRate,
ExchangeRateSource priceSource
);

Expand Down Expand Up @@ -340,7 +340,7 @@ contract BiconomyTokenPaymaster is
uint48 validUntil,
uint48 validAfter,
address feeToken,
uint256 exchangeRate,
uint128 exchangeRate,
uint32 priceMarkup
) public view returns (bytes32) {
//can't use userOp.hash(), since it contains also the paymasterAndData itself.
Expand Down Expand Up @@ -378,7 +378,7 @@ contract BiconomyTokenPaymaster is
uint48 validUntil,
uint48 validAfter,
address feeToken,
uint256 exchangeRate,
uint128 exchangeRate,
uint32 priceMarkup,
bytes calldata signature
)
Expand All @@ -393,16 +393,11 @@ contract BiconomyTokenPaymaster is
bytes1(paymasterAndData[VALID_PND_OFFSET - 1:VALID_PND_OFFSET])
)
);
(
validUntil,
validAfter,
feeToken,
exchangeRate,
priceMarkup
) = abi.decode(
paymasterAndData[VALID_PND_OFFSET:SIGNATURE_OFFSET],
(uint48, uint48, address, uint256, uint32)
);
validUntil = uint48(bytes6(paymasterAndData[21:27]));
validAfter = uint48(bytes6(paymasterAndData[27:33]));
feeToken = address(bytes20(paymasterAndData[33:53]));
exchangeRate = uint128(bytes16(paymasterAndData[53:69]));
priceMarkup = uint32(bytes4(paymasterAndData[69:73]));
signature = paymasterAndData[SIGNATURE_OFFSET:];
}

Expand Down Expand Up @@ -447,7 +442,7 @@ contract BiconomyTokenPaymaster is
uint48 validUntil,
uint48 validAfter,
address feeToken,
uint256 exchangeRate,
uint128 exchangeRate,
uint32 priceMarkup,
bytes calldata signature
) = parsePaymasterAndData(userOp.paymasterAndData);
Expand Down Expand Up @@ -520,7 +515,7 @@ contract BiconomyTokenPaymaster is
address account;
IERC20 feeToken;
ExchangeRateSource priceSource;
uint256 exchangeRate;
uint128 exchangeRate;
uint32 priceMarkup;
bytes32 userOpHash;
assembly ("memory-safe") {
Expand All @@ -544,7 +539,7 @@ contract BiconomyTokenPaymaster is
userOpHash := calldataload(offset)
}

uint256 effectiveExchangeRate = exchangeRate;
uint128 effectiveExchangeRate = exchangeRate;

if (
priceSource == ExchangeRateSource.ORACLE_BASED
Expand Down
6 changes: 3 additions & 3 deletions contracts/token/oracles/ChainlinkOracleAggregator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ contract ChainlinkOracleAggregator is Ownable, IOracleAggregator {
*/
function getTokenValueOfOneNativeToken(
address token
) external view virtual returns (uint256 exchangeRate) {
) external view virtual returns (uint128 exchangeRate) {
// we'd actually want eth / token
(
uint256 tokenPrice,
uint8 tokenOracleDecimals
) = _getTokenPriceAndDecimals(token);
exchangeRate =
10 ** (tokenOracleDecimals + IERC20Metadata(token).decimals()) /
tokenPrice;
uint128(10 ** (tokenOracleDecimals + IERC20Metadata(token).decimals()) /
tokenPrice);
}

function _getTokenPriceAndDecimals(
Expand Down
2 changes: 1 addition & 1 deletion contracts/token/oracles/IOracleAggregator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ pragma solidity ^0.8.20;
interface IOracleAggregator {
function getTokenValueOfOneNativeToken(
address _token
) external view returns (uint256 exchangeRate);
) external view returns (uint128 exchangeRate);
}
6 changes: 3 additions & 3 deletions contracts/token/oracles/OracleAggregator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,16 @@ abstract contract OracleAggregator is Ownable, IOracleAggregator {
*/
function getTokenValueOfOneNativeToken(
address token
) public view returns (uint256 exchangeRate) {
) public view returns (uint128 exchangeRate) {
// we'd actually want eth / token
(
uint256 tokenPrice,
uint8 tokenOracleDecimals,
uint8 tokenDecimals
) = _getTokenPriceAndDecimals(token);
exchangeRate =
10 ** (tokenOracleDecimals + tokenDecimals) /
tokenPrice;
uint128(10 ** (tokenOracleDecimals + tokenDecimals) /
tokenPrice);
}

function _getTokenPriceAndDecimals(
Expand Down
10 changes: 5 additions & 5 deletions test/foundry/token-paymaster/TokenPaymaster.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ contract TokenPaymasterTest is SATestBase {
.ORACLE_BASED;
uint48 validUntil = 3735928559;
uint48 validAfter = 4660;
uint256 exchangeRate = 977100;
uint128 exchangeRate = 977100;
uint32 priceMarkup = 1100000;

op.paymasterAndData = abi.encodePacked(
Expand Down Expand Up @@ -302,7 +302,7 @@ contract TokenPaymasterTest is SATestBase {
.ORACLE_BASED;
uint48 validUntil = 3735928559;
uint48 validAfter = 4660;
uint256 exchangeRate = 977100;
uint128 exchangeRate = 977100;
uint32 priceMarkup = 1100000;

op.paymasterAndData = abi.encodePacked(
Expand Down Expand Up @@ -352,7 +352,7 @@ contract TokenPaymasterTest is SATestBase {
.ORACLE_BASED;
uint48 validUntil = 3735928559;
uint48 validAfter = 4660;
uint256 exchangeRate = 977100;
uint128 exchangeRate = 977100;
uint32 priceMarkup = 1100000;

op.paymasterAndData = abi.encodePacked(
Expand Down Expand Up @@ -397,7 +397,7 @@ contract TokenPaymasterTest is SATestBase {
.ORACLE_BASED;
uint48 validUntil = 3735928559;
uint48 validAfter = 4660;
uint256 exchangeRate = 977100;
uint128 exchangeRate = 977100;
uint32 priceMarkup = 2200000;

bytes32 hash = _btpm.getHash(
Expand Down Expand Up @@ -451,7 +451,7 @@ contract TokenPaymasterTest is SATestBase {
.ORACLE_BASED;
uint48 validUntil = 3735928559;
uint48 validAfter = 4660;
uint256 exchangeRate = 977100;
uint128 exchangeRate = 977100;
uint32 priceMarkup = 1100000;

bytes32 hash = _btpm.getHash(
Expand Down

0 comments on commit c523299

Please sign in to comment.