Skip to content

Commit 6c4d122

Browse files
authored
Merge pull request #59 from polkadot-api/ink-sdk
update ink-sdk docs with latest changes
2 parents 4375ef8 + 5f150a3 commit 6c4d122

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

docs/pages/ink.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ So to generate the types for a contract, run the `ink add` command:
2929
> pnpm papi ink add "path to .contract or .json metadata file"
3030
```
3131

32-
This will add the contract to the `.papi` subfolder, and generate the type descriptors for the ink! contract. These can be found in `@polkadot-api/descriptors` within an object named "contracts".
32+
This will add the contract to the `.papi` subfolder, and generate the type descriptors for the ink! contract. These can be found in `@polkadot-api/descriptors` within an object named "contracts", and are keyed by contract name.
33+
34+
The contract name by default is the one specified in the contract's metadata, but if needed it can be overriden with the `--key` parameter in `ink add`.
3335

3436
The generated code contains all the types, and also the required info from the metadata to encode and decode values.
3537

docs/pages/sdks/ink-sdk.md

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Ink! SDK
22

3-
The Ink! SDK is a library for interacting with smart contracts, built on top of the [Ink! Client](/ink).
3+
The Ink! SDK is a library for interacting with smart contracts, built on top of the [Ink! Client](/ink). It supports both pallet contracts (for ink!v5 or below) and pallet revive (for ink!v6 and above).
44

55
## Getting Started
66

@@ -10,32 +10,38 @@ Install the sdk through your package manager:
1010
pnpm i @polkadot-api/sdk-ink
1111
```
1212

13-
Begin by generating the type definitions for your chain and contract. For example, using a PSP22 contract on the test Aleph Zero network:
13+
Begin by generating the type definitions for your chain and contract. For example, using a PSP22 contract on the test Paseo Pop network:
1414

1515
```sh
16-
pnpm papi add -w wss://aleph-zero-testnet-rpc.dwellir.com testAzero
16+
pnpm papi add -w wss://rpc1.paseo.popnetwork.xyz pop
1717
pnpm papi ink add ./psp22.json # Path to the .contract or .json metadata file
1818
```
1919

20+
:::note
21+
You can find a working example in [the ink-sdk repo](https://github.com/polkadot-api/papi-sdks/tree/main/examples/ink-playground). An example PSP22 contract and its metadata is available in the `./contracts/psp22_mod` folder.
22+
:::
23+
2024
This process uses the name defined in the contract metadata to export it as a property of `contracts` in `@polkadot-api/descriptors`. For this example, the contract name is "psp22." You can now instantiate the Ink! SDK:
2125

2226
```ts
23-
import { contracts, testAzero } from "@polkadot-api/descriptors"
27+
import { contracts, pop } from "@polkadot-api/descriptors"
2428
import { createInkSdk } from "@polkadot-api/sdk-ink"
2529
import { createClient } from "polkadot-api"
2630
import { withPolkadotSdkCompat } from "polkadot-api/polkadot-sdk-compat"
2731
import { getWsProvider } from "polkadot-api/ws-provider/web"
2832

2933
const client = createClient(
30-
withPolkadotSdkCompat(
31-
getWsProvider("wss://aleph-zero-testnet-rpc.dwellir.com"),
32-
),
34+
withPolkadotSdkCompat(getWsProvider("wss://rpc1.paseo.popnetwork.xyz")),
3335
)
34-
const typedApi = client.getTypedApi(testAzero)
36+
const typedApi = client.getTypedApi(pop)
3537

3638
const psp22Sdk = createInkSdk(typedApi, contracts.psp22)
3739
```
3840

41+
:::note
42+
When using a ink!v6 contract, use `createReviveSdk` instead of `createInkSdk`. Both have the same API, but revive sdk uses the pallet-revive instead, which is where ink!v6 contracts are deployed.
43+
:::
44+
3945
The SDK provides two main functions for different workflows:
4046

4147
- `getDeployer(code: Binary)`: Returns an API for deploying contracts.
@@ -56,7 +62,7 @@ The deployer API supports two methods: one for dry-running deployments and anoth
5662

5763
When deploying, the SDK calculates all parameters, requiring only the constructor arguments defined in the contract.
5864

59-
To calculate the gas limit required, the SDK also needs the origin account the transaction will be sent from. So it either uses the origin account or the gas limit if you already have it.
65+
To calculate the gas limit and storage deposit limit required, the SDK also needs the origin account the transaction will be sent from. So it either uses the origin account or the gas limit if you already have it.
6066

6167
The "salt" parameter ensures unique contract deployments. By default, it is empty, but you can provide a custom value for deploying multiple instances of the same contract.
6268

@@ -75,10 +81,10 @@ const tx = psp22Deployer.deploy("new", {
7581
const result = await tx.signAndSubmit(aliceSigner)
7682

7783
// To get the resulting address the contract was deployed to, we can pass the events back into the SDK:
78-
const data: {
84+
const data: Array<{
7985
address: string
8086
contractEvents: EventDescriptor[]
81-
} | null = psp22Sdk.readDeploymentEvents(ALICE, result.events)
87+
}> = psp22Sdk.readDeploymentEvents(result.events)
8288
```
8389

8490
Dry-running takes in the same arguments, but returns a Promise with the result directly instead:
@@ -105,6 +111,11 @@ if (dryRunResult.success) {
105111
gasRequired: Gas;
106112
}
107113
*/
114+
115+
// The dry-run result also has a method to get the transaction to perform the actual deployment.
116+
const deploymentResult = await dryRunResult.value
117+
.deploy()
118+
.signAndSubmit(aliceSigner)
108119
}
109120
```
110121

@@ -138,6 +149,9 @@ const result = await psp22Contract.query("PSP22::balance_of", {
138149
if (result.success) {
139150
console.log("balance of alice", result.value.response)
140151
console.log("events", result.value.events)
152+
153+
// The dry-run result also has a method to get the transaction to send the same message to the contract.
154+
const callResult = await result.value.send().signAndSubmit(aliceSigner)
141155
} else {
142156
console.log("error", result.value)
143157
}
@@ -230,3 +244,7 @@ if (aliceBalance.success) {
230244
console.log("Alice balance", aliceBalance.value)
231245
}
232246
```
247+
248+
:::note
249+
Storage for `createReviveSdk` is not yet available, pending an update on polkadot-sdk.
250+
:::

0 commit comments

Comments
 (0)