Browse Source

Added test for sending a message

master
Tal Kol 3 years ago
parent
commit
5c7362e547
  1. 6
      contracts/main.ts
  2. 43
      package-lock.json
  3. 2
      package.json
  4. 35
      test/counter.spec.ts
  5. 16
      test/utils.ts

6
contracts/main.ts

@ -12,3 +12,9 @@ export function createCode() {
export function createData(params: { ownerAddress: Address; counter: number }) { export function createData(params: { ownerAddress: Address; counter: number }) {
return beginCell().storeAddress(params.ownerAddress).storeUint(params.counter, 64).endCell(); 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();
}

43
package-lock.json generated

@ -9,9 +9,11 @@
"version": "0.0.0", "version": "0.0.0",
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"@types/bn.js": "^5.1.0",
"@types/chai": "^4.3.0", "@types/chai": "^4.3.0",
"@types/mocha": "^9.0.0", "@types/mocha": "^9.0.0",
"chai": "^4.3.4", "chai": "^4.3.4",
"chai-bn": "^0.3.1",
"fast-glob": "^3.2.11", "fast-glob": "^3.2.11",
"mocha": "^9.1.3", "mocha": "^9.1.3",
"prando": "^6.0.1", "prando": "^6.0.1",
@ -109,6 +111,15 @@
"integrity": "sha512-eZxlbI8GZscaGS7kkc/trHTT5xgrjH3/1n2JDwusC9iahPKWMRvRjJSAN5mCXviuTGQ/lHnhvv8Q1YTpnfz9gA==", "integrity": "sha512-eZxlbI8GZscaGS7kkc/trHTT5xgrjH3/1n2JDwusC9iahPKWMRvRjJSAN5mCXviuTGQ/lHnhvv8Q1YTpnfz9gA==",
"dev": true "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": { "node_modules/@types/chai": {
"version": "4.3.1", "version": "4.3.1",
"resolved": "https://registry.npmjs.org/@types/chai/-/chai-4.3.1.tgz", "resolved": "https://registry.npmjs.org/@types/chai/-/chai-4.3.1.tgz",
@ -125,8 +136,7 @@
"version": "17.0.29", "version": "17.0.29",
"resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.29.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.29.tgz",
"integrity": "sha512-tx5jMmMFwx7wBwq/V7OohKDVb/JwJU5qCVkeLMh1//xycAJ/ESuw9aJ9SEtlCZDYi2pBfe4JkisSoAtbOsBNAA==", "integrity": "sha512-tx5jMmMFwx7wBwq/V7OohKDVb/JwJU5qCVkeLMh1//xycAJ/ESuw9aJ9SEtlCZDYi2pBfe4JkisSoAtbOsBNAA==",
"dev": true, "dev": true
"peer": true
}, },
"node_modules/@ungap/promise-all-settled": { "node_modules/@ungap/promise-all-settled": {
"version": "1.1.2", "version": "1.1.2",
@ -310,6 +320,16 @@
"node": ">=4" "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": { "node_modules/chalk": {
"version": "4.1.2", "version": "4.1.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz",
@ -1712,6 +1732,15 @@
"integrity": "sha512-eZxlbI8GZscaGS7kkc/trHTT5xgrjH3/1n2JDwusC9iahPKWMRvRjJSAN5mCXviuTGQ/lHnhvv8Q1YTpnfz9gA==", "integrity": "sha512-eZxlbI8GZscaGS7kkc/trHTT5xgrjH3/1n2JDwusC9iahPKWMRvRjJSAN5mCXviuTGQ/lHnhvv8Q1YTpnfz9gA==",
"dev": true "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": { "@types/chai": {
"version": "4.3.1", "version": "4.3.1",
"resolved": "https://registry.npmjs.org/@types/chai/-/chai-4.3.1.tgz", "resolved": "https://registry.npmjs.org/@types/chai/-/chai-4.3.1.tgz",
@ -1728,8 +1757,7 @@
"version": "17.0.29", "version": "17.0.29",
"resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.29.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.29.tgz",
"integrity": "sha512-tx5jMmMFwx7wBwq/V7OohKDVb/JwJU5qCVkeLMh1//xycAJ/ESuw9aJ9SEtlCZDYi2pBfe4JkisSoAtbOsBNAA==", "integrity": "sha512-tx5jMmMFwx7wBwq/V7OohKDVb/JwJU5qCVkeLMh1//xycAJ/ESuw9aJ9SEtlCZDYi2pBfe4JkisSoAtbOsBNAA==",
"dev": true, "dev": true
"peer": true
}, },
"@ungap/promise-all-settled": { "@ungap/promise-all-settled": {
"version": "1.1.2", "version": "1.1.2",
@ -1871,6 +1899,13 @@
"type-detect": "^4.0.5" "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": { "chalk": {
"version": "4.1.2", "version": "4.1.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz",

2
package.json

@ -10,9 +10,11 @@
"test": "mocha --exit test/**/*.spec.ts" "test": "mocha --exit test/**/*.spec.ts"
}, },
"devDependencies": { "devDependencies": {
"@types/bn.js": "^5.1.0",
"@types/chai": "^4.3.0", "@types/chai": "^4.3.0",
"@types/mocha": "^9.0.0", "@types/mocha": "^9.0.0",
"chai": "^4.3.4", "chai": "^4.3.4",
"chai-bn": "^0.3.1",
"fast-glob": "^3.2.11", "fast-glob": "^3.2.11",
"mocha": "^9.1.3", "mocha": "^9.1.3",
"prando": "^6.0.1", "prando": "^6.0.1",

35
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 { SmartContract } from "ton-contract-executor";
import { createCode, createData } from "../contracts/main"; import { createCode, createData, op_increment } from "../contracts/main";
import { randomAddress } from "./utils"; import { internalMessage, randomAddress } from "./utils";
describe("Counter tests", () => { describe("Counter tests", () => {
let contract: SmartContract; let contract: SmartContract;
@ -10,19 +14,30 @@ describe("Counter tests", () => {
contract = await SmartContract.fromCell( contract = await SmartContract.fromCell(
createCode(), createCode(),
createData({ createData({
ownerAddress: randomAddress(0, "seed1"), ownerAddress: randomAddress(0, "owner"),
counter: 17, counter: 17,
}) })
); );
}); });
it("should run getter counter() and get counter value", async () => { it("should get the meaning of life", async () => {
const call = await contract.invokeGetMethod("counter", []); const call = await contract.invokeGetMethod("meaning_of_life", []);
expect(call.result[0].toNumber()).to.equal(17); expect(call.result[0]).to.be.bignumber.equal(new BN(42));
}); });
it("should run getter meaning_of_life()", async () => { it("should get counter value and increment it", async () => {
const call = await contract.invokeGetMethod("meaning_of_life", []); const call = await contract.invokeGetMethod("counter", []);
expect(call.result[0].toNumber()).to.equal(42); 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));
}); });
}); });

16
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"; import Prando from "prando";
export const zeroAddress = new Address(0, Buffer.alloc(32, 0));
export function randomAddress(workchain: number, seed: string) { export function randomAddress(workchain: number, seed: string) {
const random = new Prando(seed); const random = new Prando(seed);
const hash = Buffer.alloc(32); const hash = Buffer.alloc(32);
@ -9,3 +12,14 @@ export function randomAddress(workchain: number, seed: string) {
} }
return new Address(workchain, hash); 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 }),
});
}

Loading…
Cancel
Save