All files / src/codegen/generators fieldFuncGen.ts

95.06% Statements 77/81
84.9% Branches 45/53
100% Functions 2/2
95% Lines 76/80

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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 1571x   1x 1x   1x     17x   17x   32x 2x   30x 30x     30x 1x     1x           1x             1x 1x   1x 1x   29x 5x 5x 5x 5x 5x 80x 3x 3x 3x 3x 3x   80x 80x 8x 8x 8x   8x 8x   72x 72x         80x   5x 5x                 5x                   5x 5x   5x 5x     24x 24x       24x 14x 14x 3x   14x 10x     10x   24x 24x       24x 13x 13x 13x 13x 3x 10x     10x     11x 11x           24x 24x 20x   24x 20x         17x     17x   17x    
import { FunctionConfig } from "../../types";
import { ContractSchema, ContractStorageField } from "../contractSchema";
import { Generator, ind } from "../generator";
import { cleanAndCapitalizeFirstLetter } from "../utils";
 
export class FieldFuncGen implements Generator {
    gen(schema: ContractSchema): string {
        function generateLoadStoreFunctions(fields: ContractStorageField[]) {
            let loadStoreFunctions = [];
    
            for (let field of fields) {
                // Do not generate for dynamic arrays
                if (field.arrayLength === 0) {
                    continue;
                }
                let capName = cleanAndCapitalizeFirstLetter(field.key);
                const permissionLine = field.permissionId ?
                `if (!(_checkTokenWriteAuth(tokenId) || _permissionsAllow[msg.sender] & 0x${(1 << (field.permissionId - 1)).toString(16)} > 0)) {\n    revert IPatchworkProtocol.NotAuthorized(msg.sender);\n}` :
                `if (!_checkTokenWriteAuth(tokenId)) {\n    revert IPatchworkProtocol.NotAuthorized(msg.sender);\n}`;
                if (field.type === "string") {
                    Iif (field.arrayLength > 1) {
                        throw new Error("String arrays are not supported in PDK");
                    }
                    const loadFunction = `` +
                    `// Load Only ${field.key}\n` +
                    `function load${capName}(uint256 tokenId) public view returns (string memory) {\n` +
                    `    return _dynamicStringStorage[tokenId];\n` +
                    `}\n`;
        
                    const storeFunction = `` +
                    `// Store Only ${field.key}\n` +
                    `function store${capName}(uint256 tokenId, string memory ${field.key}) public {\n` +
                    `${ind(4, permissionLine)}\n` +
                    `    _dynamicStringStorage[tokenId] = ${field.key};\n` +
                    `}\n`;
        
                    if (field.functionConfig === FunctionConfig.ALL || field.functionConfig === FunctionConfig.LOAD) {
                        loadStoreFunctions.push(loadFunction);
                    }
                    if (field.functionConfig === FunctionConfig.ALL || field.functionConfig === FunctionConfig.STORE) {
                        loadStoreFunctions.push(storeFunction);
                    }
                } else if (field.arrayLength > 1) {
                    let loadArrayLines = [];
                    let storeArrayLines = [];
                    let slot = field.slot;
                    let offset = field.offset;
                    for (let i = 0; i < field.arrayLength; i++) {
                        if (offset >= 256) {
                            offset = 0;
                            slot++;
                            loadArrayLines.push(`slot = _metadataStorage[tokenId][${slot}];`);
                            storeArrayLines.push(`_metadataStorage[tokenId][${slot-1}] = slot;`)
                            storeArrayLines.push(`slot = 0;`);
                        }
                        const shift = offset > 0 ? ` >> ${offset}` : "";
                        if (field.isString) {
                            let conversion = `slot${shift}`;
                            if (field.elementBits < 256) {
                                conversion = `uint${field.elementBits}(${conversion})`;
                            }
                            loadArrayLines.push(`result[${i}] = PatchworkUtils.toString${field.elementBits / 8}(${conversion});`);
                            storeArrayLines.push(`slot = slot | PatchworkUtils.strToUint256(${field.key}[${i}]) >> ${256 - field.elementBits} << ${offset};`);
                        } else {
                            loadArrayLines.push(`result[${i}] = ${field.solidityType}(slot${shift});`);
                            storeArrayLines.push(`slot = slot | uint256(${field.key}[${i}]) << ${offset};`);
                        }
                        
                        // TODO Make an individual item for each array element and join but with the max 8 per line logic
                        // So instead of individual lines we'd have slot = item1 | item2 | item3...
                        offset += field.elementBits;
                    }
                    storeArrayLines.push(`_metadataStorage[tokenId][${slot}] = slot;`)
                    const loadFunction = `` +
                    `// Load Array for ${field.key}\n` +
                    `function load${capName}(uint256 tokenId) public view returns (${field.solidityType}[] memory) {\n` +
                    `    ${field.solidityType}[] memory result = new ${field.solidityType}[](${field.arrayLength});\n` +
                    `    uint256 slot = _metadataStorage[tokenId][${field.slot}];\n` +
                    `    ${loadArrayLines.join("\n    ")}\n` +
                    `    return result;\n` +
                    `}\n`;
        
                    const storeFunction = `` +
                    `// Store Array for ${field.key}\n` +
                    `function store${capName}(uint256 tokenId, ${field.solidityType}[] memory ${field.key}) public {\n` +
                    `${ind(4, permissionLine)}\n` +
                    `    if (${field.key}.length != ${field.arrayLength}) {\n` +
                    `        revert IPatchworkProtocol.BadInputLengths();\n` +
                    `    }\n` +
                    `    uint256 slot = 0;\n` +
                    `    ${storeArrayLines.join("\n    ")}\n` +
                    `}\n`;
                    if (field.functionConfig === FunctionConfig.ALL || field.functionConfig === FunctionConfig.LOAD) {
                        loadStoreFunctions.push(loadFunction);
                    }
                    if (field.functionConfig === FunctionConfig.ALL || field.functionConfig === FunctionConfig.STORE) {
                        loadStoreFunctions.push(storeFunction);
                    }
                } else {
                    const shift = field.offset > 0 ? ` >> ${field.offset}` : "";
                    let loadFunction = `` +
                    `// Load Only ${field.key}\n` +
                    `function load${capName}(uint256 tokenId) public view returns (${field.solidityType}${field.isString ? ` memory` : ``}) {\n` +
                    `    uint256 value = uint256(_metadataStorage[tokenId][${field.slot}])${shift};\n`;
                    if (field.isString) {
                        let value = `value`;
                        if (field.elementBits < 256) {
                            value = `uint${field.elementBits}(${value})`;
                        }
                        loadFunction += `    return PatchworkUtils.toString${field.elementBits / 8}(${value});\n`;
                    } else Iif (field.fieldTypeSolidityEnum == `ADDRESS`) {
                        loadFunction += `    return address(uint160(value));\n`;
                    } else {
                        loadFunction += `    return ${field.solidityType}(value);\n`;
                    }
                    loadFunction += `}\n`;
                    let storeFunction = `` +
                    `// Store Only ${field.key}\n` +
                    `function store${capName}(uint256 tokenId, ${field.solidityType} ${field.isString ? `memory ` : ``}${field.key}) public {\n` +
                    `${ind(4, permissionLine)}\n`;
                    if (field.elementBits < 256) {
                        let shift = field.offset === 0 ? `` : ` << ${field.offset}`;
                        storeFunction += `    uint256 mask = (1 << ${field.totalBits}) - 1;\n`;
                        storeFunction += `    uint256 cleared = uint256(_metadataStorage[tokenId][${field.slot}]) & ~(mask${shift});\n`;
                        if (field.isString) {
                            storeFunction += `    _metadataStorage[tokenId][${field.slot}] = cleared | (PatchworkUtils.strToUint256(${field.key}) >> ${256 - field.elementBits} & mask)${shift};\n`;
                        } else Iif (field.fieldTypeSolidityEnum == `ADDRESS`) {
                            storeFunction += `    _metadataStorage[tokenId][${field.slot}] = cleared | (uint256(uint160(${field.key})) & mask)${shift};\n`;
                        } else {
                            storeFunction += `    _metadataStorage[tokenId][${field.slot}] = cleared | (uint256(${field.key}) & mask)${shift};\n`;
                        }                    
                    } else {
                        if (field.isString) {
                            storeFunction += `    _metadataStorage[tokenId][${field.slot}] = PatchworkUtils.strToUint256(${field.key});\n`;
                        } else E{
                            storeFunction += `    _metadataStorage[tokenId][${field.slot}] = uint256(${field.key});\n`;
                        }
                    }
 
                    storeFunction += `}\n`;
                    if (field.functionConfig === FunctionConfig.ALL || field.functionConfig === FunctionConfig.LOAD) {
                        loadStoreFunctions.push(loadFunction);
                    }
                    if (field.functionConfig === FunctionConfig.ALL || field.functionConfig === FunctionConfig.STORE) {
                        loadStoreFunctions.push(storeFunction);
                    }
                }
            }
    
            return loadStoreFunctions.join("\n");
        }
    
        const loadStoreFunctions = generateLoadStoreFunctions(schema.storage.fields);
 
        return ind(4, `${loadStoreFunctions}`);
    }
}