From 1975bad423e45d0d9582d12c135c147f37e876bd Mon Sep 17 00:00:00 2001 From: Tal Kol Date: Sun, 1 May 2022 17:58:43 +0100 Subject: [PATCH] Added transfer ownership tests --- contracts/imports/constants.fc | 6 ++-- contracts/main.ts | 6 +++- test/ownership.spec.ts | 53 ++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 test/ownership.spec.ts diff --git a/contracts/imports/constants.fc b/contracts/imports/constants.fc index 8491233..c260f95 100644 --- a/contracts/imports/constants.fc +++ b/contracts/imports/constants.fc @@ -5,9 +5,9 @@ int op::withdraw() asm "0x41836980 PUSHINT"; int op::transfer_ownership() asm "0x2da38aaf PUSHINT"; ;; errors -int error::access_denied() asm "0xfffffffe PUSHINT"; -int error::unknown_op() asm "0xffffffff PUSHINT"; -int error::insufficient_balance() asm "101 PUSHINT"; +int error::unknown_op() asm "101 PUSHINT"; +int error::access_denied() asm "102 PUSHINT"; +int error::insufficient_balance() asm "103 PUSHINT"; ;; other int const::min_tons_for_storage() asm "10000000 PUSHINT"; ;; 0.01 TON \ No newline at end of file diff --git a/contracts/main.ts b/contracts/main.ts index 6410c3e..4d63119 100644 --- a/contracts/main.ts +++ b/contracts/main.ts @@ -5,8 +5,12 @@ export function data(params: { ownerAddress: Address; counter: number }): Cell { return beginCell().storeAddress(params.ownerAddress).storeUint(params.counter, 64).endCell(); } -// message encoders for all ops +// message encoders for all ops (see contracts/imports/constants.fc for consts) export function increment(): Cell { return beginCell().storeUint(0x37491f2f, 32).storeUint(0, 64).endCell(); } + +export function transferOwnership(params: { newOwnerAddress: Address }): Cell { + return beginCell().storeUint(0x2da38aaf, 32).storeUint(0, 64).storeAddress(params.newOwnerAddress).endCell(); +} diff --git a/test/ownership.spec.ts b/test/ownership.spec.ts new file mode 100644 index 0000000..7feac58 --- /dev/null +++ b/test/ownership.spec.ts @@ -0,0 +1,53 @@ +import chai, { expect } from "chai"; +import chaiBN from "chai-bn"; +import BN from "bn.js"; +chai.use(chaiBN(BN)); + +import * as fs from "fs"; +import { Cell, Slice } from "ton"; +import { SmartContract } from "ton-contract-executor"; +import * as main from "../contracts/main"; +import { internalMessage, randomAddress } from "./helpers"; + +describe("Transfer ownership tests", () => { + let contract: SmartContract; + + beforeEach(async () => { + contract = await SmartContract.fromCell( + Cell.fromBoc(fs.readFileSync("build/main.cell"))[0], // code cell from build output + main.data({ + ownerAddress: randomAddress("owner"), + counter: 17, + }) + ); + }); + + 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); + }); +});