Skip to content

Commit

Permalink
Implement Raise to Nth Power example
Browse files Browse the repository at this point in the history
  • Loading branch information
Swafox committed Apr 5, 2024
1 parent bd51798 commit 1cde34f
Show file tree
Hide file tree
Showing 14 changed files with 6,638 additions and 0 deletions.
1 change: 1 addition & 0 deletions Mathematics/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
7 changes: 7 additions & 0 deletions Mathematics/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"printWidth": 120,
"tabWidth": 4,
"singleQuote": true,
"bracketSpacing": true,
"semi": true
}
26 changes: 26 additions & 0 deletions Mathematics/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Mathematics

## Project structure

- `contracts` - source code of all the smart contracts of the project and their dependencies.
- `wrappers` - wrapper classes (implementing `Contract` from ton-core) for the contracts, including any [de]serialization primitives and compilation functions.
- `tests` - tests for the contracts.
- `scripts` - scripts used by the project, mainly the deployment scripts.

## How to use

### Build

`npx blueprint build` or `yarn blueprint build`

### Test

`npx blueprint test` or `yarn blueprint test`

### Deploy or run another script

`npx blueprint run` or `yarn blueprint run`

### Add a new contract

`npx blueprint create ContractName` or `yarn blueprint create ContractName`
625 changes: 625 additions & 0 deletions Mathematics/contracts/imports/stdlib.fc

Large diffs are not rendered by default.

67 changes: 67 additions & 0 deletions Mathematics/contracts/mathematics.fc
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include "imports/stdlib.fc";

global int ctx_counter;

() load_data() impure {
var ds = get_data().begin_parse();

ctx_counter = ds~load_uint(32);

ds.end_parse();
}

() save_data() impure {
set_data(
begin_cell()
.store_uint(ctx_counter, 32)
.end_cell()
);
}

;; Optimized binary exponentiation
(int) binpow (int n, int e) {
if (e == 0) {
return 1;
}
if (e == 1) {
return n;
}
int p = binpow(n, e / 2);
p *= p;
if ((e % 2) == 1) {
p *= n;
}
return p;
}

() recv_internal(int my_balance, int msg_value, cell in_msg_full, slice in_msg_body) impure {
if (in_msg_body.slice_empty?()) {
return ();
}

slice cs = in_msg_full.begin_parse();
int flags = cs~load_uint(4);
if (flags & 1) {
return ();
}

load_data();

int op = in_msg_body~load_uint(32);
int input_number = in_msg_body~load_uint(32);

;; How to raise number to the power of n
;; Cookbook link: https://docs.ton.org/develop/func/cookbook#how-to-raise-number-to-the-power-of-n
if (op == 1) {
ctx_counter += binpow(input_number, 3);
save_data();
return ();
}

throw(777);
}

int get_counter() method_id {
load_data();
return ctx_counter;
}
9 changes: 9 additions & 0 deletions Mathematics/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { Config } from 'jest';

const config: Config = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['/node_modules/', '/dist/'],
};

export default config;
Loading

0 comments on commit 1cde34f

Please sign in to comment.