Skip to content

Commit f817c81

Browse files
committed
handle case when account already activated
1 parent 162a308 commit f817c81

2 files changed

Lines changed: 122 additions & 49 deletions

File tree

app/lib/services/stellar_service.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,12 @@ Future<int> getTFTPriceFromXLM() async {
141141
throw Exception('Error getting price');
142142
}
143143
}
144+
145+
Future<AccountResponse> getStellarAccount(String accountId) async {
146+
return await getAccount(network: NetworkType.PUBLIC, accountId: accountId);
147+
}
148+
149+
Future<bool> addTrustline(String secret) async {
150+
final client = Client(NetworkType.PUBLIC, secret);
151+
return await client.addConfiguredTrustlines();
152+
}

app/lib/widgets/wallets/activate_wallet.dart

Lines changed: 113 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -139,58 +139,122 @@ class _ActivateWalletWidgetState extends ConsumerState<ActivateWalletWidget> {
139139
walletError = null;
140140
});
141141

142-
final activated =
143-
await StellarService.initialize(widget.wallet.stellarSecret);
144-
if (activated) {
145-
logger.d('Wallet activated successfully');
142+
bool accountExists = false;
143+
try {
144+
await StellarService.getStellarAccount(widget.wallet.stellarAddress);
145+
accountExists = true;
146+
logger.d('Account already exists on Stellar network');
147+
146148
try {
147-
logger.d('Transferring activation fee');
148-
await StellarService.transfer(
149-
_selectedWallet!.stellarSecret,
150-
Globals().activationServiceAddress,
151-
(activationFee * tftPrice).toString(),
149+
final trustlineAdded =
150+
await StellarService.addTrustline(widget.wallet.stellarSecret);
151+
if (trustlineAdded) {
152+
logger.d('TFT trustline added successfully');
153+
await showDialog(
154+
context: context,
155+
builder: (BuildContext context) => CustomDialog(
156+
type: DialogType.Info,
157+
image: Icons.check,
158+
title: 'Trustline Added',
159+
description:
160+
'Your wallet already existed on Stellar. TFT trustline has been added successfully.',
161+
actions: <Widget>[
162+
TextButton(
163+
child: const Text('Close'),
164+
onPressed: () {
165+
Navigator.pop(context);
166+
},
167+
),
168+
],
169+
),
170+
);
171+
walletRef.reloadBalances();
172+
Navigator.pop(context);
173+
return;
174+
} else {
175+
throw Exception('Failed to add trustline');
176+
}
177+
} catch (trustlineError) {
178+
logger.e('Failed to add trustline: $trustlineError');
179+
await showDialog(
180+
context: context,
181+
builder: (BuildContext context) => CustomDialog(
182+
type: DialogType.Error,
183+
image: Icons.error,
184+
title: 'Trustline Error',
185+
description:
186+
'Your wallet exists but failed to add the TFT trustline. Please try again.',
187+
actions: <Widget>[
188+
TextButton(
189+
child: const Text('Close'),
190+
onPressed: () {
191+
Navigator.pop(context);
192+
},
193+
),
194+
],
195+
),
196+
);
197+
return;
198+
}
199+
} catch (accountError) {
200+
accountExists = false;
201+
logger.d('Account does not exist, will proceed with activation');
202+
}
203+
204+
if (!accountExists) {
205+
final activated =
206+
await StellarService.initialize(widget.wallet.stellarSecret);
207+
if (activated) {
208+
logger.d('Wallet activated successfully');
209+
try {
210+
logger.d('Transferring activation fee');
211+
await StellarService.transfer(
212+
_selectedWallet!.stellarSecret,
213+
Globals().activationServiceAddress,
214+
(activationFee * tftPrice).toString(),
215+
);
216+
} catch (transferError) {
217+
logger.e('Transfer error: $transferError');
218+
}
219+
await showDialog(
220+
context: context,
221+
builder: (BuildContext context) => CustomDialog(
222+
type: DialogType.Info,
223+
image: Icons.check,
224+
title: 'Wallet Activated',
225+
description: 'Your wallet has been activated successfully',
226+
actions: <Widget>[
227+
TextButton(
228+
child: const Text('Close'),
229+
onPressed: () {
230+
Navigator.pop(context);
231+
},
232+
),
233+
],
234+
),
235+
);
236+
walletRef.reloadBalances();
237+
Navigator.pop(context);
238+
} else {
239+
logger.e('Failed to activate wallet');
240+
await showDialog(
241+
context: context,
242+
builder: (BuildContext context) => CustomDialog(
243+
type: DialogType.Error,
244+
image: Icons.error,
245+
title: 'Error',
246+
description: 'Failed to activate wallet. Please try again.',
247+
actions: <Widget>[
248+
TextButton(
249+
child: const Text('Close'),
250+
onPressed: () {
251+
Navigator.pop(context);
252+
},
253+
),
254+
],
255+
),
152256
);
153-
} catch (transferError) {
154-
logger.e('Transfer error : $transferError');
155257
}
156-
await showDialog(
157-
context: context,
158-
builder: (BuildContext context) => CustomDialog(
159-
type: DialogType.Info,
160-
image: Icons.check,
161-
title: 'Wallet Activated',
162-
description: 'Your wallet has been activated successfully',
163-
actions: <Widget>[
164-
TextButton(
165-
child: const Text('Close'),
166-
onPressed: () {
167-
Navigator.pop(context);
168-
},
169-
),
170-
],
171-
),
172-
);
173-
walletRef.reloadBalances();
174-
Navigator.pop(context);
175-
} else {
176-
logger.e('Failed to activate wallet');
177-
await showDialog(
178-
context: context,
179-
builder: (BuildContext context) => CustomDialog(
180-
type: DialogType.Error,
181-
image: Icons.error,
182-
title: 'Error',
183-
description: 'Failed to activate wallet. Please try again.',
184-
actions: <Widget>[
185-
TextButton(
186-
child: const Text('Close'),
187-
onPressed: () {
188-
Navigator.pop(context);
189-
},
190-
),
191-
],
192-
),
193-
);
194258
}
195259
} catch (e) {
196260
logger.e('Activation error: $e');

0 commit comments

Comments
 (0)