Skip to content

Commit

Permalink
polish testing non-public method
Browse files Browse the repository at this point in the history
  • Loading branch information
xhliu committed Feb 6, 2024
1 parent 09492ed commit e04129f
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions docs/how-to-test-a-contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ console.log('Demo contract deployed: ', deployTx.id)

## Call a Public Method

You can call a contract's public `@method` on the blockchain as follows:
You can call a contract's public `@method` as follows:

```ts
// build and send tx by calling `unlock()` on `methods` object.
Expand All @@ -86,7 +86,7 @@ await instance.methods.unlock(

## Call a Non-Public Method

It is also possible to call non-public methods locally.
You can also call non-public methods.

Let's add a non-public method to our contract:

Expand All @@ -97,20 +97,29 @@ hashMessage(message: ByteString): ByteString {
}
```

You can now call this method locally like the following:
You can now call this method like the following:

```ts
const message: ByteString = toByteString('hello world')
const hashRes: ByteString = instance.hashMessage(message)
```

Note the absence of `.methods` after `instance`, compared to a public method.

If the method is static, it can be called like this:

```ts
const hashRes: ByteString = Demo.hashMessage(message)
@method()
static hashMessageStatic(message: ByteString): ByteString {
return sha256(message)
}
```

```ts
const hashRes: ByteString = Demo.hashMessageStatic(message)
```

It should be noted that non-public methods are only directly callable off-chain, i.e. for testing. On-chain, they can only be invoked through a public method.
It should be noted that non-public methods are only directly callable off-chain, e.g., for testing. On chain, they can only be invoked through a public method.

## Integrate with a testing framework

Expand Down

0 comments on commit e04129f

Please sign in to comment.