docs: moving contract methods to main docs

This commit is contained in:
asiaziola
2022-05-25 16:15:50 +02:00
parent 5dbb811599
commit 4f61313328
5 changed files with 232 additions and 219 deletions

182
README.md
View File

@@ -21,14 +21,13 @@ To further improve contract state evaluation time, one can additionally use AWS
- [Installation](#installation)
- [Import](#import)
- [Using the Warp Gateway](#using-the-warp-gateway)
- [Contract methods](#contract-methods)
- [WASM](#wasm)
- [VM2](#vm2)
- [Internal writes](#internal-writes)
- [Performance - best practices](#performance---best-practices)
- [Examples](#examples)
- [Migration guide](#migration-guide)
- [Documentation](#documentation)
- [Missing features](#missing-features)
## Architecture
@@ -188,6 +187,173 @@ const warp = WarpNodeFactory.memCachedBased(arweave).useWarpGateway({ notCorrupt
More examples can be found [here](https://github.com/redstone-finance/redstone-smartcontracts-examples/blob/main/src/redstone-gateway-example.ts).
### Contract methods
- [`connect`](#connect)
- [`setEvaluationOptions`](#setevaluationoptions)
- [`readState`](#readstate)
- [`viewState`](#viewstate)
- [`writeInteraction`](#writeinteraction)
- [`bundleInteraction`](#bundleInteraction)
#### `connect`
```typescript
async function connect(wallet: ArWallet): Contract<State>;
```
Allows to connect wallet to a contract. Connecting a wallet MAY be done before "viewState" (depending on contract implementation, ie. whether called contract's function required "caller" info) Connecting a wallet MUST be done before "writeInteraction".
- `wallet` a JWK object with private key or 'use_wallet' string.
<details>
<summary>Example</summary>
```typescript
const contract = smartweave.contract('YOUR_CONTRACT_TX_ID').connect(jwk);
```
</details>
---
#### `setEvaluationOptions`
```typescript
function setEvaluationOptions(options: Partial<EvaluationOptions>): Contract<State>;
```
Allows to set (EvaluationOptions)
- `options` the interaction input
- `options.ignoreExceptions` enables exceptions ignoring
- `options.waitForConfirmation` enables waiting for transaction confirmation
<details>
<summary>Example</summary>
```typescript
const contract = smartweave.contract('YOUR_CONTRACT_TX_ID').setEvaluationOptions({
waitForConfirmation: true,
ignoreExceptions: false
});
```
</details>
---
#### `readState`
```typescript
async function readState(
blockHeight?: number,
currentTx?: { contractTxId: string; interactionTxId: string }[]
): Promise<EvalStateResult<State>>;
```
Returns state of the contract at required blockHeight. Similar to the `readContract` from the version 1.
- `blockHeight` Block height for state
- `currentTx` If specified, will be used as a current transaction
<details>
<summary>Example</summary>
```typescript
const { state, validity } = await contract.readState();
```
</details>
---
#### `viewState`
```typescript
async function viewState<Input, View>(
input: Input,
blockHeight?: number,
tags?: Tags,
transfer?: ArTransfer
): Promise<InteractionResult<State, View>>;
```
Returns the "view" of the state, computed by the SWC - ie. object that is a derivative of a current state and some specific smart contract business logic. Similar to the `interactRead` from the current SDK version.
- `input` the interaction input
- `blockHeight` if specified the contract will be replayed only to this block height
- `tags` an array of tags with name/value as objects
- `transfer` target and winstonQty for transfer
<details>
<summary>Example</summary>
```typescript
const { result } = await contract.viewState<any, any>({
function: "NAME_OF_YOUR_FUNCTION",
data: { ... }
});
```
</details>
---
#### `viewStateForTx`
```typescript
async function viewStateForTx<Input, View>(
input: Input,
transaction: InteractionTx
): Promise<InteractionResult<State, View>>;
```
A version of the viewState method to be used from within the contract's source code. The transaction passed as an argument is the currently processed interaction transaction. The "caller" will be se to the owner of the interaction transaction, that requires to call this method.
💡 Note! calling "interactRead" from withing contract's source code was not previously possible - this is a new feature.
- `input` the interaction input
- `transaction` interaction transaction
<details>
<summary>Example</summary>
```typescript
const { result } = await contract.viewStateForTx<any, any>({
function: "NAME_OF_YOUR_FUNCTION",
data: { ... }
}, transaction);
```
</details>
---
#### `writeInteraction`
```typescript
async function writeInteraction<Input>(input: Input, tags?: Tags, transfer?: ArTransfer): Promise<string>;
```
Writes a new "interaction" transaction - ie. such transaction that stores input for the contract.
- `input` the interaction input
- `tags` an array of tags with name/value as objects
- `transfer` target and winstonQty for transfer
<details>
<summary>Example</summary>
```typescript
const result = await contract.writeInteraction({
function: "NAME_OF_YOUR_FUNCTION",
data: { ... }
});
```
</details>
### WASM
WASM provides proper sandboxing ensuring execution environment isolation which guarantees security to the contracts execution. As for now - **Assemblyscript**, **Rust** and **Go** languages are supported. WASM contracts templates containing example PST contract implementation within tools for compiling contracts to WASM, testing, deploying (locally, on testnet and mainnet) and writing interactions are available in a [dedicated repository](https://github.com/redstone-finance/redstone-smartcontracts-wasm-templates).
@@ -234,13 +400,13 @@ await SmartWeave.contracts.write(contractTxId, { function: 'add' });
In order for internal calls to work you need to set `evaluationOptions` to `true`:
```
```ts
const callingContract = smartweave
.contract<ExampleContractState>(calleeTxId)
.setEvaluationOptions({
internalWrites: true
})
.connect(wallet);
.contract<ExampleContractState>(calleeTxId)
.setEvaluationOptions({
internalWrites: true
})
.connect(wallet);
```
You can also perform internal read to the contract (originally introduced by the protocol):