Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tight pack Offer struct #841

Merged
merged 1 commit into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contracts/domain/BosonTypes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,11 @@ contract BosonTypes {
uint256 buyerCancelPenalty;
uint256 quantityAvailable;
address exchangeToken;
PriceType priceType;
string metadataUri;
string metadataHash;
bool voided;
uint256 collectionIndex;
PriceType priceType;
RoyaltyInfo[] royaltyInfo;
}

Expand Down
2 changes: 1 addition & 1 deletion contracts/interfaces/handlers/IBosonOfferHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { IBosonOfferEvents } from "../events/IBosonOfferEvents.sol";
*
* @notice Handles creation, voiding, and querying of offers within the protocol.
*
* The ERC-165 identifier for this interface is: 0xfe4be526
* The ERC-165 identifier for this interface is: 0x91b54fdc
*/
interface IBosonOfferHandler is BosonErrors, IBosonOfferEvents {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { IBosonBundleEvents } from "../events/IBosonBundleEvents.sol";
*
* @notice Combines creation of multiple entities (accounts, offers, groups, twins, bundles) in a single transaction
*
* The ERC-165 identifier for this interface is: 0xb0f85bb8
* The ERC-165 identifier for this interface is: 0x44eb95ca
*/
interface IBosonOrchestrationHandler is
IBosonAccountEvents,
Expand Down
36 changes: 18 additions & 18 deletions scripts/domain/Offer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ class Offer {
uint256 buyerCancelPenalty;
uint256 quantityAvailable;
address exchangeToken;
PriceType priceType;
string metadataUri;
string metadataHash;
bool voided;
uint256 collectionIndex;
PriceType priceType;
RoyaltyInfo[] royaltyInfo;
}
*/
Expand All @@ -40,11 +40,11 @@ class Offer {
buyerCancelPenalty,
quantityAvailable,
exchangeToken,
priceType,
metadataUri,
metadataHash,
voided,
collectionIndex,
priceType,
royaltyInfo
) {
this.id = id;
Expand All @@ -54,11 +54,11 @@ class Offer {
this.buyerCancelPenalty = buyerCancelPenalty;
this.quantityAvailable = quantityAvailable;
this.exchangeToken = exchangeToken;
this.priceType = priceType;
this.metadataUri = metadataUri;
this.metadataHash = metadataHash;
this.voided = voided;
this.collectionIndex = collectionIndex;
this.priceType = priceType;
this.royaltyInfo = royaltyInfo;
}

Expand All @@ -76,11 +76,11 @@ class Offer {
buyerCancelPenalty,
quantityAvailable,
exchangeToken,
priceType,
metadataUri,
metadataHash,
voided,
collectionIndex,
priceType,
royaltyInfo,
} = o;

Expand All @@ -92,11 +92,11 @@ class Offer {
buyerCancelPenalty,
quantityAvailable,
exchangeToken,
priceType,
metadataUri,
metadataHash,
voided,
collectionIndex,
priceType,
royaltyInfo.map((ri) => RoyaltyInfo.fromObject(ri))
);
}
Expand All @@ -114,11 +114,11 @@ class Offer {
buyerCancelPenalty,
quantityAvailable,
exchangeToken,
priceType,
metadataUri,
metadataHash,
voided,
collectionIndex,
priceType,
royaltyInfo;

// destructure struct
Expand All @@ -130,11 +130,11 @@ class Offer {
buyerCancelPenalty,
quantityAvailable,
exchangeToken,
priceType,
metadataUri,
metadataHash,
voided,
collectionIndex,
priceType,
royaltyInfo,
] = struct;
if (!collectionIndex) {
Expand All @@ -149,11 +149,11 @@ class Offer {
buyerCancelPenalty: buyerCancelPenalty.toString(),
quantityAvailable: quantityAvailable.toString(),
exchangeToken,
priceType: Number(priceType),
metadataUri,
metadataHash,
voided,
collectionIndex: collectionIndex.toString(),
priceType: Number(priceType),
royaltyInfo: royaltyInfo.map((ri) => RoyaltyInfo.fromStruct(ri)),
});
}
Expand Down Expand Up @@ -187,11 +187,11 @@ class Offer {
this.buyerCancelPenalty,
this.quantityAvailable,
this.exchangeToken,
this.priceType,
this.metadataUri,
this.metadataHash,
this.voided,
this.collectionIndex,
this.priceType,
new RoyaltyInfoList(this.royaltyInfo).toStruct(),
];
}
Expand Down Expand Up @@ -269,6 +269,14 @@ class Offer {
return addressIsValid(this.exchangeToken);
}

