All files / src/codegen/generators overrideFuncGen.ts

100% Statements 72/72
100% Branches 6/6
100% Functions 3/3
100% Lines 70/70

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81  1x 1x   1x   17x 17x         17x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   17x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   17x    
import { ContractSchema } from "../contractSchema";
import { Generator, ind } from "../generator";
import { Feature } from "../../types";
 
export class OverrideFuncGen implements Generator {
    gen(schema: ContractSchema): string {
        let features = schema.features;
        let out = schema.hasLiteRef() ? `` +
        `function _checkWriteAuth() internal override(Patchwork721, PatchworkLiteRef) view returns (bool allow) {\n` +
        `    return Patchwork721._checkWriteAuth();\n` +
        `}\n` : "";
        // TODO make this work for all patch + fragment permutations
        if (features.some((feature: Feature) => feature === Feature.PATCH) && features.some((feature: Feature) => feature === Feature.FRAGMENTSINGLE)) {
            out += `\n`;
            out += `function setLocked(uint256 tokenId, bool locked_) public view virtual override(PatchworkPatch, PatchworkFragmentSingle) {\n`;
            out += `    return PatchworkPatch.setLocked(tokenId, locked_);\n`;
            out += `}\n`;
            out += `\n`;
            out += `function locked(uint256 /* tokenId */) public pure virtual override(PatchworkPatch, PatchworkFragmentSingle) returns (bool) {\n`;
            out += `    return false;\n`;
            out += `}\n`;
            out += `\n`;
            out += `function ownerOf(uint256 tokenId) public view virtual override(PatchworkPatch, PatchworkFragmentSingle) returns (address) {\n`;
            out += `    return PatchworkPatch.ownerOf(tokenId);\n`;
            out += `}\n`;
            out += `\n`;
            out += `function updateOwnership(uint256 tokenId) public virtual override(PatchworkPatch, PatchworkFragmentSingle) {\n`;
            out += `    PatchworkPatch.updateOwnership(tokenId);\n`;
            out += `}\n`;
        }
        if (features.includes(Feature.WEAKREF)) {
            out += `\n`;
            out += `/**\n`;
            out += `@dev See {IERC721-ownerOf}\n`;
            out += `*/\n`;
            out += `function ownerOf(uint256 tokenId) public view virtual override returns (address) {\n`;
            out += `    // Weak assignment uses normal ownership\n`;
            out += `    return ERC721.ownerOf(tokenId);\n`;
            out += `}\n`;
            out += `\n`;
            out += `/**\n`;
            out += `@dev See {IPatchwork721-locked}\n`;
            out += `*/\n`;
            out += `function locked(uint256 tokenId) public view virtual override returns (bool) {\n`;
            out += `    // Weak assignment uses base 721 locking behavior\n`;
            out += `    return Patchwork721.locked(tokenId);\n`;
            out += `}\n`;
            out += `\n`;
            out += `/**\n`;
            out += `@dev See {IPatchwork721-setLocked}\n`;
            out += `*/\n`;
            out += `function setLocked(uint256 tokenId, bool locked_) public virtual override {\n`;
            out += `    // Weak assignment uses base 721 locking behavior\n`;
            out += `    Patchwork721.setLocked(tokenId, locked_);\n`;
            out += `}\n`;
            out += `\n`;
            out += `/**\n`;
            out += `@dev See {IERC721-transferFrom}.\n`;
            out += `*/\n`;
            out += `function transferFrom(address from, address to, uint256 tokenId) public virtual override {\n`;
            out += `    // Weak assignment skips calling PatchworkProtocol.applyTransfer()\n`;
            out += `    if (locked(tokenId)) {\n`;
            out += `        revert IPatchworkProtocol.Locked(address(this), tokenId);\n`;
            out += `    }\n`;
            out += `    ERC721.transferFrom(from, to, tokenId);\n`;
            out += `}\n`;
            out += `\n`;
            out += `/**\n`;
            out += `@dev See {IERC721-safeTransferFrom}.\n`;
            out += `*/\n`;
            out += `function safeTransferFrom(address from, address to, uint256 tokenId, bytes memory data) public virtual override {\n`;
            out += `    // Weak assignment skips calling PatchworkProtocol.applyTransfer()\n`;
            out += `    if (locked(tokenId)) {\n`;
            out += `        revert IPatchworkProtocol.Locked(address(this), tokenId);\n`;
            out += `    }\n`;
            out += `    ERC721.safeTransferFrom(from, to, tokenId, data);\n`;
            out += `}\n`;
        }
        return ind(4, `${out}`);
    }
}