From f435304e54d4b946c31d45fb88fbd028d00e245d Mon Sep 17 00:00:00 2001 From: Swafox Date: Wed, 10 Apr 2024 22:27:01 +0300 Subject: [PATCH] Implement Current Time example --- Mathematics/contracts/mathematics.fc | 10 ++++++++++ Mathematics/tests/Mathematics.spec.ts | 27 +++++++++++++++++++++++++++ Mathematics/wrappers/Mathematics.ts | 14 ++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/Mathematics/contracts/mathematics.fc b/Mathematics/contracts/mathematics.fc index 7b9a29f..6fe0d0b 100644 --- a/Mathematics/contracts/mathematics.fc +++ b/Mathematics/contracts/mathematics.fc @@ -71,6 +71,16 @@ global int ctx_counter; return (); } + ;; How to get current time + ;; Cookbook link: https://docs.ton.org/develop/func/cookbook#how-to-get-current-time + if (op == 3) { + int current_time = now(); + ctx_counter += current_time; + + save_data(); + return (); + } + throw(777); } diff --git a/Mathematics/tests/Mathematics.spec.ts b/Mathematics/tests/Mathematics.spec.ts index 91f7ddc..e767a0a 100644 --- a/Mathematics/tests/Mathematics.spec.ts +++ b/Mathematics/tests/Mathematics.spec.ts @@ -100,4 +100,31 @@ describe("Mathematics", () => { expect(counterAfter).toBeGreaterThan(counterBefore); }); + + it("Current time: set counter to current time", async () => { + const increaser = await blockchain.treasury("increaserTime"); + + const counterBefore = await mathematics.getCounter(); + + console.log("counter before increasing", counterBefore); + + const increaseResult = await mathematics.sendCurrentTime( + increaser.getSender(), + { + value: toNano("0.05"), + }, + ); + + expect(increaseResult.transactions).toHaveTransaction({ + from: increaser.address, + to: mathematics.address, + success: true, + }); + + const counterAfter = Math.floor(Date.now() / 1000); + + console.log("counter after increasing", counterAfter); + + expect(counterAfter).toBeGreaterThan(counterBefore); + }); }); diff --git a/Mathematics/wrappers/Mathematics.ts b/Mathematics/wrappers/Mathematics.ts index d3ae7b4..2711cc6 100644 --- a/Mathematics/wrappers/Mathematics.ts +++ b/Mathematics/wrappers/Mathematics.ts @@ -75,6 +75,20 @@ export class Mathematics implements Contract { }); } + async sendCurrentTime( + provider: ContractProvider, + via: Sender, + opts: { + value: bigint; + }, + ) { + await provider.internal(via, { + value: opts.value, + sendMode: SendMode.PAY_GAS_SEPARATELY, + body: beginCell().storeUint(3, 32).storeUint(1, 32).endCell(), + }); + } + async getCounter(provider: ContractProvider) { const result = await provider.get("get_counter", []); return result.stack.readNumber();