在创建一个大型项目时,我试图理解正确的Deploy方法,然后是Import、multiple Smart-Contracts和Library。
例如,该项目将具有以下Library和三个Smart-Contracts.
我们首先:
pragma solidity >=0.4.0 <0.8.0;
library PizzaLib {
struct Pizza {
uint256 pieSize;
string ingredients;
}
function mix(Pizza calldata pizza, uint256 pSize, string theIngredients) external pure returns (Pizza memory) {
Pizza memory newPizza = pizza;
newPizza.pieSize = pSize;
newPizza.ingredients = theIngredients;
return newPizza;
}
}这个library将被所有三个即将到来的智能契约所使用(也许还会有更多的合同),因此我将独立地部署它。这样,它就坐在Blockchain上的自己的地址上,任何合同都可以访问它(我相信这就是它的工作方式--但如果我说错了,请告诉我。)
接下来是:合同#1
// Note that I'm putting an "import" statement here for the Library I just created,
// but do I actually need this statement? Or will my migration/deployment
// file - below - take care of linking the Library to this Contract
// anyway? (That's question #1)
//
// For now I'll put the import statement in:
import "PizzaLib.sol";
using PizzaLib for PizzaLib.Pizza;
contract MakePizza {
// -Has functions to make various Types of Pizza
// etc.
}现在我们继续谈合同#2
这个合同还需要使用“库”,所以问题#1也适用于这里(我是否import?)
但是这里我还有一个问题--问题2--这个合同不仅需要访问‘库’,还需要契约#1 (MakePizza),它是独立部署的,这意味着它也驻留在Blockchain上自己独特的地址中。
那么,如何在下面的MakePizza契约中实例化DeliverPizza呢?
我认为此时我需要不使用import语句,也不需要使用is语法,如下所示:
import "MakePizza.sol";
contract DeliverPizza is MakePizza {
. . .
}相反,我应该按照以下方式创建契约--省略import和is MakePizza语句:
contract DeliverPizza {
// This contract only handles the Deliveries of Pizzas. So it:
// 1. Defines a “Customer” Struct (customer’s name, phone number, etc.)
// 2. Defines a “CustomerAddress” Struct (street address, city, zip-code, etc.)
// 3. Has “delivery” type functions
}接下来是合同#3
这个家伙需要引用/实例化我们到目前为止所做的一切:PizzaLib库以及MakePizza和DeliverPizza契约:
Contract HandleTheMoney {
// Defines all things having to do with funds:
// 1. Acceptable Payment methods
// 2. Defines various Fees
// 3. Declares the Permissions re who’s allowed to withdraw funds from the contract, etc.
}这里的问题是:如何实例化/引用合同中的其他合同?
最后一份合同是4号合同。
同样的事情。到目前为止,它需要参考我们所说的一切。通常我会使用is语法,如下所示。
contract JoesPizzaShop is MakePizza, DeliverPizza, HandleTheMoney {
// The “master” contract, bringing everything together
}...but --我不认为这是独立部署所有合同的方式,所以应该是:
contract JoesPizzaShop {
// Need to instantiate all the other contracts here...
}最后,我们有了migration文件--我很确定我做得对:
module.exports = async function(deployer, network, accounts) {
await deployer.deploy(PizzaLib, { overwrite: false });
await deployer.link(PizzaLib, [MakePizza, DeliverPizza, HandleTheMoney, JoesPizzaShop]);
await deployer.deploy(MakePizza);
await deployer.deploy(DeliverPizza);
await deployer.deploy(HandleTheMoney);
await deployer.deploy(JoesPizzaShop);
}我在那里做的是:
PizzaLib。然后,Linking给我的所有contracts --它们都列在array中。最后,contracts中的每一个,每次一个。所以,让我重温一下我的问题:
import?using PizzaLib for PizzaLib.Pizza;放在每个contract中吗?is声明语句中使用contract语句?contracts中引用/实例化不同的方法,以便从一个方法调用另一个方法?发布于 2021-08-13 13:04:30
我认为这里的主要问题是如何构造代码:
如果您想使用不同级别的复杂性(继承),那么:
is如果您希望有单独的合同,也可以单独使用:
is有关如何处理库的更多信息:https://blockheroes.dev/import-external-contracts-libraries/
https://ethereum.stackexchange.com/questions/91151
复制相似问题