/**
* Is this Offer instance's priceType field valid?
* @returns {boolean}
*/
priceTypeIsValid() {
return enumIsValid(this.priceType, PriceType.Types);
}

/**
* Is this Offer instance's metadataUri field valid?
* Always present, must be a string
Expand Down Expand Up @@ -306,14 +314,6 @@ class Offer {
return bigNumberIsValid(this.collectionIndex);
}

/**
* Is this Offer instance's priceType field valid?
* @returns {boolean}
*/
priceTypeIsValid() {
return enumIsValid(this.priceType, PriceType.Types);
}

/**
* Is this Offer instance's royaltyInfo field valid?
* Must be a valid RoyaltyInfo instance
Expand Down Expand Up @@ -342,11 +342,11 @@ class Offer {
this.buyerCancelPenaltyIsValid() &&
this.quantityAvailableIsValid() &&
this.exchangeTokenIsValid() &&
this.priceTypeIsValid() &&
this.metadataUriIsValid() &&
this.metadataHashIsValid() &&
this.voidedIsValid() &&
this.collectionIndexIsValid() &&
this.priceTypeIsValid() &&
this.royaltyInfoIsValid()
);
}
Expand Down
90 changes: 45 additions & 45 deletions test/domain/OfferTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ describe("Offer", function () {
buyerCancelPenalty,
quantityAvailable,
exchangeToken,
priceType,
metadataUri,
metadataHash,
voided,
collectionIndex,
priceType,
royaltyInfo;

beforeEach(async function () {
Expand All @@ -37,11 +37,11 @@ describe("Offer", function () {
buyerCancelPenalty = parseUnits("0.05", "ether").toString();
quantityAvailable = "1";
exchangeToken = ZeroAddress.toString(); // Zero addy ~ chain base currency
priceType = PriceType.Static;
metadataHash = "QmYXc12ov6F2MZVZwPs5XeCBbf61cW3wKRk8h3D5NTYj4T"; // not an actual metadataHash, just some data for tests
metadataUri = `https://ipfs.io/ipfs/${metadataHash}`;
voided = false;
collectionIndex = "2";
priceType = PriceType.Static;
royaltyInfo = [
new RoyaltyInfo(
accounts.slice(0, 3).map((a) => a.address),
Expand All @@ -61,11 +61,11 @@ describe("Offer", function () {
buyerCancelPenalty,
quantityAvailable,
exchangeToken,
priceType,
metadataUri,
metadataHash,
voided,
collectionIndex,
priceType,
royaltyInfo
);
expect(offer.idIsValid()).is.true;
Expand All @@ -75,11 +75,11 @@ describe("Offer", function () {
expect(offer.buyerCancelPenaltyIsValid()).is.true;
expect(offer.quantityAvailableIsValid()).is.true;
expect(offer.exchangeTokenIsValid()).is.true;
expect(offer.priceTypeIsValid()).is.true;
expect(offer.metadataUriIsValid()).is.true;
expect(offer.metadataHashIsValid()).is.true;
expect(offer.voidedIsValid()).is.true;
expect(offer.collectionIndexIsValid()).is.true;
expect(offer.priceTypeIsValid()).is.true;
expect(offer.royaltyInfoIsValid()).is.true;
expect(offer.isValid()).is.true;
});
Expand All @@ -96,11 +96,11 @@ describe("Offer", function () {
buyerCancelPenalty,
quantityAvailable,
exchangeToken,
priceType,
metadataUri,
metadataHash,
voided,
collectionIndex,
priceType,
royaltyInfo
);
expect(offer.isValid()).is.true;
Expand Down Expand Up @@ -260,6 +260,43 @@ describe("Offer", function () {
expect(offer.isValid()).is.true;
});

it("Always present, priceType must be a valid PriceType", async function () {
// Invalid field value
offer.priceType = "zedzdeadbaby";
expect(offer.priceTypeIsValid()).is.false;
expect(offer.isValid()).is.false;

// Invalid field value
offer.priceType = new Date();
expect(offer.priceTypeIsValid()).is.false;
expect(offer.isValid()).is.false;

// Invalid field value
offer.priceType = 12;
expect(offer.priceTypeIsValid()).is.false;
expect(offer.isValid()).is.false;

// Invalid field value
offer.priceType = "0";
expect(offer.priceTypeIsValid()).is.false;
expect(offer.isValid()).is.false;

// Invalid field value
offer.priceType = "126";
expect(offer.priceTypeIsValid()).is.false;
expect(offer.isValid()).is.false;

// Valid field value
offer.priceType = PriceType.Static;
expect(offer.priceTypeIsValid()).is.true;
expect(offer.isValid()).is.true;

// Valid field value
offer.priceType = PriceType.Discovery;
expect(offer.priceTypeIsValid()).is.true;
expect(offer.isValid()).is.true;
});

it("Always present, metadataUri must be a non-empty string", async function () {
// Invalid field value
offer.metadataUri = 12;
Expand Down Expand Up @@ -343,43 +380,6 @@ describe("Offer", function () {
expect(offer.isValid()).is.true;
});

it("Always present, priceType must be a valid PriceType", async function () {
// Invalid field value
offer.priceType = "zedzdeadbaby";
expect(offer.priceTypeIsValid()).is.false;
expect(offer.isValid()).is.false;

// Invalid field value
offer.priceType = new Date();
expect(offer.priceTypeIsValid()).is.false;
expect(offer.isValid()).is.false;

// Invalid field value
offer.priceType = 12;
expect(offer.priceTypeIsValid()).is.false;
expect(offer.isValid()).is.false;

// Invalid field value
offer.priceType = "0";
expect(offer.priceTypeIsValid()).is.false;
expect(offer.isValid()).is.false;

// Invalid field value
offer.priceType = "126";
expect(offer.priceTypeIsValid()).is.false;
expect(offer.isValid()).is.false;

// Valid field value
offer.priceType = PriceType.Static;
expect(offer.priceTypeIsValid()).is.true;
expect(offer.isValid()).is.true;

// Valid field value
offer.priceType = PriceType.Discovery;
expect(offer.priceTypeIsValid()).is.true;
expect(offer.isValid()).is.true;
});

it("Always present, royaltyInfo must be a RoyaltyInfo instance", async function () {
// Invalid field value
offer.royaltyInfo = 12;
Expand Down Expand Up @@ -421,11 +421,11 @@ describe("Offer", function () {
buyerCancelPenalty,
quantityAvailable,
exchangeToken,
priceType,
metadataUri,
metadataHash,
voided,
collectionIndex,
priceType,
royaltyInfo
);
expect(offer.isValid()).is.true;
Expand All @@ -439,11 +439,11 @@ describe("Offer", function () {
buyerCancelPenalty,
quantityAvailable,
exchangeToken,
priceType,
metadataUri,
metadataHash,
voided,
collectionIndex,
priceType,
royaltyInfo,
};
});
Expand Down Expand Up @@ -471,11 +471,11 @@ describe("Offer", function () {
offer.buyerCancelPenalty,
offer.quantityAvailable,
offer.exchangeToken,
offer.priceType,
offer.metadataUri,
offer.metadataHash,
offer.voided,
offer.collectionIndex,
offer.priceType,
royaltyInfo.map((ri) => ri.toStruct()),
];

Expand Down
2 changes: 1 addition & 1 deletion test/protocol/MetaTransactionsHandlerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -3476,7 +3476,7 @@ describe("IBosonMetaTransactionsHandler", function () {
message.from = await assistant.getAddress();
message.contractAddress = await offerHandler.getAddress();
message.functionName =
"createOffer((uint256,uint256,uint256,uint256,uint256,uint256,address,string,string,bool,uint256,uint8,(address[],uint256[])[]),(uint256,uint256,uint256,uint256),(uint256,uint256,uint256),uint256,uint256,uint256)";
"createOffer((uint256,uint256,uint256,uint256,uint256,uint256,address,uint8,string,string,bool,uint256,(address[],uint256[])[]),(uint256,uint256,uint256,uint256),(uint256,uint256,uint256),uint256,uint256,uint256)";
message.functionSignature = functionSignature;
});

Expand Down
Loading