Skip to content

Commit 2fd3756

Browse files
authored
fix: fix the issue where sending assets using TransactionBuilder.buildPaymentToContractTransaction fails when the sender's account is the same as the asset issuer's account. (#685)
1 parent 493816c commit 2fd3756

File tree

3 files changed

+61
-12
lines changed

3 files changed

+61
-12
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## Pending
44

5+
### Update:
6+
fix: fix the issue where sending assets using `TransactionBuilder.buildPaymentToContractTransaction` fails when the sender's account is the same as the asset issuer's account. ([#685](https://github.com/stellar/java-stellar-sdk/pull/685))
7+
58
## 1.3.0
69

710
### Update:

src/main/java/org/stellar/sdk/TransactionBuilder.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -400,26 +400,27 @@ public Transaction buildPaymentToContractTransaction(
400400
.build());
401401
readWrite.add(balanceLedgerKey);
402402
} else {
403+
String assetIssuer = ((AssetTypeCreditAlphaNum) asset).getIssuer();
403404
readOnly.add(
404405
LedgerKey.builder()
405406
.discriminant(LedgerEntryType.ACCOUNT)
406407
.account(
407408
LedgerKey.LedgerKeyAccount.builder()
408-
.accountID(
409-
KeyPair.fromAccountId(((AssetTypeCreditAlphaNum) asset).getIssuer())
410-
.getXdrAccountId())
409+
.accountID(KeyPair.fromAccountId(assetIssuer).getXdrAccountId())
411410
.build())
412411
.build());
413412
readOnly.add(contractInstanceLedgerKey);
414-
readWrite.add(
415-
LedgerKey.builder()
416-
.discriminant(LedgerEntryType.TRUSTLINE)
417-
.trustLine(
418-
LedgerKey.LedgerKeyTrustLine.builder()
419-
.accountID(KeyPair.fromAccountId(fromAddress).getXdrAccountId())
420-
.asset(new TrustLineAsset(asset).toXdr())
421-
.build())
422-
.build());
413+
if (!assetIssuer.equals(fromAddress)) {
414+
readWrite.add(
415+
LedgerKey.builder()
416+
.discriminant(LedgerEntryType.TRUSTLINE)
417+
.trustLine(
418+
LedgerKey.LedgerKeyTrustLine.builder()
419+
.accountID(KeyPair.fromAccountId(fromAddress).getXdrAccountId())
420+
.asset(new TrustLineAsset(asset).toXdr())
421+
.build())
422+
.build());
423+
}
423424
readWrite.add(balanceLedgerKey);
424425
}
425426

src/test/java/org/stellar/sdk/TransactionTest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,51 @@ public void testIntegrationPaymentToContractTransactionWithAlphanum12Asset() thr
645645
server.close();
646646
}
647647

648+
@Test
649+
public void testIntegrationPaymentToContractTransactionWithAssetIssuerAsSender()
650+
throws IOException {
651+
BigDecimal amount1 = new BigDecimal("100.125");
652+
BigDecimal amount2 = new BigDecimal("120.7891");
653+
654+
KeyPair kp = KeyPair.random();
655+
IntegrationUtils.fundAccount(kp.getAccountId());
656+
Asset asset = Asset.createNonNativeAsset("CAT", kp.getAccountId());
657+
IntegrationUtils.createAssetContract(asset, kp);
658+
String destination = IntegrationUtils.getRandomContractId(kp);
659+
Server server = new Server(IntegrationUtils.HORIZON_URL);
660+
TransactionBuilderAccount sourceAccount = server.loadAccount(kp.getAccountId());
661+
662+
Transaction transaction1 =
663+
new TransactionBuilder(sourceAccount, IntegrationUtils.NETWORK)
664+
.setBaseFee(100)
665+
.setTimeout(300)
666+
.buildPaymentToContractTransaction(destination, asset, amount1, null);
667+
transaction1.sign(kp);
668+
server.submitTransaction(transaction1);
669+
BigInteger balance1 = IntegrationUtils.getBalanceForContract(destination, asset, kp);
670+
assertEquals(1001250000L, balance1.longValue());
671+
672+
Transaction transaction2 =
673+
new TransactionBuilder(sourceAccount, IntegrationUtils.NETWORK)
674+
.setBaseFee(100)
675+
.setTimeout(300)
676+
.buildPaymentToContractTransaction(destination, asset, amount2, null);
677+
transaction2.sign(kp);
678+
server.submitTransaction(transaction2);
679+
BigInteger balance2 = IntegrationUtils.getBalanceForContract(destination, asset, kp);
680+
assertEquals(2209141000L, balance2.longValue());
681+
682+
Transaction transaction3 =
683+
new TransactionBuilder(sourceAccount, IntegrationUtils.NETWORK)
684+
.setBaseFee(100)
685+
.setTimeout(300)
686+
.buildRestoreAssetBalanceEntryTransaction(destination, asset, null);
687+
transaction3.sign(kp);
688+
server.submitTransaction(transaction3);
689+
690+
server.close();
691+
}
692+
648693
@Test
649694
public void testIntegrationPaymentToContractTransactionWithDifferentSource() throws IOException {
650695
Asset asset = Asset.createNativeAsset();

0 commit comments

Comments
 (0)