Skip to content

Commit

Permalink
docs(chain): add low/high level api examples
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Nov 19, 2024
1 parent 2d6ec81 commit cb80689
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
18 changes: 18 additions & 0 deletions examples/node/_api-high-level.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { AeSdk, Node, MemoryAccount, encode, Encoding } from '@aeternity/aepp-sdk';

const aeSdk = new AeSdk({
nodes: [
{
name: 'testnet',
instance: new Node('https://testnet.aeternity.io'), // host your node for better decentralization
},
],
accounts: [new MemoryAccount('sk_2CuofqWZHrABCrM7GY95YSQn8PyFvKQadnvFnpwhjUnDCFAWmf')],
});

const transactionInfo = await aeSdk.spend(
100, // aettos
'ak_21A27UVVt3hDkBE5J7rhhqnH5YNb4Y1dqo4PnSybrH85pnWo7E',
{ payload: encode(Buffer.from('spend tx payload'), Encoding.Bytearray) },
);
console.log(transactionInfo);
19 changes: 19 additions & 0 deletions examples/node/_api-low-level.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Node, MemoryAccount, buildTxAsync, Tag, encode, Encoding } from '@aeternity/aepp-sdk';

const onNode = new Node('https://testnet.aeternity.io'); // host your node for better decentralization
const account = new MemoryAccount('sk_2CuofqWZHrABCrM7GY95YSQn8PyFvKQadnvFnpwhjUnDCFAWmf');

const spendTx = await buildTxAsync({
tag: Tag.SpendTx,
senderId: account.address,
recipientId: 'ak_21A27UVVt3hDkBE5J7rhhqnH5YNb4Y1dqo4PnSybrH85pnWo7E',
amount: 100, // aettos
payload: encode(Buffer.from('spend tx payload'), Encoding.Bytearray),
onNode,
});

const signedTx = await account.signTransaction(spendTx, { networkId: await onNode.getNetworkId() });

// broadcast the signed tx to the node
const { txHash } = await onNode.postTransaction({ tx: signedTx });
console.log(txHash);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"build:api:middleware": "autorest tooling/autorest/middleware.yaml && node tooling/autorest/postprocessing.js middleware",
"build:generate": "tsx tooling/generate-schema.ts",
"build": "run-p build:api:* build:assets build:generate && run-p build:dist build:es build:types",
"docs:examples": "node tooling/docs/examples-to-md.js examples/node/*.js",
"docs:examples": "node tooling/docs/examples-to-md.js examples/node/[^_]*.js",
"docs:api": "typedoc",
"commitlint": "commitlint --from develop",
"lint": "run-p lint:*",
Expand Down
2 changes: 2 additions & 0 deletions test/examples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ run_node_example paying-for-spend-tx.js
run_node_example transfer-ae.js
run_node_example dry-run-using-debug-endpoint.js
run_node_example oracle.js
run_node_example _api-high-level.js
run_node_example _api-low-level.js

# TODO: revisit --ignore-scripts after solving https://github.com/npm/cli/issues/4202
perl -i -pe 's/"prepare"/"rem-prepare"/g' package.json
Expand Down

0 comments on commit cb80689

Please sign in to comment.