* Some initial cleanups * Make it lint * Small s/type/interface/ additions * Small minor s/type/interface/ * More type -> interface * Add lint:new rule * Some small updates * Cleanup dangling tslint issues * Small updates * ui-signer cleanups * ui-reactive updated * Make lint:new run * Mini ui-params cleanups * Some additions * Tiny... and done for now. * 1950 problems (1241 errors, 709 warnings) * 1829 problems (1181 errors, 648 warnings) * 1674 problems (1181 errors, 493 warnings) (no any... for now) * Small additions. * Array<T> -> T[] (all)
101 lines
2.4 KiB
TypeScript
101 lines
2.4 KiB
TypeScript
// Copyright 2017-2019 @polkadot/app-contracts authors & contributors
|
|
// This software may be modified and distributed under the terms
|
|
// of the Apache-2.0 license. See the LICENSE file for details.
|
|
|
|
import { CodeJson, CodeStored } from './types';
|
|
|
|
import EventEmitter from 'eventemitter3';
|
|
import store from 'store';
|
|
import { Abi } from '@polkadot/api-contract';
|
|
import { Hash } from '@polkadot/types';
|
|
import { api } from '@polkadot/ui-api';
|
|
|
|
const KEY_CODE = 'code:';
|
|
|
|
const codeRegex = new RegExp(`^${KEY_CODE}`, '');
|
|
|
|
class Store extends EventEmitter {
|
|
private allCode: { [index: string]: CodeStored } = {};
|
|
|
|
public get hasCode (): boolean {
|
|
return Object.keys(this.allCode).length !== 0;
|
|
}
|
|
|
|
public getAllCode (): CodeStored[] {
|
|
return Object.values(this.allCode);
|
|
}
|
|
|
|
public getCode (codeHash: string): CodeStored {
|
|
return this.allCode[codeHash];
|
|
}
|
|
|
|
public async saveCode (codeHash: string | Hash, partial: Partial<CodeJson>): Promise<void> {
|
|
const hex = (typeof codeHash === 'string' ? new Hash(codeHash) : codeHash).toHex();
|
|
|
|
const existing = this.getCode(hex);
|
|
|
|
const json = {
|
|
...(existing ? existing.json : {}),
|
|
...partial,
|
|
codeHash: hex,
|
|
genesisHash: api.genesisHash.toHex()
|
|
};
|
|
|
|
store.set(`${KEY_CODE}${json.codeHash}`, json);
|
|
|
|
this.addCode(json as CodeJson);
|
|
}
|
|
|
|
public forgetCode (codeHash: string): void {
|
|
store.remove(`${KEY_CODE}${codeHash}`);
|
|
|
|
this.removeCode(codeHash);
|
|
}
|
|
|
|
public async loadAll (): Promise<void> {
|
|
try {
|
|
await api.isReady;
|
|
|
|
const genesisHash = api.genesisHash.toHex();
|
|
|
|
store.each((json: CodeJson, key: string): void => {
|
|
if (json && json.genesisHash !== genesisHash) {
|
|
return;
|
|
}
|
|
|
|
if (codeRegex.test(key)) {
|
|
this.addCode(json);
|
|
}
|
|
});
|
|
} catch (error) {
|
|
console.error('Unable to load code', error);
|
|
}
|
|
}
|
|
|
|
private addCode (json: CodeJson): void {
|
|
try {
|
|
this.allCode[json.codeHash] = {
|
|
json,
|
|
contractAbi: json.abi
|
|
? new Abi(JSON.parse(json.abi))
|
|
: undefined
|
|
};
|
|
|
|
this.emit('new-code');
|
|
} catch (error) {
|
|
console.error(error);
|
|
}
|
|
}
|
|
|
|
private removeCode (codeHash: string): void {
|
|
try {
|
|
delete this.allCode[codeHash];
|
|
this.emit('removed-code');
|
|
} catch (error) {
|
|
console.error(error);
|
|
}
|
|
}
|
|
}
|
|
|
|
export default new Store();
|