简体   繁体   English

没有事件发出-交换ERC721代币

[英]No events were emitted - Exchanging ERC721 Tokens

Function to exchange ERC721 Tokens between two addresses. 在两个地址之间交换ERC721令牌的功能。 I am implementing this on truffle and openzeppelin 2.10. 我正在松露和openzeppelin 2.10上实现此功能。 Two different tokens should be exchanged between two addresses. 两个地址之间应交换两个不同的令牌。

Here's my contract function for exchanging ERC721 tokens: 这是我交换ERC721代币的合约功能:

function exchangeStars(uint256 token1, uint256 token2, address starOwner2) public { require(this.ownerOf(token1) == msg.sender); 函数exchangeStars(uint256 token1,uint256 token2,address starOwner2)public {require(this.ownerOf(token1)== msg.sender);

    transferFrom(msg.sender, starOwner2, token1);
    transferFrom(starOwner2, msg.sender, token2);
}

This is the test I am writing for creating Tokens and exchanging between two addresses. 这是我正在编写的用于创建令牌并在两个地址之间交换的测试。

describe('Exchange Stars', () =>  {
    let token1 = 101;
    let token2 = 202;

    it('User 1 creates Star', async() => {
      await this.contract.createStar(starName, story, ra, dec, mag, token1, {from: account1});
      assert.equal(await this.contract.ownerOf.call(token1), account1);
    });

    it('User 2 creates Star', async() => {
      await this.contract.createStar(starName2, story, ra, dec, mag, token2, {from: account2});
      assert.equal(await this.contract.ownerOf.call(token2), account2);
    });

    it('Users exchange Stars', async() => {
      await this.contract.exchangeStars(token1, token2, account2);
      assert.equal(await this.contract.ownerOf.call(token2), account2);
      console.log(await this.contract.ownerOf.call(token2));
    });

  });

Here's the result for my tests: 这是我的测试结果:

 Exchange Stars √ User 1 creates Star (129ms) √ User 2 creates Star (116ms) 1) Users exchange Stars > No events were emitted 

Problem 问题

We want to make a simple ERC-721 contract where owners of tokens can (with unilateral consent) exchange them for any other existing token. 我们想要建立一个简单的ERC-721合同,令牌的所有者可以(单方面同意)将它们交换为任何其他现有令牌。

This implementation must follow ERC-721 standards and emit two Transfer events when this exchange is performed. 此实现必须遵循ERC-721标准,并在执行此交换时发出两个Transfer事件。

Solution

Let's start with a basic implementation based no the reference implementation and including a mint function so that we can create a few tokens to play with: 让我们从不包含参考实现的基本实现开始,并包括一个mint函数,以便我们可以创建一些令牌来玩:

pragma solidity 0.5.1;

import "https://github.com/0xcert/ethereum-erc721/src/contracts/tokens/nf-token.sol";

contract ExchangeableTokens is ERC721 {

    /**
    * @dev Mints a new NFT.
    * @param _to The address that will own the minted NFT.
    * @param _tokenId of the NFT to be minted by the msg.sender.
    */
    function mint(
        address _to,
        uint256 _tokenId
    )
        external
        onlyOwner
    {
        super._mint(_to, _tokenId);
    }
}

Now we can add the desired behavior: 现在我们可以添加所需的行为:

function exchangeStars(uint256 myToken, uint256 theirToken, address them) 
    public
{
    require (idToOwner[myToken] == msg.sender);
    require (idToOwner[theirToken] == them);
    require (them != address(0));
    _transfer(them, myToken);
    _transfer(msg.sender, theirToken);
}

Discussion 讨论

This implementation follows the standard and does emit the events as required. 此实现遵循标准,并且会根据需要发出事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM