Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 378x 378x 378x 301x 301x 77x 120x 120x 120x 120x 66x 1x 19x 19x 19x 19x 2x 19x 1x 19x 8x 8x 8x 8x 1x 8x 8x 8x 8x 8x 8x 1x 8x 8x 8x 5x 5x 5x 5x 1x 5x 5x 5x 5x 5x 1x 5x 5x 5x 5x | /*
* Copyright © 2019 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 { intToBuffer, stringToBuffer } from '@liskhq/lisk-cryptography';
import {
isPositiveNumberString,
isValidTransferAmount,
validator,
} from '@liskhq/lisk-validator';
import {
BaseTransaction,
StateStore,
StateStorePrepare,
} from './base_transaction';
import { BYTESIZES, MAX_TRANSACTION_AMOUNT, TRANSFER_FEE } from './constants';
import { convertToAssetError, TransactionError } from './errors';
import { TransactionJSON } from './transaction_types';
import { verifyAmountBalance, verifyBalance } from './utils';
export interface TransferAsset {
readonly data?: string;
readonly recipientId: string;
readonly amount: bigint;
}
export const transferAssetFormatSchema = {
type: 'object',
required: ['recipientId', 'amount'],
properties: {
recipientId: {
type: 'string',
format: 'address',
},
amount: {
type: 'string',
format: 'amount',
},
data: {
type: 'string',
format: 'transferData',
maxLength: 64,
},
},
};
interface RawAsset {
readonly data?: string;
readonly recipientId: string;
readonly amount: number | string;
}
export class TransferTransaction extends BaseTransaction {
public readonly asset: TransferAsset;
public static TYPE = 8;
public static FEE = TRANSFER_FEE.toString();
public constructor(rawTransaction: unknown) {
super(rawTransaction);
const tx = (typeof rawTransaction === 'object' && rawTransaction !== null
? rawTransaction
: {}) as Partial<TransactionJSON>;
// Initializes to empty object if it doesn't exist
if (tx.asset) {
const rawAsset = tx.asset as RawAsset;
this.asset = {
data: rawAsset.data,
recipientId: rawAsset.recipientId,
amount: BigInt(
isPositiveNumberString(rawAsset.amount) ? rawAsset.amount : '0',
),
};
} else {
// tslint:disable-next-line no-object-literal-type-assertion
this.asset = {
amount: BigInt('0'),
recipientId: '',
} as TransferAsset;
}
}
protected assetToBytes(): Buffer {
const transactionAmount = intToBuffer(
this.asset.amount.toString(),
BYTESIZES.AMOUNT,
'big',
);
const transactionRecipientID = this.asset.recipientId
? intToBuffer(
this.asset.recipientId.slice(0, -1),
BYTESIZES.RECIPIENT_ID,
).slice(0, BYTESIZES.RECIPIENT_ID)
: Buffer.alloc(0);
const dataBuffer = this.asset.data
? stringToBuffer(this.asset.data)
: Buffer.alloc(0);
return Buffer.concat([
transactionAmount,
transactionRecipientID,
dataBuffer,
]);
}
public assetToJSON(): object {
return {
data: this.asset.data,
amount: this.asset.amount.toString(),
recipientId: this.asset.recipientId,
};
}
public async prepare(store: StateStorePrepare): Promise<void> {
await store.account.cache([
{
address: this.senderId,
},
{
address: this.asset.recipientId,
},
]);
}
protected validateAsset(): ReadonlyArray<TransactionError> {
const asset = this.assetToJSON();
const schemaErrors = validator.validate(transferAssetFormatSchema, asset);
const errors = convertToAssetError(
this.id,
schemaErrors,
) as TransactionError[];
if (!isValidTransferAmount(this.asset.amount.toString())) {
errors.push(
new TransactionError(
'Amount must be a valid number in string format.',
this.id,
'.asset.amount',
this.asset.amount.toString(),
),
);
}
if (!this.asset.recipientId) {
errors.push(
new TransactionError(
'`recipientId` must be provided.',
this.id,
'.asset.recipientId',
),
);
}
return errors;
}
protected async applyAsset(
store: StateStore,
): Promise<ReadonlyArray<TransactionError>> {
const errors: TransactionError[] = [];
const sender = await store.account.get(this.senderId);
const balanceError = verifyAmountBalance(
this.id,
sender,
this.asset.amount,
this.fee,
);
if (balanceError) {
errors.push(balanceError);
}
const updatedSenderBalance =
BigInt(sender.balance) - BigInt(this.asset.amount);
const updatedSender = {
...sender,
balance: updatedSenderBalance.toString(),
};
store.account.set(updatedSender.address, updatedSender);
const recipient = await store.account.getOrDefault(this.asset.recipientId);
const updatedRecipientBalance =
BigInt(recipient.balance) + BigInt(this.asset.amount);
if (updatedRecipientBalance > BigInt(MAX_TRANSACTION_AMOUNT)) {
errors.push(
new TransactionError(
'Invalid amount',
this.id,
'.amount',
this.asset.amount.toString(),
),
);
}
const updatedRecipient = {
...recipient,
balance: updatedRecipientBalance.toString(),
};
store.account.set(updatedRecipient.address, updatedRecipient);
return errors;
}
protected async undoAsset(
store: StateStore,
): Promise<ReadonlyArray<TransactionError>> {
const errors: TransactionError[] = [];
const sender = await store.account.get(this.senderId);
const updatedSenderBalance =
BigInt(sender.balance) + BigInt(this.asset.amount);
if (updatedSenderBalance > BigInt(MAX_TRANSACTION_AMOUNT)) {
errors.push(
new TransactionError(
'Invalid amount',
this.id,
'.amount',
this.asset.amount.toString(),
),
);
}
const updatedSender = {
...sender,
balance: updatedSenderBalance.toString(),
};
store.account.set(updatedSender.address, updatedSender);
const recipient = await store.account.getOrDefault(this.asset.recipientId);
const balanceError = verifyBalance(this.id, recipient, this.asset.amount);
if (balanceError) {
errors.push(balanceError);
}
const updatedRecipientBalance =
BigInt(recipient.balance) - BigInt(this.asset.amount);
const updatedRecipient = {
...recipient,
balance: updatedRecipientBalance.toString(),
};
store.account.set(updatedRecipient.address, updatedRecipient);
return errors;
}
}
|