export * from './compat' ; import { UpgradesError } from '../error' ; import { StorageLayout , getDetailedLayout } from './layout' ; import { StorageOperation , StorageItem , StorageLayoutComparator } from './compare' ; import { LayoutCompatibilityReport } from './report' ; import { ValidationOptions , withValidationDefaults } from '../validate/overrides' ; import { logNote , logWarning } from '../utils/log' ; export function assertStorageUpgradeSafe ( original , updated , unsafeAllowCustomTypes ) ; export function assertStorageUpgradeSafe ( original , updated , opts ) ; export function assertStorageUpgradeSafe ( original , updated , opts = false ) { if ( typeof opts === 'boolean' ) { const unsafeAllowCustomTypes = opts ; opts = withValidationDefaults ( { unsafeAllowCustomTypes } ) ; } const report = getStorageUpgradeReport ( original , updated , opts ) ; if ( ! report . pass ) { throw new StorageUpgradeErrors ( report ) ; } } export function getStorageUpgradeReport ( original , updated , opts ) { const originalDetailed = getDetailedLayout ( original ) ; const updatedDetailed = getDetailedLayout ( updated ) ; const comparator = new StorageLayoutComparator ( opts . unsafeAllowCustomTypes , opts . unsafeAllowRenames ) ; const report = comparator . compareLayouts ( originalDetailed , updatedDetailed ) ; if ( comparator . hasAllowedUncheckedCustomTypes ) { logWarning ( `Potentially unsafe deployment` , [ `You are using \`unsafeAllowCustomTypes\` to force approve structs or enums with missing data.` , `Make sure you have manually checked the storage layout for incompatibilities.` , ] ) ; } else if ( opts . unsafeAllowCustomTypes ) { logNote ( `\`unsafeAllowCustomTypes\` is no longer necessary. Structs are enums are automatically checked.\n` ) ; } return report ; } export class StorageUpgradeErrors extends UpgradesError { constructor ( readonly report ) { super ( `New storage layout is incompatible` , ( ) => report . explain ( ) ) ; } } export function getStorageUpgradeErrors ( original , updated , opts = { } ) { try { assertStorageUpgradeSafe ( original , updated , withValidationDefaults ( opts ) ) ; } catch ( e ) { if ( e instanceof StorageUpgradeErrors ) { return e . report . ops ; } else { throw e ; } } return [ ] ; }