Skip to content

Commit

Permalink
added front-end helper to decode error result
Browse files Browse the repository at this point in the history
  • Loading branch information
oveddan committed Sep 13, 2023
1 parent 2b8f57d commit d1cf878
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 3 deletions.
88 changes: 88 additions & 0 deletions package/preminter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
http,
createWalletClient,
createPublicClient,
BaseError,
ContractFunctionRevertedError,
} from "viem";
import { foundry, zoraTestnet } from "viem/chains";
import { describe, it, beforeEach, expect } from "vitest";
Expand All @@ -24,6 +26,7 @@ import {
ContractCreationConfig,
PremintConfig,
TokenCreationConfig,
decodeErc1155ErrorResult,
preminterTypedDataDefinition,
} from "./preminter";

Expand Down Expand Up @@ -295,6 +298,91 @@ describe("ZoraCreator1155Preminter", () => {

20 * 1000
);
it.only<TestContext>("can decode the original erc1155 error from a premint error result", async ({
zoraMintFee,
anvilChainId,
preminterAddress: preminterAddress,
fixedPriceMinterAddress,
}) => {
// setup contract and token creation parameters
const premintConfig = defaultPremintConfig(fixedPriceMinterAddress);
const contractConfig = defaultContractConfig({
contractAdmin: creatorAccount,
});

// lets make it a random number to not break the existing tests that expect fresh data
premintConfig.uid = Math.round(Math.random() * 1000000);

let contractAddress = await publicClient.readContract({
abi: preminterAbi,
address: preminterAddress,
functionName: "getContractAddress",
args: [contractConfig],
});

// have creator sign the message to create the contract
// and the token
const signedMessage = await walletClient.signTypedData({
...preminterTypedDataDefinition({
verifyingContract: contractAddress,
// we need to sign here for the anvil chain, cause thats where it is run on
chainId: anvilChainId,
premintConfig,
}),
account: creatorAccount,
});

const quantityToMint = 2n;

const valueToSend =
(zoraMintFee + premintConfig.tokenConfig.pricePerToken) * quantityToMint;

// send extra money, mint call should revert.
const wrongValueToSend = valueToSend + parseEther("1");

const comment = "";

await testClient.setBalance({
address: collectorAccount,
value: parseEther("10"),
});

// get the premint status - it should not be minted
try {
const { result } = await publicClient.simulateContract({
abi: preminterAbi,
functionName: "premint",
account: collectorAccount,
address: preminterAddress,
args: [
contractConfig,
premintConfig,
signedMessage,
quantityToMint,
comment,
],
value: wrongValueToSend,
});
} catch (err) {
if (err instanceof BaseError) {
const revertError = err.walk(
(err) => err instanceof ContractFunctionRevertedError
);
if (revertError instanceof ContractFunctionRevertedError) {
// do something with `errorName`

const errorName = revertError.data!.errorName;
expect(errorName).toBe("Erc1155CallReverted");

const errorData = revertError.data!.args![0] as `0x${string}`;

const decodedError = decodeErc1155ErrorResult(errorData);

expect(decodedError.errorName).toBe("WrongValueSent");
}
}
}
});
it<TestContext>(
"can sign and mint multiple tokens",
async ({
Expand Down
20 changes: 18 additions & 2 deletions package/preminter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Address } from "abitype";
import { ExtractAbiFunction, AbiParametersToPrimitiveTypes } from "abitype";
import { zoraCreator1155PremintExecutorABI as preminterAbi } from "./wagmiGenerated";
import { TypedDataDefinition } from "viem";
import {
zoraCreator1155PremintExecutorABI as preminterAbi,
zoraCreator1155ImplABI,
zoraCreatorFixedPriceSaleStrategyABI,
} from "./wagmiGenerated";
import { TypedDataDefinition, decodeErrorResult } from "viem";

type PremintInputs = ExtractAbiFunction<
typeof preminterAbi,
Expand Down Expand Up @@ -72,3 +76,15 @@ export const preminterTypedDataDefinition = ({

return result;
};

export const decodeErc1155ErrorResult = (result: `0x${string}`) => {
const combinedAbi = [
...zoraCreator1155ImplABI.filter((x) => x.type === "error"),
...zoraCreatorFixedPriceSaleStrategyABI.filter((x) => x.type === "error"),
];

return decodeErrorResult({
abi: combinedAbi,
data: result,
});
};
2 changes: 1 addition & 1 deletion src/premint/ZoraCreator1155PremintExecutor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ contract ZoraCreator1155PremintExecutor is Ownable2StepUpgradeable, UUPSUpgradea
error MintNotYetStarted();
error InvalidSignature();

error Erc1155CallReverted(bytes errorReverted);
error Erc1155CallReverted(bytes errorRevertedData);

constructor(IZoraCreator1155Factory _factory) {
zora1155Factory = _factory;
Expand Down

0 comments on commit d1cf878

Please sign in to comment.