Skip to content

Commit 8fd8c0a

Browse files
committed
refactor proxy check code into helper function
1 parent 1245db1 commit 8fd8c0a

File tree

3 files changed

+83
-66
lines changed

3 files changed

+83
-66
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { prompt } from 'gluegun';
2+
import EthereumABI from '../protocols/ethereum/abi.js';
3+
import { ContractService } from './contracts.js';
4+
import { retryWithPrompt } from './retry.js';
5+
import { withSpinner } from './spinner.js';
6+
7+
export interface CheckForProxyResult {
8+
implementationAbi: EthereumABI | null;
9+
implementationAddress: string | null;
10+
}
11+
12+
export async function checkForProxy(
13+
contractService: ContractService,
14+
network: string,
15+
address: string,
16+
abi: EthereumABI,
17+
): Promise<CheckForProxyResult> {
18+
let implementationAddress = null;
19+
let implementationAbi = null;
20+
21+
const maybeProxy = abi.callFunctionSignatures()?.includes('upgradeTo(address)');
22+
if (maybeProxy) {
23+
const impl = await retryWithPrompt(() =>
24+
withSpinner(
25+
'Fetching proxy implementation address...',
26+
'Failed to fetch proxy implementation address',
27+
'Warning fetching proxy implementation address',
28+
() => contractService.getProxyImplementation(network, address),
29+
),
30+
);
31+
32+
if (impl) {
33+
const useImplementation = await prompt
34+
.confirm(`Proxy contract detected. Use implementation contract ABI at ${impl}?`, true)
35+
.catch(() => false);
36+
37+
if (useImplementation) {
38+
implementationAddress = impl;
39+
implementationAbi = await retryWithPrompt(() =>
40+
withSpinner(
41+
'Fetching implementation contract ABI...',
42+
'Failed to fetch implementation ABI',
43+
'Warning fetching implementation ABI',
44+
() => contractService.getABI(EthereumABI, network, implementationAddress!),
45+
),
46+
);
47+
}
48+
}
49+
}
50+
51+
return {
52+
implementationAbi,
53+
implementationAddress,
54+
};
55+
}

packages/cli/src/commands/add.ts

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Args, Command, Errors, Flags } from '@oclif/core';
44
import { ContractService } from '../command-helpers/contracts.js';
55
import * as DataSourcesExtractor from '../command-helpers/data-sources.js';
66
import { updateNetworksFile } from '../command-helpers/network.js';
7+
import { checkForProxy } from '../command-helpers/proxy.js';
78
import { loadRegistry } from '../command-helpers/registry.js';
89
import { retryWithPrompt } from '../command-helpers/retry.js';
910
import {
@@ -114,35 +115,18 @@ export default class AddCommand extends Command {
114115
),
115116
);
116117
if (!ethabi) throw Error;
117-
const isProxy = ethabi.callFunctionSignatures()?.includes('upgradeTo(address)');
118-
if (isProxy) {
119-
const impl = await retryWithPrompt(() =>
120-
withSpinner(
121-
'Fetching proxy implementation address...',
122-
'Failed to fetch proxy implementation address',
123-
'Warning fetching proxy implementation address',
124-
() => contractService.getProxyImplementation(network, address),
125-
),
126-
);
127-
if (impl) {
128-
const useImplementation = await prompt
129-
.confirm(`Proxy contract detected. Use implementation contract ABI at ${impl}?`, true)
130-
.catch(() => false);
131-
132-
if (useImplementation) {
133-
implAddress = impl;
134-
ethabi = await retryWithPrompt(() =>
135-
withSpinner(
136-
'Fetching implementation contract ABI...',
137-
'Failed to fetch implementation ABI',
138-
'Warning fetching implementation ABI',
139-
() => contractService.getABI(EthereumABI, network, implAddress!),
140-
),
141-
);
142-
}
143-
if (!ethabi) throw Error;
144-
}
118+
119+
const { implementationAbi, implementationAddress } = await checkForProxy(
120+
contractService,
121+
network,
122+
address,
123+
ethabi,
124+
);
125+
if (implementationAddress) {
126+
implAddress = implementationAddress;
127+
ethabi = implementationAbi!;
145128
}
129+
if (!ethabi) throw Error;
146130
} catch (error) {
147131
// we cannot ask user to do prompt in test environment
148132
if (process.env.NODE_ENV !== 'test') {

packages/cli/src/commands/init.ts

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { DEFAULT_IPFS_URL } from '../command-helpers/ipfs.js';
1111
import { initNetworksConfig } from '../command-helpers/network.js';
1212
import { chooseNodeUrl } from '../command-helpers/node.js';
1313
import { PromptManager } from '../command-helpers/prompt-manager.js';
14+
import { checkForProxy } from '../command-helpers/proxy.js';
1415
import { loadRegistry } from '../command-helpers/registry.js';
1516
import { retryWithPrompt } from '../command-helpers/retry.js';
1617
import { generateScaffold, writeScaffold } from '../command-helpers/scaffold.js';
@@ -645,49 +646,26 @@ async function processInitForm(
645646
() => contractService.getABI(protocolInstance.getABI(), network.id, address),
646647
),
647648
);
648-
initDebugger.extend('processInitForm')("abiFromEtherscan len: '%s'", abiFromApi?.name);
649+
initDebugger.extend('processInitForm')("ABI: '%s'", abiFromApi?.name);
649650
} else {
650651
abiFromApi = initAbi;
651652
}
652653

653-
const isProxy =
654-
abiFromApi
655-
?.callFunctions()
656-
.some(
657-
entry => entry.get('name') === 'implementation' || entry.get('name') === 'upgradeTo',
658-
) ?? false;
659-
initDebugger.extend('processInitForm')('isProxy: %O', isProxy);
660-
661-
if (isProxy) {
662-
const impl = await retryWithPrompt(() =>
663-
withSpinner(
664-
'Fetching proxy implementation address...',
665-
'Failed to fetch proxy implementation address',
666-
'Warning fetching proxy implementation address',
667-
() => contractService.getProxyImplementation(network.id, address),
668-
),
654+
if (abiFromApi) {
655+
const { implementationAbi, implementationAddress } = await checkForProxy(
656+
contractService,
657+
network.id,
658+
address,
659+
abiFromApi,
669660
);
670-
initDebugger.extend('processInitForm')("proxyImplementation: '%s'", impl);
671-
if (impl) {
672-
try {
673-
const useImplementation = await prompt.confirm(
674-
`Proxy contract detected. Use implementation contract ABI at ${impl}?`,
675-
true,
676-
);
677-
if (useImplementation) {
678-
implAddress = impl;
679-
abiFromApi = await retryWithPrompt(() =>
680-
withSpinner(
681-
'Fetching implementation contract ABI...',
682-
'Failed to fetch implementation ABI',
683-
'Warning fetching implementation ABI',
684-
() => contractService.getABI(protocolInstance.getABI(), network.id, implAddress!),
685-
),
686-
);
687-
}
688-
} catch (error) {
689-
this.error(`Error confirming proxy implementation: ${error.message}`, { exit: 1 });
690-
}
661+
if (implementationAddress) {
662+
implAddress = implementationAddress;
663+
abiFromApi = implementationAbi!;
664+
initDebugger.extend('processInitForm')(
665+
"Impl ABI: '%s', Impl Address: '%s'",
666+
abiFromApi?.name,
667+
implAddress,
668+
);
691669
}
692670
}
693671

0 commit comments

Comments
 (0)