为已部署的智能合约创建全球地址簿
Create a global Addressbook for deployed Smart Contracts
我想创建一个智能合约(称为 Deal),它在部署后调用另一个智能合约(称为 AddressBook)在全局 AddressBook 智能合约上注册他的地址。我正在使用的代码有效,但不幸的是我新部署的智能合约没有使用 AddressBook 智能合约中的地址,而是使用他自己的地址来调用 addAddress 函数。如果我在 Deal 合约中调用 addressbook.getAddresses(addresses) ,红色标记显示输出。如果我在 AddressBook 合约中调用 getAddresses 函数,蓝色标记显示输出。所以我想要的是交易合同中的蓝色输出
Image of the Output
谁能告诉我如何将已部署合约的地址添加到全局地址簿合约中?
这是我的 AddressBook 智能合约的样子。我想在此联系人处添加所有已部署的智能合约。
contract AddressBook {
mapping(address => address[]) private _addresses;
mapping(address => mapping(address => string)) private _aliases;
function getAddresses() public view returns (address[]) {
return _addresses[msg.sender];
}
function addAddress(address addr, string _alias) public payable {
_addresses[msg.sender].push(addr);
_aliases[msg.sender][addr] = _alias;
}
function removeAddress(address addr) public payable {
uint length = _addresses[msg.sender].length;
for(uint i = 0; i < length; i++) {
if (addr == _addresses[msg.sender][i]) {
if (1 < _addresses[msg.sender].length && i < length-1) {
_addresses[msg.sender][i] = _addresses[msg.sender][length-1];
}
delete _addresses[msg.sender][length-1];
_addresses[msg.sender].length--;
delete _aliases[msg.sender][addr];
break;
}
}
}
function getAlias(address addr) public view returns (string) {
return _aliases[msg.sender][addr];
}
}
这是我要在全球部署注册的智能合约
AddressBook smart contract:
contract Deal {
AddressBook addressBook;
address public owner;
address public buyerAddr;
address private addr;
string private metaData;
uint private duration;
uint private price;
string private typeOfData;
string private gateKeeper;
event SafepaySent(address buyer, uint value, uint now);
/// The smart contract's constructor
function Deal(address _addressBook) public payable {
/// The seller is the contract's owner
owner = msg.sender;
AddressBook a = AddressBook(_addressBook);
a.addAddress(owner,"test");
}
}
function getAddresses(address _addressBook) public view returns (address[]) {
return AddressBook(_addressBook).getAddresses();
}
}
contract AddressBook {
function getAddresses() public view returns (address[]);
function addAddress(address addr, string _alias) public payable ;
function removeAddress(address addr) public payable;
function getAlias(address addr) public view returns (string);
}
现在我想在 AddressBook 智能合约中调用 AddressBook.getAddresses() 函数,我想接收所有已部署交易合约的地址。但是,我没有收到 AddressBook 智能合约中的任何地址。如果我调用函数 Deal.getAddresses("address of the global Addressbook") 我会得到新部署的智能合约的地址。但这不是我想要的。
首先如果你想使用AddressBook
作为全局智能合约来跟踪和加载所有Deal
合约。您应该已经部署了 AddressBook
合约并保存了它的地址。然后你可以在 Deal
合约的 constructor
中加载 AddressBook
合约,并添加要部署的当前 Deal
合约的地址,在已经部署的全局 AddressBook
契约。我对给定的合同做了一些工作。我在 AddressBook
合约中使用一组地址来跟踪所有 Deal
合约的地址。下面的示例代码可以给你一个好主意。
//Address Book Contract
pragma solidity ^0.5.1;
contract AddressBook {
//mapping(address => address[]) private _addresses;
address[] private _addresses;
mapping(address => mapping(address => string)) private _aliases;
function getAddresses() public view returns (address [] memory) {
return _addresses;
}
function addAddress(address addr, string memory _alias) public payable {
_addresses.push(addr);
_aliases[msg.sender][addr] = _alias;
}
}
我这里只关注_address
。以下是交易合同:
pragma solidity ^0.5.1;
import "./AddressBook.sol";
contract Deal {
AddressBook addressBook;
address public owner;
address public buyerAddr;
address private addr;
string private metaData;
uint private duration;
uint private price;
string private typeOfData;
string private gateKeeper;
event SafepaySent(address buyer, uint value, uint now);
/// The smart contract's constructor
constructor(address _addressBook) public payable {
/// The seller is the contract's owner
owner = msg.sender;
addressBook = AddressBook(_addressBook);
addressBook.addAddress(address(this),"test");
}
function getAddresses() public view returns (address[] memory) {
return addressBook.getAddresses();
}
}
您也可以在混音中测试 运行。希望有用。
我想创建一个智能合约(称为 Deal),它在部署后调用另一个智能合约(称为 AddressBook)在全局 AddressBook 智能合约上注册他的地址。我正在使用的代码有效,但不幸的是我新部署的智能合约没有使用 AddressBook 智能合约中的地址,而是使用他自己的地址来调用 addAddress 函数。如果我在 Deal 合约中调用 addressbook.getAddresses(addresses) ,红色标记显示输出。如果我在 AddressBook 合约中调用 getAddresses 函数,蓝色标记显示输出。所以我想要的是交易合同中的蓝色输出
Image of the Output
谁能告诉我如何将已部署合约的地址添加到全局地址簿合约中?
这是我的 AddressBook 智能合约的样子。我想在此联系人处添加所有已部署的智能合约。
contract AddressBook {
mapping(address => address[]) private _addresses;
mapping(address => mapping(address => string)) private _aliases;
function getAddresses() public view returns (address[]) {
return _addresses[msg.sender];
}
function addAddress(address addr, string _alias) public payable {
_addresses[msg.sender].push(addr);
_aliases[msg.sender][addr] = _alias;
}
function removeAddress(address addr) public payable {
uint length = _addresses[msg.sender].length;
for(uint i = 0; i < length; i++) {
if (addr == _addresses[msg.sender][i]) {
if (1 < _addresses[msg.sender].length && i < length-1) {
_addresses[msg.sender][i] = _addresses[msg.sender][length-1];
}
delete _addresses[msg.sender][length-1];
_addresses[msg.sender].length--;
delete _aliases[msg.sender][addr];
break;
}
}
}
function getAlias(address addr) public view returns (string) {
return _aliases[msg.sender][addr];
}
}
这是我要在全球部署注册的智能合约
AddressBook smart contract:
contract Deal {
AddressBook addressBook;
address public owner;
address public buyerAddr;
address private addr;
string private metaData;
uint private duration;
uint private price;
string private typeOfData;
string private gateKeeper;
event SafepaySent(address buyer, uint value, uint now);
/// The smart contract's constructor
function Deal(address _addressBook) public payable {
/// The seller is the contract's owner
owner = msg.sender;
AddressBook a = AddressBook(_addressBook);
a.addAddress(owner,"test");
}
}
function getAddresses(address _addressBook) public view returns (address[]) {
return AddressBook(_addressBook).getAddresses();
}
}
contract AddressBook {
function getAddresses() public view returns (address[]);
function addAddress(address addr, string _alias) public payable ;
function removeAddress(address addr) public payable;
function getAlias(address addr) public view returns (string);
}
现在我想在 AddressBook 智能合约中调用 AddressBook.getAddresses() 函数,我想接收所有已部署交易合约的地址。但是,我没有收到 AddressBook 智能合约中的任何地址。如果我调用函数 Deal.getAddresses("address of the global Addressbook") 我会得到新部署的智能合约的地址。但这不是我想要的。
首先如果你想使用AddressBook
作为全局智能合约来跟踪和加载所有Deal
合约。您应该已经部署了 AddressBook
合约并保存了它的地址。然后你可以在 Deal
合约的 constructor
中加载 AddressBook
合约,并添加要部署的当前 Deal
合约的地址,在已经部署的全局 AddressBook
契约。我对给定的合同做了一些工作。我在 AddressBook
合约中使用一组地址来跟踪所有 Deal
合约的地址。下面的示例代码可以给你一个好主意。
//Address Book Contract
pragma solidity ^0.5.1;
contract AddressBook {
//mapping(address => address[]) private _addresses;
address[] private _addresses;
mapping(address => mapping(address => string)) private _aliases;
function getAddresses() public view returns (address [] memory) {
return _addresses;
}
function addAddress(address addr, string memory _alias) public payable {
_addresses.push(addr);
_aliases[msg.sender][addr] = _alias;
}
}
我这里只关注_address
。以下是交易合同:
pragma solidity ^0.5.1;
import "./AddressBook.sol";
contract Deal {
AddressBook addressBook;
address public owner;
address public buyerAddr;
address private addr;
string private metaData;
uint private duration;
uint private price;
string private typeOfData;
string private gateKeeper;
event SafepaySent(address buyer, uint value, uint now);
/// The smart contract's constructor
constructor(address _addressBook) public payable {
/// The seller is the contract's owner
owner = msg.sender;
addressBook = AddressBook(_addressBook);
addressBook.addAddress(address(this),"test");
}
function getAddresses() public view returns (address[] memory) {
return addressBook.getAddresses();
}
}
您也可以在混音中测试 运行。希望有用。