Skip to content

Commit

Permalink
account merge and premade assets
Browse files Browse the repository at this point in the history
  • Loading branch information
acharb committed Dec 19, 2023
1 parent 8ab7954 commit 298d4c1
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/walletSdk/Asset/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,18 @@ export class FiatAssetId extends AssetId {
return this.sep38;
}
}

export const Assets = {
Main: {
USDC: new IssuedAssetId(
"USDC",
"GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN",
),
},
Test: {
USDC: new IssuedAssetId(
"USDC",
"GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5",
),
},
};
16 changes: 16 additions & 0 deletions src/walletSdk/Horizon/Transaction/TransactionBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,22 @@ export class TransactionBuilder extends CommonTransactionBuilder<TransactionBuil
);
}

/**
* Merges account into a destination account.
* **Warning**: This operation will give full control of the account to the destination account,
* effectively removing the merged account from the network.
* @param {string} destination - The stellar account merging into.
* @param {string} [source] - Account that is being merged. If not given then will default to
* the TransactionBuilder source account.
* @returns {TransactionBuilder} The TransactionBuilder instance.
*/
accountMerge(destination: string, source?: string): TransactionBuilder {
this.operations.push(
StellarSdk.Operation.accountMerge({ destination, source }),
);
return this;
}

/**
* Builds the Stellar transaction so can be submitted.
* @returns {Transaction} The built Stellar transaction.
Expand Down
47 changes: 47 additions & 0 deletions test/stellar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
IssuedAssetId,
FiatAssetId,
NativeAssetId,
Assets,
} from "../src/walletSdk/Asset";
import { TransactionStatus, WithdrawTransaction } from "../src/walletSdk/Types";

Expand Down Expand Up @@ -383,6 +384,17 @@ describe("Asset", () => {
const fiat = new FiatAssetId("USD");
expect(fiat.sep38).toBe("iso4217:USD");
});
it("should use premade constants", () => {
let issued = Assets.Main.USDC;
expect(issued.sep38).toBe(
"stellar:USDC:GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN",
);

issued = Assets.Test.USDC;
expect(issued.sep38).toBe(
"stellar:USDC:GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5",
);
});
});

describe("Account Modifying", () => {
Expand Down Expand Up @@ -460,4 +472,39 @@ describe("Account Modifying", () => {
resp = await stellar.server.loadAccount(sourceKp.publicKey);
expect(resp.signers[0].weight).toBe(0);
}, 45000);

it("should merge account", async () => {
const wallet = Wallet.TestNet();
const stellar = wallet.stellar();
const account = wallet.stellar().account();

const accountKp = account.createKeypair();
const sourceKp = account.createKeypair();
await stellar.fundTestnetAccount(accountKp.publicKey);
await stellar.fundTestnetAccount(sourceKp.publicKey);

const txBuilder = await stellar.transaction({
sourceAddress: accountKp,
baseFee: 1000,
});
const mergeTxn = txBuilder
.accountMerge(accountKp.publicKey, sourceKp.publicKey)
.build();
mergeTxn.sign(accountKp.keypair);
mergeTxn.sign(sourceKp.keypair);
await stellar.submitTransaction(mergeTxn);

let found;
try {
const accResp = await stellar.server.loadAccount(sourceKp.publicKey);
if (accResp) {
found = true;
}
} catch (e) {
found = false;
}
expect(found).toBeFalsy();
const accResp = await stellar.server.loadAccount(accountKp.publicKey);
expect(parseInt(accResp.balances[0].balance)).toBeGreaterThan(19998);
}, 30000);
});

0 comments on commit 298d4c1

Please sign in to comment.