import chai, { assert, expect } from "chai"; import chaiBN from "chai-bn"; import BN from "bn.js"; chai.use(chaiBN(BN)); import { Builder, Cell, Slice } from "ton"; import { SmartContract } from "ton-contract-executor"; import * as main from "../contracts/main"; import { internalMessage, randomAddress } from "./helpers"; import { hex } from "../build/main.compiled.json"; describe("Transfer ownership tests", () => { let contract: SmartContract; let debug: boolean = false; let keyPair = main.genKeyPair(); beforeEach(async () => { contract = await SmartContract.fromCell( Cell.fromBoc(hex)[0], main.data({ ownerAddress: randomAddress("owner"), code: Cell.fromBoc(hex)[0], collectionAddress: randomAddress("collection"), domain: "alice", publicKey: keyPair.publicKey }), { debug: debug } ); }); it("transfers ownership to self with no error", async () => { main.setContractBalance(contract, 10 * main.TON()); let ownerAddr = randomAddress("owner"); const sendToSelfMessage = internalMessage({ from: ownerAddr, body: main.transferOwnership({ newOwnerAddress: ownerAddr }), }); const res = await contract.sendInternalMessage(sendToSelfMessage); expect(res.type).to.equal("success"); expect(res.exit_code).to.equal(0); assert(main.currentState(contract).ownerAddress?.equals(ownerAddr), "owner address changed after transferring to self"); }); it("should allow the owner to transfer ownership", async () => { main.setContractBalance(contract, 10 * main.TON()); let ownerAddr = randomAddress("owner"); let receiverAddr = randomAddress("receiver"); const sendToSelfMessage = internalMessage({ from: ownerAddr, body: main.transferOwnership({ newOwnerAddress: receiverAddr }), }); const res = await contract.sendInternalMessage(sendToSelfMessage); expect(res.type).to.equal("success"); expect(res.exit_code).to.equal(0); assert(main.currentState(contract).ownerAddress?.equals(receiverAddr)); }); it("should prevent others from changing owners", async () => { main.setContractBalance(contract, 10 * main.TON()); let ownerAddr = randomAddress("owner"); let otherAddr = randomAddress("receiver"); const sendToSelfMessage = internalMessage({ from: otherAddr, body: main.transferOwnership({ newOwnerAddress: otherAddr }), }); const res = await contract.sendInternalMessage(sendToSelfMessage); expect(res.type).to.equal("failed"); expect(res.exit_code).to.equal(401); assert(main.currentState(contract).ownerAddress?.equals(ownerAddr)); }); // it("should allow the owner to change owners", async () => { // const send = await contract.sendInternalMessage( // internalMessage({ // from: randomAddress("owner"), // body: main.transferOwnership({ newOwnerAddress: randomAddress("newowner") }), // }) // ); // expect(send.type).to.equal("success"); // // const call = await contract.invokeGetMethod("owner_address", []); // const address = (call.result[0] as Slice).readAddress(); // expect(address?.equals(randomAddress("newowner"))).to.equal(true); // }); // // it("should prevent others from changing owners", async () => { // const send = await contract.sendInternalMessage( // internalMessage({ // from: randomAddress("notowner"), // body: main.transferOwnership({ newOwnerAddress: randomAddress("newowner") }), // }) // ); // expect(send.type).to.equal("failed"); // expect(send.exit_code).to.equal(102); // access_denied in contracts/imports/constants.fc // // const call = await contract.invokeGetMethod("owner_address", []); // const address = (call.result[0] as Slice).readAddress(); // expect(address?.equals(randomAddress("owner"))).to.equal(true); // }); });