Skip to content

Commit

Permalink
Move time lock to advanced, explain contract call as well.
Browse files Browse the repository at this point in the history
  • Loading branch information
msinkec committed Sep 23, 2023
1 parent f4195b4 commit e9ab171
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 29 deletions.
69 changes: 52 additions & 17 deletions docs/tutorials/timeLock.md → docs/advanced/timeLock.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
sidebar_position: 2
sidebar_position: 9
---

# Tutorial 2: Time Lock
# Time Lock

## Overview

In this tutorial, we will go over how to create a smart contract, which has a public method, that can only be unlocked once a certain point in time has passed.
In this section, we will go over how to create a smart contract, which has a public method, that can only be unlocked once a certain point in time has passed.

### What is a time lock?

Expand All @@ -26,19 +26,13 @@ Note that the value of `locktime` can either be a Unix timestamp or a block heig

sCrypt offers a convenient built-in function `timeLock` to enforce this constraint.

## Contract properties

Let's declare the properties of our smart contract:

```ts
// Time after which our public method can be called.
@prop()
readonly matureTime: bigint // Can be a timestamp or block height.
```

## The Time-Locked Public Method
// ...

```ts
@method()
public unlock() {
// The following assertion ensures that the `unlock` method can
Expand All @@ -49,6 +43,53 @@ public unlock() {

It is important to note that this mechanism can be employed solely to ensure that a method can be called **after** a specific point in time. In contrast, it cannot be employed to ensure that a method is called **before** a specific point in time.


#### Calling

Upon a method call to the `unlock` method defined above, we need to set the `locktime` value of the transaction that will call the public method. We can do this by simply setting the `locktime` paramater of `MethodCallOptions`.

```ts
timeLock.methods.unlock(
{
lockTime: 1673523720
} as MethodCallOptions<TimeLock>
)
```

Internally this will also set the inputs `sequence` to a value lower than `0xffffffff`. We can also set this value explicitly.


```ts
timeLock.methods.unlock(
{
lockTime: 1673523720,
sequence: 0
} as MethodCallOptions<TimeLock>
)
```

Lastly, if we are using a [custom transaction builder](../how-to-deploy-and-call-a-contract/how-to-customize-a-contract-tx.md) we need to set these values for the unsigned transaction that we are building there.

```ts
instance.bindTxBuilder('unlock',
async (
current: TimeLock,
options: MethodCallOptions<TimeLock>
) => {

// ...

if (options.lockTime) {
unsignedTx.setLockTime(options.lockTime)
}
unsignedTx.setInputSequence(0, 0)

// ...
}
)
```


## How does it work?

Under the hood, the `timeLock` function asserts that the `sequence` value of our calling transaction is less than `UINT_MAX`. This ensures that the Bitcoin network will enforce the `locktime` value.
Expand All @@ -57,10 +98,4 @@ Next, it checks if our target time-lock value indicates a block height or a Unix

Lastly, the method verifies that the value of `locktime` is greater than or equal to the time-lock we have passed as an argument.

For more information on how the `locktime` and `sequence` values work, please read the [BSV wiki page](https://wiki.bitcoinsv.io/index.php/NLocktime_and_nSequence)."

## Conclusion

Congratulations! You have completed the time-lock tutorial!

The full code along with [tests](https://github.com/sCrypt-Inc/boilerplate/blob/master/tests/timeLock.test.ts) can be found in sCrypt's [boilerplate repository](https://github.com/sCrypt-Inc/boilerplate/blob/master/src/contracts/timeLock.ts).
For more information on how the `locktime` and `sequence` values work, please read the [BSV wiki page](https://wiki.bitcoinsv.io/index.php/NLocktime_and_nSequence).
2 changes: 2 additions & 0 deletions docs/how-to-write-a-contract/built-ins.md
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,8 @@ class TimeLock extends SmartContract {
This mechanism can be employed solely to ensure that a method can be called **after** a specific point in time. In contrast, it cannot be employed to ensure that a method is called **before** a specific point in time.
:::

To learn more about time locks, see the [dedicated doc section](../advanced/timeLock.md).

### `insertCodeSeparator`

Method `insertCodeSeparator(): void` inserts an [`OP_CODESEPARATOR`](../advanced/codeseparator.md), where it is invoked.
Expand Down
4 changes: 2 additions & 2 deletions docs/tutorials/auction.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
sidebar_position: 3
sidebar_position: 2
---

# Tutorial 3: Auction
# Tutorial 2: Auction

## Overview

Expand Down
4 changes: 2 additions & 2 deletions docs/tutorials/escrow.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
sidebar_position: 8
sidebar_position: 7
---

# Tutorial 8: Escrow
# Tutorial 7: Escrow

## Overview

Expand Down
4 changes: 2 additions & 2 deletions docs/tutorials/oracle.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
sidebar_position: 4
sidebar_position: 3
---

# Tutorial 4: Oracle
# Tutorial 3: Oracle

## Overview

Expand Down
4 changes: 2 additions & 2 deletions docs/tutorials/tic-tac-toe.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
sidebar_position: 5
sidebar_position: 4
---

# Tutorial 5: Tic Tac Toe
# Tutorial 4: Tic Tac Toe

## Overview
In this tutorial, we will go over how to use sCrypt to build a Tic-Tac-Toe Contract on Bitcoin.
Expand Down
4 changes: 2 additions & 2 deletions docs/tutorials/voting.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
sidebar_position: 7
sidebar_position: 6
---

# Tutorial 7: Voting
# Tutorial 6: Voting

## Overview

Expand Down
4 changes: 2 additions & 2 deletions docs/tutorials/zkp.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
sidebar_position: 6
sidebar_position: 5
---

# Tutorial 6: Zero Knowledge Proofs
# Tutorial 5: Zero Knowledge Proofs

## Overview

Expand Down

0 comments on commit e9ab171

Please sign in to comment.