我正在开发一个众筹DApp,它经常需要通过函数调用来访问合同变量,以便检索它们。
getDeployedCampaigns函数返回已部署的市场活动地址的数组。然后,它们被传递到映射函数,以便创建一个带有相应地址的反应组件
在理想的情况下,我希望能够在竞选合同中检索和使用campaignTitle变量,但我很难找到一种在不破坏整个渲染程序的情况下这样做的方法。
这就是代码当前的工作方式
import { Card, Button } from "semantic-ui-react";
import factory from "../ethereum/factory";
import Layout from "../components/Layout";
import { Link } from "../routes";
class CampaignIndex extends Component {
static async getInitialProps() {
const campaigns = await factory.methods.getDeployedCampaigns().call();
return { campaigns };
}
renderCampaigns() {
const items = this.props.campaigns.map((address) => {
return {
header: address,
description: (
<Link route={`/campaigns/${address}`}>
<a>View Campaign</a>
</Link>
),
fluid: true,
};
});
return <Card.Group items={items} />;
}
render() {
return (
<Layout>
<div>
<h3>Open Campaigns</h3>
<Link route="/campaigns/new">
<a>
<Button
floated="right"
content="Create Campaign"
icon="add circle"
primary
/>
</a>
</Link>
{this.renderCampaigns()}
</div>
</Layout>
);
}
}
export default CampaignIndex;
这就是我试图做的,但根本不起作用
import { Card, Button } from 'semantic-ui-react';
import factory from '../ethereum/factory';
import Layout from '../components/Layout';
import { Link } from '../routes';
import Campaign from '../ethereum/campaign';
class CampaignIndex extends Component {
static async getInitialProps() {
const campaigns = await factory.methods.getDeployedCampaigns().call();
const campaignInfo = Promise.all(
campaigns.map(async (address) => {
const campaign = Campaign(address);
const title = await campaign.methods.campaignTitle().call();
return {
header: title,
meta: address,
description: (
<Link route={`/campaigns/${address}`}>
<a>View Campaign</a>
</Link>
),
fluid: true,
};
})
);
return { campaignInfo };
}
renderCampaigns() {
const items = this.props.campaignInfo;
return <Card.Group items={items} />;
}
render() {
return (
<Layout>
<div>
<h3>Open Campaigns</h3>
<Link route="/campaigns/new">
<a>
<Button
floated="right"
content="Create Campaign"
icon="add circle"
primary
/>
</a>
</Link>
{this.renderCampaigns()}
</div>
</Layout>
);
}
}
export default CampaignIndex;
智能合约让你看到
contract CampaignFactory {
address[] public deployedCampaigns;
function createCampaign(uint minimum, string title, string description) public {
address newCampaign = new Campaign(minimum, msg.sender, title, description);
deployedCampaigns.push(newCampaign);
}
function getDeployedCampaigns() public view returns (address[]) {
return deployedCampaigns;
}
}
contract Campaign {
struct Request {
string description;
uint value;
address recipient;
bool complete;
uint approvalCount;
mapping(address => bool) approvals;
}
Request[] public requests;
address public manager;
uint public minimumContribution;
string public campaignTitle;
string public campaignDescription;
mapping(address => bool) public approvers;
uint public approversCount;
modifier restricted() {
require(msg.sender == manager);
_;
}
function Campaign(uint minimum, address creator, string givenTitle, string givenDescription) public {
manager = creator;
minimumContribution = minimum;
campaignTitle = givenTitle;
campaignDescription = givenDescription;
}
function contribute() public payable {
require(msg.value > minimumContribution);
approvers[msg.sender] = true;
approversCount++;
}
function createRequest(string description, uint value, address recipient) public restricted {
Request memory newRequest = Request({
description: description,
value: value,
recipient: recipient,
complete: false,
approvalCount: 0
});
requests.push(newRequest);
}
function approveRequest(uint index) public {
Request storage request = requests[index];
require(approvers[msg.sender]);
require(!request.approvals[msg.sender]);
request.approvals[msg.sender] = true;
request.approvalCount++;
}
function finalizeRequest(uint index) public restricted {
Request storage request = requests[index];
require(request.approvalCount > (approversCount / 2));
require(!request.complete);
request.recipient.transfer(request.value);
request.complete = true;
}
function getSummary() public view returns (
uint, uint, uint, uint, address
) {
return (
minimumContribution,
this.balance,
requests.length,
approversCount,
manager
);
}
function getRequestsCount() public view returns (uint) {
return requests.length;
}
}
在工厂合同中,您直接返回一个项目数组,这意味着您使用的是更新版本的solidity。然而,在Campaign合同中,您使用的是function Campaign
,它在早期版本的solidity中用作构造函数。如果您使用的是较新版本的solidity,则使用constructor
关键字初始化契约,因为您不使用constructor
,所以不会为title
赋值。
使用constructor
代替function Campaign
constructor(uint minimum, address creator, string givenTitle, string givenDescription) {
manager = creator;
minimumContribution = minimum;
campaignTitle = givenTitle;
campaignDescription = givenDescription;
}
我认为这是因为您没有在getSummary函数中检索CampaignName。