-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
doc: Documents statements and expressions for declaring and accessing…
… variables
- Loading branch information
Showing
7 changed files
with
144 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# Accessing Variables | ||
|
||
## Access a local variable | ||
|
||
The expression `GetVariable` is used to get the value stored in a local variable. | ||
|
||
- [TypeDoc](https://romarq.github.io/smartts-sdk/api/modules/expression.html#GetVariable) | ||
|
||
```ts | ||
const { | ||
Contract, | ||
EntryPoint, | ||
SetValue, | ||
NewVariable, | ||
ContractStorage, | ||
GetVariable, | ||
TString, | ||
} = require('@tezwell/smartts-sdk'); | ||
|
||
const contract = new Contract() | ||
.setStorageType(TString()) | ||
.addEntrypoint( | ||
new EntryPoint('entry_point_1') | ||
.setInputType(TString()) | ||
.code((argument) => [ | ||
// let some_variable = "SOME_PREFIX_" + <argument>; | ||
NewVariable("some_variable", Concat(["SOME_PREFIX_", argument])), | ||
// <contract>.<storage> = some_variable | ||
SetValue(ContractStorage(), GetVariable("some_variable")) | ||
]), | ||
); | ||
``` | ||
|
||
## Access the contract storage | ||
|
||
The expression `ContractStorage` is used to access the contract storage. | ||
|
||
- [TypeDoc](https://romarq.github.io/smartts-sdk/api/modules/expression.html#ContractStorage) | ||
|
||
```ts | ||
const { | ||
Contract, | ||
EntryPoint, | ||
Require, | ||
ContractStorage, | ||
String, | ||
TBool, | ||
} = require('@tezwell/smartts-sdk'); | ||
|
||
const contract = new Contract() | ||
.setStorageType(TBool()) | ||
.addEntrypoint( | ||
new EntryPoint('entry_point_1').code(() => [ | ||
// The storage is a boolean that will be "true" only when the contract is active and false otherwise | ||
Require(ContractStorage(), String("The contract is paused!")), | ||
]), | ||
); | ||
``` | ||
|
||
## Access the list of operations | ||
|
||
The expression `GetOperations` is used to get the operations list from the stack or an empty list otherwise. | ||
|
||
- [TypeDoc](https://romarq.github.io/smartts-sdk/api/modules/expression.html#GetOperations) | ||
|
||
```ts | ||
const { | ||
Contract, | ||
EntryPoint, | ||
SetValue, | ||
GetOperations, | ||
SetDelegate, | ||
PrependToList, | ||
None | ||
} = require('@tezwell/smartts-sdk'); | ||
|
||
const contract = new Contract() | ||
.addEntrypoint( | ||
new EntryPoint('entry_point_1').code(() => [ | ||
// Add a new operation to the stack | ||
SetValue( | ||
GetOperations(), | ||
PrependToList( | ||
GetOperations(), | ||
SetDelegate(None()), | ||
), | ||
), | ||
]), | ||
); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Declaring variables | ||
|
||
## NewVariable | ||
|
||
The statement `NewVariable` is used to declare local scoped variables. | ||
|
||
- [TypeDoc](https://romarq.github.io/smartts-sdk/api/modules/statement.html#NewVariable) | ||
|
||
```ts | ||
const { | ||
Contract, | ||
EntryPoint, | ||
ContractStorage, | ||
ForEachOf, | ||
NewVariable, | ||
SetValue, | ||
Add, | ||
PrependToList, | ||
GetVariable, | ||
TUnit, | ||
List, | ||
Nat | ||
} = require('@tezwell/smartts-sdk'); | ||
|
||
const contract = new Contract() | ||
// let <contract>.<storage> = [1, 2, 3]; | ||
.setStorage(List([Nat(1), Nat(2), Nat(3)])) | ||
.addEntrypoint( | ||
new EntryPoint('entry_point_1') | ||
.setInputType(TUnit()) | ||
.code(() => [ | ||
// let sum = 0; | ||
NewVariable("sum", Nat(0)), | ||
// <contract>.<storage>.forEach((el) => { | ||
// sum += el; | ||
// }) | ||
ForEachOf(ContractStorage()).Do((el) => [ | ||
SetValue(GetVariable("sum"), Add(GetVariable("sum"), el)) | ||
]), | ||
// <contract>.<storage> = [...<contract>.<storage>, sum]; | ||
SetValue(ContractStorage(), PrependToList(ContractStorage(), GetVariable("sum"))) | ||
]) | ||
); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters