Skip to content

Commit

Permalink
wip on operand
Browse files Browse the repository at this point in the history
  • Loading branch information
thedavidmeister committed Aug 23, 2024
1 parent a15e3cc commit aff8b4a
Show file tree
Hide file tree
Showing 17 changed files with 492 additions and 488 deletions.
2 changes: 1 addition & 1 deletion lib/rain.math.float
13 changes: 7 additions & 6 deletions src/concrete/extern/RainterpreterReferenceExternNPE2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
INTEGRITY_FUNCTION_POINTERS,
OPCODE_FUNCTION_POINTERS
} from "../../generated/RainterpreterReferenceExternNPE2.pointers.sol";
import {LibDecimalFloat} from "rain.math.float/src/lib/LibDecimalFloat.sol";

/// @dev The number of subparser functions available to the parser. This is NOT
/// 1:1 with the number of opcodes provided by the extern component of this
Expand Down Expand Up @@ -246,17 +247,17 @@ contract RainterpreterReferenceExternNPE2 is BaseRainterpreterSubParserNPE2, Bas
ParseState memory state = LibParseState.newState("", "", "", "");
// If we have a match on the keyword then the next chars MUST
// be a decimal, otherwise it's an error.
uint256 value;
(cursor, value) = LibParseLiteralDecimal.parseDecimal(
int256 signedCoefficient;
int256 exponent;
(cursor, signedCoefficient, exponent) = LibParseLiteralDecimal.parseDecimalFloat(
state, cursor + SUB_PARSER_LITERAL_REPEAT_KEYWORD_BYTES_LENGTH, end
);
value = LibFixedPointDecimalScale.scaleToIntegerLossless(value);
// We can only repeat a single digit.
if (value > 9) {
revert InvalidRepeatCount(value);
if (LibDecimalFloat.lt(signedCoefficient, exponent, 9, 0)) {
revert InvalidRepeatCount();
}

return (true, SUB_PARSER_LITERAL_REPEAT_INDEX, value);
return (true, SUB_PARSER_LITERAL_REPEAT_INDEX, LibDecimalFloat.pack(signedCoefficient, exponent));
} else {
return (false, 0, 0);
}
Expand Down
16 changes: 8 additions & 8 deletions src/lib/op/LibAllStandardOpsNP.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ import {LibOpUint256ERC20Allowance} from "./erc20/uint256/LibOpUint256ERC20Allow
import {LibOpUint256ERC20BalanceOf} from "./erc20/uint256/LibOpUint256ERC20BalanceOf.sol";
import {LibOpUint256ERC20TotalSupply} from "./erc20/uint256/LibOpUint256ERC20TotalSupply.sol";

import {LibOpERC20Allowance} from "./erc20/LibOpERC20Allowance.sol";
import {LibOpERC20BalanceOf} from "./erc20/LibOpERC20BalanceOf.sol";
import {LibOpERC20TotalSupply} from "./erc20/LibOpERC20TotalSupply.sol";
// import {LibOpERC20Allowance} from "./erc20/LibOpERC20Allowance.sol";
// import {LibOpERC20BalanceOf} from "./erc20/LibOpERC20BalanceOf.sol";
// import {LibOpERC20TotalSupply} from "./erc20/LibOpERC20TotalSupply.sol";

import {LibOpUint256ERC721BalanceOf} from "./erc721/uint256/LibOpUint256ERC721BalanceOf.sol";
import {LibOpERC721OwnerOf} from "./erc721/LibOpERC721OwnerOf.sol";
Expand Down Expand Up @@ -70,7 +70,7 @@ import {LibOpAdd} from "./math/LibOpAdd.sol";
// import {LibOpCeil} from "./math/LibOpCeil.sol";
// import {LibOpMul} from "./math/LibOpMul.sol";
// import {LibOpDiv} from "./math/LibOpDiv.sol";
import {LibOpE} from "./math/LibOpE.sol";
// import {LibOpE} from "./math/LibOpE.sol";
// import {LibOpExp} from "./math/LibOpExp.sol";
// import {LibOpExp2} from "./math/LibOpExp2.sol";
// import {LibOpFloor} from "./math/LibOpFloor.sol";
Expand All @@ -85,10 +85,10 @@ import {LibOpMin} from "./math/LibOpMin.sol";
import {LibOpMod} from "./math/LibOpMod.sol";
// import {LibOpLog2} from "./math/LibOpLog2.sol";
// import {LibOpPow} from "./math/LibOpPow.sol";
import {LibOpScale18Dynamic} from "./math/LibOpScale18Dynamic.sol";
import {LibOpScale18} from "./math/LibOpScale18.sol";
import {LibOpScaleNDynamic} from "./math/LibOpScaleNDynamic.sol";
import {LibOpScaleN} from "./math/LibOpScaleN.sol";
// import {LibOpScale18Dynamic} from "./math/LibOpScale18Dynamic.sol";
// import {LibOpScale18} from "./math/LibOpScale18.sol";
// import {LibOpScaleNDynamic} from "./math/LibOpScaleNDynamic.sol";
// import {LibOpScaleN} from "./math/LibOpScaleN.sol";
// import {LibOpSnapToUnit} from "./math/LibOpSnapToUnit.sol";
// import {LibOpSqrt} from "./math/LibOpSqrt.sol";
// import {LibOpSub} from "./math/LibOpSub.sol";
Expand Down
4 changes: 2 additions & 2 deletions src/lib/op/bitwise/LibOpCtPopNP.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ library LibOpCtPopNP {
value := mload(stackTop)
}
unchecked {
value = LibCtPop.ctpop(value) * FIXED_POINT_ONE;
value = LibCtPop.ctpop(value);
}
assembly ("memory-safe") {
mstore(stackTop, value)
Expand All @@ -42,7 +42,7 @@ library LibOpCtPopNP {
pure
returns (uint256[] memory)
{
inputs[0] = LibCtPop.ctpopSlow(inputs[0]) * FIXED_POINT_ONE;
inputs[0] = LibCtPop.ctpopSlow(inputs[0]);
return inputs;
}
}
120 changes: 60 additions & 60 deletions src/lib/op/erc20/LibOpERC20Allowance.sol
Original file line number Diff line number Diff line change
@@ -1,68 +1,68 @@
// SPDX-License-Identifier: CAL
pragma solidity ^0.8.18;
// // SPDX-License-Identifier: CAL
// pragma solidity ^0.8.18;

import {IERC20} from "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
import {Pointer} from "rain.solmem/lib/LibPointer.sol";
import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol";
import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol";
import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol";
import {IERC20Metadata} from "openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Metadata.sol";
// import {IERC20} from "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
// import {Pointer} from "rain.solmem/lib/LibPointer.sol";
// import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol";
// import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol";
// import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol";
// import {IERC20Metadata} from "openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Metadata.sol";

/// @title LibOpERC20Allowance
/// @notice Opcode for getting the current erc20 allowance of an account.
library LibOpERC20Allowance {
function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) {
// Always 3 inputs, the token, the owner and the spender.
// Always 1 output, the allowance.
return (3, 1);
}
// /// @title LibOpERC20Allowance
// /// @notice Opcode for getting the current erc20 allowance of an account.
// library LibOpERC20Allowance {
// function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) {
// // Always 3 inputs, the token, the owner and the spender.
// // Always 1 output, the allowance.
// return (3, 1);
// }

function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal view returns (Pointer) {
uint256 token;
uint256 owner;
uint256 spender;
assembly ("memory-safe") {
token := mload(stackTop)
owner := mload(add(stackTop, 0x20))
stackTop := add(stackTop, 0x40)
spender := mload(stackTop)
}
uint256 tokenAllowance =
IERC20(address(uint160(token))).allowance(address(uint160(owner)), address(uint160(spender)));
// function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal view returns (Pointer) {
// uint256 token;
// uint256 owner;
// uint256 spender;
// assembly ("memory-safe") {
// token := mload(stackTop)
// owner := mload(add(stackTop, 0x20))
// stackTop := add(stackTop, 0x40)
// spender := mload(stackTop)
// }
// uint256 tokenAllowance =
// IERC20(address(uint160(token))).allowance(address(uint160(owner)), address(uint160(spender)));

// This can fail as `decimals` is an OPTIONAL part of the ERC20 standard.
uint256 tokenDecimals = IERC20Metadata(address(uint160(token))).decimals();
tokenAllowance = LibFixedPointDecimalScale.scale18(
tokenAllowance,
tokenDecimals,
// Saturate scaling as "infinite approve" is a fairly common pattern
// so erroring would make a lot of contracts unusable in practise.
// Rounding down is the default.
FLAG_SATURATE
);
// // This can fail as `decimals` is an OPTIONAL part of the ERC20 standard.
// uint256 tokenDecimals = IERC20Metadata(address(uint160(token))).decimals();
// tokenAllowance = LibFixedPointDecimalScale.scale18(
// tokenAllowance,
// tokenDecimals,
// // Saturate scaling as "infinite approve" is a fairly common pattern
// // so erroring would make a lot of contracts unusable in practise.
// // Rounding down is the default.
// FLAG_SATURATE
// );

assembly ("memory-safe") {
mstore(stackTop, tokenAllowance)
}
return stackTop;
}
// assembly ("memory-safe") {
// mstore(stackTop, tokenAllowance)
// }
// return stackTop;
// }

function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs)
internal
view
returns (uint256[] memory)
{
uint256 token = inputs[0];
uint256 owner = inputs[1];
uint256 spender = inputs[2];
// function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs)
// internal
// view
// returns (uint256[] memory)
// {
// uint256 token = inputs[0];
// uint256 owner = inputs[1];
// uint256 spender = inputs[2];

uint256 tokenDecimals = IERC20Metadata(address(uint160(token))).decimals();
uint256 tokenAllowance =
IERC20(address(uint160(token))).allowance(address(uint160(owner)), address(uint160(spender)));
tokenAllowance = LibFixedPointDecimalScale.scale18(tokenAllowance, tokenDecimals, FLAG_SATURATE);
// uint256 tokenDecimals = IERC20Metadata(address(uint160(token))).decimals();
// uint256 tokenAllowance =
// IERC20(address(uint160(token))).allowance(address(uint160(owner)), address(uint160(spender)));
// tokenAllowance = LibFixedPointDecimalScale.scale18(tokenAllowance, tokenDecimals, FLAG_SATURATE);

uint256[] memory outputs = new uint256[](1);
outputs[0] = tokenAllowance;
return outputs;
}
}
// uint256[] memory outputs = new uint256[](1);
// outputs[0] = tokenAllowance;
// return outputs;
// }
// }
120 changes: 60 additions & 60 deletions src/lib/op/erc20/LibOpERC20BalanceOf.sol
Original file line number Diff line number Diff line change
@@ -1,69 +1,69 @@
// SPDX-License-Identifier: CAL
pragma solidity ^0.8.18;
// // SPDX-License-Identifier: CAL
// pragma solidity ^0.8.18;

