![](/img/trans.png)
[英]Call function of smart contract after deploying smart contract in Solidity
[英]Solidity - How to call smart contract instances from external contract?
我創建了一個AccountManager智能合約及其兩個實例(PARTYA和PARTYB)。 當調用TransferAgent.transfer(AccountManager.address,AccountManager.address)時,我可以按預期看到帳戶[msg.sender] .balance更新。 但是,在調用實例(PARTYA和PARTYB)時,例如TransferAgent.transfer(PARTYA.address,PARTYB.address),余額中沒有反映出任何變化。
我花了一些時間研究如何使用地址從TransferAgent(外部合同)調用AccountManager(實例),但找不到任何特定於此的內容。 我無法平衡反映變化。 有什么建議?
環境 - 以太坊 - 開始框架 - 堅持
我的設置如下
contracts.json
{
"default": {
"gas": "auto",
"contracts": {
"SimpleStorage": {
"args": [
100
]
},
"Agent" : {
"args": [
]
},
"AccountManager" : {
"args": [
]
},
"PARTYA" : {
"instanceOf" : "AccountManager",
"args" : [
]
},
"PARTYB" : {
"instanceOf" : "AccountManager",
"args" : [
]
},
"TransferAgent" : {
"args": [
]
}
}
}
}
Agent.sol
pragma solidity ^0.4.0;
contract Agent {
/* Define variable owner of the type address*/
address owner;
/* this function is executed at initialization and sets the owner of the contract */
function Agent() { owner = msg.sender; }
/* Function to recover the funds on the contract */
function kill() { if (msg.sender == owner) selfdestruct(owner); }
}
contract AccountManager is Agent {
enum ACTIVE { Y, N }
enum STATUS { CREDIT, DEBIT }
mapping (address => Account) public accounts;
struct Account {
bytes32 ssn;
int balance;
ACTIVE active;
STATUS status;
}
modifier withdrawValidation(int withdrawAmt) {
if( withdrawAmt <= accounts[msg.sender].balance) {
throw;
}
_;
}
// Check for current account matching
modifier transferValidation(address _from, address _to, bytes32 _ssn) {
if( AccountManager(_from).getSSN() != _ssn ||
AccountManager(_to).getSSN() != _ssn ||
AccountManager(_from).getStatus() == STATUS.CREDIT ) {
throw;
}
_;
}
function register(bytes32 _ssn) public returns (bool success) {
Account memory newRegistree;
newRegistree.ssn = _ssn;
newRegistree.balance = 0;
newRegistree.active = ACTIVE.Y;
newRegistree.status = STATUS.DEBIT;
accounts[msg.sender] = newRegistree;
return true;
}
function update(bytes32 _ssn) public returns (bool success) {
accounts[msg.sender].ssn = _ssn;
return true;
}
function deposit(int _depositAmt) public returns(bool success) {
accounts[msg.sender].balance += _depositAmt;
return true;
}
function withdraw(int _withdrawAmt) public returns(bool success) {
accounts[msg.sender].balance = (accounts[msg.sender].balance - _withdrawAmt);
return true;
}
function getBalance() public constant returns(int balance) {
return accounts[msg.sender].balance;
}
function setBalance(int _balance) external returns(bool success) {
accounts[msg.sender].balance = _balance;
return true;
}
function setStatus() internal {
if(accounts[msg.sender].balance >= 0)
accounts[msg.sender].status = STATUS.DEBIT;
else
accounts[msg.sender].status = STATUS.CREDIT;
}
function getStatus() external constant returns (STATUS status) {
return accounts[msg.sender].status;
}
function getSSN() external constant returns(bytes32 ssn) {
return accounts[msg.sender].ssn;
}
function getAccount() public constant returns (bytes32 ssn, int balance, ACTIVE active, STATUS status) {
return (accounts[msg.sender].ssn, accounts[msg.sender].balance, accounts[msg.sender].active, accounts[msg.sender].status);
}
}
contract TransferAgent is Agent {
function transfer(address _from, address _to) public returns (bool success) {
AccountManager(_from).setBalance(100); // not working for instances PARTYA,PARTYB
AccountManager(_to).setBalance(200); // not working for instances PARTYA,PARTYB }
}
這是一個極簡主義的例子,專注於將交易發送到另一個合約。
“Hub”將部署兩個“Spoke”,然后您可以使用function local()
將消息發送到其中一個輻條。 雙方都記錄了事件,因此您可以看到正在發生的事情。
contract Hub {
Spoke public A;
Spoke public B;
event LogTXNSent(address spoke);
function Hub() {
A = new Spoke();
B = new Spoke();
}
function local(address spokeAddress)
public
returns(bool success)
{
if(spokeAddress != address(A) && spokeAddress != address(B)) throw;
if(!Spoke(spokeAddress).logEvent()) throw;
LogTXNSent(spokeAddress);
return true;
}
}
contract Spoke {
event LogTXNFrom(address sender);
function logEvent()
public
returns(bool success)
{
LogTXNFrom(msg.sender);
return true;
}
}
希望能幫助到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.