-
Notifications
You must be signed in to change notification settings - Fork 286
/
delegate-resignation.ts
77 lines (62 loc) · 2.95 KB
/
delegate-resignation.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
import { ApplicationEvents } from "@arkecosystem/core-event-emitter";
import { Database, EventEmitter, State, TransactionPool } from "@arkecosystem/core-interfaces";
import { Interfaces, Transactions } from "@arkecosystem/crypto";
import { WalletAlreadyResignedError, WalletUsernameEmptyError } from "../errors";
import { TransactionHandler } from "./transaction";
export class DelegateResignationTransactionHandler extends TransactionHandler {
public getConstructor(): Transactions.TransactionConstructor {
return Transactions.DelegateResignationTransaction;
}
public async bootstrap(connection: Database.IConnection, walletManager: State.IWalletManager): Promise<void> {
const transactions = await connection.transactionsRepository.getAssetsByType(this.getConstructor().type);
for (const transaction of transactions) {
walletManager.findByPublicKey(transaction.senderPublicKey).resigned = true;
}
}
public canBeApplied(
transaction: Interfaces.ITransaction,
wallet: State.IWallet,
databaseWalletManager: State.IWalletManager,
): boolean {
if (!wallet.username) {
throw new WalletUsernameEmptyError();
}
if (wallet.resigned) {
throw new WalletAlreadyResignedError();
}
return super.canBeApplied(transaction, wallet, databaseWalletManager);
}
public emitEvents(transaction: Interfaces.ITransaction, emitter: EventEmitter.EventEmitter): void {
emitter.emit(ApplicationEvents.DelegateResigned, transaction.data);
}
public canEnterTransactionPool(
data: Interfaces.ITransactionData,
pool: TransactionPool.IConnection,
processor: TransactionPool.IProcessor,
): boolean {
if (this.typeFromSenderAlreadyInPool(data, pool, processor)) {
const wallet: State.IWallet = pool.walletManager.findByPublicKey(data.senderPublicKey);
processor.pushError(
data,
"ERR_PENDING",
`Delegate resignation for "${wallet.username}" already in the pool`,
);
return false;
}
return true;
}
protected applyToSender(transaction: Interfaces.ITransaction, walletManager: State.IWalletManager): void {
super.applyToSender(transaction, walletManager);
walletManager.findByPublicKey(transaction.data.senderPublicKey).resigned = true;
}
protected revertForSender(transaction: Interfaces.ITransaction, walletManager: State.IWalletManager): void {
super.revertForSender(transaction, walletManager);
walletManager.findByPublicKey(transaction.data.senderPublicKey).resigned = false;
}
protected applyToRecipient(transaction: Interfaces.ITransaction, walletManager: State.IWalletManager): void {
return;
}
protected revertForRecipient(transaction: Interfaces.ITransaction, walletManager: State.IWalletManager): void {
return;
}
}