Browse Source

Added transfer ownership tests

master
Tal Kol 3 years ago
parent
commit
1975bad423
  1. 6
      contracts/imports/constants.fc
  2. 6
      contracts/main.ts
  3. 53
      test/ownership.spec.ts

6
contracts/imports/constants.fc

@ -5,9 +5,9 @@ int op::withdraw() asm "0x41836980 PUSHINT";
int op::transfer_ownership() asm "0x2da38aaf PUSHINT"; int op::transfer_ownership() asm "0x2da38aaf PUSHINT";
;; errors ;; errors
int error::access_denied() asm "0xfffffffe PUSHINT"; int error::unknown_op() asm "101 PUSHINT";
int error::unknown_op() asm "0xffffffff PUSHINT"; int error::access_denied() asm "102 PUSHINT";
int error::insufficient_balance() asm "101 PUSHINT"; int error::insufficient_balance() asm "103 PUSHINT";
;; other ;; other
int const::min_tons_for_storage() asm "10000000 PUSHINT"; ;; 0.01 TON int const::min_tons_for_storage() asm "10000000 PUSHINT"; ;; 0.01 TON

6
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(); 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 { export function increment(): Cell {
return beginCell().storeUint(0x37491f2f, 32).storeUint(0, 64).endCell(); 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();
}

53
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);
});
});
Loading…
Cancel
Save