-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.component.ts
191 lines (164 loc) · 6.16 KB
/
app.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { CommonModule } from '@angular/common';
import { AfterViewInit, ChangeDetectionStrategy, Component, ElementRef, Inject, OnDestroy, OnInit, ViewChild, } from '@angular/core';
import { CONFIG, ENV, EnvironmentConfig } from '@util/config';
import { SDK_TOKEN } from '@util/wasm';
import { SDK, PeerEntry, Transaction } from "casper-sdk";
import { ResultComponent, HeaderComponent, ErrorComponent, StatusComponent, ActionComponent, SubmitActionComponent, PublicKeyComponent, SecretKeyComponent, FormComponent } from '@components';
import { Subscription } from 'rxjs';
import { State, StateService } from '@util/state';
import { ResultService } from '@util/result';
import { ClientService } from '@util/client';
import { FormService } from '@util/form';
import { FormGroup, ReactiveFormsModule } from '@angular/forms';
import { ErrorService } from '@util/error';
import { StorageService } from '@util/storage';
import { BinaryService } from '@util-binary';
const imports = [
CommonModule,
ReactiveFormsModule,
FormComponent,
ResultComponent,
HeaderComponent,
ErrorComponent,
StatusComponent,
ActionComponent,
SubmitActionComponent,
PublicKeyComponent,
SecretKeyComponent,
];
@Component({
standalone: true,
imports,
changeDetection: ChangeDetectionStrategy.OnPush,
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
})
export class AppComponent implements OnInit, AfterViewInit, OnDestroy {
action!: string;
peers!: PeerEntry[];
form: FormGroup = this.formService.form;
@ViewChild('selectDictIdentifierElt') selectDictIdentifierElt!: ElementRef;
private wasm!: Uint8Array | undefined;
private stateSubscription!: Subscription;
constructor(
@Inject(SDK_TOKEN) private readonly sdk: SDK,
@Inject(CONFIG) public readonly config: EnvironmentConfig,
@Inject(ENV) public readonly env: EnvironmentConfig,
private readonly clientService: ClientService,
private readonly binaryService: BinaryService,
private readonly resultService: ResultService,
private readonly stateService: StateService,
private readonly formService: FormService,
private readonly errorService: ErrorService,
private readonly storageService: StorageService,
) {
this.setStateSubscription();
}
public async ngOnInit(): Promise<void> {
console.info(this.sdk);
}
public ngOnDestroy() {
this.stateSubscription && this.stateSubscription.unsubscribe();
}
private setStateSubscription() {
this.stateSubscription = this.stateService.getState().subscribe((state: State) => {
state.action && (this.action = state.action);
});
}
public async ngAfterViewInit() {
const no_mark_for_check = true;
const action = this.storageService.get('action') || this.config['default_action'].toString();
try {
if (action == this.config['default_action'].toString()) {
await this.handleAction(action, true);
}
await this.get_state_root_hash(no_mark_for_check);
} catch (error) {
console.error(error);
this.errorService.setError(error as string);
}
this.stateService.setState({
action
});
this.setStateSubscription();
}
async selectAction(action: string) {
await this.cleanResult();
this.stateService.setState({
action
});
await this.handleAction(action);
this.storageService.setState({
action
});
}
public async submitAction(action: string) {
await this.cleanResult();
const exec = true;
if (this.form.disabled || this.form.valid) {
await this.handleAction(action, exec);
}
}
async walletSign(_$event: Event, _action: string) {
this.clientService.wallet_sign_deploy();
}
private async handleAction(action: string, exec = false) {
const resolveMethod = (obj: unknown) =>
(obj as { [key: string]: () => Promise<void>; })[action]?.bind(obj);
const fn = resolveMethod(this) || resolveMethod(this.clientService) || resolveMethod(this.binaryService);
if (fn && typeof fn === 'function') {
if (exec) {
try {
await fn();
} catch (error) {
this.errorService.setError(error as string);
}
}
} else {
const error = `Method ${action} is not defined on the component or clientService.`;
console.error(error);
this.errorService.setError(error);
}
}
async onWasmSelected(wasm: Uint8Array) {
wasm && (this.wasm = wasm);
}
private async cleanResult() {
this.errorService.setError('');
await this.resultService.setResult('');
}
private async deploy(deploy_result = true, speculative?: boolean) {
return await this.clientService.deploy(deploy_result, speculative, this.wasm);
}
private async transaction(deploy_result = true, speculative?: boolean) {
return await this.clientService.transaction(deploy_result, speculative, this.wasm);
}
private async get_account(account_identifier_param: string) {
return await this.clientService.get_account(account_identifier_param);
}
private async get_entity(entity_identifier_param: string) {
return await this.clientService.get_entity(entity_identifier_param);
}
public async get_state_root_hash(no_mark_for_check?: boolean) {
const state_root_hash = await this.clientService.get_state_root_hash(no_mark_for_check);
this.stateService.setState({
state_root_hash
});
return state_root_hash;
}
private async transfer(deploy_result = true, speculative?: boolean) {
return await this.clientService.transfer(deploy_result, speculative);
}
private async transfer_transaction(transaction_result = true, speculative?: boolean) {
return await this.clientService.transfer_transaction(transaction_result, speculative);
}
private async get_binary_try_accept_transaction() {
let transaction = await this.clientService.transaction(false, false, this.wasm) as Transaction;
return await this.binaryService.get_binary_try_accept_transaction(transaction);
}
private async get_binary_try_speculative_execution() {
let transaction = await this.clientService.transaction(false, true, this.wasm) as Transaction;
return await this.binaryService.get_binary_try_speculative_execution(transaction);
}
}