Skip to content

Commit fcf0884

Browse files
committed
fix(sdk-coin-hbar): support self-transfer for claim rewards
HBAR claim rewards uses a 1-tinybar self-transfer (sender == recipient). getTransferData() filtered out the positive-amount entry when its accountID matched the sender, leaving recipients[] empty. toJson() then crashed reading recipients[0].address. Fix: after the forEach loop, if transferData is still empty and transfers exist, include the positive-amount entry as the sole recipient. Normal transfers are unaffected. Also adds unit tests covering: build with source == recipient, two separate accountAmounts protobuf entries, serialisation round-trip, and signing. Ticket: SI-539
1 parent abb73a8 commit fcf0884

2 files changed

Lines changed: 123 additions & 0 deletions

File tree

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,21 @@ export class Transaction extends BaseTransaction {
153153
}
154154
});
155155

156+
// Handle self-transfer: when sender == recipient, the positive entry is filtered out above
157+
// because its accountID matches the sender. Fall back to including it as the recipient.
158+
if (transferData.length === 0 && transfers.length > 0) {
159+
const selfTransferEntry = transfers.find((t) => Long.fromValue(t.amount!).isPositive());
160+
if (selfTransferEntry) {
161+
transferData.push({
162+
address: stringifyAccountId(selfTransferEntry.accountID!),
163+
amount: Long.fromValue(selfTransferEntry.amount!).toString(),
164+
...(tokenTransfers.length && {
165+
tokenName: tokenName,
166+
}),
167+
});
168+
}
169+
}
170+
156171
return {
157172
...(tokenTransfers.length && {
158173
tokenName: tokenName,
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import * as should from 'should';
2+
import { getBuilderFactory } from '../getBuilderFactory';
3+
import * as testData from '../../resources/hbar';
4+
5+
/**
6+
* Tests for CoinTransferBuilder self-transfer (stakeClaimRewards).
7+
*
8+
* HBAR staking rewards are claimed by submitting a 1-tinybar CryptoTransfer
9+
* where sender == receiver == accountId. Hedera atomically flushes
10+
* pending_reward into account balance on any CryptoTransfer touching the
11+
* account. staking-service uses this as the CLAIM_REWARDS operation.
12+
*
13+
* The key behaviour under test:
14+
* buildTransferData() must produce TWO separate accountAmounts entries:
15+
* [{accountId, -1}, {accountId, +1}]
16+
* The Hedera SDK TransferTransaction merges same-account entries (nets to 0),
17+
* so CoinTransferBuilder.buildTransferData() bypasses that by building the
18+
* proto list directly.
19+
*
20+
* initTransfers() must reconstruct the self-transfer from serialised bytes:
21+
* it filters for positive amounts only, so recipients[0].address == source.
22+
*/
23+
describe('HBAR CoinTransferBuilder - self-transfer (stakeClaimRewards)', () => {
24+
const factory = getBuilderFactory('thbar');
25+
26+
const SOURCE = testData.ACCOUNT_1.accountId; // '0.0.81320'
27+
28+
const initSelfTransferBuilder = () => {
29+
const txBuilder = factory.getTransferBuilder();
30+
txBuilder.fee({ fee: testData.FEE });
31+
txBuilder.source({ address: SOURCE });
32+
txBuilder.send({ address: SOURCE, amount: '1' }); // self-transfer: 1 tinybar
33+
txBuilder.node({ nodeId: '0.0.3' });
34+
txBuilder.startTime('1596110493.372646570');
35+
return txBuilder;
36+
};
37+
38+
describe('build', () => {
39+
it('should build a self-transfer transaction with source equal to recipient', async () => {
40+
const tx = await initSelfTransferBuilder().build();
41+
const txJson = tx.toJson();
42+
43+
// Source and recipient are the same account
44+
should.deepEqual(txJson.from, SOURCE);
45+
should.deepEqual(txJson.to, SOURCE);
46+
should.deepEqual(txJson.amount, '1');
47+
48+
// inputs and outputs both reference the same address
49+
tx.inputs.length.should.equal(1);
50+
tx.inputs[0].address.should.equal(SOURCE);
51+
tx.inputs[0].value.should.equal('1');
52+
53+
tx.outputs.length.should.equal(1);
54+
tx.outputs[0].address.should.equal(SOURCE);
55+
tx.outputs[0].value.should.equal('1');
56+
});
57+
58+
it('should produce two separate accountAmounts entries in the protobuf', async () => {
59+
const tx = await initSelfTransferBuilder().build();
60+
61+
// Access the raw protobuf transfer list
62+
const transfers = (tx as any).txBody.cryptoTransfer.transfers.accountAmounts as any[];
63+
should.exist(transfers);
64+
transfers.length.should.equal(2, 'expected exactly two entries: [{source,-1},{source,+1}]');
65+
66+
// Both entries reference the same account
67+
const accountNums = transfers.map((a: any) => {
68+
const id = a.accountID;
69+
return `${id.shardNum || 0}.${id.realmNum || 0}.${id.accountNum}`;
70+
});
71+
accountNums.every((id: string) => id === SOURCE || id.endsWith('.81320')).should.be.true();
72+
73+
// One entry is -1 (debit), one is +1 (credit)
74+
const amounts = transfers.map((a: any) => Number(a.amount.toString()));
75+
amounts.should.containEql(-1);
76+
amounts.should.containEql(1);
77+
});
78+
79+
it('should round-trip through serialisation: deserialized tx has source == recipient', async () => {
80+
const originalTx = await initSelfTransferBuilder().build();
81+
const txHex = originalTx.toBroadcastFormat();
82+
should.exist(txHex);
83+
txHex.length.should.be.greaterThan(0);
84+
85+
// Rebuild from serialised hex
86+
const rebuiltBuilder = factory.getTransferBuilder();
87+
rebuiltBuilder.from(txHex);
88+
const rebuiltTx = await rebuiltBuilder.build();
89+
const rebuiltJson = rebuiltTx.toJson();
90+
91+
// After round-trip, source and recipient must still be the same account
92+
should.deepEqual(rebuiltJson.from, SOURCE);
93+
should.deepEqual(rebuiltJson.to, SOURCE);
94+
should.deepEqual(rebuiltJson.amount, '1');
95+
});
96+
97+
it('should sign a self-transfer transaction successfully', async () => {
98+
const builder = initSelfTransferBuilder();
99+
builder.sign({ key: testData.ACCOUNT_1.prvKeyWithPrefix });
100+
const tx = await builder.build();
101+
102+
tx.signature.length.should.equal(1);
103+
const txJson = tx.toJson();
104+
should.deepEqual(txJson.from, SOURCE);
105+
should.deepEqual(txJson.to, SOURCE);
106+
});
107+
});
108+
});

0 commit comments

Comments
 (0)