-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathdelegateClauseTransaction.ts
More file actions
181 lines (154 loc) · 5.67 KB
/
delegateClauseTransaction.ts
File metadata and controls
181 lines (154 loc) · 5.67 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
176
177
178
179
180
181
import { TransactionType, InvalidTransactionError } from '@bitgo/sdk-core';
import { BaseCoin as CoinConfig } from '@bitgo/statics';
import { Transaction as VetTransaction, Secp256k1 } from '@vechain/sdk-core';
import { Transaction } from './transaction';
import { VetTransactionData } from '../iface';
import EthereumAbi from 'ethereumjs-abi';
import utils from '../utils';
import BigNumber from 'bignumber.js';
import { addHexPrefix, BN } from 'ethereumjs-util';
import { ZERO_VALUE_AMOUNT } from '../constants';
export class DelegateClauseTransaction extends Transaction {
private _stakingContractAddress: string;
private _tokenId: string;
private _validator: string;
constructor(_coinConfig: Readonly<CoinConfig>) {
super(_coinConfig);
this._type = TransactionType.StakingDelegate;
}
get validator(): string {
return this._validator;
}
set validator(address: string) {
this._validator = address;
}
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;
}
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');
}
if (this.validator === undefined || this.validator === null) {
throw new Error('Validator address is not set');
}
const data = this.getDelegateData(this.tokenId, this.validator);
this._transactionData = data;
// Create the clause for delegation
this._clauses = [
{
to: this.stakingContractAddress,
value: ZERO_VALUE_AMOUNT,
data: this._transactionData,
},
];
// Set recipients based on the clauses
this._recipients = [
{
address: this.stakingContractAddress,
amount: ZERO_VALUE_AMOUNT,
},
];
}
/**
* Encodes delegation transaction data using ethereumjs-abi for delegate method
*
* @param {number} tokenId - The Token ID for delegation
* @returns {string} - The encoded transaction data
*/
getDelegateData(tokenId: string, validatorAddress: string): string {
const methodName = 'delegate';
const types = ['uint256', 'address'];
const params = [new BN(tokenId), validatorAddress];
const method = EthereumAbi.methodID(methodName, types);
const args = EthereumAbi.rawEncode(types, params);
return addHexPrefix(Buffer.concat([method, args]).toString('hex'));
}
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,
data: this.transactionData,
value: ZERO_VALUE_AMOUNT,
sender: this.sender,
to: this.stakingContractAddress,
stakingContractAddress: this.stakingContractAddress,
amountToStake: ZERO_VALUE_AMOUNT,
tokenId: this.tokenId,
validatorAddress: this.validator,
};
return json;
}
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);
// Set delegation-specific properties
if (body.clauses.length > 0) {
const clause = body.clauses[0];
if (clause.to) {
this.stakingContractAddress = clause.to;
}
if (clause.data) {
this.transactionData = clause.data;
const decoded = utils.decodeDelegateClauseData(clause.data);
this.tokenId = decoded.tokenId;
this.validator = decoded.validator;
}
}
// Set recipients from clauses
this.recipients = body.clauses.map((clause) => ({
address: (clause.to || '0x0').toString().toLowerCase(),
amount: new BigNumber(clause.value || 0).toString(),
}));
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}`);
}
}
}