Skip to content
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.

Commit

Permalink
Account wiper cleanup (#154)
Browse files Browse the repository at this point in the history
- Split Merge in the playground into a new file
- Check that the destination is funded
  • Loading branch information
Morley Zhi authored May 5, 2020
1 parent 35cb12b commit 50bbd4d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 24 deletions.
27 changes: 4 additions & 23 deletions playground/src/components/AccountDetails.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import React, { Component } from "react";
import Json from "react-json-view";
import { Keypair } from "stellar-sdk";

import TransactionViewer from "components/TransactionViewer";
import MergeTransaction from "components/MergeTransaction";

class AccountDetails extends Component {
state = {
Expand All @@ -11,7 +10,6 @@ class AccountDetails extends Component {
updateTimes: [],
streamEnder: null,
isAccountFunded: null,
mergeTransaction: null,
};

componentDidMount() {
Expand Down Expand Up @@ -75,31 +73,14 @@ class AccountDetails extends Component {
};

render() {
const {
accountDetails,
err,
updateTimes,
isAccountFunded,
mergeTransaction,
} = this.state;

const handleGetTransaction = async () => {
const trans = await this.props.dataProvider.getStripAndMergeAccountTransaction(
Keypair.random().publicKey(),
);
console.log("~!!!!!!!!!!! rans: ", trans);
debugger;
this.setState({ mergeTransaction: trans });
};
const { accountDetails, err, updateTimes, isAccountFunded } = this.state;

return (
<div>
<h2>Account Details</h2>

<button onClick={handleGetTransaction}>Run merge command</button>

{mergeTransaction && (
<TransactionViewer transaction={mergeTransaction} />
{isAccountFunded && (
<MergeTransaction dataProvider={this.props.dataProvider} />
)}

{!isAccountFunded && <p>Account isn't funded yet.</p>}
Expand Down
52 changes: 52 additions & 0 deletions playground/src/components/MergeTransaction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React, { Component } from "react";
import { StrKey } from "stellar-sdk";

import TransactionViewer from "components/TransactionViewer";

const STORAGE_KEY = "merge-transaction-destination";

class MergeTransaction extends Component {
state = {
destination: localStorage.getItem(STORAGE_KEY),
tx: null,
};

render() {
const { destination, tx } = this.state;

const handleDestination = (ev) => {
const dest = ev.target.value;
if (StrKey.isValidEd25519PublicKey(dest)) {
localStorage.setItem(STORAGE_KEY, dest);
}
this.setState({ destination: dest });
};

const handleGetTransaction = async () => {
try {
const trans = await this.props.dataProvider.getStripAndMergeAccountTransaction(
destination,
);
this.setState({ tx: trans });
} catch (e) {
debugger;
}
};

return (
<div>
<h3>Merge into another account</h3>

<input type="text" value={destination} onChange={handleDestination} />

{destination && (
<button onClick={handleGetTransaction}>Run merge command</button>
)}

{tx && <TransactionViewer transaction={tx} />}
</div>
);
}
}

export default MergeTransaction;
20 changes: 19 additions & 1 deletion src/data/DataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,29 @@ export class DataProvider {
* @throws Throws if the destination account is invalid.
*/
public async getStripAndMergeAccountTransaction(destinationKey: string) {
// throw if the destination is invalid
// make sure the destination is a funded account
if (!StrKey.isValidEd25519PublicKey(destinationKey)) {
throw new Error("The destination is not a valid Stellar address.");
}

try {
const destinationProvider = new DataProvider({
serverUrl: this.serverUrl,
accountOrKey: destinationKey,
networkPassphrase: this.networkPassphrase,
});

destinationProvider.fetchAccountDetails();
} catch (e) {
if (e.isUnfunded) {
throw new Error("The destination account is not funded yet.");
}

throw new Error(
`Couldn't fetch the destination account, error: ${e.toString()}`,
);
}

let account: AccountDetails;

// fetch the current account
Expand Down

0 comments on commit 50bbd4d

Please sign in to comment.