Skip to content

Commit

Permalink
move getMappinStoragePosition to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
zajck committed Nov 18, 2022
1 parent 859e724 commit 8c5f1b8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
27 changes: 2 additions & 25 deletions test/upgrade/2.0.0-2.1.0.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const {
mockCondition,
mockTwin,
} = require("../util/mock");
const { setNextBlockTimestamp } = require("../util/utils.js");
const { setNextBlockTimestamp, paddingType, getMappinStoragePosition } = require("../util/utils.js");
const { oneMonth, oneDay } = require("../util/constants");
const { readContracts } = require("../../scripts/util/utils");
const { getInterfaceIds } = require("../../scripts/config/supported-interfaces.js");
Expand Down Expand Up @@ -396,7 +396,7 @@ describe("[@skip-on-coverage] After facet upgrade, everything is still operation
twin1155.supplyAvailable = `${30 * (i + 1)}`;
twin1155.id = ++twinId;
await twinHandler.connect(seller.wallet).createTwin(twin1155);
twins.push(twin20);
twins.push(twin1155);
}
}

Expand Down Expand Up @@ -788,29 +788,6 @@ describe("[@skip-on-coverage] After facet upgrade, everything is still operation
return {};
}

const paddingType = {
NONE: 0,
START: 1,
END: 2,
};

function getMappinStoragePosition(slot, key, padding = paddingType.NONE) {
let keyBuffer;
switch (padding) {
case paddingType.NONE:
keyBuffer = ethers.utils.toUtf8Bytes(key);
break;
case paddingType.START:
keyBuffer = Buffer.from(ethers.utils.hexZeroPad(key, 32).toString().slice(2), "hex");
break;
case paddingType.END:
keyBuffer = Buffer.from(key.slice(2).padEnd(64, "0"), "hex"); // assume key is prefixed with 0x
break;
}
const pBuffer = Buffer.from(slot.toHexString().slice(2), "hex");
return keccak256(Buffer.concat([keyBuffer, pBuffer]));
}

async function getMetaTxPrivateContractState() {
/*
ProtocolMetaTxInfo storage layout
Expand Down
25 changes: 25 additions & 0 deletions test/util/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,35 @@ function calculateContractAddress(senderAddress, senderNonce) {
return ethers.utils.getAddress(contract_address);
}

const paddingType = {
NONE: 0,
START: 1,
END: 2,
};

function getMappinStoragePosition(slot, key, padding = paddingType.NONE) {
let keyBuffer;
switch (padding) {
case paddingType.NONE:
keyBuffer = ethers.utils.toUtf8Bytes(key);
break;
case paddingType.START:
keyBuffer = Buffer.from(ethers.utils.hexZeroPad(key, 32).toString().slice(2), "hex");
break;
case paddingType.END:
keyBuffer = Buffer.from(key.slice(2).padEnd(64, "0"), "hex"); // assume key is prefixed with 0x
break;
}
const pBuffer = Buffer.from(slot.toHexString().slice(2), "hex");
return keccak256(Buffer.concat([keyBuffer, pBuffer]));
}

exports.setNextBlockTimestamp = setNextBlockTimestamp;
exports.getEvent = getEvent;
exports.eventEmittedWithArgs = eventEmittedWithArgs;
exports.prepareDataSignatureParameters = prepareDataSignatureParameters;
exports.calculateVoucherExpiry = calculateVoucherExpiry;
exports.calculateContractAddress = calculateContractAddress;
exports.applyPercentage = applyPercentage;
exports.paddingType = paddingType;
exports.getMappinStoragePosition = getMappinStoragePosition;

0 comments on commit 8c5f1b8

Please sign in to comment.