-
Notifications
You must be signed in to change notification settings - Fork 226
Add support for proxy contracts #1888
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
YaroShkvorets
wants to merge
3
commits into
main
Choose a base branch
from
yaro/proxy-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@graphprotocol/graph-cli': minor | ||
--- | ||
|
||
Add EIP-1967/OpenZeppelin proxy contracts support. When proxy contract is detected, user is given an option to use ABI of the implementation contract. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { prompt } from 'gluegun'; | ||
import { describe, expect, it, vi } from 'vitest'; | ||
import EthereumABI from '../protocols/ethereum/abi.js'; | ||
import { ContractService } from './contracts.js'; | ||
import { checkForProxy } from './proxy.js'; | ||
import { loadRegistry } from './registry.js'; | ||
|
||
// Mock gluegun's prompt | ||
vi.mock('gluegun', async () => { | ||
const actual = await vi.importActual('gluegun'); | ||
return { | ||
...actual, | ||
prompt: { | ||
confirm: vi.fn().mockResolvedValue(true), | ||
}, | ||
}; | ||
}); | ||
|
||
describe('Proxy detection', async () => { | ||
const NETWORK = 'mainnet'; | ||
const registry = await loadRegistry(); | ||
const contractService = new ContractService(registry); | ||
|
||
interface ProxyTestCase { | ||
name: string; | ||
type: string; | ||
address: string; | ||
implementationAddress: string | null; | ||
expectedFunctions: string[]; | ||
} | ||
|
||
const testCases: ProxyTestCase[] = [ | ||
{ | ||
name: 'USDC', | ||
type: 'EIP-1967 Upgradeable', | ||
address: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', | ||
implementationAddress: '0x43506849d7c04f9138d1a2050bbf3a0c054402dd', | ||
expectedFunctions: ['mint(address,uint256)', 'configureMinter(address,uint256)'], | ||
}, | ||
{ | ||
name: 'BUSD', | ||
type: 'OpenZeppelin Unstructured Storage', | ||
address: '0x4Fabb145d64652a948d72533023f6E7A623C7C53', | ||
implementationAddress: '0x2A3F1A37C04F82aA274f5353834B2d002Db91015', | ||
expectedFunctions: ['reclaimBUSD()', 'claimOwnership()'], | ||
}, | ||
{ | ||
name: 'Gelato', | ||
type: 'EIP-2535 Diamond Pattern (not supported)', | ||
address: '0x3caca7b48d0573d793d3b0279b5f0029180e83b6', | ||
implementationAddress: null, | ||
expectedFunctions: [], | ||
}, | ||
]; | ||
|
||
for (const testCase of testCases) { | ||
it(`should handle ${testCase.name} ${testCase.type} Proxy`, async () => { | ||
const abi = await contractService.getABI(EthereumABI, NETWORK, testCase.address); | ||
expect(abi).toBeDefined(); | ||
|
||
const { implementationAddress, implementationAbi } = await checkForProxy( | ||
contractService, | ||
NETWORK, | ||
testCase.address, | ||
abi!, | ||
); | ||
|
||
expect(implementationAddress === testCase.implementationAddress); | ||
|
||
const implFunctions = implementationAbi?.callFunctionSignatures(); | ||
for (const expectedFunction of testCase.expectedFunctions) { | ||
expect(implFunctions).toContain(expectedFunction); | ||
} | ||
}); | ||
} | ||
|
||
it('should handle when user declines to use implementation', async () => { | ||
vi.mocked(prompt.confirm).mockResolvedValueOnce(false); | ||
const abi = await contractService.getABI(EthereumABI, NETWORK, testCases[0].address); | ||
expect(abi).toBeDefined(); | ||
|
||
const { implementationAddress, implementationAbi } = await checkForProxy( | ||
contractService, | ||
NETWORK, | ||
testCases[0].address, | ||
abi!, | ||
); | ||
expect(implementationAddress).toBeNull(); | ||
expect(implementationAbi).toBeNull(); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { prompt } from 'gluegun'; | ||
import EthereumABI from '../protocols/ethereum/abi.js'; | ||
import { ContractService } from './contracts.js'; | ||
import { retryWithPrompt } from './retry.js'; | ||
import { withSpinner } from './spinner.js'; | ||
|
||
export interface CheckForProxyResult { | ||
implementationAbi: EthereumABI | null; | ||
implementationAddress: string | null; | ||
} | ||
|
||
export async function checkForProxy( | ||
contractService: ContractService, | ||
network: string, | ||
address: string, | ||
abi: EthereumABI, | ||
): Promise<CheckForProxyResult> { | ||
let implementationAddress = null; | ||
let implementationAbi = null; | ||
|
||
const maybeProxy = abi.callFunctionSignatures()?.includes('upgradeTo(address)'); | ||
if (maybeProxy) { | ||
const impl = await retryWithPrompt(() => | ||
withSpinner( | ||
'Fetching proxy implementation address...', | ||
'Failed to fetch proxy implementation address', | ||
'Warning fetching proxy implementation address', | ||
() => contractService.getProxyImplementation(network, address), | ||
), | ||
); | ||
|
||
if (impl) { | ||
const useImplementation = await prompt | ||
.confirm(`Proxy contract detected. Use implementation contract ABI at ${impl}?`, true) | ||
.catch(() => false); | ||
|
||
if (useImplementation) { | ||
implementationAddress = impl; | ||
implementationAbi = await retryWithPrompt(() => | ||
withSpinner( | ||
'Fetching implementation contract ABI...', | ||
'Failed to fetch implementation ABI', | ||
'Warning fetching implementation ABI', | ||
() => contractService.getABI(EthereumABI, network, implementationAddress!), | ||
), | ||
); | ||
} | ||
} | ||
} | ||
|
||
return { | ||
implementationAbi, | ||
implementationAddress, | ||
}; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.