Skip to content
Snippets Groups Projects
Commit b7752837 authored by Benoit Lavenier's avatar Benoit Lavenier
Browse files

initial import

parent b0380564
No related branches found
No related tags found
No related merge requests found
# cesium2s # cesium2s
Cesium 2, running on Substrate Cesium 2, running on Substrate
\ No newline at end of file
npm install -g yarn @ionic/cli @angular/cli
import {Injectable} from "@angular/core";
import {Platform} from "@ionic/angular";
import {environment} from "../../environments/environment";
@Injectable()
export abstract class AppBaseService {
private readonly _debug: boolean;
private readonly _logPrefix: string = null;
private _started = false;
private _startPromise: Promise<void> = null;
get started(): boolean {
return this._started;
}
protected constructor(protected platform: Platform, opts?: {
logPrefix?: string;
name?: string;
}) {
this._debug = !environment.production;
this._logPrefix = (opts && opts.logPrefix) || ("[" + (opts && opts.name || 'base-service') + "] ");
}
start(): Promise<any> {
if (this._startPromise) return this._startPromise;
if (this._started) return Promise.resolve();
this._started = false;
const now = Date.now();
this.info('Starting service...');
this._startPromise = this.platform.ready()
.then(() => this.doStart())
.then((result: any) => {
this._started = true;
this._startPromise = undefined;
this.info(`Starting service [OK] in ${Date.now() - now}ms`);
return result;
})
.catch((err) => {
this.error('Cannot start:', err);
throw err; // Rethrow
});
return this._startPromise;
}
ready(): Promise<boolean> {
if (this._started) return Promise.resolve(true);
return this.start()
.then((_) => {
return true;
})
.catch((err) => {
return false;
});
}
protected abstract doStart(): Promise<any>;
protected debug(msg, ...params: any[]) {
if (this._debug) console.debug(this._logPrefix + msg, params);
}
protected info(msg, ...params: any[]) {
console.info(this._logPrefix + msg, params);
}
protected warn(msg, ...params: any[]) {
console.warn(this._logPrefix + msg, params);
}
protected error(msg, ...params: any[]) {
console.error(this._logPrefix + msg, params);
}
}
import {Injectable} from "@angular/core";
import {Platform} from "@ionic/angular";
import {AppBaseService} from "./base.service";
import {ApiPromise, WsProvider} from "@polkadot/api";
import {web3Accounts, web3Enable, web3FromAddress} from "@polkadot/extension-dapp";
import {environment} from "../../environments/environment";
@Injectable({providedIn: 'root'})
export class NodeService extends AppBaseService {
private _api: ApiPromise;
get api(): ApiPromise {
return this._api
}
constructor(
platform: Platform
) {
super(platform, {
name: 'node-service'
});
}
protected async doStart(): Promise<any> {
// Construct
const wsProvider = new WsProvider('ws://localhost:9944');
const api = await ApiPromise.create({ provider: wsProvider });
// Do something
this.info("Connected to Blockchain genesis: " + api.genesisHash.toHex());
this._api = api;
}
}
import {Injectable} from "@angular/core";
import {Platform} from "@ionic/angular";
import {AppBaseService} from "./base.service";
import {NodeService} from "./node.service";
@Injectable({providedIn: 'root'})
export class PlatformService extends AppBaseService {
private _mobile: boolean = null;
private _touchUi: boolean = null;
get mobile(): boolean {
return this._mobile != null ? this._mobile : this.platform.is('mobile');
}
get touchUi(): boolean {
return this._touchUi != null ? this._touchUi :
(this.mobile || this.platform.is('tablet') || this.platform.is('phablet'));
}
constructor(
platform: Platform,
private node: NodeService
) {
super(platform, {
name: 'platform-service'
})
this.start();
}
async doStart(): Promise<any> {
this._mobile = this.mobile;
this._touchUi = this.touchUi;
await Promise.all([
this.node.start()
]
)
// TODO: Init required service
}
}
import {Injectable} from "@angular/core";
import {Platform} from "@ionic/angular";
import {AppBaseService} from "./base.service";
import {web3Accounts, web3Enable, web3FromAddress, web3FromSource} from "@polkadot/extension-dapp";
import {environment} from "../../environments/environment";
import {NodeService} from "./node.service";
import {ApiPromise} from "@polkadot/api";
import {InjectedAccountWithMeta} from "@polkadot/extension-inject/types";
@Injectable({providedIn: 'root'})
export class WalletService extends AppBaseService {
accounts: InjectedAccountWithMeta[];
get api(): ApiPromise {
return this.node.api;
}
constructor(
platform: Platform,
protected node: NodeService
) {
super(platform, {
name: 'wallet-service'
})
}
protected async doStart(): Promise<any> {
await this.node.ready();
// returns an array of all the injected sources
// (this needs to be called first, before other requests)
const extensions = await web3Enable(environment.name);
if (extensions.length === 0) {
// no extension installed, or the user did not accept the authorization
// in this case we should inform the use and give a link to the extension
console.debug('No web3 extension found');
}
// returns an array of { address, meta: { name, source } }
// meta.source contains the name of the extension that provides this account
this.accounts = await web3Accounts();
if (this.accounts?.length === 0) {
this.accounts = [{
address: '5ESoncgJ42j8WAyh9SBk2ztMZhUzejEZxF93LnZVBCXR77Kg',
meta: {
name: 'Alice',
source: '0xc74be00087825d9f8ba924d38fde43a8e8953c40c8f6a269b8a4ca337fbef7b7'
}
}]
}
}
async transfer() {
// the address we use to use for signing, as injected
const SENDER = this.accounts[0].address;
// finds an injector for an address
//const injector = await web3FromAddress(SENDER);
const injector = await web3FromSource(this.accounts[0].meta.source);
// sign and send our transaction - notice here that the address of the account
// (as retrieved injected) is passed through as the param to the `signAndSend`,
// the API then calls the extension to present to the user and get it signed.
// Once complete, the api sends the tx + signature via the normal process
this.api.tx.balances
.transfer('5C5555yEXUcmEJ5kkcCMvdZjUo7NGJiQJMS7vZXEeoMhj3VQ', 12)
.signAndSend(SENDER, { signer: injector.signer }, (status) => {
if (status.isInBlock) {
console.log(`Completed at block hash #${status}`);
} else {
console.log(`Current status: ${status}`);
}
}).catch((error: any) => {
console.log(':( transaction failed', error);
});
}
}
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { WalletPage } from './wallet.page';
const routes: Routes = [
{
path: '',
component: WalletPage
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
})
export class WalletPageRoutingModule {}
import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import {FormsModule} from '@angular/forms';
import {IonicModule} from '@ionic/angular';
import {WalletPage} from './wallet.page';
import {WalletPageRoutingModule} from "./wallet-routing.module";
@NgModule({
imports: [
CommonModule,
FormsModule,
IonicModule,
WalletPageRoutingModule
],
declarations: [WalletPage]
})
export class WalletPageModule {}
<ion-header [translucent]="true">
<ion-toolbar>
<ion-buttons slot="start">
<ion-menu-button></ion-menu-button>
</ion-buttons>
<ion-title>{{ walletId }}</ion-title>
</ion-toolbar>
</ion-header>
<ion-content [fullscreen]="true">
<ion-header collapse="condense">
<ion-toolbar>
<ion-title size="large">{{ walletId }}</ion-title>
</ion-toolbar>
</ion-header>
<div id="container">
<ion-list>
<ion-item>
<ion-icon slot="start" name="person"></ion-icon>
<ion-text>Test</ion-text>
</ion-item>
</ion-list>
<ion-button (click)="wallet.transfer()">
<ion-icon slot="start" name="paper-plane"></ion-icon>
<ion-label>Envoyer</ion-label>
</ion-button>
</div>
</ion-content>
ion-menu-button {
color: var(--ion-color-primary);
}
#container {
}
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { IonicModule } from '@ionic/angular';
import { RouterModule } from '@angular/router';
import { WalletPage } from './wallet.page';
describe('FolderPage', () => {
let component: WalletPage;
let fixture: ComponentFixture<WalletPage>;
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
declarations: [ WalletPage ],
imports: [IonicModule.forRoot(), RouterModule.forRoot([])]
}).compileComponents();
fixture = TestBed.createComponent(WalletPage);
component = fixture.componentInstance;
fixture.detectChanges();
}));
it('should create', () => {
expect(component).toBeTruthy();
});
});
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import {WalletService} from "../services/wallet.service";
@Component({
selector: 'app-wallet',
templateUrl: './wallet.page.html',
styleUrls: ['./wallet.page.scss'],
})
export class WalletPage implements OnInit {
public walletId: string;
constructor(
public wallet: WalletService,
private activatedRoute: ActivatedRoute
) {
this.load();
}
ngOnInit() {
this.walletId = this.activatedRoute.snapshot.paramMap.get('id');
}
async load() {
await this.wallet.ready();
}
}
export interface Environment {
name: string;
version?: string;
production: boolean;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment