Skip to content
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
5 changes: 3 additions & 2 deletions contracts/libraries/Utils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ library Utils {
bytes32 public constant OFFER_TYPE_HASH =
keccak256(
// solhint-disable-next-line max-line-length
"Offer(bytes32 id,uint256 expire,bytes32 supplierId,uint256 chainId,bytes32 requestHash,bytes32 optionsHash,bytes32 paymentHash,bytes32 cancelHash,bool transferable,uint256 checkIn)"
"Offer(bytes32 id,uint256 expire,bytes32 supplierId,uint256 chainId,bytes32 requestHash,bytes32 optionsHash,bytes32 paymentHash,bytes32 cancelHash,bool transferable,uint256 checkIn,uint256 checkOut)"
);

bytes32 public constant CHECK_IN_TYPE_HASH =
Expand Down Expand Up @@ -180,7 +180,8 @@ library Utils {
_offer.paymentHash,
_offer.cancelHash,
_offer.transferable,
_offer.checkIn
_offer.checkIn,
_offer.checkOut
)
);
}
Expand Down
2 changes: 1 addition & 1 deletion deploy/001.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
protocolFee,
retailerFee,
minDeposit,
} from "../utils/constants";
} from "../utils";

const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const { network, deployments, getNamedAccounts } = hre;
Expand Down
71 changes: 31 additions & 40 deletions deploy/002.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@ import {
DeployOptions,
DeployResult,
} from "hardhat-deploy/types";
import {
kindsArr,
eip712name,
eip712version,
claimPeriod,
protocolFee,
retailerFee,
minDeposit,
} from "../utils/constants";

