首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >transferFrom不工作

transferFrom不工作
EN

Ethereum用户
提问于 2018-02-04 16:43:53
回答 1查看 1.2K关注 0票数 0

我正在尝试创建一个简单的airDrop合同。但不管我做什么似乎都没有用。

我的定制合同和齐柏林飞艇-坚固合同的象征。

这是我用于空投资金的代码:

代码语言:javascript
复制
function distribute(
    uint _airDropID
) public
{
    for (uint i = 0; i < airDrops[_airDropID].participantCount; i++) {
        TokenDrop(msg.sender, airDrops[_airDropID].participants[i], airDrops[_airDropID].amount);

        ERC20(token).transfer(airDrops[_airDropID].participants[i], airDrops[_airDropID].amount);

        //token.mint(airDrops[_airDropID].participants[i], airDrops[_airDropID].amount);
    }
}

我也试着去筹集资金,但是这两个函数都会产生一个VM Exception while processing transaction: revert

这就说明了一个要求()正在失败。我尝试进一步调试我的代码,似乎msg.sender地址在transfer()或mint()函数中发生了变化。

这是我目前正在执行的测试。

代码语言:javascript
复制
let AirDrop = artifacts.require("./AirDrop.sol");
let GameToken = artifacts.require("./GameToken.sol");

//TestData
let gameTokenMarketCap = 1*1000*1000*1000;
let amountToMint = 1*1000*1000;
let amountToApprove = 1*1000*1000*1000*1000;
let amountToDrop = 1000;
let airDropParticipants = 2;

//Expected Test Results
let expectedAirDropID = 0;

contract('Airdrop', (accounts) => {
    let tokenContract;
    let airDropContract;
    let participantID;
    let dropper = accounts[0];
    let receiver = accounts[1];
    let receiver2 = accounts[2];

    /**
     * Initial setup. 
     * Mint tokens to dropper address and check it.
     */
    before(() => {
        return GameToken.new(gameTokenMarketCap, dropper, {from: dropper}).then((instance) => {
            tokenContract = instance;

            return AirDrop.new(tokenContract.address, dropper, {from: dropper});
        }).then((instance) => {
            airDropContract = instance;

            return tokenContract.mint(dropper, amountToMint).then(() => {
                return tokenContract.balanceOf.call(dropper);
            }).then((balance) => {
                console.log(balance.valueOf());
                assert.equal(balance.valueOf(), amountToMint, "Token dropper balance should equal minted balance!");
            });
        });
    });

    describe('Creation and Execution', () => {
        let airDropID;

        it('Allows creating an airDrop and increments the ID', async () => {
            //Firstly test with .call
            airDropContract.createAirDrop.call(airDropParticipants, amountToDrop).then((airDrop) => {
                airDropID = airDrop;

                assert.equal(airDropID.valueOf(), expectedAirDropID, "AirDrop ID not correctly created!");
            });

            //Execute on blockchain
            await airDropContract.createAirDrop(airDropParticipants, amountToDrop);

            return true;
        });

        //Add an participant to the created airDrop
        it('Allows adding a participant to the AirDrop', () => {
            airDropContract.addParticipantToAirDrop.call(airDropID, receiver).then(participantID => {
                assert.equal(participantID.valueOf(), expectedAirDropID, "Participant ID not correctly created!");
            });

            airDropContract.addParticipantToAirDrop(airDropID, receiver);
            airDropContract.addParticipantToAirDrop(airDropID, receiver2);

            return true;
        });

        it('Allows distributing an airDrop and sends the right amount of credits to both participants', async () => {
            //Distribute and check the airDrop
            return airDropContract.distribute(airDropID, {from: dropper}).then(success => {
                return tokenContract.balanceOf.call(receiver);
            }).then(balance => {
                assert.equal(balance.valueOf(), amountToDrop, "airDrop was not executed correctly.");

                return tokenContract.balanceOf.call(receiver2);
            }).then((balance) => {
                assert.equal(balance.valueOf(), amountToDrop, "airDrop was not executed correctly.");

                return tokenContract.balanceOf.call(dropper);
            }).then((balance) => {
                assert.equal(balance.valueOf(), amountToMint - amountToDrop * 2);

                return true;
            });
        });
    });
});

这是一个失败的函数:airDropContract.distribute(airDropID, {from: dropper})

有谁知道为什么msg.sender的价值在变化,为什么空投不起作用?

提前谢谢。

EN

回答 1

Ethereum用户

回答已采纳

发布于 2018-02-04 17:12:13

问题解决了,我不得不批准合同,代表我寄送代币。

tokenContract.approve(airDropContract.address, amountToApprove);

票数 3
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/38514

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档