Function to exchange ERC721 Tokens between two addresses. I am implementing this on truffle and openzeppelin 2.10. Two different tokens should be exchanged between two addresses.
Here's my contract function for exchanging ERC721 tokens:
function 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