Skip to content

Commit 8a02601

Browse files
authored
Revert "Revert "feat(sdk-coin-apt): send many: addition of recipients (single recipient)""
1 parent 4f46881 commit 8a02601

File tree

9 files changed

+423
-57
lines changed

9 files changed

+423
-57
lines changed

modules/sdk-coin-apt/src/lib/iface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export interface TxData {
1616
id: string;
1717
sender: string;
1818
recipient: TransactionRecipient;
19+
recipients: TransactionRecipient[];
1920
sequenceNumber: number;
2021
maxGasAmount: number;
2122
gasUnitPrice: number;

modules/sdk-coin-apt/src/lib/transaction/digitalAssetTransfer.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,20 @@ export class DigitalAssetTransfer extends Transaction {
3737
throw new InvalidTransactionError('Invalid transaction payload');
3838
}
3939
const entryFunction = payload.entryFunction;
40-
if (!this._recipient) {
41-
this._recipient = {} as TransactionRecipient;
42-
}
4340
this._assetId = entryFunction.args[0].toString();
44-
this._recipient.address = entryFunction.args[1].toString();
45-
this._recipient.amount = DIGITAL_ASSET_TRANSFER_AMOUNT;
41+
this.recipients = [
42+
{
43+
address: entryFunction.args[1].toString(),
44+
amount: DIGITAL_ASSET_TRANSFER_AMOUNT,
45+
},
46+
] as TransactionRecipient[];
4647
}
4748

