Skip to content

Commit

Permalink
Merge pull request #191 from casper-ecosystem/feat/events-in-js
Browse files Browse the repository at this point in the history
1.2 casper-cep78-js-client
  • Loading branch information
hoffmannjan authored Mar 17, 2023
2 parents f4143d2 + f7a3054 commit 4daf28a
Show file tree
Hide file tree
Showing 14 changed files with 838 additions and 171 deletions.
3 changes: 3 additions & 0 deletions client-js/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ module.exports = {
'plugin:eslint-comments/recommended',
'prettier',
],
rules: {
"import/prefer-default-export": "off"
}
};
17 changes: 17 additions & 0 deletions client-js/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.2.0] - 2023-03-16

### Added

- Added support for CEP47Events
- Added support for CES events basing on ces-js-parser
- Some small code cleanups (added typings etc)
- Some changes in `examples/`
- Added `OwnerReverseLookupMode.TransfersOnly` modality
- Added `revoke` entrypoint support

### Fixed

- Added missing `collectionName` argument in mint when using a sessionCode: true
- `contract_whitelist` is now build with Hashes rather then Keys
- Fixed inconsistency in `getBurnModeConfig()` (now it returns Number as other similar methods)

## [1.1.0] - 2023-01-10

### Added
Expand Down
56 changes: 56 additions & 0 deletions client-js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,59 @@ NETWORK_NAME=casper-net-1
MASTER_KEY_PAIR_PATH=/Users/someuser/.casper/casper-node/utils/nctl/assets/net-1/faucet
USER1_KEY_PAIR_PATH=/Users/someuser/.casper/casper-node/utils/nctl/assets/net-1/users/user-1
```

## Events Handling

As CEP-78 1.2 supports two events modes - `CEP47` and `CES` we have two parsers as a part of this SDK.

* Example usage of CEP47 parser

```
import { EventStream, EventName } from 'casper-js-sdk';
import { CEP47EventParserFactory, CEP47Events } from 'casper-cep78-js-sdk';
const cep47EventParser = CEP47EventParserFactory({
contractPackageHash,
eventNames: [
CEP47Events.Mint,
CEP47Events.Transfer,
CEP47Events.Burn
],
});
const es = new EventStream(EVENT_STREAM_ADDRESS);
es.subscribe(EventName.DeployProcessed, async (event) => {
const parsedEvents = cep47EventParser(event);
if (parsedEvents?.success) {
console.log(parsedEvents.data);
}
});
es.start();
```

* Example usage of CES parser

```
import { EventStream, EventName, CasperServiceByJsonRPC } from 'casper-js-sdk';
import { CESEventParserFactory } from 'casper-cep78-js-sdk';
const casperClient = new CasperServiceByJsonRPC(NODE_URL);
const cesEventParser = CESEventParserFactory({
contractHashes: [contractHash],
casperClient,
});
const es = new EventStream(EVENT_STREAM_ADDRESS);
es.subscribe(EventName.DeployProcessed, async (event) => {
const parsedEvents = await cesEventParser(event);
if (parsedEvents?.success) {
console.log(parsedEvents.data);
}
});
es.start();
```
12 changes: 8 additions & 4 deletions client-js/examples/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
MetadataMutability,
OwnerReverseLookupMode,
MintingMode,
EventsMode
} from "../src/index";

import {
Expand All @@ -19,9 +20,11 @@ import {
const install = async () => {
const cc = new CEP78Client(process.env.NODE_URL!, process.env.NETWORK_NAME!);

const collectionName = "my-collection";

const installDeploy = await cc.install(
{
collectionName: "my-collection",
collectionName,
collectionSymbol: "MY-NFTS",
totalTokenSupply: "1000",
ownershipMode: NFTOwnershipMode.Transferable,
Expand All @@ -38,7 +41,8 @@ const install = async () => {
identifierMode: NFTIdentifierMode.Ordinal,
metadataMutability: MetadataMutability.Immutable,
mintingMode: MintingMode.Installer,
ownerReverseLookupMode: OwnerReverseLookupMode.Complete
ownerReverseLookupMode: OwnerReverseLookupMode.NoLookup,
eventsMode: EventsMode.CES
},
"250000000000",
FAUCET_KEYS.publicKey,
Expand All @@ -63,12 +67,12 @@ const install = async () => {

const contractHash = await getAccountNamedKeyValue(
accountInfo,
`nft_contract`
`cep78_contract_hash_${collectionName}`
);

const contractPackageHash = await getAccountNamedKeyValue(
accountInfo,
`nft_contract_package`
`cep78_contract_package_${collectionName}`
);

console.log(`... Contract Hash: ${contractHash}`);
Expand Down
65 changes: 56 additions & 9 deletions client-js/examples/usage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { CEP78Client, OwnerReverseLookupMode } from "../src/index";
import {
CEP78Client,
OwnerReverseLookupMode,
CEP47EventParserFactory,
CESEventParserFactory,
CEP47Events,
} from "../src/index";

import {
FAUCET_KEYS,
Expand All @@ -10,9 +16,20 @@ import {
printHeader,
} from "./common";

import { DeployUtil, CLPublicKey } from "casper-js-sdk";

const { NODE_URL } = process.env;
import {
DeployUtil,
CLPublicKey,
EventStream,
EventName,
CLValueParsers,
CLTypeTag,
CLMap,
CLValue,
CLValueBuilder,
CasperServiceByJsonRPC
} from "casper-js-sdk";

const { NODE_URL, EVENT_STREAM_ADDRESS } = process.env;

const runDeployFlow = async (deploy: DeployUtil.Deploy) => {
const deployHash = await deploy.send(NODE_URL!);
Expand Down Expand Up @@ -48,12 +65,12 @@ const run = async () => {

const contractHash = await getAccountNamedKeyValue(
accountInfo,
`nft_contract`
`cep78_contract_hash_my-collection`
);

const contractPackageHash = await getAccountNamedKeyValue(
accountInfo,
`nft_contract_package`
`cep78_contract_package_my-collection`
);

console.log(`... Contract Hash: ${contractHash}`);
Expand All @@ -79,16 +96,45 @@ const run = async () => {
console.log(`WhitelistMode: ${whitelistModeSetting}`);

const ownerReverseLookupModeSetting = await cc.getReportingModeConfig();
console.log(
`OwnerReverseLookupMode: ${ownerReverseLookupModeSetting}`
);
console.log(`OwnerReverseLookupMode: ${ownerReverseLookupModeSetting}`);

const useSessionCode =
ownerReverseLookupModeSetting ===
OwnerReverseLookupMode[OwnerReverseLookupMode.Complete];

const JSONSetting = await cc.getJSONSchemaConfig();

const cep47EventParser = CEP47EventParserFactory({
contractPackageHash,
eventNames: [
CEP47Events.Mint,
CEP47Events.Transfer,
CEP47Events.Burn
],
});

const casperClient = new CasperServiceByJsonRPC(NODE_URL);
const cesEventParser = CESEventParserFactory({
contractHashes: [contractHash],
casperClient,
});

const es = new EventStream(EVENT_STREAM_ADDRESS!);

es.subscribe(EventName.DeployProcessed, async (event) => {
const parsedEvents = await cesEventParser(event); //cep47EventParser(event);

if (parsedEvents?.success) {
console.log("*** EVENT ***");
console.log(parsedEvents.data);
console.log("*** ***");
} else {
console.log("*** EVENT NOT RELATED TO WATCHED CONTRACT ***");
}
});

es.start();

/* Mint */
printHeader("Mint");

Expand All @@ -101,6 +147,7 @@ const run = async () => {
material: "Aluminum",
condition: "Used",
},
collectionName: "my-collection",
},
{ useSessionCode },
"2000000000",
Expand Down
Loading

0 comments on commit 4daf28a

Please sign in to comment.