-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathclaimRewards.ts
More file actions
175 lines (146 loc) · 5.35 KB
/
claimRewards.ts
File metadata and controls
175 lines (146 loc) · 5.35 KB
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
import EthereumAbi from 'ethereumjs-abi';
import { addHexPrefix } from 'ethereumjs-util';
import { TransactionType, InvalidTransactionError, TransactionRecipient } from '@bitgo/sdk-core';
import { BaseCoin as CoinConfig } from '@bitgo/statics';
import { Transaction as VetTransaction, Secp256k1, TransactionClause } from '@vechain/sdk-core';
import { Transaction } from './transaction';
import { VetTransactionData } from '../iface';
import { CLAIM_STAKING_REWARDS_METHOD_ID } from '../constants';
import utils from '../utils';
export class ClaimRewardsTransaction extends Transaction {
private _stakingContractAddress: string;
private _tokenId: string;
constructor(_coinConfig: Readonly<CoinConfig>) {
super(_coinConfig);
this._type = TransactionType.StakingClaim;
}
get stakingContractAddress(): string {
return this._stakingContractAddress;
}
set stakingContractAddress(address: string) {
this._stakingContractAddress = address;
}
get tokenId(): string {
return this._tokenId;
}
set tokenId(tokenId: string) {
this._tokenId = tokenId;
}
/** @inheritdoc */
async build(): Promise<void> {
this.buildClauses();
await this.buildRawTransaction();
this.generateTxnIdAndSetSender();
this.loadInputsAndOutputs();
}
get clauses(): TransactionClause[] {
return this._clauses;
}
set clauses(clauses: TransactionClause[]) {
this._clauses = clauses;
}
get recipients(): TransactionRecipient[] {
return this._recipients;
}
set recipients(recipients: TransactionRecipient[]) {
this._recipients = recipients;
}
/** @inheritdoc */
buildClauses(): void {
if (!this.stakingContractAddress) {
throw new Error('Staking contract address is not set');
}
utils.validateStakingContractAddress(this.stakingContractAddress, this._coinConfig);
if (this.tokenId === undefined || this.tokenId === null) {
throw new Error('Token ID is not set');
}
const data = this.encodeClaimRewardsMethod(this.tokenId);
this._transactionData = data;
// Create the clause for claim rewards
this._clauses = [
{
to: this.stakingContractAddress,
value: '0x0',
data: this._transactionData,
},
];
// Set recipients as empty since claim rewards doesn't send value
this.recipients = [];
}
/**
* Encode the claim rewards method call data
*/
private encodeClaimRewardsMethod(tokenId: string): string {
const methodName = 'claimRewards';
const types = ['uint256'];
const params = [tokenId];
const method = EthereumAbi.methodID(methodName, types);
const args = EthereumAbi.rawEncode(types, params);
return addHexPrefix(Buffer.concat([method, args]).toString('hex'));
}
/** @inheritdoc */
toJson(): VetTransactionData {
const json: VetTransactionData = {
id: this.id,
chainTag: this.chainTag,
blockRef: this.blockRef,
expiration: this.expiration,
gasPriceCoef: this.gasPriceCoef,
gas: this.gas,
dependsOn: this.dependsOn,
nonce: this.nonce,
sender: this.sender,
feePayer: this.feePayerAddress,
recipients: this.recipients,
tokenId: this.tokenId,
stakingContractAddress: this.stakingContractAddress,
};
return json;
}
/** @inheritdoc */
fromDeserializedSignedTransaction(signedTx: VetTransaction): void {
try {
if (!signedTx || !signedTx.body) {
throw new InvalidTransactionError('Invalid transaction: missing transaction body');
}
// Store the raw transaction
this.rawTransaction = signedTx;
// Set transaction body properties
const body = signedTx.body;
this.chainTag = typeof body.chainTag === 'number' ? body.chainTag : 0;
this.blockRef = body.blockRef || '0x0';
this.expiration = typeof body.expiration === 'number' ? body.expiration : 64;
this.clauses = body.clauses || [];
this.gasPriceCoef = typeof body.gasPriceCoef === 'number' ? body.gasPriceCoef : 128;
this.gas = typeof body.gas === 'number' ? body.gas : Number(body.gas) || 0;
this.dependsOn = body.dependsOn || null;
this.nonce = String(body.nonce);
if (body.clauses.length === 1) {
const clause = body.clauses[0];
if (clause.data && clause.data.startsWith(CLAIM_STAKING_REWARDS_METHOD_ID)) {
// claimRewards should go to STARGATE_DELEGATION_ADDRESS
this.tokenId = utils.decodeClaimRewardsData(clause.data);
this.stakingContractAddress = clause.to || '0x0';
}
}
// Set recipients as empty for claim rewards
this.recipients = [];
this.loadInputsAndOutputs();
// Set sender address
if (signedTx.signature && signedTx.origin) {
this.sender = signedTx.origin.toString().toLowerCase();
}
// Set signatures if present
if (signedTx.signature) {
// First signature is sender's signature
this.senderSignature = Buffer.from(signedTx.signature.slice(0, Secp256k1.SIGNATURE_LENGTH));
// If there's additional signature data, it's the fee payer's signature
if (signedTx.signature.length > Secp256k1.SIGNATURE_LENGTH) {
this.feePayerSignature = Buffer.from(signedTx.signature.slice(Secp256k1.SIGNATURE_LENGTH));
}
}
} catch (e) {
throw new InvalidTransactionError(`Failed to deserialize transaction: ${e.message}`);
}
}
}