Skip to content

Commit

Permalink
Implement Current Time example
Browse files Browse the repository at this point in the history
  • Loading branch information
Swafox committed Apr 10, 2024
1 parent c202a73 commit f435304
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Mathematics/contracts/mathematics.fc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
27 changes: 27 additions & 0 deletions Mathematics/tests/Mathematics.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
14 changes: 14 additions & 0 deletions Mathematics/wrappers/Mathematics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit f435304

Please sign in to comment.