-
Notifications
You must be signed in to change notification settings - Fork 115
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: update token for upgradable transactions where implementation i… #348
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { types } from "zksync-ethers"; | ||
import { mock } from "jest-mock-extended"; | ||
import { defaultContractUpgradableHandler } from "./default.handler"; | ||
|
||
describe("defaultContractUpgradableHandler", () => { | ||
let log: types.Log; | ||
beforeEach(() => { | ||
log = mock<types.Log>({ | ||
transactionIndex: 1, | ||
blockNumber: 3233097, | ||
transactionHash: "0x5e018d2a81dbd1ef80ff45171dd241cb10670dcb091e324401ff8f52293841b0", | ||
address: "0x1BEB2aBb1678D8a25431d9728A425455f29d12B7", | ||
topics: [ | ||
"0xbc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b", | ||
"0x000000000000000000000000a1810a1f32F4DC6c5112b5b837b6975E56b489cc", | ||
], | ||
data: "0x", | ||
index: 8, | ||
blockHash: "0xdfd071dcb9c802f7d11551f4769ca67842041ffb81090c49af7f089c5823f39c", | ||
l1BatchNumber: 604161, | ||
}); | ||
}); | ||
|
||
describe("matches", () => { | ||
it("returns true", () => { | ||
const result = defaultContractUpgradableHandler.matches(log); | ||
expect(result).toBe(true); | ||
}); | ||
}); | ||
|
||
describe("extract", () => { | ||
let transactionReceipt; | ||
|
||
beforeEach(() => { | ||
transactionReceipt = mock<types.TransactionReceipt>({ | ||
blockNumber: 10, | ||
hash: "transactionHash", | ||
from: "from", | ||
}); | ||
}); | ||
|
||
it("extracts upgraded contract address", () => { | ||
const result = defaultContractUpgradableHandler.extract(log, transactionReceipt); | ||
expect(result.address).toBe("0x1BEB2aBb1678D8a25431d9728A425455f29d12B7"); | ||
}); | ||
|
||
it("extracts block number for the upgraded contract", () => { | ||
const result = defaultContractUpgradableHandler.extract(log, transactionReceipt); | ||
expect(result.blockNumber).toBe(transactionReceipt.blockNumber); | ||
}); | ||
|
||
it("extracts transaction hash for the upgraded contract", () => { | ||
const result = defaultContractUpgradableHandler.extract(log, transactionReceipt); | ||
expect(result.transactionHash).toBe(transactionReceipt.hash); | ||
}); | ||
|
||
it("extracts creator address for the upgraded contract", () => { | ||
const result = defaultContractUpgradableHandler.extract(log, transactionReceipt); | ||
expect(result.creatorAddress).toBe(transactionReceipt.from); | ||
}); | ||
|
||
it("extracts logIndex for the upgraded contract", () => { | ||
const result = defaultContractUpgradableHandler.extract(log, transactionReceipt); | ||
expect(result.logIndex).toBe(log.index); | ||
}); | ||
|
||
it("extracts implementation address for the upgraded contract", () => { | ||
const result = defaultContractUpgradableHandler.extract(log, transactionReceipt); | ||
expect(result.implementationAddress).toBe("0xa1810a1f32F4DC6c5112b5b837b6975E56b489cc"); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { types } from "zksync-ethers"; | ||
import { AbiCoder, keccak256 } from "ethers"; | ||
import { ExtractProxyAddressHandler } from "../interface/extractProxyHandler.interface"; | ||
import { ProxyAddress } from "../interface/proxyAddress.interface"; | ||
|
||
const abiCoder: AbiCoder = AbiCoder.defaultAbiCoder(); | ||
|
||
export const encodedUpgradableEvents = [ | ||
"Upgraded(address)", | ||
"BeaconUpgraded(address)", | ||
"OwnershipTransferred(address,address)", | ||
"AdminChanged(address,address)", | ||
"OwnershipTransferred(address,address)", | ||
]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
const decodedUpgradableEvents = encodedUpgradableEvents.map((event) => `${keccak256(Buffer.from(event)).toString()}`); | ||
|
||
export const defaultContractUpgradableHandler: ExtractProxyAddressHandler = { | ||
matches: (log: types.Log): boolean => { | ||
return decodedUpgradableEvents.includes(log.topics[0]); | ||
}, | ||
extract: (log: types.Log, txReceipt: types.TransactionReceipt): ProxyAddress => { | ||
if (!log.topics[1]) { | ||
return null; | ||
} | ||
|
||
const [address] = abiCoder.decode(["address"], log.topics[1]); | ||
return { | ||
address: log.address, | ||
blockNumber: txReceipt.blockNumber, | ||
transactionHash: txReceipt.hash, | ||
creatorAddress: txReceipt.from, | ||
logIndex: log.index, | ||
implementationAddress: address, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After the latest changes were merged from main, the TS compiler complains that the |
||
}; | ||
}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./default.handler"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { types } from "zksync-ethers"; | ||
import { ProxyAddress } from "./proxyAddress.interface"; | ||
|
||
export interface ExtractProxyAddressHandler { | ||
matches: (log: types.Log) => boolean; | ||
extract: (log: types.Log, txReceipt: types.TransactionReceipt) => ProxyAddress | null; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { ContractAddress } from "../../address/interface/contractAddress.interface"; | ||
|
||
export type ProxyAddress = ContractAddress & { | ||
implementationAddress: string; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { Test } from "@nestjs/testing"; | ||
import { Logger } from "@nestjs/common"; | ||
import { mock } from "jest-mock-extended"; | ||
import { types } from "zksync-ethers"; | ||
|
||
import { UpgradableService } from "./upgradable.service"; | ||
describe("UpgradableService", () => { | ||
let upgradableService: UpgradableService; | ||
|
||
beforeEach(async () => { | ||
const app = await Test.createTestingModule({ | ||
providers: [UpgradableService], | ||
}).compile(); | ||
|
||
app.useLogger(mock<Logger>()); | ||
|
||
upgradableService = app.get<UpgradableService>(UpgradableService); | ||
}); | ||
|
||
describe("getUpgradableAddresses", () => { | ||
const logs = [ | ||
mock<types.Log>({ | ||
topics: [ | ||
"0x290afdae231a3fc0bbae8b1af63698b0a1d79b21ad17df0342dfb952fe74f8e5", | ||
"0x000000000000000000000000c7e0220d02d549c4846a6ec31d89c3b670ebe35c", | ||
"0x0100014340e955cbf39159da998b3374bee8f3c0b3c75a7a9e3df6b85052379d", | ||
"0x000000000000000000000000dc187378edd8ed1585fb47549cc5fe633295d571", | ||
], | ||
index: 1, | ||
}), | ||
mock<types.Log>({ | ||
topics: [ | ||
"0xbc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b", | ||
"0x0000000000000000000000000db321efaa9e380d0b37b55b530cdaa62728b9a3", | ||
], | ||
address: "0xdc187378edD8Ed1585fb47549Cc5fe633295d571", | ||
index: 2, | ||
}), | ||
mock<types.Log>({ | ||
topics: [ | ||
"0x8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e0", | ||
"0x000000000000000000000000481e48ce19781c3ca573967216dee75fdcf70f54", | ||
], | ||
address: "0xD144ca8Aa2E7DFECD56a3CCcBa1cd873c8e5db58", | ||
index: 3, | ||
}), | ||
]; | ||
|
||
const transactionReceipt = mock<types.TransactionReceipt>({ | ||
blockNumber: 10, | ||
hash: "transactionHash", | ||
from: "from", | ||
}); | ||
|
||
it("returns upgradable addresses", async () => { | ||
const upgradableAddresses = await upgradableService.getUpgradableAddresses(logs, transactionReceipt); | ||
expect(upgradableAddresses).toStrictEqual([ | ||
{ | ||
address: "0xdc187378edD8Ed1585fb47549Cc5fe633295d571", | ||
implementationAddress: "0x0Db321EFaa9E380d0B37B55B530CDaA62728B9a3", | ||
blockNumber: transactionReceipt.blockNumber, | ||
transactionHash: transactionReceipt.hash, | ||
creatorAddress: transactionReceipt.from, | ||
logIndex: logs[1].index, | ||
}, | ||
{ | ||
address: "0xD144ca8Aa2E7DFECD56a3CCcBa1cd873c8e5db58", | ||
implementationAddress: "0x481E48Ce19781c3cA573967216deE75FDcF70F54", | ||
blockNumber: transactionReceipt.blockNumber, | ||
transactionHash: transactionReceipt.hash, | ||
creatorAddress: transactionReceipt.from, | ||
logIndex: logs[2].index, | ||
}, | ||
]); | ||
}); | ||
|
||
it("returns an empty array if no logs specified", async () => { | ||
const result = await upgradableService.getUpgradableAddresses(null, transactionReceipt); | ||
expect(result).toStrictEqual([]); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add test examples that test all event types that the handler processes. Currently only
0xbc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b
is covered. Ideally, I'd use real log examples.