說明

zkSync 可以使用 hardhat-zksync-verify 工具,來讓合約在瀏覽器上開源,通常可以使用下面指令進行驗證:

1
npx hardhat verify --network zkSync 合約地址

例如使用合約地址 0xdD5AE451A75a654146747235FDB515F06A55D018,為一個 TransparentUpgradeableProxy 的合約,嘗試使用下面指令進行驗證:

1
npx hardhat verify --network zkSync 0xdD5AE451A75a654146747235FDB515F06A55D018

結果會出現下面錯誤:

1
2
3
Error in plugin @matterlabs/hardhat-zksync-verify: The number of constructor arguments you provided (0) does not match the number of constructor arguments the contract has been deployed with (3).

For more info run Hardhat with --show-stack-traces

這是因為 TransparentUpgradeableProxy 建立的時候有 constructor 參數,所以必須填入參數,我們要先找出參數。

找出參數

方法一

用瀏覽器來找出,在瀏覽器查詢合約地址,例如範例合約,在上面找到
參數

點擊右邊的 0x007bc04b…4333,然後找到下面區塊
參數

最後一個 _input 裡面的 DATA 就是要填入的參數

1
0x00000000000000000000000064728622ac581eb13a335c4b6177bd33bb6c6911000000000000000000000000b5bbcbd781aad5c28ee804491f26f1aa75f59d4e00000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000

方法二

如果知道 Implementation 和 Proxy Admin,且後面沒有其他資料的話,可以直接手動組出來,例如:

  • Implementation:0x64728622ac581eb13a335c4b6177bd33bb6c6911
  • Proxy Admin:0xe8f72249c379abc2cb01f5dfe083554fb9951c14

把地址長度增加到 32 bytes,後面再加上一段空白 bytes

1
00000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000

可以組出

1
0x00000000000000000000000064728622ac581eb13a335c4b6177bd33bb6c6911000000000000000000000000b5bbcbd781aad5c28ee804491f26f1aa75f59d4e00000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000

填入參數

將著將參數存成 arguments.js

1
module.exports = '0x00000000000000000000000064728622ac581eb13a335c4b6177bd33bb6c6911000000000000000000000000b5bbcbd781aad5c28ee804491f26f1aa75f59d4e00000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000‘;

輸入

1
npx hardhat verify --network zkSync 0xdD5AE451A75a654146747235FDB515F06A55D018 --constructor-args arguments.js

應該會成功看到類似下面結果

1
2
Your verification ID is: 39550
Contract successfully verified on zkSync block explorer!

其他問題

如果出現下面的錯誤

1
2
3
4
5
6
7
8
An unexpected error occurred:

[Error: ENOENT: no such file or directory, open './artifacts-zk/build-info/316c81a48085edbfe9bc719c2e5a16c7.json'] {
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: './artifacts-zk/build-info/316c81a48085edbfe9bc719c2e5a16c7.json'
}

這是 cache 中沒有當初編譯部署的合約相關資料,清除 cache 重新產生即可。

1
rm -rf artifacts-zk