TON contracts for Agorata
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
3.8 KiB

import chai, { assert, expect } from "chai";
2 years ago
import chaiBN from "chai-bn";
import BN from "bn.js";
chai.use(chaiBN(BN));
import { Builder, Cell, Slice } from "ton";
2 years ago
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;
2 years ago
let keyPair = main.genKeyPair();
2 years ago
beforeEach(async () => {
contract = await SmartContract.fromCell(
Cell.fromBoc(hex)[0],
2 years ago
main.data({
ownerAddress: randomAddress("owner"),
code: Cell.fromBoc(hex)[0],
collectionAddress: randomAddress("collection"),
domain: "alice",
2 years ago
publicKey: keyPair.publicKey
}),
{ debug: debug }
2 years ago
);
});
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));
});
2 years ago
// 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);
// });
});