Skip to content

Commit

Permalink
Merge pull request #58 from nemgrouplimited/dev
Browse files Browse the repository at this point in the history
Dev -> Main
  • Loading branch information
AnthonyLaw authored Dec 9, 2020
2 parents 6f07a0c + ff47d24 commit d43fe71
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 89 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@ All notable changes to this project will be documented in this file.

The changelog format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).


## [0.4.1] - 9-Dec-2020

### Milestone: [[email protected]](https://github.com/nemtech/catapult-server/releases/tag/v0.10.0.4)

Package | Version | Link
---|---|---
REST Core| v2.2.0 | [catapult-rest](https://github.com/nemtech/catapult-rest/releases/tag/v2.2.0)
SDK Core| v0.22.0 | [symbol-sdk](https://www.npmjs.com/package/symbol-sdk)

### Added
- Added compatibility for 0.10.0.4 server.

### Fixed
- Lazy node health check when loading the page

## [0.4.0] - 24-Nov-2020

### Milestone: [[email protected]](https://github.com/nemtech/catapult-server/releases/tag/v0.10.0.3)
Expand Down
143 changes: 111 additions & 32 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@
"pug-loader": "^2.4.0",
"pug-plain-loader": "^1.0.0",
"querystring": "^0.2.0",
"rxjs": "^6.5.5",
"stylus": "^0.54.7",
"stylus-loader": "^3.0.2",
"symbol-sdk": "^0.21.0",
"symbol-sdk": "^0.22.0",
"tls": "0.0.1",
"tsconfig-paths": "^3.9.0",
"utf-8-validate": "^5.0.2",
"utf8": "^3.0.0",
"vue-material-design-icons": "^4.4.0",
"vue-property-decorator": "^8.4.0"
},
Expand Down
57 changes: 27 additions & 30 deletions server/app.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,60 @@
import { Account, RepositoryFactoryHttp, RepositoryFactory, NetworkType } from 'symbol-sdk';
import { Account, RepositoryFactoryHttp, RepositoryFactory, NetworkType, CurrencyService } from 'symbol-sdk';
import { timeout } from 'rxjs/operators';
import { IConfig, Config } from './config';

export interface IApp {
networkType: NetworkType;
isNodeHealth: boolean;
networkGenerationHash: string;
faucetAccount: Account;
networkType: Promise<NetworkType>;
isNodeHealth: Promise<boolean>;
networkGenerationHash: Promise<string>;
epochAdjustment: Promise<number>;
faucetAccount: Promise<Account>;
config: IConfig;
repositoryFactory: RepositoryFactory;
currencyService: CurrencyService;
}

export default class App implements IApp {
constructor(
private readonly _repositoryFactory: RepositoryFactory,
private readonly _config: IConfig,
private readonly _networkType: NetworkType,
private readonly _networkGenerationHash: string,
private readonly _isNodeHealth: boolean,
) {}
constructor(private readonly _repositoryFactory: RepositoryFactory, private readonly _config: IConfig) {}
public static async init(): Promise<App> {
const repositoryFactory = new RepositoryFactoryHttp(Config.DEFAULT_NODE);
const isNodeHealth: boolean = await App.isNodeHealth(repositoryFactory);

if (!isNodeHealth) {
return new App(repositoryFactory, Config, NetworkType.TEST_NET, '', isNodeHealth);
}
return new App(repositoryFactory, Config);
}

const [networkType, networkGenerationHash] = await Promise.all([
repositoryFactory.getNetworkType().toPromise(),
repositoryFactory.getGenerationHash().toPromise(),
]);
return new App(repositoryFactory, Config, networkType, networkGenerationHash, isNodeHealth);
get networkType(): Promise<NetworkType> {
// network type is lazily cached in repo factory.
return this._repositoryFactory.getNetworkType().toPromise();
}

get networkType(): NetworkType {
return this._networkType;
get isNodeHealth(): Promise<boolean> {
// perform a health check when is requested.
return App.isNodeHealth(this._repositoryFactory);
}

get isNodeHealth(): boolean {
return this._isNodeHealth;
get networkGenerationHash(): Promise<string> {
// generation hash is lazily cached in repo factory.
return this._repositoryFactory.getGenerationHash().toPromise();
}

get networkGenerationHash(): string {
return this._networkGenerationHash;
get epochAdjustment(): Promise<number> {
return this._repositoryFactory.getEpochAdjustment().toPromise();
}

get config(): IConfig {
return this._config;
}

get faucetAccount(): Account {
return Account.createFromPrivateKey(this._config.FAUCET_PRIVATE_KEY as string, this._networkType as NetworkType);
get faucetAccount(): Promise<Account> {
return this.networkType.then((networkType) => Account.createFromPrivateKey(this._config.FAUCET_PRIVATE_KEY, networkType));
}

get repositoryFactory(): RepositoryFactory {
return this._repositoryFactory;
}

get currencyService(): CurrencyService {
return new CurrencyService(this._repositoryFactory);
}

static isNodeHealth(repositoryFactory: RepositoryFactory): Promise<boolean> {
return new Promise((resolve) => {
repositoryFactory
Expand Down
16 changes: 8 additions & 8 deletions server/handlers/claims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ export const claimsHandler = (appConfig: IApp) => {
return async (req: any, res: any, next: any) => {
const { recipient, amount, selectedMosaics } = req.body;

const { repositoryFactory, config, faucetAccount } = appConfig;
if (typeof recipient !== 'string' || recipient.length !== 39) throw new Error(`recipient address invalid.`);

const { repositoryFactory, config } = appConfig;
const faucetAccount = await appConfig.faucetAccount;
const networkType = await appConfig.networkType;
const generationHash = await appConfig.networkGenerationHash;
const epochAdjustment = await appConfig.epochAdjustment;
console.debug({ recipient, amount, selectedMosaics });

if (typeof recipient !== 'string' || recipient.length !== 39) throw new Error(`recipient address invalid.`);

if (typeof amount !== 'number') throw new Error(`amount format invalid.`);

if (!Array.isArray(selectedMosaics)) throw new Error(`mosaics is not array.`);

const mosaicIds = selectedMosaics.map((mosaic) => new MosaicId(mosaic));
const recipientAddress: Address = Address.createFromRawAddress(recipient);
const networkType = await repositoryFactory.getNetworkType().toPromise();
const generationHash = await repositoryFactory.getGenerationHash().toPromise();
const feeMultiplier = await (await repositoryFactory.createNetworkRepository().getTransactionFees().toPromise())
.highestFeeMultiplier;
const feeMultiplier = (await repositoryFactory.createNetworkRepository().getTransactionFees().toPromise()).highestFeeMultiplier;

forkJoin(
repositoryFactory.createNamespaceRepository().getMosaicsNames(mosaicIds),
Expand Down Expand Up @@ -151,7 +151,7 @@ export const claimsHandler = (appConfig: IApp) => {
});

const transaction = TransferTransaction.create(
Deadline.create(),
Deadline.create(epochAdjustment),
recipientAddress,
requestedMosicList,
EmptyMessage,
Expand Down
Loading

0 comments on commit d43fe71

Please sign in to comment.