4849
protected async buildRawTransaction(): Promise<void> {
4950
const network: Network = this._coinConfig.network.type === NetworkType.MAINNET ? Network.MAINNET : Network.TESTNET;
5051
const aptos = new Aptos(new AptosConfig({ network }));
5152
const senderAddress = AccountAddress.fromString(this._sender);
52-
const recipientAddress = AccountAddress.fromString(this._recipient.address);
53+
const recipientAddress = AccountAddress.fromString(this.recipients[0].address);
5354
const digitalAssetAddress = AccountAddress.fromString(this._assetId);
5455

5556
const transferDigitalAssetAbi: EntryFunctionABI = {

modules/sdk-coin-apt/src/lib/transaction/fungibleAssetTransfer.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,21 @@ export class FungibleAssetTransfer extends Transaction {
3232
throw new InvalidTransactionError('Invalid transaction payload');
3333
}
3434
const entryFunction = payload.entryFunction;
35-
if (!this._recipient) {
36-
this._recipient = {} as TransactionRecipient;
37-
}
3835
this._assetId = entryFunction.args[0].toString();
39-
this._recipient.address = entryFunction.args[1].toString();
40-
this._recipient.amount = utils.getAmountFromPayloadArgs(entryFunction.args[2].bcsToBytes());
36+
this.recipients = [
37+
{
38+
address: entryFunction.args[1].toString(),
39+
amount: utils.getAmountFromPayloadArgs(entryFunction.args[2].bcsToBytes()),
40+
},
41+
] as TransactionRecipient[];
4142
}
4243

4344
protected async buildRawTransaction(): Promise<void> {
4445
const network: Network = this._coinConfig.network.type === NetworkType.MAINNET ? Network.MAINNET : Network.TESTNET;
4546
const aptos = new Aptos(new AptosConfig({ network }));
4647
const senderAddress = AccountAddress.fromString(this._sender);
47-
const recipientAddress = AccountAddress.fromString(this._recipient.address);
48+
const recipientAddress = AccountAddress.fromString(this.recipients[0].address);
4849
const fungibleTokenAddress = this._assetId;
49-
5050
const faTransferAbi: EntryFunctionABI = {
5151
typeParameters: [{ constraints: [] }],
5252
parameters: [parseTypeTag('0x1::object::Object'), new TypeTagAddress(), new TypeTagU64()],
@@ -57,7 +57,7 @@ export class FungibleAssetTransfer extends Transaction {
5757
data: {
5858
function: FUNGIBLE_ASSET_TRANSFER_FUNCTION,
5959
typeArguments: [FUNGIBLE_ASSET_TYPE_ARGUMENT],
60-
functionArguments: [fungibleTokenAddress, recipientAddress, this.recipient.amount],
60+
functionArguments: [fungibleTokenAddress, recipientAddress, this.recipients[0].amount],
6161
abi: faTransferAbi,
6262
},
6363
options: {

modules/sdk-coin-apt/src/lib/transaction/transaction.ts

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@ import { DEFAULT_GAS_UNIT_PRICE, UNAVAILABLE_TEXT } from '../constants';
3232
import utils from '../utils';
3333
import BigNumber from 'bignumber.js';
3434
import { AptTransactionExplanation, TxData } from '../iface';
35+
import assert from 'assert';
3536

3637
export abstract class Transaction extends BaseTransaction {
3738
protected _rawTransaction: RawTransaction;
3839
protected _senderSignature: Signature;
3940
protected _feePayerSignature: Signature;
4041
protected _sender: string;
41-
protected _recipient: TransactionRecipient;
42+
protected _recipients: TransactionRecipient[];
4243
protected _sequenceNumber: number;
4344
protected _maxGasAmount: number;
4445
protected _gasUnitPrice: number;
@@ -59,6 +60,7 @@ export abstract class Transaction extends BaseTransaction {
5960
this._expirationTime = utils.getTxnExpirationTimestamp();
6061
this._sequenceNumber = 0;
6162
this._sender = AccountAddress.ZERO.toString();
63+
this._recipients = [];
6264
this._assetId = AccountAddress.ZERO.toString();
6365
this._isSimulateTxn = false;
6466
this._senderSignature = {
@@ -90,12 +92,27 @@ export abstract class Transaction extends BaseTransaction {
9092
this._sender = value;
9193
}
9294

95+
/**
96+
* @deprecated - use `recipients()`.
97+
*/
9398
get recipient(): TransactionRecipient {
94-
return this._recipient;
99+
assert(this._recipients.length > 0, 'No recipients available');
100+
return this._recipients[0];
95101
}
96102

103+
/**
104+
* @deprecated - use `recipients()`.
105+
*/
97106
set recipient(value: TransactionRecipient) {
98-
this._recipient = value;
107+
this.recipients = [value];
108+
}
109+
110+
get recipients(): TransactionRecipient[] {
111+
return this._recipients;
112+
}
113+
114+
set recipients(value: TransactionRecipient[]) {
115+
this._recipients = value;
99116
}
100117

101118
get sequenceNumber(): number {
@@ -258,20 +275,24 @@ export abstract class Transaction extends BaseTransaction {
258275
}
259276

260277
loadInputsAndOutputs(): void {
278+
const totalAmount = this._recipients.reduce(
279+
(accumulator, current) => accumulator.plus(current.amount),
280+
new BigNumber('0')
281+
);
261282
this._inputs = [
262283
{
263284
address: this.sender,
264-
value: this.recipient.amount as string,
285+
value: totalAmount.toString(),
265286
coin: this._coinConfig.name,
266287
},
267288
];
268-
this._outputs = [
269-
{
270-
address: this.recipient.address,
271-
value: this.recipient.amount as string,
289+
this._outputs = this._recipients.map((recipient) => {
290+
return {
291+
address: recipient.address,
292+
value: recipient.amount as string,
272293
coin: this._coinConfig.name,
273-
},
274-
];
294+
};
295+
});
275296
}
276297

277298
fromRawTransaction(rawTransaction: string): void {
@@ -303,6 +324,7 @@ export abstract class Transaction extends BaseTransaction {
303324
id: this.id,
304325
sender: this.sender,
305326
recipient: this.recipient,
327+
recipients: this.recipients,
306328
sequenceNumber: this.sequenceNumber,
307329
maxGasAmount: this.maxGasAmount,
308330
gasUnitPrice: this.gasUnitPrice,
@@ -335,8 +357,10 @@ export abstract class Transaction extends BaseTransaction {
335357
'type',
336358
];
337359

338-
const outputs: TransactionRecipient[] = [this.recipient];
339-
const outputAmount = outputs[0].amount;
360+
const outputs: TransactionRecipient[] = this._recipients;
361+
const outputAmount = outputs
362+
.reduce((accumulator, current) => accumulator.plus(current.amount), new BigNumber('0'))
363+
.toString();
340364
return {
341365
displayOrder,
342366
id: this.id,

modules/sdk-coin-apt/src/lib/transaction/transferTransaction.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,26 @@ export class TransferTransaction extends Transaction {
3030
throw new InvalidTransactionError('Invalid transaction payload');
3131
}
3232
const entryFunction = payload.entryFunction;
33-
if (!this._recipient) {
34-
this._recipient = {} as TransactionRecipient;
35-
}
3633
this._assetId = entryFunction.type_args[0].toString();
37-
this._recipient.address = entryFunction.args[0].toString();
38-
this._recipient.amount = utils.getAmountFromPayloadArgs(entryFunction.args[1].bcsToBytes());
34+
this.recipients = [
35+
{
36+
address: entryFunction.args[0].toString(),
37+
amount: utils.getAmountFromPayloadArgs(entryFunction.args[1].bcsToBytes()),
38+
},
39+
] as TransactionRecipient[];
3940
}
4041

4142
protected async buildRawTransaction(): Promise<void> {
4243
const network: Network = this._coinConfig.network.type === NetworkType.MAINNET ? Network.MAINNET : Network.TESTNET;
4344
const aptos = new Aptos(new AptosConfig({ network }));
4445
const senderAddress = AccountAddress.fromString(this._sender);
45-
const recipientAddress = AccountAddress.fromString(this._recipient.address);
46+
const recipientAddress = AccountAddress.fromString(this.recipients[0].address);
4647
const simpleTxn = await aptos.transaction.build.simple({
4748
sender: senderAddress,
4849
data: {
4950
function: COIN_TRANSFER_FUNCTION,
5051
typeArguments: [this.assetId],
51-
functionArguments: [recipientAddress, this.recipient.amount],
52+
functionArguments: [recipientAddress, this.recipients[0].amount],
5253
},
5354
options: {
5455
maxGasAmount: this.maxGasAmount,

modules/sdk-coin-apt/src/lib/transactionBuilder/transactionBuilder.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,22 @@ export abstract class TransactionBuilder extends BaseTransactionBuilder {
6363
return this;
6464
}
6565

66+
/**
67+
* @deprecated - use `recipients()`.
68+
*/
6669
recipient(recipient: Recipient): this {
6770
this.validateAddress({ address: recipient.address });
6871
this.validateValue(new BigNumber(recipient.amount));
69-
this.transaction.recipient = recipient;
72+
this.transaction.recipients = [recipient];
73+
return this;
74+
}
75+
76+
recipients(recipients: Recipient[]): this {
77+
for (const recipient of recipients) {
78+
this.validateAddress({ address: recipient.address });
79+
this.validateValue(new BigNumber(recipient.amount));
80+
}
81+
this.transaction.recipients = recipients;
7082
return this;
7183
}
7284

@@ -143,8 +155,10 @@ export abstract class TransactionBuilder extends BaseTransactionBuilder {
143155
throw new Error('transaction not defined');
144156
}
145157
this.validateAddress({ address: transaction.sender });
146-
this.validateAddress({ address: transaction.recipient.address });
147-
this.validateValue(new BigNumber(transaction.recipient.amount));
158+
for (const recipient of transaction.recipients) {
159+
this.validateAddress({ address: recipient.address });
160+
this.validateValue(new BigNumber(recipient.amount));
161+
}
148162
}
149163

150164
isValidRawTransaction(rawTransaction: string): boolean {

0 commit comments

Comments
 (0)