Skip to content
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

feat: proxy beacon #893

Merged
merged 5 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/models/evm_smart_contract.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable import/no-cycle */
import { Model } from 'objection';
import { keccak256, toHex } from 'viem';
import BaseModel from './base';
import { EvmProxyHistory } from './evm_proxy_history';

Expand Down Expand Up @@ -60,6 +61,7 @@ export class EVMSmartContract extends BaseModel {
PROXY_EIP_1822: 'PROXY_EIP_1822',
PROXY_OPEN_ZEPPELIN_IMPLEMENTATION: 'PROXY_OPEN_ZEPPELIN_IMPLEMENTATION',
PROXY_EIP_1167: 'PROXY_EIP_1167',
PROXY_EIP_1967_BEACON: 'PROXY_EIP_1967_BEACON',
};
}

Expand All @@ -82,4 +84,24 @@ export class EVMSmartContract extends BaseModel {
},
};
}

static PROXY_EVENT_TOPIC0 = {
BEACON_UPGRADED: keccak256(toHex('BeaconUpgraded(address)')),
};

static BEACON_ABI = [
{
inputs: [],
name: 'implementation',
outputs: [
{
internalType: 'address',
name: '',
type: 'address',
},
],
stateMutability: 'view',
type: 'function',
},
];
}
8 changes: 4 additions & 4 deletions src/services/evm/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ export const EIPProxyContractSupportByteCode = {
TYPE: EVMSmartContract.TYPES.PROXY_EIP_1967,
},
// TODO: support beacon soon.
// EIP_1967_BEACON: {
// SLOT: 'a3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50', // eip1967.proxy.beacon
// TYPE: EVMSmartContract.TYPES.PROXY_EIP_1967,
// },
EIP_1967_BEACON: {
SLOT: 'a3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50', // eip1967.proxy.beacon
TYPE: EVMSmartContract.TYPES.PROXY_EIP_1967_BEACON,
},
// EIP_1967_ADMIN: {
// SLOT: 'b53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103', // eip1967.proxy.admin
// TYPE: EVMSmartContract.TYPES.PROXY_EIP_1967,
Expand Down
26 changes: 24 additions & 2 deletions src/services/evm/crawl_contract_evm.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
BlockCheckpoint,
EVMSmartContract,
EVMTransaction,
EvmEvent,
EvmInternalTransaction,
} from '../../models';
import {
Expand Down Expand Up @@ -169,8 +170,22 @@ export default class CrawlSmartContractEVMService extends BullableService {
);
const bytecodesByAddress: _.Dictionary<GetBytecodeReturnType> =
await this.getBytecodeContracts(notFoundAddresses);
const beaconContracts = (
await EvmEvent.query()
.where('evm_tx_id', '>=', fromTx.id)
.andWhere('evm_tx_id', '<=', toTx.id)
.andWhere('topic0', EVMSmartContract.PROXY_EVENT_TOPIC0.BEACON_UPGRADED)
.select('address', 'topic1')
).map((e) => ({
address: e.address,
beacon: `0x${e.topic1.slice(26)}`,
}));
const proxyInfoByAddress: _.Dictionary<DetectEVMProxyContract | null> =
await this.getContractsProxyInfo(notFoundAddresses, bytecodesByAddress);
await this.getContractsProxyInfo(
notFoundAddresses,
bytecodesByAddress,
beaconContracts
);
notFoundAddresses.forEach((address: string) => {
const code = bytecodesByAddress[address];
// check if this address has code -> is smart contract
Expand Down Expand Up @@ -304,7 +319,11 @@ export default class CrawlSmartContractEVMService extends BullableService {

async getContractsProxyInfo(
addrs: string[],
bytecodes: _.Dictionary<GetBytecodeReturnType>
bytecodes: _.Dictionary<GetBytecodeReturnType>,
beaconContracts: {
address: string;
beacon: string;
}[]
) {
const result: _.Dictionary<DetectEVMProxyContract | null> = {};
const proxiesInfo = await Promise.all(
Expand Down Expand Up @@ -334,6 +353,9 @@ export default class CrawlSmartContractEVMService extends BullableService {
byteCode,
EIPProxyContractSupportByteCode.EIP_1167_IMPLEMENTATION.SLOT
),
this.contractHelper.detectBeaconProxyContract(
peara marked this conversation as resolved.
Show resolved Hide resolved
beaconContracts.find((e) => e.address === addr)?.beacon
),
]).catch(() => Promise.resolve(null));
})
);
Expand Down
39 changes: 31 additions & 8 deletions src/services/evm/helpers/contract_helper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import _ from 'lodash';
import { PublicClient } from 'viem';
import { getContract, PublicClient } from 'viem';
import { EvmEvent, EVMSmartContract } from '../../../models';
import '../../../../fetch-polyfill.js';
import {
DetectEVMProxyContract,
Expand Down Expand Up @@ -62,6 +63,24 @@ export class ContractHelper {
return resultReturn;
}

public async detectBeaconProxyContract(
beacon?: string
): Promise<DetectEVMProxyContract> {
if (!beacon) {
throw Error('Not beacon contract!');
}
const contract = getContract({
address: beacon as `0x${string}`,
abi: EVMSmartContract.BEACON_ABI,
client: this.viemClient,
});
const implementation = (await contract.read.implementation()) as string;
return {
logicContractAddress: implementation,
EIP: EIPProxyContractSupportByteCode.EIP_1967_BEACON.TYPE,
};
}

// Detect contract is proxy contract or not
public async isContractProxy(
contractAddress: string,
Expand All @@ -78,6 +97,16 @@ export class ContractHelper {
if (!byteCode) {
return null;
}
let beaconContract = (
await EvmEvent.query()
.where('address', contractAddress)
.andWhere('topic0', EVMSmartContract.PROXY_EVENT_TOPIC0.BEACON_UPGRADED)
.first()
.select('topic1')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

orderBy? limit 1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sao ko thêm order?

Copy link
Collaborator Author

@phamphong9981 phamphong9981 Sep 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Em đọc trong code beacon contract, thì proxy chỉ emit event BeaconUpgraded khi khởi tạo thôi. Nên query của em nếu đúng logic thì chỉ ra 1 record thôi ạ

)?.topic1.slice(26);
if (beaconContract) {
beaconContract = `0x${beaconContract}`;
}
try {
if (byteCodeSlot) {
result = await this.detectProxyContractByByteCode(
Expand All @@ -100,13 +129,7 @@ export class ContractHelper {
EIPProxyContractSupportByteCode.EIP_1967_IMPLEMENTATION.SLOT,
blockHeight
),
// TODO: support beacon soon.
// this.detectProxyContractByByteCode(
// contractAddress,
// byteCode,
// EIPProxyContractSupportByteCode.EIP_1967_BEACON.SLOT,
// blockHeight
// ),
this.detectBeaconProxyContract(beaconContract),
this.detectProxyContractByByteCode(
contractAddress,
byteCode,
Expand Down
Loading