繁体   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