Skip to content

Commit

Permalink
BT operator as lib and single add msg bot: 20.147 (-0.394)
Browse files Browse the repository at this point in the history
  • Loading branch information
havan committed Aug 21, 2024
1 parent cf753db commit c82371c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 33 deletions.
38 changes: 25 additions & 13 deletions contracts/account/CMAccount.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";

// Access
// Size Impact: +0.411 (Enumerable)
import "@openzeppelin/contracts-upgradeable/access/extensions/AccessControlEnumerableUpgradeable.sol";
//import "@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol";

// ERC721
import { IERC721 } from "@openzeppelin/contracts/token/ERC721/IERC721.sol";
Expand Down Expand Up @@ -42,7 +44,6 @@ contract CMAccount is
UUPSUpgradeable,
IERC721Receiver,
ChequeManager,
BookingTokenOperator,
PartnerConfiguration,
GasMoneyManager
{
Expand Down Expand Up @@ -331,16 +332,23 @@ contract CMAccount is
uint256 expirationTimestamp,
uint256 price,
IERC20 paymentToken
) external override onlyRole(BOOKING_OPERATOR_ROLE) {
) external onlyRole(BOOKING_OPERATOR_ROLE) {
// Mint the token
_mintBookingToken(getBookingTokenAddress(), reservedFor, uri, expirationTimestamp, price, paymentToken);
BookingTokenOperator._mintBookingToken(
getBookingTokenAddress(),
reservedFor,
uri,
expirationTimestamp,
price,
paymentToken
);
}

