Ethereum 与来自其他合同的合同进行交互以太坊

Ethereum 与来自其他合同的合同进行交互以太坊,ethereum,smartcontracts,Ethereum,Smartcontracts,我有一个问题,一周来我一直在努力解决,我想我已经解决了90% 如果我在私有区块链上部署合同MerchantA,并通过solidity命令行solc--ABI-MerchantA.sol检索其合同地址和ABI,并将其存储 在全新合同中,我在何处输入此ABI和地址,比如在SendMoneyContract方法中,在该方法中调用部署在地址0xrandom处的AnimalContract函数之一 我在网上找到的材料是在同一个文件中包含两个solidity源代码,但就我而言,我不能这样做。原因是Merch

我有一个问题,一周来我一直在努力解决,我想我已经解决了90%

如果我在私有区块链上部署合同
MerchantA
,并通过solidity命令行
solc--ABI-MerchantA.sol
检索其合同
地址
ABI
,并将其存储

全新合同中,我在何处输入此ABI和地址,比如在
SendMoneyContract
方法中,在该方法中调用部署在地址
0xrandom
处的AnimalContract函数之一

我在网上找到的材料是在同一个文件中包含两个solidity源代码,但就我而言,我不能这样做。原因是
MerchantAContract
对于每个部署都是唯一的[添加的每个商户都会获得唯一的合同'


到目前为止,据我所知,我需要包括
MerchantA
合同地址和ABI。我不知道如何在solidity函数中执行该操作。

您不需要对ABI执行任何操作。 假设您想从MerchantB调用MerchantA上的
function
函数,您所做的只是从MerchantA获取接口,ERC20令牌接口的示例如下:

contract Token {
  function totalSupply() constant returns (uint256 supply) {}
  function balanceOf(address _owner) constant returns (uint256 balance) {}
  function transfer(address _to, uint256 _value) returns (bool success) {}
  function transferFrom(address _from, address _to, uint256 _value) returns (bool success) {}
  function approve(address _spender, uint256 _value) returns (bool success) {}
  function allowance(address _owner, address _spender) constant returns (uint256 remaining) {}

  event Transfer(address indexed _from, address indexed _to, uint256 _value);
  event Approval(address indexed _owner, address indexed _spender, uint256 _value);

  uint public decimals;
  string public name;
}
在MerchantB上,无论您想在何处调用functionA,都要输入以下代码:

MerchantA merchantA = MerchantA(0x0000000000000000000000000000000000000000); //Replace 0x000000000000000000000000000000000000000 with the address of MerchantA
merchantA.functionA();

您需要调出MerchantA的接口,因为您没有使用ERC20令牌以及要调用的函数。

您不需要对ABI执行任何操作。 假设您想从MerchantB调用MerchantA上的
function
函数,您所做的只是从MerchantA获取接口,ERC20令牌接口的示例如下:

contract Token {
  function totalSupply() constant returns (uint256 supply) {}
  function balanceOf(address _owner) constant returns (uint256 balance) {}
  function transfer(address _to, uint256 _value) returns (bool success) {}
  function transferFrom(address _from, address _to, uint256 _value) returns (bool success) {}
  function approve(address _spender, uint256 _value) returns (bool success) {}
  function allowance(address _owner, address _spender) constant returns (uint256 remaining) {}

  event Transfer(address indexed _from, address indexed _to, uint256 _value);
  event Approval(address indexed _owner, address indexed _spender, uint256 _value);

  uint public decimals;
  string public name;
}
在MerchantB上,无论您想在何处调用functionA,都要输入以下代码:

MerchantA merchantA = MerchantA(0x0000000000000000000000000000000000000000); //Replace 0x000000000000000000000000000000000000000 with the address of MerchantA
merchantA.functionA();

您需要交换MerchantA的接口,因为您没有使用ERC20令牌以及您要调用的函数。

如果我理解正确,我需要在MerchantB中包含MerchantA的整个代码,并使用其地址调用MerchantA的实例。如果我错了,请更正我。最后,是否有MerchantA代码不在MerchantB中但仍然可以使用的一种方法MerchantA中的一个功能,比如说使用前者的付费方法从MerchB向MerchA发送以太。谢谢。我刚刚在remix IDE上进行了测试,它正在工作!但是如果有方法不包括接口,请让我知道。在区块链上保存一个巨大的合同是昂贵的。你知道吗应该在Rinkeby上测试。它是免费的。一旦完成,部署只需要5美元。删除接口不会节省太多。如果那样的话,可能需要0.25美元。@devyJava在接口中,你不需要放置所有函数。只需要你想调用的函数。如果我理解正确,我需要包含MerchantA在MerchantB中,并使用其地址调用MerchantA的实例。如果我错了,请纠正我。最后,有没有办法让MerchantA代码不在MerchantB中,但仍然可以使用MerchantA中的一个功能,比如使用前者的付费方法从MerchB发送乙醚到MerchA。谢谢。我刚刚在remix IDE上测试过,它是w工作!但是如果有办法不包括接口,请让我知道。在区块链上保存一个巨大的合同是昂贵的。你应该在Rinkeby上测试。它是免费的。一旦完成,部署只需要5美元。删除接口不会节省太多。如果那样的话,可能需要0.25美元。@devyJava在接口中,你不需要把所有的功能都放进去只有您希望能够调用的函数。