Skip to content

Commit 4e45cce

Browse files
committed
add proxy support to graph add
1 parent 6843235 commit 4e45cce

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

packages/cli/src/commands/add.ts

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ export default class AddCommand extends Command {
8484
let startBlock = startBlockFlag ? parseInt(startBlockFlag).toString() : startBlockFlag;
8585
let contractName = contractNameFlag || DEFAULT_CONTRACT_NAME;
8686

87-
let ethabi = null;
87+
let ethabi: EthereumABI | null = null;
88+
let implAddress = null;
8889
if (abi) {
8990
ethabi = EthereumABI.load(contractName, abi);
9091
} else {
@@ -100,6 +101,36 @@ export default class AddCommand extends Command {
100101
),
101102
);
102103
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.confirm(
116+
`Proxy contract detected. Index implementation contract at ${impl}?`,
117+
true,
118+
);
119+
120+
if (useImplementation) {
121+
implAddress = impl;
122+
ethabi = await retryWithPrompt(() =>
123+
withSpinner(
124+
'Fetching implementation contract ABI...',
125+
'Failed to fetch implementation ABI',
126+
'Warning fetching implementation ABI',
127+
() => contractService.getABI(EthereumABI, network, implAddress!),
128+
),
129+
);
130+
}
131+
if (!ethabi) throw Error;
132+
}
133+
}
103134
} catch (error) {
104135
// we cannot ask user to do prompt in test environment
105136
if (process.env.NODE_ENV !== 'test') {
@@ -122,10 +153,15 @@ export default class AddCommand extends Command {
122153
}
123154
}
124155
}
156+
if (!ethabi) {
157+
this.error('Failed to load ABI', { exit: 1 });
158+
}
125159

126160
try {
127161
if (isLocalHost) throw Error; // Triggers user prompting without waiting for Etherscan lookup to fail
128-
startBlock ||= Number(await contractService.getStartBlock(network, address)).toString();
162+
startBlock ||= Number(
163+
await contractService.getStartBlock(network, implAddress ?? address),
164+
).toString();
129165
} catch (error) {
130166
// we cannot ask user to do prompt in test environment
131167
if (process.env.NODE_ENV !== 'test') {
@@ -150,7 +186,8 @@ export default class AddCommand extends Command {
150186
if (isLocalHost) throw Error; // Triggers user prompting without waiting for Etherscan lookup to fail
151187
if (contractName === DEFAULT_CONTRACT_NAME) {
152188
contractName =
153-
(await contractService.getContractName(network, address)) ?? DEFAULT_CONTRACT_NAME;
189+
(await contractService.getContractName(network, implAddress ?? address)) ??
190+
DEFAULT_CONTRACT_NAME;
154191
}
155192
} catch (error) {
156193
// not asking user to do prompt in test environment
@@ -248,8 +285,6 @@ export default class AddCommand extends Command {
248285
'Warning during codegen',
249286
async () => await system.run(yarn ? 'yarn codegen' : 'npm run codegen'),
250287
);
251-
252-
this.exit(0);
253288
}
254289
}
255290

packages/cli/src/commands/init.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -623,10 +623,7 @@ async function processInitForm(
623623
),
624624
);
625625
initDebugger.extend('processInitForm')("abiFromEtherscan len: '%s'", abiFromApi?.name);
626-
const isProxy =
627-
abiFromApi?.callFunctions().some(entry => entry.get('name') === 'implementation') ??
628-
false;
629-
initDebugger.extend('processInitForm')('isProxy: %O', isProxy);
626+
const isProxy = abiFromApi?.callFunctionSignatures().includes('upgradeTo(address)');
630627
if (isProxy) {
631628
const impl = await retryWithPrompt(() =>
632629
withSpinner(
@@ -1307,7 +1304,7 @@ async function addAnotherContract(
13071304
name: 'contract',
13081305
initial: ProtocolContract.identifierName(),
13091306
required: true,
1310-
message: () => `\nContract ${ProtocolContract.identifierName()}`,
1307+
message: () => `Contract ${ProtocolContract.identifierName()}`,
13111308
validate: value => {
13121309
const { valid, error } = validateContract(value, ProtocolContract);
13131310
return valid ? true : error;

0 commit comments

Comments
 (0)