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 5c4a40e
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions docs/how-to-test-a-contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ const deployTx = await instance.deploy(1)
console.log('Demo contract deployed: ', deployTx.id)
```

## Call a Public Method
## Call a Method

You can call a contract's public `@method` on the blockchain as follows:
### Public
You can call a contract's public `@method` as [before](./how-to-deploy-and-call-a-contract/how-to-deploy-and-call-a-contract.md#contract-call):

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

## Call a Non-Public Method
### Non-Public

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 +98,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 5c4a40e

Please sign in to comment.