Ich habe einen Token-Vertrag bereitgestellt und möchte jetzt mehrere Übertragungen verwenden. Ich dachte mir, dass ich dies tun kann, indem ich einen anderen Vertrag bereitstelle, der eine Iteration über die Übertragungsfunktion meines bereitgestellten Token-Vertrags ausführt.
Aber ich bekomme eine fehlgeschlagene Transaktion. Ich habe auch einen weiteren Test zur Klärung durchgeführt, unten sind die Details:
Beispiel für bereitgestellten Vertrag:
contract MyToken {
/* This creates an array with all balances */
mapping (address => uint256) public balanceOf;
/* Initializes contract with initial supply tokens to the creator of the contract */
function MyToken(
uint256 initialSupply
) {
balanceOf[msg.sender] = initialSupply; // Give the creator all initial tokens
}
/* Send coins */
function transfer(address _to, uint256 _value) {
require(balanceOf[msg.sender] >= _value); // Check if the sender has enough
require(balanceOf[_to] + _value >= balanceOf[_to]); // Check for overflows
balanceOf[msg.sender] -= _value; // Subtract from the sender
balanceOf[_to] += _value; // Add the same to the recipient
}
}
Anrufervertrag Muster:
pragma solidity ^0.4.21;
contract MyToken {function transfer(address _to, uint256 _value) public; }
contract ADTest {
function CallTransfer(address tokenAddress, address _to, uint256 _value) public {
MyToken(tokenAddress).transfer(_to, _value);
}
}
Kann bitte jemand bei der Lösung helfen? Ich bin mir nicht sicher, was ich falsch mache.
UPDATE Ich dachte, wenn ich einige Token an den Anrufervertrag sende, sendet der Anrufervertrag problemlos an verschiedene Adressen. Ich bin mir nicht sicher, ob es vorerst eine geeignete Lösung ist, aber vielleicht wird es jemandem helfen.
Versuchen Sie es so. Kommentare inline.
pragma solidity ^0.4.21;
// added I as naming convention (Interface)
contract MyTokenI {function transfer(address _to, uint256 _value) public; }
contract ADTest {
// will hold an instance cast as type MyToken
MyTokenI myToken;
// pass the deployed MyToken address into this constructor
function ADTest(address _myToken) public {
// instantiate the deployed contract at _myToken
myToken = MyTokenI(_myToken);
}
function CallTransfer(address _to, uint256 _value) public {
// use the instance
myToken.transfer(_to, _value);
}
}
Ich hoffe es hilft.
Nun, zuerst erstellen Sie MyToken
mit Ihrem Benutzer 0x1234
dann Adressaufrufe 0x1234...
ADTest.CallTransfer(). MyToken
Der bereitgestellte Vertrag prüft den Saldo von msg.sender
. Aber msg.sender
ist Vertrag ADTest
, nicht Benutzer "0x1234". Wird also require(balanceOf[msg.sender] >= _value);
fehlschlagen und die Transaktion wird zurückgesetzt.
Hier ist mein Vorschlag, der auch Robs Vorschlag verwendet:
contract MyToken {
/* This creates an array with all balances */
mapping (address => uint256) public balanceOf;
/* Initializes contract with initial supply tokens to the creator of the contract */
function MyToken(
uint256 initialSupply
) {
balanceOf[msg.sender] = initialSupply; // Give the creator all initial tokens
}
/* Send coins */
function transfer(address sender, address _to, uint256 _value) {
require(balanceOf[sender] >= _value); // Check if the sender has enough
require(balanceOf[_to] + _value >= balanceOf[_to]); // Check for overflows
balanceOf[sender] -= _value; // Subtract from the sender
balanceOf[_to] += _value; // Add the same to the recipient
}
}
contract MyTokenI {function transfer(address sender, address _to, uint256 _value) public; }
contract ADTest {
// will hold an instance cast as type MyToken
MyTokenI myToken;
// pass the deployed MyToken address into this constructor
function ADTest(address _myToken) public {
// instantiate the deployed contract at _myToken
myToken = MyTokenI(_myToken);
}
function CallTransfer(address _to, uint256 _value) public {
// use the instance
myToken.transfer(msg.sender, _to, _value);
}
}
MyToken
übertragen, der bereitgestellt wird, da dies erforderlich ist:balanceOf[msg.sender] = initialSupply;
Wajid Khilji