@iqprotocol/abstract-storage
Advanced tools
Comparing version 0.12.0 to 0.13.0
@@ -1,5 +0,5 @@ | ||
interface Account { | ||
type Account = { | ||
id: string; | ||
data: AccountData; | ||
} | ||
}; | ||
type AccountOwnershipProof = { | ||
@@ -15,3 +15,3 @@ v: string; | ||
}; | ||
interface AccountState { | ||
type AccountState = { | ||
serviceId: string; | ||
@@ -25,3 +25,3 @@ accountId: string; | ||
energyCalculatedAt: number; | ||
} | ||
}; | ||
type AccountStateChangeResult = { | ||
@@ -31,2 +31,6 @@ successful: boolean; | ||
}; | ||
interface AccountStateValidator { | ||
validateAccount(account: Account): void; | ||
validateAccountState(accountState: AccountState): void; | ||
} | ||
interface StorageProvider { | ||
@@ -41,16 +45,11 @@ saveAccount(account: Account): Promise<Account>; | ||
} | ||
interface AccountStateValidator { | ||
validateAccount(account: Account): void; | ||
validateAccountState(accountState: AccountState): void; | ||
declare class AccountStateError extends Error { | ||
constructor(msg: string); | ||
} | ||
declare class DefaultValidator implements AccountStateValidator { | ||
validateAccount(account: Account): void; | ||
validateAccountState(accountState: AccountState): void; | ||
} | ||
type AbstractStoreConfig = { | ||
validator?: AccountStateValidator; | ||
}; | ||
declare abstract class AbstractStore implements StorageProvider { | ||
declare abstract class AccountStore implements StorageProvider { | ||
private readonly validator; | ||
protected constructor(config: AbstractStoreConfig); | ||
protected constructor({ validator }: AbstractStoreConfig); | ||
saveAccount(account: Account): Promise<Account>; | ||
@@ -67,6 +66,7 @@ initAccountState(accountState: AccountState): Promise<AccountState>; | ||
} | ||
declare class AccountStateError extends Error { | ||
constructor(msg: string); | ||
declare class DefaultValidator implements AccountStateValidator { | ||
validateAccount(account: Account): void; | ||
validateAccountState(accountState: AccountState): void; | ||
} | ||
export { Account, AccountOwnershipProof, AccountData, AccountState, AccountStateChangeResult, StorageProvider, AbstractStoreConfig, AbstractStore, AccountStateValidator, DefaultValidator, AccountStateError }; | ||
export { Account, AccountOwnershipProof, AccountData, AccountState, AccountStateChangeResult, AccountStateValidator, StorageProvider, AccountStateError, AccountStore, DefaultValidator }; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});class t extends Error{constructor(e){super(e),Error.captureStackTrace&&Error.captureStackTrace(this,t),this.name="AccountStateError"}}const e=t=>null==t||!String(t).trim().length;class r{validateAccount(t){if(e(t.id))throw new Error("Empty id")}validateAccountState(r){if(["serviceId","accountId","power","lockedPower","energyCap","energy","energyCalculatedAt","energyGapHalvingPeriod"].forEach((a=>{if(e(r[a]))throw new t(`Empty ${a}`)})),r.power<0)throw new t("Negative power");if(r.energyGapHalvingPeriod<=0)throw new t("Invalid energy gap halving period");if(r.lockedPower<0)throw new t("Negative locked power");if(r.energy>r.energyCap)throw new t("Energy above cap");if(r.energyCalculatedAt<0)throw new t("Negative energy calculation time")}}exports.AbstractStore=class{constructor(t){this.validator=t.validator??new r}async saveAccount(t){return this.validator.validateAccount(t),this._saveAccount(t)}async initAccountState(t){return this.validator.validateAccountState(t),this._initAccountState(t)}async changeAccountState(t,e){if(t.accountId!==e.accountId)throw new Error("Account ID mismatch");if(t.serviceId!==e.serviceId)throw new Error("Service ID mismatch");return this.validator.validateAccountState(e),this._changeAccountState(t,e)}},exports.AccountStateError=t,exports.DefaultValidator=r; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});class t extends Error{constructor(e){super(e),Error.captureStackTrace&&Error.captureStackTrace(this,t),this.name="AccountStateError"}}const e=t=>null==t||!String(t).trim().length;class r{validateAccount(t){if(e(t.id))throw new Error("Empty id")}validateAccountState(r){if(["serviceId","accountId","power","lockedPower","energyCap","energy","energyCalculatedAt","energyGapHalvingPeriod"].forEach((a=>{if(e(r[a]))throw new t(`Empty ${a}`)})),r.power<0)throw new t("Negative power");if(r.energyGapHalvingPeriod<=0)throw new t("Invalid energy gap halving period");if(r.lockedPower<0)throw new t("Negative locked power");if(r.energy>r.energyCap)throw new t("Energy above cap");if(r.energyCalculatedAt<0)throw new t("Negative energy calculation time")}}exports.AccountStateError=t,exports.AccountStore=class{constructor({validator:t}){this.validator=t??new r}async saveAccount(t){return this.validator.validateAccount(t),this._saveAccount(t)}async initAccountState(t){return this.validator.validateAccountState(t),this._initAccountState(t)}async changeAccountState(t,e){if(t.accountId!==e.accountId)throw new Error("Account ID mismatch");if(t.serviceId!==e.serviceId)throw new Error("Service ID mismatch");return this.validator.validateAccountState(e),this._changeAccountState(t,e)}},exports.DefaultValidator=r; | ||
//# sourceMappingURL=index.js.map |
@@ -1,5 +0,5 @@ | ||
interface Account { | ||
type Account = { | ||
id: string; | ||
data: AccountData; | ||
} | ||
}; | ||
type AccountOwnershipProof = { | ||
@@ -15,3 +15,3 @@ v: string; | ||
}; | ||
interface AccountState { | ||
type AccountState = { | ||
serviceId: string; | ||
@@ -25,3 +25,3 @@ accountId: string; | ||
energyCalculatedAt: number; | ||
} | ||
}; | ||
type AccountStateChangeResult = { | ||
@@ -31,2 +31,6 @@ successful: boolean; | ||
}; | ||
interface AccountStateValidator { | ||
validateAccount(account: Account): void; | ||
validateAccountState(accountState: AccountState): void; | ||
} | ||
interface StorageProvider { | ||
@@ -41,16 +45,11 @@ saveAccount(account: Account): Promise<Account>; | ||
} | ||
interface AccountStateValidator { | ||
validateAccount(account: Account): void; | ||
validateAccountState(accountState: AccountState): void; | ||
declare class AccountStateError extends Error { | ||
constructor(msg: string); | ||
} | ||
declare class DefaultValidator implements AccountStateValidator { | ||
validateAccount(account: Account): void; | ||
validateAccountState(accountState: AccountState): void; | ||
} | ||
type AbstractStoreConfig = { | ||
validator?: AccountStateValidator; | ||
}; | ||
declare abstract class AbstractStore implements StorageProvider { | ||
declare abstract class AccountStore implements StorageProvider { | ||
private readonly validator; | ||
protected constructor(config: AbstractStoreConfig); | ||
protected constructor({ validator }: AbstractStoreConfig); | ||
saveAccount(account: Account): Promise<Account>; | ||
@@ -67,6 +66,7 @@ initAccountState(accountState: AccountState): Promise<AccountState>; | ||
} | ||
declare class AccountStateError extends Error { | ||
constructor(msg: string); | ||
declare class DefaultValidator implements AccountStateValidator { | ||
validateAccount(account: Account): void; | ||
validateAccountState(accountState: AccountState): void; | ||
} | ||
export { Account, AccountOwnershipProof, AccountData, AccountState, AccountStateChangeResult, StorageProvider, AbstractStoreConfig, AbstractStore, AccountStateValidator, DefaultValidator, AccountStateError }; | ||
export { Account, AccountOwnershipProof, AccountData, AccountState, AccountStateChangeResult, AccountStateValidator, StorageProvider, AccountStateError, AccountStore, DefaultValidator }; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -1,2 +0,2 @@ | ||
class t extends Error{constructor(e){super(e),Error.captureStackTrace&&Error.captureStackTrace(this,t),this.name="AccountStateError"}}const e=t=>null==t||!String(t).trim().length;class r{validateAccount(t){if(e(t.id))throw new Error("Empty id")}validateAccountState(r){if(["serviceId","accountId","power","lockedPower","energyCap","energy","energyCalculatedAt","energyGapHalvingPeriod"].forEach((a=>{if(e(r[a]))throw new t(`Empty ${a}`)})),r.power<0)throw new t("Negative power");if(r.energyGapHalvingPeriod<=0)throw new t("Invalid energy gap halving period");if(r.lockedPower<0)throw new t("Negative locked power");if(r.energy>r.energyCap)throw new t("Energy above cap");if(r.energyCalculatedAt<0)throw new t("Negative energy calculation time")}}class a{constructor(t){this.validator=t.validator??new r}async saveAccount(t){return this.validator.validateAccount(t),this._saveAccount(t)}async initAccountState(t){return this.validator.validateAccountState(t),this._initAccountState(t)}async changeAccountState(t,e){if(t.accountId!==e.accountId)throw new Error("Account ID mismatch");if(t.serviceId!==e.serviceId)throw new Error("Service ID mismatch");return this.validator.validateAccountState(e),this._changeAccountState(t,e)}}export{a as AbstractStore,t as AccountStateError,r as DefaultValidator}; | ||
class t extends Error{constructor(e){super(e),Error.captureStackTrace&&Error.captureStackTrace(this,t),this.name="AccountStateError"}}const e=t=>null==t||!String(t).trim().length;class r{validateAccount(t){if(e(t.id))throw new Error("Empty id")}validateAccountState(r){if(["serviceId","accountId","power","lockedPower","energyCap","energy","energyCalculatedAt","energyGapHalvingPeriod"].forEach((a=>{if(e(r[a]))throw new t(`Empty ${a}`)})),r.power<0)throw new t("Negative power");if(r.energyGapHalvingPeriod<=0)throw new t("Invalid energy gap halving period");if(r.lockedPower<0)throw new t("Negative locked power");if(r.energy>r.energyCap)throw new t("Energy above cap");if(r.energyCalculatedAt<0)throw new t("Negative energy calculation time")}}class a{constructor({validator:t}){this.validator=t??new r}async saveAccount(t){return this.validator.validateAccount(t),this._saveAccount(t)}async initAccountState(t){return this.validator.validateAccountState(t),this._initAccountState(t)}async changeAccountState(t,e){if(t.accountId!==e.accountId)throw new Error("Account ID mismatch");if(t.serviceId!==e.serviceId)throw new Error("Service ID mismatch");return this.validator.validateAccountState(e),this._changeAccountState(t,e)}}export{t as AccountStateError,a as AccountStore,r as DefaultValidator}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@iqprotocol/abstract-storage", | ||
"version": "0.12.0", | ||
"version": "0.13.0", | ||
"bugs": "https://github.com/iqlabsorg/iq-sdk-js/issues", | ||
@@ -29,3 +29,3 @@ "repository": { | ||
"typedocMain": "src/index.ts", | ||
"gitHead": "91320f84a804bf0cc6520d8254cd53d7e582d618" | ||
"gitHead": "01bad7f14ccc0e1f1921972b49f102fa1e0e0256" | ||
} |
@@ -8,3 +8,3 @@ # Abstract Storage | ||
The IQ Protocol off-chain state management is storage agnostic and relies on storage providers to implement required functionality. | ||
This package provides `AbstractStore` class which can be extended to implement store specific provider. | ||
This package provides `AccountStore` class which can be extended to implement store specific provider. | ||
@@ -19,7 +19,7 @@ Use this package when you want to implement a new storage provider. | ||
## Usage | ||
All `AbstractStore` implementations use built-in account state validator by default. It is also possible to provide a custom validator. | ||
All `AccountStore` implementations use built-in account state validator by default. It is also possible to provide a custom validator. | ||
```ts | ||
import { AbstractStore, AccountStateValidator } from '@iqprotocol/abstract-storage'; | ||
import { AccountStore, AccountStateValidator } from '@iqprotocol/abstract-storage'; | ||
class CustomStore extends AbstractStore { | ||
class CustomStore extends AccountStore { | ||
// ... | ||
@@ -26,0 +26,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21621