83 lines
3.0 KiB
JavaScript
83 lines
3.0 KiB
JavaScript
const ShardAssembler = artifacts.require("ShardAssembler");
|
|
const NFT = artifacts.require("NFT");
|
|
const Shard = artifacts.require("BEShard");
|
|
const FT = artifacts.require("FT");
|
|
const { BN,expectEvent } = require("@openzeppelin/test-helpers");
|
|
const { expect } = require("chai");
|
|
|
|
contract("ShardAssembler", (accounts) => {
|
|
const admin = accounts[0];
|
|
const user = accounts[1];
|
|
const executor = accounts[0];
|
|
let hero;
|
|
let shardAssembler;
|
|
let shard;
|
|
let payToken;
|
|
|
|
let ids = [1, 2, 3];
|
|
let amounts = [2, 2, 2];
|
|
|
|
beforeEach(async () => {
|
|
hero = await NFT.new("CRYPTO ELITE'S HERO", "HERO", 0);
|
|
// Deploy the mock Shard contract
|
|
shard = await Shard.new();
|
|
// Deploy the ShardAssembler contract
|
|
shardAssembler = await ShardAssembler.new([hero.address, shard.address]);
|
|
payToken = await FT.new("FT", "FT", 0);
|
|
await payToken.setMintRole(admin);
|
|
await payToken.mint(user, '100000000000000000000000000');
|
|
await hero.setMintRole(shardAssembler.address);
|
|
await shard.setMintRole(admin);
|
|
await shard.setApprovalForAll(shardAssembler.address, true);
|
|
await shardAssembler.updateExecutor(executor);
|
|
await shardAssembler.setFeeToAddress(admin);
|
|
await shardAssembler.addERC20Support(payToken.address);
|
|
});
|
|
|
|
it("should mint shards correctly", async () => {
|
|
await shard.mintBatch(user, ids, amounts, []);
|
|
const userShardBalance = await shard.balanceOfBatch([user, user, user], ids);
|
|
assert.equal(userShardBalance[0], amounts[0], "User's shard balance is incorrect");
|
|
});
|
|
|
|
it("should assemble shards correctly", async () => {
|
|
// Call the assembleShard function
|
|
await shard.mintBatch(user, ids, amounts, []);
|
|
let tokenAmount = 1;
|
|
let tokenId = '100';
|
|
const startTime = Date.now() / 1000 | 0;
|
|
const saltNonce = (Math.random() * 1000) | 0;
|
|
let signArr = [];
|
|
for (let i = 0; i < ids.length; i++) {
|
|
signArr.push(ids[i]);
|
|
signArr.push(amounts[i]);
|
|
}
|
|
let signStr = web3.utils.soliditySha3.apply(this,
|
|
[user, hero.address, shardAssembler.address, tokenId, payToken.address,
|
|
tokenAmount,
|
|
startTime, saltNonce, ...signArr]);
|
|
let signature = await web3.eth.sign(signStr, executor);
|
|
signature = signature.replace(/00$/, "1b").replace(/01$/, "1c");
|
|
await payToken.approve(shardAssembler.address, tokenAmount, { from: user });
|
|
await shard.setApprovalForAll(shardAssembler.address, true, { from: user });
|
|
const receipt = await shardAssembler.assembleShard(
|
|
[tokenId, payToken.address, tokenAmount, startTime, saltNonce],
|
|
ids,
|
|
amounts,
|
|
signature,
|
|
{ from: user }
|
|
);
|
|
expectEvent(receipt, "ShardAssembled", {
|
|
to: user,
|
|
tokenId: tokenId,
|
|
payToken: payToken.address,
|
|
tokenAmount: new BN(tokenAmount),
|
|
});
|
|
// Get the user's hero balance
|
|
const userHeroBalance = await hero.balanceOf(user);
|
|
expect(userHeroBalance).to.be.bignumber.equal(new BN(1));
|
|
});
|
|
|
|
// Add more test cases as needed
|
|
});
|