1234567891011121314151617import {createCollectionExpectSuccess, transfer, UNIQUE} from '../util/helpers';18import {collectionIdToAddress, createEthAccount, createEthAccountWithBalance, evmCollection, evmCollectionHelpers, GAS_ARGS, getCollectionAddressFromResult, itWeb3, normalizeEvents, recordEthFee, tokenIdToAddress} from './util/helpers';19import reFungibleAbi from './reFungibleAbi.json';20import reFungibleTokenAbi from './reFungibleTokenAbi.json';21import {expect} from 'chai';2223describe('Refungible: Information getting', () => {24 itWeb3('totalSupply', async ({api, web3, privateKeyWrapper}) => {25 const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);26 const helper = evmCollectionHelpers(web3, caller);27 const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();28 const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);29 const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});30 const nextTokenId = await contract.methods.nextTokenId().call();31 await contract.methods.mint(caller, nextTokenId).send();32 const totalSupply = await contract.methods.totalSupply().call();33 expect(totalSupply).to.equal('1');34 });3536 itWeb3('balanceOf', async ({api, web3, privateKeyWrapper}) => {37 const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);38 const helper = evmCollectionHelpers(web3, caller);39 const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();40 const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);41 const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});4243 {44 const nextTokenId = await contract.methods.nextTokenId().call();45 await contract.methods.mint(caller, nextTokenId).send();46 }47 {48 const nextTokenId = await contract.methods.nextTokenId().call();49 await contract.methods.mint(caller, nextTokenId).send();50 }51 {52 const nextTokenId = await contract.methods.nextTokenId().call();53 await contract.methods.mint(caller, nextTokenId).send();54 }5556 const balance = await contract.methods.balanceOf(caller).call();5758 expect(balance).to.equal('3');59 });6061 itWeb3('ownerOf', async ({api, web3, privateKeyWrapper}) => {62 const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);63 const helper = evmCollectionHelpers(web3, caller);64 const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();65 const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);66 const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});6768 const tokenId = await contract.methods.nextTokenId().call();69 await contract.methods.mint(caller, tokenId).send();7071 const owner = await contract.methods.ownerOf(tokenId).call();7273 expect(owner).to.equal(caller);74 });7576 itWeb3('ownerOf after burn', async ({api, web3, privateKeyWrapper}) => {77 const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);78 const receiver = createEthAccount(web3);79 const helper = evmCollectionHelpers(web3, caller);80 const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();81 const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);82 const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});8384 const tokenId = await contract.methods.nextTokenId().call();85 await contract.methods.mint(caller, tokenId).send();8687 const tokenAddress = tokenIdToAddress(collectionId, tokenId);88 const tokenContract = new web3.eth.Contract(reFungibleTokenAbi as any, tokenAddress, {from: caller, ...GAS_ARGS});8990 await tokenContract.methods.repartition(2).send();91 await tokenContract.methods.transfer(receiver, 1).send();9293 await tokenContract.methods.burnFrom(caller, 1).send();9495 const owner = await contract.methods.ownerOf(tokenId).call();9697 expect(owner).to.equal(receiver);98 });99100 itWeb3('ownerOf for partial ownership', async ({api, web3, privateKeyWrapper}) => {101 const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);102 const receiver = createEthAccount(web3);103 const helper = evmCollectionHelpers(web3, caller);104 const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();105 const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);106 const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});107108 const tokenId = await contract.methods.nextTokenId().call();109 await contract.methods.mint(caller, tokenId).send();110111 const tokenAddress = tokenIdToAddress(collectionId, tokenId);112 const tokenContract = new web3.eth.Contract(reFungibleTokenAbi as any, tokenAddress, {from: caller, ...GAS_ARGS});113114 await tokenContract.methods.repartition(2).send();115 await tokenContract.methods.transfer(receiver, 1).send();116117 const owner = await contract.methods.ownerOf(tokenId).call();118119 expect(owner).to.equal('0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF');120 });121});122123describe('Refungible: Plain calls', () => {124 itWeb3('Can perform mint()', async ({web3, api, privateKeyWrapper}) => {125 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);126 const helper = evmCollectionHelpers(web3, owner);127 let result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();128 const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);129 const receiver = createEthAccount(web3);130 const contract = evmCollection(web3, owner, collectionIdAddress, {type: 'ReFungible'});131 const nextTokenId = await contract.methods.nextTokenId().call();132133 expect(nextTokenId).to.be.equal('1');134 result = await contract.methods.mintWithTokenURI(135 receiver,136 nextTokenId,137 'Test URI',138 ).send();139140 const events = normalizeEvents(result.events);141142 expect(events).to.include.deep.members([143 {144 address: collectionIdAddress,145 event: 'Transfer',146 args: {147 from: '0x0000000000000000000000000000000000000000',148 to: receiver,149 tokenId: nextTokenId,150 },151 },152 ]);153154 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');155 });156157 itWeb3('Can perform mintBulk()', async ({web3, api, privateKeyWrapper}) => {158 const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);159 const helper = evmCollectionHelpers(web3, caller);160 const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();161 const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);162 const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});163164 const receiver = createEthAccount(web3);165166 {167 const nextTokenId = await contract.methods.nextTokenId().call();168 expect(nextTokenId).to.be.equal('1');169 const result = await contract.methods.mintBulkWithTokenURI(170 receiver,171 [172 [nextTokenId, 'Test URI 0'],173 [+nextTokenId + 1, 'Test URI 1'],174 [+nextTokenId + 2, 'Test URI 2'],175 ],176 ).send();177 const events = normalizeEvents(result.events);178179 expect(events).to.include.deep.members([180 {181 address: collectionIdAddress,182 event: 'Transfer',183 args: {184 from: '0x0000000000000000000000000000000000000000',185 to: receiver,186 tokenId: nextTokenId,187 },188 },189 {190 address: collectionIdAddress,191 event: 'Transfer',192 args: {193 from: '0x0000000000000000000000000000000000000000',194 to: receiver,195 tokenId: String(+nextTokenId + 1),196 },197 },198 {199 address: collectionIdAddress,200 event: 'Transfer',201 args: {202 from: '0x0000000000000000000000000000000000000000',203 to: receiver,204 tokenId: String(+nextTokenId + 2),205 },206 },207 ]);208209 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI 0');210 expect(await contract.methods.tokenURI(+nextTokenId + 1).call()).to.be.equal('Test URI 1');211 expect(await contract.methods.tokenURI(+nextTokenId + 2).call()).to.be.equal('Test URI 2');212 }213 });214215 itWeb3('Can perform burn()', async ({web3, api, privateKeyWrapper}) => {216 const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);217 const helper = evmCollectionHelpers(web3, caller);218 const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();219 const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);220 const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});221222 const tokenId = await contract.methods.nextTokenId().call();223 await contract.methods.mint(caller, tokenId).send();224 {225 const result = await contract.methods.burn(tokenId).send();226 const events = normalizeEvents(result.events);227228 expect(events).to.be.deep.equal([229 {230 address: collectionIdAddress,231 event: 'Transfer',232 args: {233 from: caller,234 to: '0x0000000000000000000000000000000000000000',235 tokenId: tokenId.toString(),236 },237 },238 ]);239 }240 });241242 itWeb3('Can perform transferFrom()', async ({web3, api, privateKeyWrapper}) => {243 const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);244 const helper = evmCollectionHelpers(web3, caller);245 const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();246 const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);247 const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});248249 const receiver = createEthAccount(web3);250251 const tokenId = await contract.methods.nextTokenId().call();252 await contract.methods.mint(caller, tokenId).send();253 {254 const result = await contract.methods.transferFrom(caller, receiver, tokenId).send();255 const events = normalizeEvents(result.events);256 expect(events).to.include.deep.members([257 {258 address: collectionIdAddress,259 event: 'Transfer',260 args: {261 from: caller,262 to: receiver,263 tokenId: tokenId.toString(),264 },265 },266 ]);267 }268269 {270 const balance = await contract.methods.balanceOf(receiver).call();271 expect(+balance).to.equal(1);272 }273274 {275 const balance = await contract.methods.balanceOf(caller).call();276 expect(+balance).to.equal(0);277 }278 });279280 itWeb3('Can perform transfer()', async ({web3, api, privateKeyWrapper}) => {281 const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);282 const helper = evmCollectionHelpers(web3, caller);283 const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();284 const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);285 const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});286287 const receiver = createEthAccount(web3);288289 const tokenId = await contract.methods.nextTokenId().call();290 await contract.methods.mint(caller, tokenId).send();291292 {293 const result = await contract.methods.transfer(receiver, tokenId).send();294 const events = normalizeEvents(result.events);295 expect(events).to.include.deep.members([296 {297 address: collectionIdAddress,298 event: 'Transfer',299 args: {300 from: caller,301 to: receiver,302 tokenId: tokenId.toString(),303 },304 },305 ]);306 }307308 {309 const balance = await contract.methods.balanceOf(caller).call();310 expect(+balance).to.equal(0);311 }312313 {314 const balance = await contract.methods.balanceOf(receiver).call();315 expect(+balance).to.equal(1);316 }317 });318319 itWeb3('transfer event on transfer from partial ownership to full ownership', async ({api, web3, privateKeyWrapper}) => {320 const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);321 const receiver = createEthAccount(web3);322 const helper = evmCollectionHelpers(web3, caller);323 const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();324 const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);325 const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});326327 const tokenId = await contract.methods.nextTokenId().call();328 await contract.methods.mint(caller, tokenId).send();329330 const tokenAddress = tokenIdToAddress(collectionId, tokenId);331 const tokenContract = new web3.eth.Contract(reFungibleTokenAbi as any, tokenAddress, {from: caller, ...GAS_ARGS});332333 await tokenContract.methods.repartition(2).send();334 await tokenContract.methods.transfer(receiver, 1).send();335336 let transfer;337 contract.events.Transfer({}, function(_error: any, event: any){ transfer = event;});338 await tokenContract.methods.transfer(receiver, 1).send();339 const events = normalizeEvents([transfer]);340 expect(events).to.deep.equal([341 {342 address: collectionIdAddress,343 event: 'Transfer',344 args: {345 from: '0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF',346 to: receiver,347 tokenId: tokenId.toString(),348 },349 },350 ]);351 });352353 itWeb3('transfer event on transfer from full ownership to partial ownership', async ({api, web3, privateKeyWrapper}) => {354 const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);355 const receiver = createEthAccount(web3);356 const helper = evmCollectionHelpers(web3, caller);357 const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();358 const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);359 const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});360361 const tokenId = await contract.methods.nextTokenId().call();362 await contract.methods.mint(caller, tokenId).send();363364 const tokenAddress = tokenIdToAddress(collectionId, tokenId);365 const tokenContract = new web3.eth.Contract(reFungibleTokenAbi as any, tokenAddress, {from: caller, ...GAS_ARGS});366367 await tokenContract.methods.repartition(2).send();368 369 let transfer;370 contract.events.Transfer({}, function(_error: any, event: any){ transfer = event;});371 await tokenContract.methods.transfer(receiver, 1).send();372373 const events = normalizeEvents([transfer]);374 expect(events).to.deep.equal([375 {376 address: collectionIdAddress,377 event: 'Transfer',378 args: {379 from: caller,380 to: '0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF',381 tokenId: tokenId.toString(),382 },383 },384 ]);385 });386});387388describe('RFT: Fees', () => {389 itWeb3('transferFrom() call fee is less than 0.2UNQ', async ({web3, api, privateKeyWrapper}) => {390 const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);391 const helper = evmCollectionHelpers(web3, caller);392 const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();393 const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);394 const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});395396 const receiver = createEthAccount(web3);397398 const tokenId = await contract.methods.nextTokenId().call();399 await contract.methods.mint(caller, tokenId).send();400401 const cost = await recordEthFee(api, caller, () => contract.methods.transferFrom(caller, receiver, tokenId).send());402 expect(cost < BigInt(0.2 * Number(UNIQUE)));403 expect(cost > 0n);404 });405406 itWeb3('transfer() call fee is less than 0.2UNQ', async ({web3, api, privateKeyWrapper}) => {407 const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);408 const helper = evmCollectionHelpers(web3, caller);409 const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();410 const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);411 const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});412413 const receiver = createEthAccount(web3);414415 const tokenId = await contract.methods.nextTokenId().call();416 await contract.methods.mint(caller, tokenId).send();417418 const cost = await recordEthFee(api, caller, () => contract.methods.transfer(receiver, tokenId).send());419 expect(cost < BigInt(0.2 * Number(UNIQUE)));420 expect(cost > 0n);421 });422});423424describe('Common metadata', () => {425 itWeb3('Returns collection name', async ({api, web3, privateKeyWrapper}) => {426 const collection = await createCollectionExpectSuccess({427 name: 'token name',428 mode: {type: 'ReFungible'},429 });430 const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);431432 const address = collectionIdToAddress(collection);433 const contract = evmCollection(web3, caller, address, {type: 'ReFungible'});434 const name = await contract.methods.name().call();435436 expect(name).to.equal('token name');437 });438439 itWeb3('Returns symbol name', async ({api, web3, privateKeyWrapper}) => {440 const collection = await createCollectionExpectSuccess({441 tokenPrefix: 'TOK',442 mode: {type: 'ReFungible'},443 });444 const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);445446 const address = collectionIdToAddress(collection);447 const contract = evmCollection(web3, caller, address, {type: 'ReFungible'});448 const symbol = await contract.methods.symbol().call();449450 expect(symbol).to.equal('TOK');451 });452});