在两个地址之间交换 ERC721 令牌的功能。我在 truffle 和 openzeppelin 2.10 上实现了这个。应该在两个地址之间交换两个不同的令牌。
这是我交换 ERC721 代币的合约函数:
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);
}
这是我为创建令牌和在两个地址之间交换而编写的测试。
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));
});
});
这是我的测试结果:
Exchange Stars √ User 1 creates Star (129ms) √ User 2 creates Star (116ms) 1) Users exchange Stars > No events were emitted