import {IERC20} from "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
import {Pointer} from "rain.solmem/lib/LibPointer.sol";
import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol";
import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol";
import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol";
import {IERC20Metadata} from "openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Metadata.sol";
// import {IERC20} from "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
// import {Pointer} from "rain.solmem/lib/LibPointer.sol";
// import {IntegrityCheckStateNP} from "../../integrity/LibIntegrityCheckNP.sol";
// import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol";
// import {InterpreterStateNP} from "../../state/LibInterpreterStateNP.sol";
// import {IERC20Metadata} from "openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Metadata.sol";

/// @title LibOpERC20BalanceOf
/// @notice Opcode for getting the current erc20 balance of an account.
library LibOpERC20BalanceOf {
function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) {
// Always 2 inputs, the token and the account.
// Always 1 output, the balance.
return (2, 1);
}
// /// @title LibOpERC20BalanceOf
// /// @notice Opcode for getting the current erc20 balance of an account.
// library LibOpERC20BalanceOf {
// function integrity(IntegrityCheckStateNP memory, Operand) internal pure returns (uint256, uint256) {
// // Always 2 inputs, the token and the account.
// // Always 1 output, the balance.
// return (2, 1);
// }

function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal view returns (Pointer) {
uint256 token;
uint256 account;
assembly ("memory-safe") {
token := mload(stackTop)
stackTop := add(stackTop, 0x20)
account := mload(stackTop)
}
uint256 tokenBalance = IERC20(address(uint160(token))).balanceOf(address(uint160(account)));
// function run(InterpreterStateNP memory, Operand, Pointer stackTop) internal view returns (Pointer) {
// uint256 token;
// uint256 account;
// assembly ("memory-safe") {
// token := mload(stackTop)
// stackTop := add(stackTop, 0x20)
// account := mload(stackTop)
// }
// uint256 tokenBalance = IERC20(address(uint160(token))).balanceOf(address(uint160(account)));

// This can fail as `decimals` is an OPTIONAL part of the ERC20 standard.
uint256 tokenDecimals = IERC20Metadata(address(uint160(token))).decimals();
tokenBalance = LibFixedPointDecimalScale.scale18(
tokenBalance,
tokenDecimals,
// Error on overflow as balance is a critical value.
// Rounding down is the default.
0
);
// // This can fail as `decimals` is an OPTIONAL part of the ERC20 standard.
// uint256 tokenDecimals = IERC20Metadata(address(uint160(token))).decimals();
// tokenBalance = LibFixedPointDecimalScale.scale18(
// tokenBalance,
// tokenDecimals,
// // Error on overflow as balance is a critical value.
// // Rounding down is the default.
// 0
// );

assembly ("memory-safe") {
mstore(stackTop, tokenBalance)
}
return stackTop;
}
// assembly ("memory-safe") {
// mstore(stackTop, tokenBalance)
// }
// return stackTop;
// }