const setupToken = async (
proxySettings: { owner: string; proxyContract: string },
Expand All @@ -29,10 +20,10 @@ const setupToken = async (
contract: contractName,
proxy: {
...proxySettings,
execute: {
methodName: "initialize",
args: [tokenName, tokenSymbol, owner],
},
// execute: {
// methodName: "initialize",
// args: [tokenName, tokenSymbol, owner],
// },
},
from: owner,
log: true,
Expand Down Expand Up @@ -128,19 +119,19 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const protocolConfig = await deploy("Config", {
proxy: {
...PROXY_SETTINGS_WITH_UPGRADE,
execute: {
methodName: "initialize",
args: [
owner,
lif.address,
claimPeriod,
protocolFee,
retailerFee,
owner,
kindsArr,
kindsArr.map(() => minDeposit), // same limit for all
],
},
// execute: {
// methodName: "initialize",
// args: [
// owner,
// lif.address,
// claimPeriod,
// protocolFee,
// retailerFee,
// owner,
// kindsArr,
// kindsArr.map(() => minDeposit), // same limit for all
// ],
// },
},
from: owner,
log: true,
Expand All @@ -158,10 +149,10 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const entities = await deploy("EntitiesRegistry", {
proxy: {
...PROXY_SETTINGS_WITH_UPGRADE,
execute: {
methodName: "initialize",
args: [owner, protocolConfig.address],
},
// execute: {
// methodName: "initialize",
// args: [owner, protocolConfig.address],
// },
},
from: owner,
log: true,
Expand All @@ -179,16 +170,16 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const market = await deploy("Market", {
proxy: {
...PROXY_SETTINGS_WITH_UPGRADE,
execute: {
methodName: "initialize",
args: [
owner,
eip712name,
eip712version,
protocolConfig.address,
entities.address,
],
},
// execute: {
// methodName: "initialize",
// args: [
// owner,
// eip712name,
// eip712version,
// protocolConfig.address,
// entities.address,
// ],
// },
},
from: owner,
log: true,
Expand Down
16 changes: 3 additions & 13 deletions deployments/polzktest/Market.json

Large diffs are not rendered by default.

158 changes: 79 additions & 79 deletions deployments/polzktest/Market_Implementation.json

Large diffs are not rendered by default.

119 changes: 119 additions & 0 deletions deployments/polzktest/solcInputs/21eac61dc4f78e216bbddc7cc9f68411.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
"@typechain/hardhat": "^6.1.6",
"@typechain/ethers-v5": "^10.2.1",
"@typechain/ethers-v6": "^0.3.2",
"dotenv": "^16.0.3"
"dotenv": "^16.0.3",
"viem": "^0.3.37"
},
"dependencies": {
"@openzeppelin/contracts-upgradeable": "^4.8.3"
Expand Down
10 changes: 5 additions & 5 deletions package/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,30 @@ export const kinds = {
retailer: '0x72657461696c6572000000000000000000000000000000000000000000000000',
};

// ethers.solidityPackedKeccak256(
// viem.encodePacked(
// ['string'],
// ['PaymentOption(bytes32 id,uint256 price,address asset)'],
// );
export const PAYMENT_OPTION_TYPE_HASH =
'0x2f8fc0b3ad3f58f6deb367673d38e4112a3c8c64de033c5b780b84ef8f67cde6';

// ethers.solidityPackedKeccak256(
// viem.encodePacked(
// ['string'],
// ['CancelOption(uint256 time,uint256 penalty)'],
// );
export const CANCEL_OPTION_TYPE_HASH =
'0x8ea27057ea8a0239f02c8b75748218a035a5a2a2a0785b53aaa99af91ff538c5';

// ethers.solidityPackedKeccak256(
// viem.encodePacked(
// ['string'],
// [
// 'Offer(bytes32 id,uint256 expire,bytes32 supplierId,uint256 chainId,bytes32 requestHash,bytes32 optionsHash,bytes32 paymentHash,bytes32 cancelHash,bool transferable,uint256 checkIn)',
// ],
// );
export const OFFER_TYPE_HASH =
'0xcf2addd2f89a78825d3f130a17e47b4e9963adfd09837fa9c454569faa073354';
'0x4fb12343a6f44152999c71291770d97fc1eace9d7d04889330d5a6d1af4a57c7';

// ethers.solidityPackedKeccak256(
// viem.encodePacked(
// ['string'],
// [
// 'Voucher(bytes32 id,address signer)',
Expand Down
210 changes: 210 additions & 0 deletions package/src/hash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
import { Hash, Address, keccak256, toHex, stringify, encodePacked } from 'viem';
import {
CANCEL_OPTION_TYPE_HASH,
OFFER_TYPE_HASH,
PAYMENT_OPTION_TYPE_HASH,
} from './constants.js';

/** Offered payment option type */
export interface PaymentOption {
/** Unique payment option Id */
id: Hash;
/** Asset price in WEI */
price: bigint;
/** ERC20 asset contract address */
asset: Address;
}

/** Offered cancellation option type */
export interface CancelOption {
/** Seconds before checkIn */
time: bigint;
/** Percents of total sum */
penalty: bigint;
}

/** Unsigned offer payload type */
export interface UnsignedOfferPayload extends Record<string, unknown> {
/** Unique Offer Id */
id: Hash;
/** Expiration time */
expire: bigint;
/** Unique supplier Id registered on the protocol contract */
supplierId: Hash;
/** Target network chain Id */
chainId: bigint;
/** <keccak256(request.hash())> */
requestHash: Hash;
/** <keccak256(hash(offer.options))> */
optionsHash: Hash;
/** <keccak256(hash(offer.payment))> */
paymentHash: Hash;
/** <keccak256(hash(offer.cancel || []))> */
cancelHash: Hash;
/** Makes the deal NFT transferable or not */
transferable: boolean;
/** Check-in time in seconds */
checkIn: bigint;
/** Check-out time in seconds */
checkOut: bigint;
}

/**
* Converts an object that contains bigint values to a JSON string representation.
*
* @param {unknown} data The data to stringify.
* @returns {string} The JSON string representation of the data.
*/
export { stringify };

/**
* Generates simple unique Id
*
* @param {number} [length=14] Default is `14`
* @returns {string}
*/
export const simpleUid = (length = 14): string => {
if (length < 5 || length > 14) {
throw new Error('Length value must be between 5 and 14');
}
return Math.random()
.toString(16)
.replace('.', '')
.split('')
.sort(() => (Math.random() > 0.5 ? 1 : -1))
.join('')
.slice(0, length);
};

/**
* Generates random salt (bytes32 string)
*
* @returns {Hash}
*/
export const randomSalt = (): Hash => keccak256(toHex(simpleUid()));

/**
* Computes the keccak256 hash of an object.
*
* @param {unknown} data The data object to hash.
* @returns {Hash} The keccak256 hash of the data.
*/
export const hashObject = (data: unknown): Hash => {
return keccak256(toHex(stringify(data)));
};

/**
* Computes the keccak256 hash of a PaymentOption object.
*
* @param {PaymentOption} option The PaymentOption object to hash.
* @returns {Hash} The keccak256 hash of the PaymentOption.
*/
export const hashPaymentOption = (option: PaymentOption): Hash => {
return keccak256(
encodePacked(
['bytes32', 'bytes32', 'uint256', 'address'],
[PAYMENT_OPTION_TYPE_HASH, option.id, option.price, option.asset],
),
);
};

/**
* Computes the keccak256 hash of a CancelOption object.
*
* @param {CancelOption} option The CancelOption object to hash.
* @returns {Hash} The keccak256 hash of the CancelOption.
*/
export const hashCancelOption = (option: CancelOption): Hash => {
return keccak256(
encodePacked(
['bytes32', 'uint256', 'uint256'],
[CANCEL_OPTION_TYPE_HASH, option.time, option.penalty],
),
);
};

/**
* Computes the keccak256 hash of an array of PaymentOption objects.
*
* @param {PaymentOption[]} options The array of PaymentOption objects to hash.
* @returns {Hash} The keccak256 hash of the PaymentOption array.
*/
export const hashPaymentOptionArray = (options: PaymentOption[]): Hash => {
const hashes: Hash[] = [];

for (let i = 0; i < options.length; i++) {
hashes[i] = hashPaymentOption(options[i]);
}

return keccak256(encodePacked(['bytes32[]'], [hashes]));
};

/**
* Computes the keccak256 hash of an array of CancelOption objects.
*
* @param {CancelOption[]} options The array of CancelOption objects to hash.
* @returns {Hash} The keccak256 hash of the CancelOption array.
*/
export const hashCancelOptionArray = (options: CancelOption[]): Hash => {
const hashes: Hash[] = [];

for (let i = 0; i < options.length; i++) {
hashes[i] = hashCancelOption(options[i]);
}

return keccak256(encodePacked(['bytes32[]'], [hashes]));
};

/**
* Computes the keccak256 hash of an UnsignedOfferPayload object.
*
* @param {UnsignedOfferPayload} payload The UnsignedOfferPayload object to hash.
* @returns {Hash} The keccak256 hash of the UnsignedOfferPayload.
*/
export const hashOfferPayload = (payload: UnsignedOfferPayload): Hash => {
return keccak256(
encodePacked(
[
'bytes32',
'bytes32',
'uint256',
'bytes32',
'uint256',
'bytes32',
'bytes32',
'bytes32',
'bytes32',
'bool',
'uint256',
'uint256',
],
[
OFFER_TYPE_HASH,
payload.id,
payload.expire,
payload.supplierId,
payload.chainId,
payload.requestHash,
payload.optionsHash,
payload.paymentHash,
payload.cancelHash,
payload.transferable,
payload.checkIn,
payload.checkOut,
],
),
);
};

/**
* Computes the keccak256 hash of a CheckInOut voucher.
*
* @param {string} offerId The ID of the offer.
* @param {string} signer The signer's address.
* @returns {Hash} The keccak256 hash of the CheckInOut operation.
*/
export const hashCheckInOut = (offerId: Hash, signer: Address): Hash => {
return keccak256(
encodePacked(['bytes32', 'bytes32', 'address'], [OFFER_TYPE_HASH, offerId, signer]),
);
};
1 change: 1 addition & 0 deletions package/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ export {
};
export * from './constants.js';
export * from '../wagmi/index.js';
export * from './hash.js';
Loading