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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | 5x 5x 5x 67x 5x 5x 42x 42x 42x 42x 42x 42x 42x 42x 42x 82x 42x 61x 1x 60x 23x 37x 4x 33x 42x 42x 62x 62x 62x 92x 92x 82x 82x 82x 82x 36x 62x 62x 82x 42x 42x 82x 82x 82x 82x 82x 42x 42x 82x 2x 2x 42x 42x 82x 8x 8x 42x 42x 42x 42x 42x 42x 25x 25x 25x 25x 42x 82x 4x 4x 78x 12x 8x 8x 12x 12x 66x 66x 66x 82x 82x 82x 82x 6x 6x 6x 6x 82x 35x 35x 11x 82x 42x 292x 85x 74x 37x 26x 26x 7x 7x 6x 35x 81x 35x 82x 82x 82x 82x 82x | import { ContractConfig, Feature, FieldConfig, FieldType, FunctionConfig, Visibility } from '../types';
export type ContractFieldType = {
solidityType: string;
name: string;
bits: number;
isString: boolean;
};
export class ContractStorageField implements FieldConfig {
// from Entry
id!: number;
permissionId!: number;
type!: FieldType;
arrayLength!: number;
visibility!: Visibility;
key!: string;
description!: string;
functionConfig!: FunctionConfig;
// calculated
solidityType!: string;
fieldTypeSolidityEnum!: string;
slot!: number;
isString!: boolean;
elementBits!: number;
totalBits!: number;
offset!: number;
}
export class ContractStorageSlot {
fieldIDs: number[] = [];
}
export class ContractStorage {
slots: ContractStorageSlot[] = [];
fields: ContractStorageField[] = [];
}
export interface ContractSchema extends ContractConfig {
features: Feature[];
storage: ContractStorage;
hasLiteRef(): boolean;
getMetadataStructName(): string;
liteRefField(which: number): ContractStorageField;
liteRefFieldCount(): number;
liteRefArrayLength(which: number): number;
liteRefSlotNumber(which: number): number;
validate(): any;
slots(): number;
getField(fieldID: number): ContractStorageField;
}
// ContractConfig from user or file ->> ContractSchemaImpl
export class ContractSchemaImpl implements ContractSchema {
scopeName!: string;
name!: string;
symbol!: string;
baseURI!: string;
schemaURI!: string;
imageURI!: string;
fields!: FieldConfig[];
features!: Feature[];
storage: ContractStorage;
constructor(config: ContractConfig) {
this.scopeName = config.scopeName;
this.name = config.name;
this.symbol = config.symbol;
this.baseURI = config.baseURI;
this.schemaURI = config.schemaURI;
this.imageURI = config.imageURI;
this.fields = config.fields;
this.features = config.features || [];
this.storage = this.buildStorage(config.fields);
}
orderFieldsPacked(origFields: ContractStorageField[]): ContractStorageField[] {
let fields = origFields.map(x => x);
fields.sort((a: ContractStorageField, b: ContractStorageField) => {
// sort static arrays to the end so that other fields can pack easily before we start needing 0 offsets
if (a.arrayLength > 1 && b.arrayLength == 1) {
return 1;
}
if (a.arrayLength == 1 && b.arrayLength > 1) {
return -1;
}
if (a.totalBits >= 256 && b.totalBits >= 256) {
return 0;
}
return b.totalBits - a.totalBits;
});
let newFields: ContractStorageField[] = [];
while (fields.length > 0) {
let curSlotRemainder = 256;
let spliceList = [];
for (let i = 0; i < fields.length; i++) {
const field = fields[i];
if (curSlotRemainder >= field.totalBits || field.totalBits > 256 && curSlotRemainder == 256) {
newFields.push(field);
// mark for removal
spliceList.push(i);
curSlotRemainder -= field.totalBits;
if (curSlotRemainder == 0) {
// this slot is fully packed.
// stop here so we start back at the largest fields for the next iteration.
break;
}
}
}
spliceList.reverse();
for (let idx of spliceList) {
fields.splice(idx, 1);
}
}
return newFields;
}
buildStorage(entries: FieldConfig[]): ContractStorage {
// console.log(entries);
let fields: ContractStorageField[] = entries.map((entry: FieldConfig, index: number) => {
const fieldTypeEnum = this.getFieldTypeEnum(entry.type);
const fieldArrayLength = entry.arrayLength === undefined ? 1 : entry.arrayLength;
const bits = fieldTypeEnum.bits * fieldArrayLength;
const field: ContractStorageField = {
id: entry.id,
permissionId: entry.permissionId || 0,
solidityType: fieldTypeEnum.solidityType,
type: entry.type,
fieldTypeSolidityEnum: fieldTypeEnum.name,
arrayLength: fieldArrayLength,
visibility: "public",
isString: fieldTypeEnum.isString,
elementBits: fieldTypeEnum.bits,
totalBits: bits,
key: entry.key,
slot: -1,
offset: -1,
description: entry.description || "",
functionConfig: entry.functionConfig || FunctionConfig.ALL,
};
return field;
});
let hasDynString = false;
for (let i = 0; i < fields.length; i++) {
if (fields[i].type === "string") {
Iif (hasDynString) {
throw new Error("Only one dynamic string field is currently supported in PDK.");
}
hasDynString = true;
}
}
let hasLiteRef = false;
for (let i = 0; i < fields.length; i++) {
if (fields[i].type === "literef") {
Iif (hasLiteRef) {
throw new Error("Only one field of literefs is currently supported in PDK.");
}
hasLiteRef = true;
}
}
fields = this.orderFieldsPacked(fields);
let slots: ContractStorageSlot[] = [];
let slot = new ContractStorageSlot();
slots.push(slot);
let slotNumber = 0;
let offset = 0;
function nextSlot() {
slot = new ContractStorageSlot();
slots.push(slot);
slotNumber += Math.floor(offset / 256);
offset = offset % 256;
}
fields = fields.map((field: ContractStorageField, index: number) => {
if (field.arrayLength == 0) {
field.slot = 0;
field.offset = 0;
} else if (field.arrayLength > 1) {
if (offset > 0) {
offset = 256;
nextSlot();
}
field.slot = slotNumber;
field.offset = 0;
} else {
Iif (offset > 0 && offset + field.totalBits > 256) {
// This field would span starting at a non-0 offset over another slot which is unsupported
// leave the rest of the slot empty and move on to the next.
offset = 256;
nextSlot();
}
field.slot = slotNumber;
field.offset = offset;
}
slot.fieldIDs.push(field.id);
// how many more slots this spans
let slotSpan = Math.floor(field.totalBits / 256);
let bitsUsedThisSlot = field.totalBits;
if (slotSpan > 1) {
for (let i = 0; i < slotSpan - 1; i++) {
nextSlot();
slot.fieldIDs.push(field.id);
}
bitsUsedThisSlot = field.totalBits - ((slotSpan-1) * 256)
}
// skip this if there aren't more bits later
if (index < entries.length-1 && fields[index+1].totalBits > 0) {
offset += bitsUsedThisSlot;
if (offset >= 256) {
nextSlot();
}
}
return field;
});
return {slots: slots, fields: fields};
}
hasLiteRef(): boolean {
return this.fields.some((field: any) => field.type === "literef")
}
getMetadataStructName(): string {
return `Metadata`;
}
liteRefField(which: number): ContractStorageField {
const liteRefFields = this.storage.fields.filter((field: any) => field.fieldTypeSolidityEnum === "LITEREF");
return liteRefFields[which];
}
liteRefFieldCount(): number {
// Count fields where type is "literef"
return this.storage.fields.filter((field: any) => field.fieldTypeSolidityEnum === "LITEREF").length;
}
liteRefArrayLength(which: number): number {
// Filter fields by type "literef" and find by index
const liteRefField = this.liteRefField(which);
return liteRefField ? liteRefField.arrayLength : 0;
}
liteRefSlotNumber(which: number): number {
// Filter fields by type "literef" and find by index
const liteRefField = this.liteRefField(which);
return liteRefField ? liteRefField.slot : 0;
}
validate() {
const patchTypes = [Feature.PATCH, Feature['1155PATCH'], Feature.ACCOUNTPATCH];
const patchTypeCount = this.features.filter(feature => patchTypes.includes(feature)).length;
Iif (patchTypeCount > 1) {
throw new Error('PATCH, 1155PATCH, and ACCOUNTPATCH are mutually exclusive.');
}
const fragmentTypes = [Feature.FRAGMENTMULTI, Feature.FRAGMENTSINGLE];
const fragmentTypeCount = this.features.filter(feature => fragmentTypes.includes(feature)).length;
Iif (fragmentTypeCount > 1) {
throw new Error('FRAGMENTMULTI and FRAGMENTSINGLE are mutually exclusive.');
}
const hasReversible = this.features.includes(Feature.REVERSIBLE);
Iif (hasReversible && patchTypeCount === 0) {
throw new Error('REVERSIBLE feature requires at least one of PATCH, 1155PATCH, or ACCOUNTPATCH to be present.');
}
const hasWeakRef = this.features.includes(Feature.WEAKREF);
Iif (hasWeakRef && !this.features.includes(Feature.FRAGMENTSINGLE)) {
throw new Error('WEAKREF feature requires FRAGMENTSINGLE feature');
}
Iif (this.features.includes(Feature.DYNAMICREFLIBRARY)) {
Iif (!(this.hasLiteRef() && this.liteRefArrayLength(0) === 0)) {
throw new Error('DYNAMICREFLIBRARY feature requires a dynamic array length literef field');
}
}
}
slots(): number {
return this.storage.slots.length;
}
getField(fieldID: number): ContractStorageField {
for (let i = 0; i < this.storage.fields.length; i++) {
if (this.storage.fields[i].id == fieldID) {
return this.storage.fields[i];
}
}
throw Error("No field with ID " + fieldID);
}
getFieldTypeEnum(name: string) {
const fieldTypeMap: Record<string, ContractFieldType> = {
bool: { solidityType: "bool", name: "BOOLEAN", bits: 1, isString: false },
int8: { solidityType: "int8", name: "INT8", bits: 8, isString: false },
int16: { solidityType: "int16", name: "INT16", bits: 16, isString: false },
int32: { solidityType: "int32", name: "INT32", bits: 32, isString: false },
int64: { solidityType: "int64", name: "INT64", bits: 64, isString: false },
int128: { solidityType: "int128", name: "INT128", bits: 128, isString: false },
int256: { solidityType: "int256", name: "INT256", bits: 256, isString: false },
uint8: { solidityType: "uint8", name: "UINT8", bits: 8, isString: false },
uint16: { solidityType: "uint16", name: "UINT16", bits: 16, isString: false },
uint32: { solidityType: "uint32", name: "UINT32", bits: 32, isString: false },
uint64: { solidityType: "uint64", name: "UINT64", bits: 64, isString: false },
uint128: { solidityType: "uint128", name: "UINT128", bits: 128, isString: false },
uint256: { solidityType: "uint256", name: "UINT256", bits: 256, isString: false },
char8: { solidityType: "string", name: "CHAR8", bits: 64, isString: true },
char16: { solidityType: "string", name: "CHAR16", bits: 128, isString: true },
char32: { solidityType: "string", name: "CHAR32", bits: 256, isString: true },
char64: { solidityType: "string", name: "CHAR64", bits: 512, isString: true },
literef: { solidityType: "uint64", name: "LITEREF", bits: 64, isString: false },
address: { solidityType: "address", name: "ADDRESS", bits: 160, isString: false },
string: { solidityType: "string", name: "STRING", bits: 0, isString: true },
};
const fieldType = fieldTypeMap[name];
Iif (!fieldType) {
throw new Error(`Unknown field type: ${name}`);
}
Iif (fieldType.name === "CHAR64") {
throw new Error("CHAR64 is not currently supported in the pdk. Consider using the dynamic type string instead.");
}
return fieldType;
}
} |