function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs)
internal
view
returns (uint256[] memory)
{
uint256 token = inputs[0];
uint256 account = inputs[1];
// function referenceFn(InterpreterStateNP memory, Operand, uint256[] memory inputs)
// internal
// view
// returns (uint256[] memory)
// {
// uint256 token = inputs[0];
// uint256 account = inputs[1];

uint256 tokenBalance = IERC20(address(uint160(token))).balanceOf(address(uint160(account)));
// uint256 tokenBalance = IERC20(address(uint160(token))).balanceOf(address(uint160(account)));

uint256 tokenDecimals = IERC20Metadata(address(uint160(token))).decimals();
tokenBalance = LibFixedPointDecimalScale.scale18(
tokenBalance,
tokenDecimals,
// Error on overflow as balance is a critical value.
// Rounding down is the default.
0
);
// uint256 tokenDecimals = IERC20Metadata(address(uint160(token))).decimals();
// tokenBalance = LibFixedPointDecimalScale.scale18(
// tokenBalance,
// tokenDecimals,
// // Error on overflow as balance is a critical value.
// // Rounding down is the default.
// 0
// );

uint256[] memory outputs = new uint256[](1);
outputs[0] = tokenBalance;
return outputs;
}
}
// uint256[] memory outputs = new uint256[](1);
// outputs[0] = tokenBalance;
// return outputs;
// }
// }
Loading

0 comments on commit aff8b4a

Please sign in to comment.