This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reclaim.ts
123 lines (103 loc) · 3.53 KB
/
reclaim.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
/*
* Copyright © 2022 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*/
import {
BaseCommand,
CommandExecuteContext,
CommandVerifyContext,
VerificationResult,
VerifyStatus,
validator as liskValidator,
cryptography,
TokenMethod,
} from 'lisk-sdk';
import { ADDRESS_LEGACY_RESERVE } from '../constants';
import { reclaimLSKParamsSchema } from '../schemas';
import { ReclaimLSKParamsData, TokenIDReclaim, ModuleName } from '../types';
import { getLegacyAddress } from '../utils';
import { LegacyAccountStore } from '../stores/legacyAccount';
import { AccountReclaimedEvent } from '../events/accountReclaimed';
// eslint-disable-next-line prefer-destructuring
const validator: liskValidator.LiskValidator = liskValidator.validator;
const {
address: { getAddressFromPublicKey },
} = cryptography;
export class ReclaimLSKCommand extends BaseCommand {
public schema = reclaimLSKParamsSchema;
public legacyReserveAddress = ADDRESS_LEGACY_RESERVE;
private _tokenMethod!: TokenMethod;
private _tokenIDReclaim!: TokenIDReclaim;
private _moduleName!: ModuleName;
public addDependencies(tokenMethod: TokenMethod) {
this._tokenMethod = tokenMethod;
}
public init(args: { tokenIDReclaim: TokenIDReclaim; moduleName: ModuleName }) {
this._tokenIDReclaim = args.tokenIDReclaim;
this._moduleName = args.moduleName;
}
public async verify(ctx: CommandVerifyContext): Promise<VerificationResult> {
const params = (ctx.params as unknown) as ReclaimLSKParamsData;
try {
validator.validate(reclaimLSKParamsSchema, params);
} catch (err) {
return {
status: VerifyStatus.FAIL,
error: err as Error,
};
}
const legacyAddress = getLegacyAddress(ctx.transaction.senderPublicKey);
const legacyStore = this.stores.get(LegacyAccountStore);
const isLegacyAddressExists = await legacyStore.has(ctx, legacyAddress);
if (!isLegacyAddressExists) {
return {
status: VerifyStatus.FAIL,
error: new Error(`Public key does not correspond to a reclaimable account.`),
};
}
const legacyAccount = await legacyStore.get(ctx, legacyAddress);
if (legacyAccount.balance !== params.amount) {
return {
status: VerifyStatus.FAIL,
error: new Error('Input amount does not equal the balance of the legacy account.'),
};
}
return { status: VerifyStatus.OK };
}
public async execute(ctx: CommandExecuteContext): Promise<void> {
const params = (ctx.params as unknown) as ReclaimLSKParamsData;
const legacyAddress = getLegacyAddress(ctx.transaction.senderPublicKey);
const legacyStore = this.stores.get(LegacyAccountStore);
await legacyStore.del(ctx, legacyAddress);
const address = getAddressFromPublicKey(ctx.transaction.senderPublicKey);
await this._tokenMethod.unlock(
ctx.getMethodContext(),
this.legacyReserveAddress,
this._moduleName,
this._tokenIDReclaim,
params.amount,
);
await this._tokenMethod.transfer(
ctx.getMethodContext(),
this.legacyReserveAddress,
address,
this._tokenIDReclaim,
params.amount,
);
const accountReclaimedEvent = this.events.get(AccountReclaimedEvent);
accountReclaimedEvent.log(ctx.getMethodContext(), {
legacyAddress,
address,
amount: params.amount,
});
}
}