/**
* @dev Buy booking token
*/
function buyBookingToken(uint256 tokenId) external override onlyRole(BOOKING_OPERATOR_ROLE) {
_buyBookingToken(getBookingTokenAddress(), tokenId);
function buyBookingToken(uint256 tokenId) external onlyRole(BOOKING_OPERATOR_ROLE) {
BookingTokenOperator._buyBookingToken(getBookingTokenAddress(), tokenId);
}

/**
Expand Down Expand Up @@ -587,21 +595,25 @@ contract CMAccount is
/**
* @dev Add messenger bot
*/
function addMessengerBot(address bot) public onlyRole(BOT_ADMIN_ROLE) {
// Grant roles to bot
_grantRole(CHEQUE_OPERATOR_ROLE, bot);
_grantRole(BOOKING_OPERATOR_ROLE, bot);
_grantRole(GAS_WITHDRAWER_ROLE, bot);
// function addMessengerBot(address bot) public onlyRole(BOT_ADMIN_ROLE) {
// // Grant roles to bot
// _grantRole(CHEQUE_OPERATOR_ROLE, bot);
// _grantRole(BOOKING_OPERATOR_ROLE, bot);
// _grantRole(GAS_WITHDRAWER_ROLE, bot);

emit MessengerBotAdded(bot);
}
// emit MessengerBotAdded(bot);
// }

function addMessengerBot(address bot, uint256 gasMoney) public onlyRole(BOT_ADMIN_ROLE) {
// Check if we can spend the gasMoney to send it to the bot
_checkPrefundSpent(gasMoney);

// Grant roles to bot
addMessengerBot(bot);
_grantRole(CHEQUE_OPERATOR_ROLE, bot);
_grantRole(BOOKING_OPERATOR_ROLE, bot);
_grantRole(GAS_WITHDRAWER_ROLE, bot);

emit MessengerBotAdded(bot);

// Send gasMoney to bot
payable(bot).sendValue(gasMoney);
Expand Down
22 changes: 11 additions & 11 deletions contracts/booking-token/BookingTokenOperator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import "./IBookingToken.sol";
// ERC-20 Utils
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";

abstract contract BookingTokenOperator {
library BookingTokenOperator {
using SafeERC20 for IERC20;

function _mintBookingToken(
Expand All @@ -16,14 +16,14 @@ abstract contract BookingTokenOperator {
uint256 expirationTimestamp,
uint256 price,
IERC20 paymentToken
) internal virtual {
) public {
IBookingToken(bookingToken).safeMintWithReservation(reservedFor, uri, expirationTimestamp, price, paymentToken);
}

/**
* @dev Buy a booking token with the specified price
*/
function _buyBookingToken(address bookingToken, uint256 tokenId) internal virtual {
function _buyBookingToken(address bookingToken, uint256 tokenId) public {
// Get the price from the booking token contract
(uint256 price, IERC20 paymentToken) = IBookingToken(bookingToken).getReservationPrice(tokenId);

Expand All @@ -48,18 +48,18 @@ abstract contract BookingTokenOperator {
*
* This function should be overridden by the implementation
*/
function mintBookingToken(
address reservedFor,
string memory uri,
uint256 expirationTimestamp,
uint256 price,
IERC20 paymentToken
) external virtual;
// function mintBookingToken(
// address reservedFor,
// string memory uri,
// uint256 expirationTimestamp,
// uint256 price,
// IERC20 paymentToken
// ) external;

/**
* @dev Buy a booking token
*
* This function should be overridden by the implementation
*/
function buyBookingToken(uint256 tokenId) external virtual;
//function buyBookingToken(uint256 tokenId) external virtual;
}
16 changes: 12 additions & 4 deletions test/CMAccount.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ describe("CMAccount", function () {
const oldImplementationAddress = await cmAccountManager.getAccountImplementation();

// Create a new implementation for CMAccount
const CMAccountImplV2 = await ethers.getContractFactory("CMAccount");
const BookingTokenOperator = await ethers.getContractFactory("BookingTokenOperator");
const bookingTokenOperator = await BookingTokenOperator.deploy();
const CMAccountImplV2 = await ethers.getContractFactory("CMAccount", {
libraries: { BookingTokenOperator: await bookingTokenOperator.getAddress() },
});
const cmAccountImplV2 = await CMAccountImplV2.deploy();
await cmAccountImplV2.waitForDeployment();
const newImplementationAddress = await cmAccountImplV2.getAddress();
Expand All @@ -48,7 +52,11 @@ describe("CMAccount", function () {
const oldImplementationAddress = await cmAccountManager.getAccountImplementation();

// Create a new implementation for CMAccount
const CMAccountImplV2 = await ethers.getContractFactory("CMAccount");
const BookingTokenOperator = await ethers.getContractFactory("BookingTokenOperator");
const bookingTokenOperator = await BookingTokenOperator.deploy();
const CMAccountImplV2 = await ethers.getContractFactory("CMAccount", {
libraries: { BookingTokenOperator: await bookingTokenOperator.getAddress() },
});
const cmAccountImplV2 = await CMAccountImplV2.deploy();
await cmAccountImplV2.waitForDeployment();
const newImplementationAddress = await cmAccountImplV2.getAddress();
Expand Down Expand Up @@ -235,7 +243,7 @@ describe("CMAccount", function () {
const bot = signers.otherAccount1;

// Register bot
await expect(cmAccount.connect(signers.cmAccountAdmin).addMessengerBot(bot.address))
await expect(cmAccount.connect(signers.cmAccountAdmin).addMessengerBot(bot.address, 0n))
.to.emit(cmAccount, "MessengerBotAdded")
.withArgs(bot.address);

Expand All @@ -254,7 +262,7 @@ describe("CMAccount", function () {
const bot = signers.otherAccount1;

// Register bot
await expect(cmAccount.connect(signers.cmAccountAdmin).addMessengerBot(bot.address))
await expect(cmAccount.connect(signers.cmAccountAdmin).addMessengerBot(bot.address, 0n))
.to.emit(cmAccount, "MessengerBotAdded")
.withArgs(bot.address);

Expand Down
8 changes: 4 additions & 4 deletions test/GasMoneyManager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe("GasMoneyManager", function () {
const withdrawer = signers.withdrawer;

// Register withdrawer as a bot
await expect(cmAccount.connect(signers.cmAccountAdmin).addMessengerBot(withdrawer.address))
await expect(cmAccount.connect(signers.cmAccountAdmin).addMessengerBot(withdrawer.address, 0n))
.to.emit(cmAccount, "MessengerBotAdded")
.withArgs(withdrawer.address);

Expand Down Expand Up @@ -94,7 +94,7 @@ describe("GasMoneyManager", function () {
const expectedLimit = ethers.parseEther("10"); // 10 CAM

// Register withdrawer as a bot
await expect(cmAccount.connect(signers.cmAccountAdmin).addMessengerBot(withdrawer.address))
await expect(cmAccount.connect(signers.cmAccountAdmin).addMessengerBot(withdrawer.address, 0n))
.to.emit(cmAccount, "MessengerBotAdded")
.withArgs(withdrawer.address);

Expand All @@ -113,7 +113,7 @@ describe("GasMoneyManager", function () {
const expectedLimit = ethers.parseEther("10"); // 10 CAM

// Register withdrawer as a bot
await expect(cmAccount.connect(signers.cmAccountAdmin).addMessengerBot(withdrawer.address))
await expect(cmAccount.connect(signers.cmAccountAdmin).addMessengerBot(withdrawer.address, 0n))
.to.emit(cmAccount, "MessengerBotAdded")
.withArgs(withdrawer.address);

Expand Down Expand Up @@ -154,7 +154,7 @@ describe("GasMoneyManager", function () {
const expectedLimit = ethers.parseEther("10"); // 10 CAM

// Register withdrawer as a bot
await expect(cmAccount.connect(signers.cmAccountAdmin).addMessengerBot(withdrawer.address))
await expect(cmAccount.connect(signers.cmAccountAdmin).addMessengerBot(withdrawer.address, 0n))
.to.emit(cmAccount, "MessengerBotAdded")
.withArgs(withdrawer.address);

Expand Down
7 changes: 6 additions & 1 deletion test/utils/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ async function deployCMAccountManagerFixture() {
}

async function deployCMAccountImplFixture() {
const CMAccount = await ethers.getContractFactory("CMAccount");
const BookingTokenOperator = await ethers.getContractFactory("BookingTokenOperator");
const bookingTokenOperator = await BookingTokenOperator.deploy();
const CMAccount = await ethers.getContractFactory("CMAccount", {
libraries: { BookingTokenOperator: await bookingTokenOperator.getAddress() },
});
const cmAccountImpl = await CMAccount.deploy();
await cmAccountImpl.waitForDeployment();

Expand Down Expand Up @@ -110,6 +114,7 @@ async function deployAndConfigureAllFixture() {
await cmAccountManager.grantRole(await cmAccountManager.FEE_ADMIN_ROLE(), signers.feeAdmin.address);

// Deploy BookingToken

const BookingToken = await ethers.getContractFactory("BookingToken");
const bookingToken = await upgrades.deployProxy(
BookingToken,
Expand Down

0 comments on commit c82371c

Please sign in to comment.