Skip to content

Commit

Permalink
Merge pull request #1563 from aeternity/fix/examples
Browse files Browse the repository at this point in the history
docs(examples): make it v12 compatible
  • Loading branch information
davidyuk authored Jun 17, 2022
2 parents 5ed5999 + 392da3b commit 967de89
Show file tree
Hide file tree
Showing 21 changed files with 149 additions and 210 deletions.
34 changes: 4 additions & 30 deletions docs/contrib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ low-level access to it's endpoints, when necessary.

It uses the following Javascript technologies and principles:

- [stampit] provides composable Factories based on the [Stamp
Specification]. This is how aepp-sdk approached the [composition over
inheritance] principle.
- [JavaScript the Good Parts] (because Crockford is always right)
- [ES6 modules], using `export` and `import`
- [Promises] using ES7 [async/await] syntax, where applicable
Expand Down Expand Up @@ -65,19 +62,19 @@ npm run docs:examples && npm run docs:api

## Building

aepp-sdk is built using [pnpm]. In order to build a development version, launch the `build:dev` command.
aepp-sdk is built using npm. In order to build a development version, launch the `build:dev` command.

```bash
pnpm install
pnpm run build:dev
npm install
npm run build:dev
```

## Testing

To test, launch the `test` command. This will run [mocha](https://mochajs.org/)'s tests locally.

```bash
pnpm run test
npm test
```

This repository also includes a docker-compose file, to allow you to **run your own æternity node locally**. If you want to do so, **from the root of the project**:
Expand All @@ -96,34 +93,11 @@ services:
2. Run `docker-compose up node`
3. Congrats! you're now running your own æternity node locally.


## Composing new Flavors
You can also "compose" your own flavor by mixing 2 or more flavors likes so:

```js
import { Wallet, Contract, MemoryAccount } from '@aeternity/aepp-sdk'
// make a "mixed flavor" containing Wallet and Contracts flavors
Wallet.compose(Contract)({
url: 'https://testnet.aeternity.io',
accounts: [new MemoryAccount({keypair: {secretKey: account.priv, publicKey: account.pub}})],
address: account.pub,
onTx: true, // or a function to Guard the Rpc client
onChain: true, // or a function to Guard the Rpc client
onAccount: true, // or a function to Guard the Rpc client
networkId: 'ae_uat'
}).then(ae => {
// ae is your initialised client now! :)
// ...
```

The WebPack compilation provides two different build artifacts in `dist/`, one
for Node.js and one for browsers. When referencing aepp-sdk through any modern
build tooling, it should pick the right one automatically through the entry
points defined in `package.json`.

[pnpm]: https://pnpm.js.org/

## Installation / Linking

In order to add a local development version of aepp-sdk to a project, `npm link`[1] can be used.
Expand Down
12 changes: 6 additions & 6 deletions docs/guides/aens.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Claiming an AENS name requires you (at least) 2 transactions:
```js
// imports

const aeSdk = await Universal({ ... }) // init the SDK instance with Universal Stamp
const aeSdk = new AeSdk({ ... }) // init the SDK instance with AeSdk class

const name = 'testNameForTheGuide.chain'

Expand Down Expand Up @@ -188,7 +188,7 @@ const pointers = {
channel: 'ch_2519mBsgjJEVEFoRgno1ryDsn3BEaCZGRbXPEjThWYLX9MTpmk',
}

// using aeSdk directly (instance of Universal Stamp)
// using aeSdk directly (instance of AeSdk class)
const nameUpdateTx = await aeSdk.aensUpdate(name, pointers)

// OR using the instance of a name
Expand Down Expand Up @@ -244,7 +244,7 @@ In case you want to extend a name using a custom TTL and keep the current pointe
```js
const name = 'testNameForTheGuide.chain'

// using aeSdk directly (instance of Universal Stamp)
// using aeSdk directly (instance of AeSdk class
const nameUpdateTx = await aeSdk.aensUpdate(name, {}, { nameTtl: 100000, extendPointers: true })

// OR using the instance of a name
Expand Down Expand Up @@ -289,7 +289,7 @@ In some cases you might want to transfer the ownership of a name to another acco
```js
const recipient = 'ak_...'

// using aeSdk directly (instance of Universal Stamp)
// using aeSdk directly (instance of AeSdk class)
const nameTransferTx = await aeSdk.aensTransfer(name, recipient)

// OR using the instance of a name
Expand Down Expand Up @@ -323,7 +323,7 @@ console.log(nameTransferTx)
## 4. Revoke a name
In case you want to revoke a name prior to its expiration for whatever reason you can do that as follows:
```js
// using aeSdk directly (instance of Universal Stamp)
// using aeSdk directly (instance of AeSdk class)
const nameRevokeTx = await aeSdk.aensRevoke(name)

// OR using the instance of a name
Expand Down Expand Up @@ -366,7 +366,7 @@ This functionality could for example be used to build an AENS marketplace.
```js
// imports

const aeSdk = await Universal({ ... }) // init the SDK instance with Universal Stamp
const aeSdk = new AeSdk({ ... }) // init the SDK instance with AeSdk class

// contract address
const contractId = 'ct_asd2ks...'
Expand Down
67 changes: 32 additions & 35 deletions docs/guides/build-wallet.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ const readyStateCheckInterval = setInterval(function () {
}, 10)
```

### 2. Initialize `RpcWallet` Stamp
Then you need to initialize `RpcWallet` Stamp in your extension and subscribe for new `runtime` connections.
### 2. Initialize `AeSdkWallet` class
Then you need to initialize `AeSdkWallet` class in your extension and subscribe for new `runtime` connections.
After the connection is established you can share the wallet details with the application.

```js
Expand All @@ -54,15 +54,12 @@ const accounts = [
const aeppInfo = {}

async function init () {
// Init extension stamp from sdk
RpcWallet({
const aeSdk = new AeSdkWallet({
compilerUrl: COMPILER_URL,
nodes: [{ name: 'testnet', instance: new Node(NODE_URL) }],
id: browser.runtime.id,
type: WALLET_TYPE.extension,
name: 'Wallet WebExtension',
// The `ExtensionProvider` uses the first account by default. You can change active account using `selectAccount(address)` function
accounts,
// Hook for sdk registration
onConnection (aeppId, params) {
if (!confirm(`Aepp ${params.name} with id ${aeppId} wants to connect`)) {
Expand Down Expand Up @@ -97,18 +94,19 @@ async function init () {
throw new RpcRejectedByUserError()
}
}
}).then(wallet => {
chrome.runtime.onConnect.addListener(async function (port) {
// create connection
const connection = new BrowserRuntimeConnection({ port })
// add new aepp to wallet
const clientId = aeSdk.addRpcClient(connection)
// share wallet details
aeSdk.shareWalletInfo(clientId)
setInterval(() => aeSdk.shareWalletInfo(clientId), 3000)
})
}).catch(err => {
console.error(err)
})
// You can change active account using `selectAccount(address)` function
await aeSdk.addAccount(accounts[0], { select: true })
await aeSdk.addAccount(accounts[1])

chrome.runtime.onConnect.addListener(async function (port) {
// create connection
const connection = new BrowserRuntimeConnection({ port })
// add new aepp to wallet
const clientId = aeSdk.addRpcClient(connection)
// share wallet details
aeSdk.shareWalletInfo(clientId)
setInterval(() => aeSdk.shareWalletInfo(clientId), 3000)
})
}

Expand All @@ -128,20 +126,18 @@ const accounts = [
]

async function init () {
// Init extension stamp from sdk
RpcWallet({
// Init extension class from sdk
const aeSdk = new AeSdkWallet({
compilerUrl: COMPILER_URL,
nodes: [{ name: 'testnet', instance: new Node(NODE_URL) }],
id: browser.runtime.id,
type: WALLET_TYPE.extension,
name: 'Wallet WebExtension',
// The `ExtensionProvider` uses the first account by default. You can change active account using `selectAccount(address)` function
accounts,
// Hook for sdk registration
onConnection (aeppId, params, origin) {
if (confirm(`Aepp ${params.name} with id ${aeppId} wants to connect`)) {
// Whitelist aepp domains for node connection
const aepps = ['https://test', 'https://aepp.aeternity.com']
const aepps = ['https://aepps.superhero.com', 'https://aepp.aeternity.com']
if (params.connectNode && aepps.includes(origin)) {}
else throw new RpcConnectionDenyError()
// Connect to aepp without sharing node URLs
Expand Down Expand Up @@ -177,18 +173,19 @@ async function init () {
throw new RpcRejectedByUserError()
}
}
}).then(wallet => {
chrome.runtime.onConnect.addListener(async function (port) {
// create connection
const connection = new BrowserRuntimeConnection({ port })
// add new aepp to wallet
const clientId = aeSdk.addRpcClient(connection)
// share wallet details
aeSdk.shareWalletInfo(clientId)
setInterval(() => aeSdk.shareWalletInfo(clientId), 3000)
})
}).catch(err => {
console.error(err)
})
// You can change active account using `selectAccount(address)` function
await aeSdk.addAccount(accounts[0], { select: true })
await aeSdk.addAccount(accounts[1])

chrome.runtime.onConnect.addListener(async function (port) {
// create connection
const connection = new BrowserRuntimeConnection({ port })
// add new aepp to wallet
const clientId = aeSdk.addRpcClient(connection)
// share wallet details
aeSdk.shareWalletInfo(clientId)
setInterval(() => aeSdk.shareWalletInfo(clientId), 3000)
})
}

Expand Down
12 changes: 6 additions & 6 deletions docs/guides/connect-aepp-to-wallet.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ You can build your own wallet in the next example
## 1. Specify imports and constants and state

```js
import { RpcAepp, walletDetector, BrowserWindowMessageConnection, Node } from '@aeternity/aepp-sdk'
import { AeSdkAepp, walletDetector, BrowserWindowMessageConnection, Node } from '@aeternity/aepp-sdk'

const TESTNET_NODE_URL = 'https://testnet.aeternity.io'
const MAINNET_NODE_URL = 'https://mainnet.aeternity.io'
Expand All @@ -27,11 +27,11 @@ export default {
}
```

## 2. Initialize the `RpcAepp` Stamp
## 2. Initialize the `AeSdkAepp` class

```js
async created () {
this.aeSdk = await RpcAepp({
created () {
this.aeSdk = new AeSdkAepp({
name: 'Simple Aepp',
nodes: [
{ name: 'ae_uat', instance: new Node(TESTNET_NODE_URL) },
Expand Down Expand Up @@ -73,7 +73,7 @@ methods: {
}
```

## 4. Connect to a wallet
## 4a. Connect to a wallet

Append method for wallet connection

Expand All @@ -87,7 +87,7 @@ async connect(wallet) {
}
```

## 4. Use Wallet's Node for chain communication
## 4b. Connect to a wallet: Use Wallet's Node for chain communication

Aepp can request the wallet to share its connected node URLs if any to interact with the chain.

Expand Down
2 changes: 1 addition & 1 deletion docs/guides/contract-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Note:
Of course it is also possible to decode the event log if you request the transaction details from the node for a transaction that has been mined already. You can request the transaction details by providing the tx-hash and then decode the event log using the `contractInstance` as follows:
```js
const txHash = 'th_2YV3AmAz2kXdTnQxXtR2uxQi3KuLS9wfvXyqKkQQ2Y6dE6RnET';
// aeSdk is an instance of the Universal Stamp
// aeSdk is an instance of the AeSdk class
const txInfo = await aeSdk.api.getTransactionInfoByHash(txHash)

// decode events using contract instance
Expand Down
9 changes: 4 additions & 5 deletions docs/guides/contracts.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ Note:
## 1. Specify imports
```js
// node.js import
const { Universal, MemoryAccount, Node } = require('@aeternity/aepp-sdk')
const { AeSdk, MemoryAccount, Node } = require('@aeternity/aepp-sdk')
// ES import
import { Universal, MemoryAccount, Node } from '@aeternity/aepp-sdk'
import { AeSdk, MemoryAccount, Node } from '@aeternity/aepp-sdk'
```

## 2. Create an instance of the SDK
Expand All @@ -32,14 +32,13 @@ const account = new MemoryAccount({
keypair: { secretKey: SECRET_KEY, publicKey: PUBLIC_KEY }
})

const aeSdk = await Universal({
const aeSdk = new AeSdk({
nodes: [
{ name: 'testnet', instance: node }
],
compilerUrl: 'https://compiler.aepps.com', // ideally host your own compiler
accounts: [account]
})

await aeSdk.addAccount(accoount, { select: true })
```

Note:
Expand Down
7 changes: 4 additions & 3 deletions docs/guides/error-handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ BaseError
```js
// import required error classes
const {
Universal,
AeSdk,
Node,
MemoryAccount,
generateKeyPair,
Expand All @@ -129,10 +129,11 @@ const NEW_USER_KEYPAIR = generateKeyPair()
const payerAccount = new MemoryAccount({ keypair: PAYER_ACCOUNT_KEYPAIR })
const newUserAccount = new MemoryAccount({ keypair: NEW_USER_KEYPAIR })
const node = new Node(NODE_URL)
const aeSdk = await Universal({
const aeSdk = new AeSdk({
nodes: [{ name: 'testnet', instance: node }],
accounts: [payerAccount, newUserAccount]
})
await aeSdk.addAccount(payerAccount, { select: true })
await aeSdk.addAccount(newUserAccount)

// catch exceptions
try {
Expand Down
20 changes: 13 additions & 7 deletions docs/guides/low-vs-high-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*Alexander Kahl.*

The purist uses the functions generated out of the Swagger
file. After creating the SDK instance `aeSdk` with the Universal Stamp it exposes a mapping of all `operationId`s as functions, converted to camelCase (from PascalCase). So e.g. in order to get a transaction
file. After creating the SDK instance `aeSdk` with the AeSdk class it exposes a mapping of all `operationId`s as functions, converted to camelCase (from PascalCase). So e.g. in order to get a transaction
based on its hash you would invoke `aeSdk.api.getTransactionByHash('th_...')`.

In this way the SDK is simply a mapping of the raw API calls into
Expand All @@ -18,16 +18,19 @@ of chain operations, so the SDK provides abstractions for these.
Example spend function, using æternity's SDK abstraction.

```js
import { MemoryAccount, Node, Universal } from '@aeternity/aepp-sdk'
import { MemoryAccount, Node, AeSdk } from '@aeternity/aepp-sdk'

async function init () {
const node = new Node('https://testnet.aeternity.io') // ideally host your own node!

const aeSdk = await Universal({
const aeSdk = new AeSdk({
nodes: [{ name: 'testnet', instance: node }],
compilerUrl: 'https://compiler.aepps.com', // ideally host your own compiler!
accounts: [new MemoryAccount({keypair: {secretKey: '<PRIV_KEY_HERE>', publicKey: '<PUB_KEY_HERE>'}})],
})
await aeSdk.addAccount(
new MemoryAccount({keypair: {secretKey: '<PRIV_KEY_HERE>', publicKey: '<PUB_KEY_HERE>'}}),
{ select: true }
)

// log transaction info
console.log(await aeSdk.spend(100, 'ak_...'))
Expand All @@ -37,15 +40,18 @@ async function init () {
## Low-level SDK usage (use [API](https://aeternity.com/protocol/node/api) endpoints directly)
Example spend function, using the SDK, talking directly to the [**API**](https://aeternity.com/protocol/node/api):
```js
import { MemoryAccount, Node, Universal } from '@aeternity/aepp-sdk'
import { MemoryAccount, Node, AeSdk } from '@aeternity/aepp-sdk'

async function spend (amount, recipient) {
const node = new Node('https://testnet.aeternity.io') // ideally host your own node!
const aeSdk = await Universal({
const aeSdk = new AeSdk({
nodes: [{ name: 'testnet', instance: node }],
compilerUrl: 'https://compiler.aepps.com', // ideally host your own compiler!
accounts: [new MemoryAccount({keypair: {secretKey: '<PRIV_KEY_HERE>', publicKey: '<PUB_KEY_HERE>'}})],
})
await aeSdk.addAccount(
new MemoryAccount({keypair: {secretKey: '<PRIV_KEY_HERE>', publicKey: '<PUB_KEY_HERE>'}}),
{ select: true }
)

// builds an unsigned SpendTx using the debug endpoint of the node's API
const spendTx = await aeSdk.buildTx(TX_TYPE.spend, {
Expand Down
Loading

0 comments on commit 967de89

Please sign in to comment.