简体   繁体   English

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

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

According to ethers.js documentation:根据 ethers.js 文档:

contract.attach( addressOrName ) ⇒ Contract contract.attach(addressOrName) ⇒ 合约

Returns a new instance of the Contract attached to a new address.返回附加到新地址的合同的新实例。 This is useful if there are multiple similar or identical copies of a Contract on the network and you wish to interact with each of them.如果网络上有多个相似或相同的合同副本并且您希望与每个副本进行交互,这将很有用。

But I don't really understand the use of it.但是我真的不明白它的用途。 Can someone explain it in a more easy-to-follow manner?有人可以以更易于理解的方式解释它吗? Here's an example of .attach in use found in "Compromise" level of CTF challenge DamnVulnerableDefi.以下是 CTF 挑战 DamnVulnerableDefi 的“妥协”级别中使用的.attach示例。 ( https://github.com/tinchoabbate/damn-vulnerable-defi/blob/v2.1.0/test/compromised/compromised.challenge.js ) 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()

What is this code exactly doing and why is the attach method needed?这段代码到底在做什么,为什么需要附加方法?

Can someone explain it in a more easy-to-follow manner?有人可以以更易于理解的方式解释它吗?

Attach creates a new contract instance from an already deployed contract, and from an existing instance ( reuses the same ABI and Signer ). Attach 从已部署的合约和现有实例(重用相同的 ABI 和 Signer )创建一个新的合约实例。

This is useful for example, if you have the same contract deployed on different blockchains, so you won't need to recreate the instance when the user changes to the network.这很有用,例如,如果您在不同的区块链上部署了相同的合约,那么当用户更改网络时,您不需要重新创建实例。

Or if you have a contract factory that deploys the same contract several times on the same network, and instead of creating an instance of each one of them, you can create them all from just one instance, so you don't have to go through the hassle of specifying the ABI file and the Signer.或者,如果您有一个在同一个网络上多次部署同一个合约的合约工厂,而不是为每个合约创建一个实例,您可以只从一个实例创建它们,因此您不必通过 go指定 ABI 文件和签名者的麻烦。

What is this code exactly doing and why is the attach method needed?这段代码到底在做什么,为什么需要附加方法?

I am not an expert, nor I'm 100% sure what this code does since I don't have that contracts code.我不是专家,也不是 100% 确定这段代码的作用,因为我没有那个合约代码。 Yet I can speculate the following.但我可以推测以下内容。

 // 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