Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 634a121

Browse files
authored
fix: bug where user could proceed with invalid destination address (#176)
closes #164
1 parent 762e95a commit 634a121

File tree

4 files changed

+49
-24
lines changed

4 files changed

+49
-24
lines changed

packages/widget/src/components/address-input/address-input.ts

+5-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { when } from 'lit/directives/when.js';
66

77
import type { PropertyValues } from '@lit/reactive-element';
88
import { validateAddress } from '../../utils';
9-
import { BaseComponent } from '../common/base-component/base-component';
9+
import { BaseComponent } from '../common';
1010

1111
import { styles } from './styles';
1212

@@ -22,9 +22,7 @@ export class AddressInput extends BaseComponent {
2222
@property({ attribute: false })
2323
onAddressChange: (address: string) => void = () => {};
2424

25-
@property({
26-
type: String
27-
})
25+
@property({ attribute: false })
2826
networkType: Network = Network.EVM;
2927

3028
@state()
@@ -48,7 +46,6 @@ export class AddressInput extends BaseComponent {
4846
}
4947

5048
this.errorMessage = validateAddress(trimedValue, this.networkType);
51-
5249
this.onAddressChange(trimedValue);
5350
};
5451

@@ -60,13 +57,13 @@ export class AddressInput extends BaseComponent {
6057
}
6158

6259
render(): HTMLTemplateResult {
63-
return html` <section class="inputAddressSection">
60+
return html`<section class="inputAddressSection">
6461
<div class="inputAddressContainer">
6562
<label class="labelContainer">
6663
<span>Send to </span>
6764
${when(
6865
this.errorMessage,
69-
() => html` <span class="errorMessage">${this.errorMessage}</span>`
66+
() => html`<span class="errorMessage">${this.errorMessage}</span>`
7067
)}</label
7168
>
7269
<textarea
@@ -79,7 +76,7 @@ export class AddressInput extends BaseComponent {
7976
}
8077
}}
8178
@input=${(evt: Event) =>
82-
this.handleAddressChange((evt.target as HTMLInputElement).value)}
79+
this.handleAddressChange((evt.target as HTMLTextAreaElement).value)}
8380
></textarea>
8481
</div>
8582
</section>`;

packages/widget/src/components/transfer/fungible/transfer-button/transfer-button.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import type { Button } from '../../../common';
77
import { BaseComponent } from '../../../common';
88

99
const enabledStates = [
10-
FungibleTransferState.WRONG_CHAIN,
11-
FungibleTransferState.WALLET_NOT_CONNECTED,
1210
FungibleTransferState.PENDING_APPROVALS,
1311
FungibleTransferState.PENDING_TRANSFER,
14-
FungibleTransferState.COMPLETED
12+
FungibleTransferState.COMPLETED,
13+
FungibleTransferState.WRONG_CHAIN,
14+
FungibleTransferState.WALLET_NOT_CONNECTED
1515
];
1616

1717
const loadingStates = [
@@ -54,6 +54,10 @@ export class FungibleTransferButton extends BaseComponent {
5454
],
5555
[FungibleTransferState.WALLET_NOT_CONNECTED, () => 'Connect Wallet'],
5656
[FungibleTransferState.WRONG_CHAIN, () => 'Switch chain'],
57+
[
58+
FungibleTransferState.INVALID_DESTINATION_ADDRESS,
59+
() => 'Invalid Address'
60+
],
5761
[FungibleTransferState.PENDING_APPROVALS, () => 'Approve token'],
5862
[FungibleTransferState.PENDING_TRANSFER, () => 'Transfer'],
5963
[
@@ -67,7 +71,7 @@ export class FungibleTransferButton extends BaseComponent {
6771
[FungibleTransferState.COMPLETED, () => 'Start new transfer']
6872
],
6973
() => 'Loading'
70-
)!}
74+
)}
7175
@click=${this.onClick}
7276
></sygma-action-button>`;
7377
}

packages/widget/src/controllers/transfers/fungible-token-transfer.ts

+35-11
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type { ReactiveController, ReactiveElement } from 'lit';
1212
import type { WalletContext } from '../../context';
1313
import { walletContext } from '../../context';
1414
import { MAINNET_EXPLORER_URL, TESTNET_EXPLORER_URL } from '../../constants';
15+
import { validateAddress } from '../../utils';
1516
import { buildEvmFungibleTransactions, executeNextEvmTransaction } from './evm';
1617

