From 5c7362e547d146fd16284bcca120d000b613ced1 Mon Sep 17 00:00:00 2001 From: Tal Kol Date: Sat, 30 Apr 2022 04:48:01 +0100 Subject: [PATCH] Added test for sending a message --- contracts/main.ts | 6 ++++++ package-lock.json | 43 +++++++++++++++++++++++++++++++++++++++---- package.json | 2 ++ test/counter.spec.ts | 35 +++++++++++++++++++++++++---------- test/utils.ts | 16 +++++++++++++++- 5 files changed, 87 insertions(+), 15 deletions(-) diff --git a/contracts/main.ts b/contracts/main.ts index 148ea6f..280c0a0 100644 --- a/contracts/main.ts +++ b/contracts/main.ts @@ -12,3 +12,9 @@ export function createCode() { export function createData(params: { ownerAddress: Address; counter: number }) { return beginCell().storeAddress(params.ownerAddress).storeUint(params.counter, 64).endCell(); } + +// message generators for all ops + +export function op_increment() { + return beginCell().storeUint(1, 32).storeUint(0, 64).endCell(); +} diff --git a/package-lock.json b/package-lock.json index 5d4c149..90c3b3f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,9 +9,11 @@ "version": "0.0.0", "license": "MIT", "devDependencies": { + "@types/bn.js": "^5.1.0", "@types/chai": "^4.3.0", "@types/mocha": "^9.0.0", "chai": "^4.3.4", + "chai-bn": "^0.3.1", "fast-glob": "^3.2.11", "mocha": "^9.1.3", "prando": "^6.0.1", @@ -109,6 +111,15 @@ "integrity": "sha512-eZxlbI8GZscaGS7kkc/trHTT5xgrjH3/1n2JDwusC9iahPKWMRvRjJSAN5mCXviuTGQ/lHnhvv8Q1YTpnfz9gA==", "dev": true }, + "node_modules/@types/bn.js": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/@types/bn.js/-/bn.js-5.1.0.tgz", + "integrity": "sha512-QSSVYj7pYFN49kW77o2s9xTCwZ8F2xLbjLLSEVh8D2F4JUhZtPAGOFLTD+ffqksBx/u4cE/KImFjyhqCjn/LIA==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/chai": { "version": "4.3.1", "resolved": "https://registry.npmjs.org/@types/chai/-/chai-4.3.1.tgz", @@ -125,8 +136,7 @@ "version": "17.0.29", "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.29.tgz", "integrity": "sha512-tx5jMmMFwx7wBwq/V7OohKDVb/JwJU5qCVkeLMh1//xycAJ/ESuw9aJ9SEtlCZDYi2pBfe4JkisSoAtbOsBNAA==", - "dev": true, - "peer": true + "dev": true }, "node_modules/@ungap/promise-all-settled": { "version": "1.1.2", @@ -310,6 +320,16 @@ "node": ">=4" } }, + "node_modules/chai-bn": { + "version": "0.3.1", + "resolved": "https://registry.npmjs.org/chai-bn/-/chai-bn-0.3.1.tgz", + "integrity": "sha512-vuzEy0Cb+k8zqi2SHOmvZdRSbKcSOJfS1Nv8+6YDJIyCzfxkTCHLNRyjRIoRJ3WJtYb/c7OHjrvLoGeyO4A/gA==", + "dev": true, + "peerDependencies": { + "bn.js": "^5.0.0", + "chai": "^4.0.0" + } + }, "node_modules/chalk": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", @@ -1712,6 +1732,15 @@ "integrity": "sha512-eZxlbI8GZscaGS7kkc/trHTT5xgrjH3/1n2JDwusC9iahPKWMRvRjJSAN5mCXviuTGQ/lHnhvv8Q1YTpnfz9gA==", "dev": true }, + "@types/bn.js": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/@types/bn.js/-/bn.js-5.1.0.tgz", + "integrity": "sha512-QSSVYj7pYFN49kW77o2s9xTCwZ8F2xLbjLLSEVh8D2F4JUhZtPAGOFLTD+ffqksBx/u4cE/KImFjyhqCjn/LIA==", + "dev": true, + "requires": { + "@types/node": "*" + } + }, "@types/chai": { "version": "4.3.1", "resolved": "https://registry.npmjs.org/@types/chai/-/chai-4.3.1.tgz", @@ -1728,8 +1757,7 @@ "version": "17.0.29", "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.29.tgz", "integrity": "sha512-tx5jMmMFwx7wBwq/V7OohKDVb/JwJU5qCVkeLMh1//xycAJ/ESuw9aJ9SEtlCZDYi2pBfe4JkisSoAtbOsBNAA==", - "dev": true, - "peer": true + "dev": true }, "@ungap/promise-all-settled": { "version": "1.1.2", @@ -1871,6 +1899,13 @@ "type-detect": "^4.0.5" } }, + "chai-bn": { + "version": "0.3.1", + "resolved": "https://registry.npmjs.org/chai-bn/-/chai-bn-0.3.1.tgz", + "integrity": "sha512-vuzEy0Cb+k8zqi2SHOmvZdRSbKcSOJfS1Nv8+6YDJIyCzfxkTCHLNRyjRIoRJ3WJtYb/c7OHjrvLoGeyO4A/gA==", + "dev": true, + "requires": {} + }, "chalk": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", diff --git a/package.json b/package.json index fbe2623..fb9ede4 100644 --- a/package.json +++ b/package.json @@ -10,9 +10,11 @@ "test": "mocha --exit test/**/*.spec.ts" }, "devDependencies": { + "@types/bn.js": "^5.1.0", "@types/chai": "^4.3.0", "@types/mocha": "^9.0.0", "chai": "^4.3.4", + "chai-bn": "^0.3.1", "fast-glob": "^3.2.11", "mocha": "^9.1.3", "prando": "^6.0.1", diff --git a/test/counter.spec.ts b/test/counter.spec.ts index 9567cfa..b7d817d 100644 --- a/test/counter.spec.ts +++ b/test/counter.spec.ts @@ -1,7 +1,11 @@ -import { expect } from "chai"; +import chai, { expect } from "chai"; +import chaiBN from "chai-bn"; +import BN from "bn.js"; +chai.use(chaiBN(BN)); + import { SmartContract } from "ton-contract-executor"; -import { createCode, createData } from "../contracts/main"; -import { randomAddress } from "./utils"; +import { createCode, createData, op_increment } from "../contracts/main"; +import { internalMessage, randomAddress } from "./utils"; describe("Counter tests", () => { let contract: SmartContract; @@ -10,19 +14,30 @@ describe("Counter tests", () => { contract = await SmartContract.fromCell( createCode(), createData({ - ownerAddress: randomAddress(0, "seed1"), + ownerAddress: randomAddress(0, "owner"), counter: 17, }) ); }); - it("should run getter counter() and get counter value", async () => { - const call = await contract.invokeGetMethod("counter", []); - expect(call.result[0].toNumber()).to.equal(17); + it("should get the meaning of life", async () => { + const call = await contract.invokeGetMethod("meaning_of_life", []); + expect(call.result[0]).to.be.bignumber.equal(new BN(42)); }); - it("should run getter meaning_of_life()", async () => { - const call = await contract.invokeGetMethod("meaning_of_life", []); - expect(call.result[0].toNumber()).to.equal(42); + it("should get counter value and increment it", async () => { + const call = await contract.invokeGetMethod("counter", []); + expect(call.result[0]).to.be.bignumber.equal(new BN(17)); + + const send = await contract.sendInternalMessage( + internalMessage({ + from: randomAddress(0, "notowner"), + body: op_increment(), + }) + ); + expect(send.type).to.equal("success"); + + const call2 = await contract.invokeGetMethod("counter", []); + expect(call2.result[0]).to.be.bignumber.equal(new BN(18)); }); }); diff --git a/test/utils.ts b/test/utils.ts index 239bf29..a9e090b 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -1,6 +1,9 @@ -import { Address } from "ton"; +import BN from "bn.js"; +import { Address, Cell, CellMessage, InternalMessage, CommonMessageInfo } from "ton"; import Prando from "prando"; +export const zeroAddress = new Address(0, Buffer.alloc(32, 0)); + export function randomAddress(workchain: number, seed: string) { const random = new Prando(seed); const hash = Buffer.alloc(32); @@ -9,3 +12,14 @@ export function randomAddress(workchain: number, seed: string) { } return new Address(workchain, hash); } + +export function internalMessage(params: { from?: Address; to?: Address; value?: BN; bounce?: boolean; body?: Cell }) { + const message = params.body ? new CellMessage(params.body) : undefined; + return new InternalMessage({ + from: params.from ?? randomAddress(0, "seed"), + to: params.to ?? zeroAddress, + value: params.value ?? 0, + bounce: params.bounce ?? true, + body: new CommonMessageInfo({ body: message }), + }); +}