From e3291e9be50a99ce71b728d7b30249d038a5b06d Mon Sep 17 00:00:00 2001 From: ennucore Date: Mon, 16 Jan 2023 22:57:00 +0100 Subject: [PATCH] Tests for the item --- contracts/main.ts | 37 +++++++++++++++++++++++++++++- contracts/nft-item.fc | 13 ----------- test/creation.spec.ts | 2 +- test/item.spec.ts | 53 +++++++++++++++++++++++++++++++++++++++++++ test/signing.spec.ts | 4 ++-- 5 files changed, 92 insertions(+), 17 deletions(-) create mode 100644 test/item.spec.ts diff --git a/contracts/main.ts b/contracts/main.ts index aba25b8..dd26109 100644 --- a/contracts/main.ts +++ b/contracts/main.ts @@ -60,6 +60,29 @@ export function collectionData(params: { .endCell(); } +export function itemDataUninit(params: { domain: String, collectionAddress: Address }): Cell { + let domain = params.domain.split('.')[0]; + let index = new BN(hashCell(makeSnakeCell(Buffer.from(domain)))); + return beginCell() + .storeUint(index, 256) + .storeAddress(params.collectionAddress).endCell(); +} + +export function itemData(params: { + domain: String, collectionAddress: Address, + ownerAddress: Address }): Cell { + let domain = params.domain.split('.')[0]; + let zone = params.domain.split('.').slice(1).join('.'); + let index = new BN(hashCell(makeSnakeCell(Buffer.from(domain)))); + return beginCell() + .storeUint(index, 256) + .storeAddress(params.collectionAddress) + .storeAddress(params.ownerAddress) + .storeRef(itemContent(domain, zone)) + .storeRef(makeSnakeCell(Buffer.from(domain))) + .storeUint(0, 64).endCell(); +} + export function auctionWithWinner(winnerAddress: Address) { return beginCell().storeAddress(winnerAddress).storeCoins(0).storeUint(0, 64) } @@ -134,10 +157,22 @@ export function createItem(params: { domain: String, signature?: String }): Cell .endCell(); } +export function initializeItemMsg(params: { domain: String, ownerAddr: Address }): Cell { + return beginCell() + .storeAddress(params.ownerAddr) + .storeRef(makeSnakeCell(Buffer.from(params.domain))) + .endCell(); +} + +function itemContent(domain: string, zone: string): Cell { + return encodeOffChainContent(`https://api.agorata.io/data/${zone}/${domain}.json`) +} + export function setContent(params: { domain: string, zone: string }) { return beginCell() .storeUint(0x1a0b9d51 , 32) - .storeRef(encodeOffChainContent(`https://api.agorata.io/data/${params.zone}/${params.domain}.json`)) + .storeUint(0, 64) + .storeRef(itemContent(params.domain, params.zone)) .endCell(); } diff --git a/contracts/nft-item.fc b/contracts/nft-item.fc index d347136..afd274a 100644 --- a/contracts/nft-item.fc +++ b/contracts/nft-item.fc @@ -174,19 +174,6 @@ int min_tons_for_storage() asm "1000000000 PUSHINT"; ;; 1 TON } return (); } - if (op == op::dns_balance_release) { ;; release domain - throw_unless(414, (now() - last_fill_up_time > one_year)); - int min_price = get_min_price(domain.begin_parse().slice_bits(), now()); - throw_unless(407, msg_value >= min_price); - int balance_without_msg = my_balance - msg_value; - int amount_to_send = balance_without_msg - min_tons_for_storage(); - if (amount_to_send > 0) { - send_msg(owner_address, amount_to_send, op::dns_balance_release, query_id, null(), 2); ;; ignore errors - } - owner_address = zero_address(); - store_data(index, collection_address, owner_address, content, domain, now()); - return (); - } if (op == op::get_static_data()) { send_msg(sender_address, 0, op::report_static_data(), query_id, begin_cell().store_uint(index, 256).store_slice(collection_address), 64); ;; carry all the remaining value of the inbound message return (); diff --git a/test/creation.spec.ts b/test/creation.spec.ts index ead285f..2bdfd95 100644 --- a/test/creation.spec.ts +++ b/test/creation.spec.ts @@ -47,7 +47,7 @@ describe("Creating items tests", () => { const sendToSelfMessage = internalMessage({ from: ownerAddr, body: main.createItem({ domain: "test" }), - value: new BN(10 * main.TON()), + value: new BN(main.TON() / 100), }); const res = await contract.sendInternalMessage(sendToSelfMessage); expect(res.type).to.equal("failed"); diff --git a/test/item.spec.ts b/test/item.spec.ts new file mode 100644 index 0000000..f6a8746 --- /dev/null +++ b/test/item.spec.ts @@ -0,0 +1,53 @@ +import chai, {assert, expect} from "chai"; +import chaiBN from "chai-bn"; +import BN from "bn.js"; + +chai.use(chaiBN(BN)); + +import {Address, Builder, Cell, contractAddress, Slice} from "ton"; +import {runContract, SmartContract} from "ton-contract-executor"; +import * as main from "../contracts/main"; +import {internalMessage, randomAddress} from "./helpers"; + +import {hex as item_code} from "../build/nft-item.compiled.json"; +import {makeSnakeCell} from "../contracts/utils"; +import {keyPairFromSeed, KeyPair, sign, keyPairFromSecretKey} from "ton-crypto"; +import {signBuy} from "../contracts/main"; + +let data = main.itemDataUninit({domain: "test", collectionAddress: randomAddress("collection")}); + +describe("Creating items tests", () => { + let contract: SmartContract; + let debug: boolean = true; + + beforeEach(async () => { + contract = await SmartContract.fromCell( + Cell.fromBoc(item_code)[0], + data, + {debug: debug} + ); + contract.setC7Config({ + myself: randomAddress("item") + }) + }); + + it("allows to set the data", async () => { + let ownerAddr = randomAddress("dude"); + const initializeMsg = internalMessage({ + from: randomAddress("collection"), + body: main.initializeItemMsg({domain: "test", ownerAddr}), + value: new BN(0), + }); + const res = await contract.sendInternalMessage(initializeMsg); + expect(res.type).to.equal("success"); + expect(res.exit_code).to.equal(0); + const setDataMsg = internalMessage({ + from: ownerAddr, + body: main.setContent({domain: "test", zone: "example.ton"}), + value: new BN(0), + }) + const res2 = await contract.sendInternalMessage(setDataMsg); + expect(res2.type).to.equal("success"); + expect(res2.exit_code).to.equal(0); + }); +}); diff --git a/test/signing.spec.ts b/test/signing.spec.ts index 24d2f40..20b28e1 100644 --- a/test/signing.spec.ts +++ b/test/signing.spec.ts @@ -49,8 +49,8 @@ describe("Creating items tests", () => { }); const res = await contract.sendInternalMessage(sendToSelfMessage); // console.log(res); - let fs = require('fs'); - fs.writeFile('logs.txt', res.logs, (_: any) => {}); + // let fs = require('fs'); + // fs.writeFile('logs.txt', res.logs, (_: any) => {}); expect(res.type).to.equal("success"); expect(res.exit_code).to.equal(0); });