Skip to content

Commit 721bc22

Browse files
authored
Fix/blockchain compatibility check (#31)
* fix: blockchain comp check * add monad devnet * temp: override confirm tx in action config * fix: compatibility checks on changing action adapter * Revert "temp: override confirm tx in action config" This reverts commit 1069f85. * fix: stale adapter * add comment * harmonize update handling * restore setAdapter + add comment for react env * harmonize action update
1 parent 5718872 commit 721bc22

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

packages/blinks-core/src/api/Action/Action.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ export class Action {
174174
return this._adapter;
175175
}
176176

177+
/**
178+
* Set the adapter for the action.
179+
* Not recommended to use in react environments, consider using Action.update() instead that returns a new instance.
180+
* @param adapter The adapter to set
181+
*/
177182
public setAdapter(adapter: ActionAdapter) {
178183
this._adapter = adapter;
179184
}
@@ -342,6 +347,22 @@ export class Action {
342347
this._id,
343348
);
344349
}
350+
351+
withUpdate(update: {
352+
adapter?: ActionAdapter;
353+
supportStrategy?: ActionSupportStrategy;
354+
}) {
355+
return new Action(
356+
this._url,
357+
this._data,
358+
this._metadata,
359+
update.supportStrategy ?? this._supportStrategy,
360+
update.adapter ?? this._adapter,
361+
this._chainMetadata,
362+
this._id,
363+
this._experimental,
364+
);
365+
}
345366
}
346367

347368
const getActionMetadata = (response: Response): ActionMetadata => {

packages/blinks-core/src/hooks/useAction.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export function useAction({
5353
const { actionApiUrl } = useActionApiUrl(url);
5454
const [action, setAction] = useState<Action | null>(null);
5555
const [isLoading, setIsLoading] = useState(false);
56+
const [hasFetched, setHasFetched] = useState(false);
5657

5758
useEffect(() => {
5859
setIsLoading(true);
@@ -61,12 +62,14 @@ export function useAction({
6162
}
6263

6364
let ignore = false;
65+
setHasFetched(false);
6466
Action.fetch(actionApiUrl, undefined, supportStrategy)
6567
.then((action) => {
6668
if (ignore) {
6769
return;
6870
}
6971
setAction(action);
72+
setHasFetched(true);
7073
})
7174
.catch((e) => {
7275
console.error('[@dialectlabs/blinks-core] Failed to fetch action', e);
@@ -81,11 +84,26 @@ export function useAction({
8184
return () => {
8285
ignore = true;
8386
};
84-
}, [actionApiUrl, isRegistryLoaded, supportStrategy]);
87+
// eslint-disable-next-line react-hooks/exhaustive-deps -- only update if actionApiUrl changes
88+
}, [actionApiUrl, isRegistryLoaded]);
8589

90+
// this effect handles race conditions between fetching the action adapter change
91+
// hasFetched dependency is used instead of action dependency to ensure there's no infinite loop
8692
useEffect(() => {
87-
action?.setAdapter(adapter);
88-
}, [action, adapter]);
93+
if (!action || !hasFetched) {
94+
return;
95+
}
96+
try {
97+
const updated = action.withUpdate({
98+
adapter,
99+
supportStrategy,
100+
});
101+
setAction(updated);
102+
} catch (e) {
103+
console.error('[@dialectlabs/blinks-core] Failed to update action', e);
104+
}
105+
// eslint-disable-next-line react-hooks/exhaustive-deps -- only update if adapter or supportStrategy changes
106+
}, [adapter, supportStrategy, hasFetched]);
89107

90108
return { action, isLoading };
91109
}

packages/blinks-core/src/utils/caip-2.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ export const BlockchainIds = {
66
SOLANA_DEVNET: 'solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1',
77
SOLANA_TESTNET: 'solana:4uhcVJyU9pJkvQyS88uRDiswHXSCkY3',
88
ETHEREUM_MAINNET: 'eip155:1',
9+
MONAD_DEVNET: 'eip155:41454',
910
};
1011
const BlockchainNames: Record<string, string> = {
1112
[BlockchainIds.SOLANA_MAINNET]: 'Solana Mainnet',
1213
[BlockchainIds.SOLANA_DEVNET]: 'Solana Devnet',
1314
[BlockchainIds.SOLANA_TESTNET]: 'Solana Testnet',
1415
[BlockchainIds.ETHEREUM_MAINNET]: 'Ethereum Mainnet',
16+
[BlockchainIds.MONAD_DEVNET]: 'Monad Devnet',
1517
};
1618

1719
export function getShortBlockchainName(id: string) {

0 commit comments

Comments
 (0)