1718
export enum FungibleTransferState {
@@ -20,6 +21,7 @@ export enum FungibleTransferState {
2021
MISSING_RESOURCE,
2122
MISSING_RESOURCE_AMOUNT,
2223
MISSING_DESTINATION_ADDRESS,
24+
INVALID_DESTINATION_ADDRESS,
2325
WALLET_NOT_CONNECTED,
2426
WRONG_CHAIN,
2527
PENDING_APPROVALS,
@@ -40,7 +42,7 @@ export class FungibleTokenTransferController implements ReactiveController {
4042
public destinationNetwork?: Domain;
4143
public selectedResource?: Resource;
4244
public resourceAmount: BigNumber = ethers.constants.Zero;
43-
public destinationAddress: string = '';
45+
public destinationAddress?: string | null = '';
4446

4547
public supportedSourceNetworks: Domain[] = [];
4648
public supportedDestinationNetworks: Domain[] = [];
@@ -110,6 +112,7 @@ export class FungibleTokenTransferController implements ReactiveController {
110112
this.sourceNetwork = undefined;
111113
}
112114
this.destinationNetwork = undefined;
115+
this.selectedResource = undefined;
113116
this.pendingEvmApprovalTransactions = [];
114117
this.pendingEvmTransferTransaction = undefined;
115118
this.destinationAddress = '';
@@ -166,30 +169,31 @@ export class FungibleTokenTransferController implements ReactiveController {
166169

167170
onDestinationAddressChange = (address: string): void => {
168171
this.destinationAddress = address;
169-
if (this.destinationAddress.length === 0) {
172+
173+
if (this.destinationAddress && this.destinationAddress.length === 0) {
170174
this.pendingEvmApprovalTransactions = [];
171175
this.pendingEvmTransferTransaction = undefined;
176+
this.destinationAddress = null;
172177
}
173178
void this.buildTransactions();
174179
this.host.requestUpdate();
175180
};
176181

177182
getTransferState(): FungibleTransferState {
183+
// Enabled state
178184
if (this.transferTransactionId) {
179185
return FungibleTransferState.COMPLETED;
180186
}
187+
188+
// Loading states
181189
if (this.waitingUserConfirmation) {
182190
return FungibleTransferState.WAITING_USER_CONFIRMATION;
183191
}
184192
if (this.waitingTxExecution) {
185193
return FungibleTransferState.WAITING_TX_EXECUTION;
186194
}
187-
if (this.pendingEvmApprovalTransactions.length > 0) {
188-
return FungibleTransferState.PENDING_APPROVALS;
189-
}
190-
if (this.pendingEvmTransferTransaction) {
191-
return FungibleTransferState.PENDING_TRANSFER;
192-
}
195+
196+
// Error States
193197
if (!this.sourceNetwork) {
194198
return FungibleTransferState.MISSING_SOURCE_NETWORK;
195199
}
@@ -199,12 +203,24 @@ export class FungibleTokenTransferController implements ReactiveController {
199203
if (!this.selectedResource) {
200204
return FungibleTransferState.MISSING_RESOURCE;
201205
}
202-
if (this.resourceAmount.eq(0)) {
203-
return FungibleTransferState.MISSING_RESOURCE_AMOUNT;
204-
}
206+
205207
if (this.destinationAddress === '') {
206208
return FungibleTransferState.MISSING_DESTINATION_ADDRESS;
207209
}
210+
211+
if (
212+
this.destinationAddress === null ||
213+
this.destinationAddress === undefined ||
214+
validateAddress(this.destinationAddress, this.destinationNetwork.type)
215+
) {
216+
return FungibleTransferState.INVALID_DESTINATION_ADDRESS;
217+
}
218+
219+
if (this.resourceAmount.eq(0)) {
220+
return FungibleTransferState.MISSING_RESOURCE_AMOUNT;
221+
}
222+
223+
// Enabled States
208224
if (
209225
!this.walletContext.value?.evmWallet &&
210226
!this.walletContext.value?.substrateWallet
@@ -218,6 +234,14 @@ export class FungibleTokenTransferController implements ReactiveController {
218234
) {
219235
return FungibleTransferState.WRONG_CHAIN;
220236
}
237+
238+
if (this.pendingEvmApprovalTransactions.length > 0) {
239+
return FungibleTransferState.PENDING_APPROVALS;
240+
}
241+
if (this.pendingEvmTransferTransaction) {
242+
return FungibleTransferState.PENDING_TRANSFER;
243+
}
244+
221245
return FungibleTransferState.UNKNOWN;
222246
}
223247

packages/widget/tests/unit/components/transfer/fungible/fungible-token-transfer.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,6 @@ describe('Fungible token Transfer', function () {
9797
'sygma-address-input'
9898
) as AddressInput;
9999

100-
assert(sygmaAddressInput.address === '');
100+
assert.equal(sygmaAddressInput.address, '');
101101
});
102102
});

0 commit comments

Comments
 (0)