Skip to content

Commit

Permalink
commit1, Fibonacci and Arithmetic contracts running
Browse files Browse the repository at this point in the history
  • Loading branch information
fabcotech committed Jul 8, 2024
0 parents commit b80cfec
Show file tree
Hide file tree
Showing 20 changed files with 8,143 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
env: {
es2021: true,
node: true,
},
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended', 'prettier'],
overrides: [],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
},
plugins: ['@typescript-eslint'],
rules: {
'@typescript-eslint/no-unused-vars': 'error',
'@typescript-eslint/no-explicit-any': 'off',
'sonarjs/no-duplicate-string': 'off',
},
};
46 changes: 46 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: 'Build, run and test'

on:
push:
branches:
- '*'

jobs:
test:
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Node JS
uses: actions/setup-node@v4
with:
node-version: 20.11

- name: Install dependencies
run: npm i

- name: Build
run: npm run build

- name: Build
run: npm run test

lintandcheck:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Node JS
uses: actions/setup-node@v4
with:
node-version: 20.11

- name: Install dependencies
run: npm i

- name: Prettier check
run: npm run prettier:check

- name: ESLint lint
run: npm run lint
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
temp
build
dist
.DS_Store
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
7 changes: 7 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"printWidth": 120,
"tabWidth": 2,
"singleQuote": true,
"bracketSpacing": true,
"semi": true
}
27 changes: 27 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"printWidth": 80,
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": true,
"quoteProps": "as-needed",
"jsxSingleQuote": false,
"trailingComma": "es5",
"bracketSpacing": true,
"arrowParens": "always",
"rangeStart": 0,
"requirePragma": false,
"insertPragma": false,
"proseWrap": "preserve",
"htmlWhitespaceSensitivity": "css",
"vueIndentScriptAndStyle": false,
"endOfLine": "auto",
"overrides": [
{
"files": "*.md",
"options": {
"proseWrap": "always"
}
}
]
}
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# TON (The Open Network) blockchain - The duck repo

![The duck](https://sl.combot.org/utyaduck/webp/6xf09f98b3.webp)

A set of low, high value TON blockchain (Telegram Open Network) FunC smart contracts and test suites. New code coming in regularly.

- **Arithmetic** : TON Smart contract that performs basic add/substract/multiply operations on a integer
- **Fibonacci** : TON Smart contract that stores two integers, and continues the fibonacci sequence everytime it is touched.

```sh
npm i
npm run build
npm run test

# Only test arithmetic
npm run test tests/Arithmetic.spec.ts

# Only test fibonacci
npm run test tests/Fibonacci.spec.ts
```
77 changes: 77 additions & 0 deletions contracts/arithmetic.fc
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include "imports/stdlib.fc";

const op::increase = "op::increase"c; ;;0x7e8764ef
const op::decrease = "op::decrease"c; ;;0xe78525c4
const op::multiply = "op::multiply"c; ;;0x6f6bc17a

;; storage variables

;; id is required to be able to create different instances of counters
;; since addresses in TON depend on the initial state of the contract
global int ctx_counter;

;; load_data populates storage variables using stored data
() load_data() impure {
var ds = get_data().begin_parse();

ctx_counter = ds~load_uint(32);

ds.end_parse();
}

;; save_data stores storage variables as a cell into persistent storage
() save_data() impure {
set_data(
begin_cell()
.store_uint(ctx_counter, 32)
.end_cell()
);
}

;; recv_internal is the main function of the contract and is called when it receives a message from other contracts
() recv_internal(int my_balance, int msg_value, cell in_msg_full, slice in_msg_body) impure {
if (in_msg_body.slice_empty?()) { ;; ignore all empty messages
return ();
}

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

load_data(); ;; here we populate the storage variables

int op = in_msg_body~load_uint(32); ;; by convention, the first 32 bits of incoming message is the op
int query_id = in_msg_body~load_uint(64); ;; also by convention, the next 64 bits contain the "query id", although this is not always the case

if (op == op::increase) {
int increase_by = in_msg_body~load_uint(32);
ctx_counter += increase_by;
save_data();
return ();
}
if (op == op::decrease) {
int decrease_by = in_msg_body~load_uint(32);
ctx_counter -= decrease_by;
save_data();
return ();
}
if (op == op::multiply) {
int multiply_by = in_msg_body~load_uint(32);
ctx_counter *= multiply_by;
save_data();
return ();
}

throw(0xffff); ;; if the message contains an op that is not known to this contract, we throw
}

;; get methods are a means to conveniently read contract data using, for example, HTTP APIs
;; they are marked with method_id
;; note that unlike in many other smart contract VMs, get methods cannot be called by other contracts

int get_counter() method_id {
load_data();
return ctx_counter;
}
62 changes: 62 additions & 0 deletions contracts/fibonacci.fc
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "imports/stdlib.fc";

global int ctx_n1;
global int ctx_n2;

;; save_data stores storage variables as a cell into persistent storage
() save_data() impure {
set_data(
begin_cell()
.store_uint(ctx_n1, 32)
.store_uint(ctx_n2, 32)
.end_cell()
);
}

() recv_internal(int my_balance, int msg_value, cell in_msg_full, slice in_msg_body) impure {
;;throw_if(35, in_msg_body.slice_bits() < 32);

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

var ds = get_data().begin_parse();
ctx_n1 = ds~load_uint(32);
ctx_n2 = ds~load_uint(32);
ds.end_parse();

if (ctx_n2 == 0) {
if (ctx_n1 == 0) {
ctx_n1 = 1;
ctx_n2 = 0;
set_data(begin_cell().store_uint(ctx_n1, 32).store_uint(ctx_n2, 32).end_cell());
save_data();
return ();
}
}
int sum = ctx_n1 + ctx_n2;
int n1 = ctx_n1;
ctx_n1 = sum;
ctx_n2 = n1;
set_data(begin_cell().store_uint(ctx_n1, 32).store_uint(ctx_n2, 32).end_cell());
save_data();
return ();
}

int get_n1() method_id {
slice ds = get_data().begin_parse();
ctx_n1 = ds~load_uint(32);
ctx_n2 = ds~load_uint(32);
ds.end_parse();
return ctx_n1;
}

int get_n2() method_id {
slice ds = get_data().begin_parse();
ctx_n1 = ds~load_uint(32);
ctx_n2 = ds~load_uint(32);
ds.end_parse();
return ctx_n2;
}
Loading

0 comments on commit b80cfec

Please sign in to comment.