簡體   English   中英

ethers.js 中的 attach 方法有什么作用?

[英]What does the attach method do in ethers.js?

根據 ethers.js 文檔:

contract.attach(addressOrName) ⇒ 合約

返回附加到新地址的合同的新實例。 如果網絡上有多個相似或相同的合同副本並且您希望與每個副本進行交互,這將很有用。

但是我真的不明白它的用途。 有人可以以更易於理解的方式解釋它嗎? 以下是 CTF 挑戰 DamnVulnerableDefi 的“妥協”級別中使用的.attach示例。 https://github.com/tinchoabbate/damn-vulnerable-defi/blob/v2.1.0/test/compromised/compromised.challenge.js

this.oracle = await TrustfulOracleFactory.attach(
    await (
        await TrustfulOracleInitializerFactory.deploy(
            sources,
            ['DVNFT', 'DVNFT', 'DVNFT'],
            [INITIAL_NFT_PRICE, INITIAL_NFT_PRICE, INITIAL_NFT_PRICE]
        )
    ).oracle()

這段代碼到底在做什么,為什么需要附加方法?

有人可以以更易於理解的方式解釋它嗎?

Attach 從已部署的合約和現有實例(重用相同的 ABI 和 Signer )創建一個新的合約實例。

這很有用,例如,如果您在不同的區塊鏈上部署了相同的合約,那么當用戶更改網絡時,您不需要重新創建實例。

或者,如果您有一個在同一個網絡上多次部署同一個合約的合約工廠,而不是為每個合約創建一個實例,您可以只從一個實例創建它們,因此您不必通過 go指定 ABI 文件和簽名者的麻煩。

這段代碼到底在做什么,為什么需要附加方法?

我不是專家,也不是 100% 確定這段代碼的作用,因為我沒有那個合約代碼。 但我可以推測以下內容。

 // Returns value of calling the oracle() method of the new contract instance. this.oracle = await TrustfulOracleFactory.attach( // waits for the deployment to finish ( kinda redundat here ). await ( // waits until the contract is deployed, this returns an address. // the values in.deploy(...) are values passed to the constructor of the contract. await TrustfulOracleInitializerFactory.deploy( sources, ['DVNFT', 'DVNFT', 'DVNFT'], [INITIAL_NFT_PRICE, INITIAL_NFT_PRICE, INITIAL_NFT_PRICE] // On the new returned instance, call method Oracle. )).oracle() //--------- Step by Step code --------------- // Deploys contract, returns address. let newContractAddress = await TrustfulOracleInitializerFactory.deploy( sources, ['DVNFT', 'DVNFT', 'DVNFT'], [INITIAL_NFT_PRICE, INITIAL_NFT_PRICE, INITIAL_NFT_PRICE] ); // Get instance from address and previusly created instance ( reuse of the same ABI as TrustfulOracleFactory ) let oracleInstance = await TrustfulOracleFactory.attach(newContractAddress); // Calls oracle method. this.oracle = await oracleInstance.oracle();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM