Skip to content

Commit 3f1b940

Browse files
committed
refactor proxy check code into helper function
1 parent 6b4b00b commit 3f1b940

File tree

3 files changed

+83
-59
lines changed

3 files changed

+83
-59
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 {
@@ -101,35 +102,18 @@ export default class AddCommand extends Command {
101102
),
102103
);
103104
if (!ethabi) throw Error;
104-
const isProxy = ethabi.callFunctionSignatures()?.includes('upgradeTo(address)');
105-
if (isProxy) {
106-
const impl = await retryWithPrompt(() =>
107-
withSpinner(
108-
'Fetching proxy implementation address...',
109-
'Failed to fetch proxy implementation address',
110-
'Warning fetching proxy implementation address',
111-
() => contractService.getProxyImplementation(network, address),
112-
),
113-
);
114-
if (impl) {
115-
const useImplementation = await prompt
116-
.confirm(`Proxy contract detected. Use implementation contract ABI at ${impl}?`, true)
117-
.catch(() => false);
118-
119-
if (useImplementation) {
120-
implAddress = impl;
121-
ethabi = await retryWithPrompt(() =>
122-
withSpinner(
123-
'Fetching implementation contract ABI...',
124-
'Failed to fetch implementation ABI',
125-
'Warning fetching implementation ABI',
126-
() => contractService.getABI(EthereumABI, network, implAddress!),
127-
),
128-
);
129-
}
130-
if (!ethabi) throw Error;
131-
}
105+
106+
const { implementationAbi, implementationAddress } = await checkForProxy(
107+
contractService,
108+
network,
109+
address,
110+
ethabi,
111+
);
112+
if (implementationAddress) {
113+
implAddress = implementationAddress;
114+
ethabi = implementationAbi!;
132115
}
116+
if (!ethabi) throw Error;
133117
} catch (error) {
134118
// we cannot ask user to do prompt in test environment
135119
if (process.env.NODE_ENV !== 'test') {

packages/cli/src/commands/init.ts

Lines changed: 16 additions & 31 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';
@@ -622,38 +623,22 @@ async function processInitForm(
622623
() => contractService.getABI(protocolInstance.getABI(), network.id, address),
623624
),
624625
);
625-
initDebugger.extend('processInitForm')("abiFromEtherscan len: '%s'", abiFromApi?.name);
626-
const isProxy = abiFromApi?.callFunctionSignatures().includes('upgradeTo(address)');
627-
if (isProxy) {
628-
const impl = await retryWithPrompt(() =>
629-
withSpinner(
630-
'Fetching proxy implementation address...',
631-
'Failed to fetch proxy implementation address',
632-
'Warning fetching proxy implementation address',
633-
() => contractService.getProxyImplementation(network.id, address),
634-
),
626+
initDebugger.extend('processInitForm')("ABI: '%s'", abiFromApi?.name);
627+
if (abiFromApi) {
628+
const { implementationAbi, implementationAddress } = await checkForProxy(
629+
contractService,
630+
network.id,
631+
address,
632+
abiFromApi,
635633
);
636-
initDebugger.extend('processInitForm')("proxyImplementation: '%s'", impl);
637-
if (impl) {
638-
const useImplementation = await prompt
639-
.confirm(
640-
`Proxy contract detected. Use implementation contract ABI at ${impl}?`,
641-
true,
642-
)
643-
.catch(() => false);
644-
645-
if (useImplementation) {
646-
implAddress = impl;
647-
abiFromApi = await retryWithPrompt(() =>
648-
withSpinner(
649-
'Fetching implementation contract ABI...',
650-
'Failed to fetch implementation ABI',
651-
'Warning fetching implementation ABI',
652-
() =>
653-
contractService.getABI(protocolInstance.getABI(), network.id, implAddress!),
654-
),
655-
);
656-
}
634+
if (implementationAddress) {
635+
implAddress = implementationAddress;
636+
abiFromApi = implementationAbi!;
637+
initDebugger.extend('processInitForm')(
638+
"Impl ABI: '%s', Impl Address: '%s'",
639+
abiFromApi?.name,
640+
implAddress,
641+
);
657642
}
658643
}
659644
}

0 commit comments

Comments
 (0)