Skip to content

Commit bcc3797

Browse files
feat(doc): add example of contract in contracts topic (#42)
1 parent 28ee92d commit bcc3797

File tree

3 files changed

+113
-1
lines changed

3 files changed

+113
-1
lines changed

gitlab-pages/docs/syntax/contracts/contracts.md

+67-1
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,76 @@ import Syntax from '@theme/Syntax';
77

88
Smart contracts are programs that run on a blockchain.
99
For an overview of how smart contracts work on Tezos, see [An introduction to smart contracts](https://docs.tezos.com/smart-contracts) on docs.tezos.com.
10-
For an example LIGO contract, see [Quickstart](../../tutorials/getting-started) or load one of the templates in the [Online IDE](https://ide.ligolang.org/).
1110

1211
For the data type that represents a contract, see [Contracts](../../data-types/contracts-type).
1312

13+
## Example contract
14+
15+
This example contract stores an integer and provides two entrypoints that allow callers to add to that integer or subtract from that integer.
16+
The code includes automated tests for the contract that are not part of the contract itself; for more information about tests, see [Testing](../../testing).
17+
18+
<Syntax syntax="cameligo">
19+
20+
```cameligo group=starter_counter
21+
module Test = Test.Next
22+
23+
module Counter = struct
24+
type storage_type = int
25+
type return_type = operation list * storage_type
26+
27+
[@entry]
28+
let add (value : int) ( store: storage_type) : return_type =
29+
[], store + value
30+
31+
[@entry]
32+
let sub (value : int) ( store: storage_type) : return_type =
33+
[], store - value
34+
35+
end
36+
37+
let test =
38+
39+
let contract = Test.Originate.contract (contract_of Counter) 0 0tez in
40+
let _ = Test.Contract.transfer_exn (Test.Typed_address.get_entrypoint "add" contract.taddr) 5 0tez in
41+
let _ = Test.Contract.transfer_exn (Test.Typed_address.get_entrypoint "sub" contract.taddr) 2 0tez in
42+
Assert.assert ((Test.Typed_address.get_storage contract.taddr) = 3)
43+
```
44+
45+
</Syntax>
46+
47+
<Syntax syntax="jsligo">
48+
49+
```jsligo group=starter_counter
50+
import Test = Test.Next;
51+
52+
namespace Counter {
53+
type storage_type = int;
54+
type return_type = [list<operation>, storage_type];
55+
56+
@entry
57+
const add = (value: int, store: storage_type): return_type =>
58+
[[], store + value];
59+
60+
@entry
61+
const sub = (value: int, store: storage_type): return_type =>
62+
[[], store - value];
63+
64+
}
65+
66+
const test = (() => {
67+
68+
const contract = Test.Originate.contract(contract_of(Counter), 0, 0tez);
69+
Test.Contract.transfer_exn(Test.Typed_address.get_entrypoint("add", contract.taddr), 5, 0tez);
70+
Test.Contract.transfer_exn(Test.Typed_address.get_entrypoint("sub", contract.taddr), 2, 0tez);
71+
Assert.assert(Test.Typed_address.get_storage(contract.taddr) == 3);
72+
73+
})();
74+
```
75+
76+
</Syntax>
77+
78+
For more examples of contracts, see [Quickstart](../../tutorials/getting-started) or load one of the templates in the [Online IDE](https://ide.ligolang.org/).
79+
1480
## Components of a contract
1581

1682
A smart contract has three main elements:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import Test = Test.Next;
2+
3+
namespace Counter {
4+
type storage_type = int;
5+
type return_type = [list<operation>, storage_type];
6+
7+
@entry
8+
const add = (value: int, store: storage_type): return_type =>
9+
[[], store + value];
10+
11+
@entry
12+
const sub = (value: int, store: storage_type): return_type =>
13+
[[], store - value];
14+
15+
}
16+
17+
const test = (() => {
18+
19+
const contract = Test.Originate.contract(contract_of(Counter), 0, 0tez);
20+
Test.Contract.transfer_exn(Test.Typed_address.get_entrypoint("add", contract.taddr), 5, 0tez);
21+
Test.Contract.transfer_exn(Test.Typed_address.get_entrypoint("sub", contract.taddr), 2, 0tez);
22+
Assert.assert(Test.Typed_address.get_storage(contract.taddr) == 3);
23+
24+
})();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module Test = Test.Next
2+
3+
module Counter = struct
4+
type storage_type = int
5+
type return_type = operation list * storage_type
6+
7+
[@entry]
8+
let add (value : int) ( store: storage_type) : return_type =
9+
[], store + value
10+
11+
[@entry]
12+
let sub (value : int) ( store: storage_type) : return_type =
13+
[], store - value
14+
15+
end
16+
17+
let test =
18+
19+
let contract = Test.Originate.contract (contract_of Counter) 0 0tez in
20+
let _ = Test.Contract.transfer_exn (Test.Typed_address.get_entrypoint "add" contract.taddr) 5 0tez in
21+
let _ = Test.Contract.transfer_exn (Test.Typed_address.get_entrypoint "sub" contract.taddr) 2 0tez in
22+
Assert.assert ((Test.Typed_address.get_storage contract.taddr) = 3)

0 commit comments

Comments
 (0)