Skip to content

Commit

Permalink
Added generic contract JS client with e2e-tests and documentation (#72)
Browse files Browse the repository at this point in the history
* Finished generic client with e2e-tests and documentation
* Extended CLTypeDict
  • Loading branch information
piotrwitek authored Jun 17, 2022
1 parent aba3ed1 commit 7c24f46
Show file tree
Hide file tree
Showing 13 changed files with 811 additions and 72 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ CARGO_TEST = cargo test --features=test-support --no-default-features

prepare:
rustup target add wasm32-unknown-unknown
cargo install cargo-expand --version 1.0.17
# cargo install cargo-expand

build-proxy-getter:
$(CARGO_BUILD) -p casper-dao-utils --bin getter_proxy
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Reusable smart contracts for building DAOs on top of Casper.

Repository contains following modules:
- `dao-contracts` and `dao-modules` provides smart contracts implementation,
- `dao-erc20` and `dao-erc721` allows to use those standards,
- `dao-utils` and `dao-macros` makes writing code easier,
- `client` implements a JavaScript client for smart contracts interactions.

Expand Down
39 changes: 28 additions & 11 deletions client/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# `dao-contracts-js-client`

This JavaScript client gives you an easy way to install and interact with the DAO Reputation contract.
This JavaScript client gives you an easy way to install and interact with all the DAO contracts.

## Installation

Expand All @@ -15,24 +15,29 @@ npm i dao-contracts-js-client
### Install the contract on the network

```ts
const installDeployHash = await installReputationContract(
// create deploy
const deploy = createInstallReputationContractDeploy(
CHAIN_NAME,
NODE_ADDRESS,
KEYS, // Key pair used for signing
200000000000, // Payment amount
"../target/wasm32-unknown-unknown/release/reputation_contract.wasm" // Path to WASM file
OWNER_KEYS, // Key pair used for signing deploy
);

// send deploy to network
const installDeployHash = await installDeploy.send(NODE_ADDRESS);
```

### Create an instance to interact with the contract
### Create a client instance to interact with the contract

```ts
const reputationContract = new ReputationContractJSClient(
const reputationContract = new GenericContractJSClient(
http://localhost:11101, // RPC address
"casper-net-1", // Network name
"http://localhost:18101/events/main", // Event stream address
"hash-XXXXXXXXXXXXXXXXXXXXx", // contractPackageHash
"hash-XXXXXXXXXXXXXXXXXXXXx", // contractHash
'path-to-contract-yaml-schema-file'
);
```

Expand All @@ -43,8 +48,11 @@ const reputationContract = new ReputationContractJSClient(
Use getter methods to retrieve values:

```ts
const owner = await reputationContract.getOwner();
const total_supply = await reputationContract.getTotalSupply();
const total_supply =
await reputationContract.getNamedKey("total_supply");

const isWhitelisted =
await reputationContract.getNamedKey("whitelist", publicKey);
```

### Deploys
Expand All @@ -54,12 +62,21 @@ Use deploys to interact with contract:
```ts
const mintAmount = "200000000000";

const deployHashMint = await reputationContract.mint(
const mintResult: Result<string, string> = await reputationContract.callEntryPoint(
"mint",
ownerKeys,
ownerKeys.publicKey,
mintAmount,
DEPLOY_PAYMENT_AMOUNT
DEPLOY_PAYMENT_AMOUNT,
createRecipientAddress(ownerKeys.publicKey), // import { createRecipientAddress } from "casper-js-client-helper/dist/helpers/lib";
CLValueBuilder.u256(mintAmount)
);

if (mintResult.ok) {
// handle success
mintResult.val
} else {
// handle error
mintResult.val
}
```

## Development
Expand Down
122 changes: 122 additions & 0 deletions client/e2e/e2e-generic-client-local.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import BigNumber from "bignumber.js";
import { utils } from "casper-js-client-helper";
import { createRecipientAddress } from "casper-js-client-helper/dist/helpers/lib";
import {
CLKey,
CLKeyParameters,
CLPublicKey,
CLU256,
CLValue,
CLValueBuilder,
EventName,
EventStream,
Keys,
} from "casper-js-sdk";
import { Option, Result } from "ts-results";

if (process.env.NODE_ENV !== "ci") {
require("dotenv").config({ path: "./.env", debug: true });
}

import {
createInstallReputationContractDeploy,
ReputationContractEventParser,
ReputationContractEvents,
GenericContractJSClient,
} from "../src";
import { createRpcClient } from "../src/common/rpc-client";
import {
getAccountInfo,
getAccountNamedKeyValue,
waitForDeploy,
assert,
} from "./utils";

const {
NODE_ENV,
CHAIN_NAME,
NODE_ADDRESS,
EVENT_STREAM_ADDRESS,
WASM_RELEASE_PATH,
NCTL_USERS_FOLDER_PATH,
INSTALL_PAYMENT_AMOUNT,
DEPLOY_PAYMENT_AMOUNT,
} = process.env;

console.log("testing env variables", {
NODE_ENV,
CHAIN_NAME,
NODE_ADDRESS,
EVENT_STREAM_ADDRESS,
WASM_RELEASE_PATH,
NCTL_USERS_FOLDER_PATH,
INSTALL_PAYMENT_AMOUNT,
DEPLOY_PAYMENT_AMOUNT,
});

const ownerKeys = Keys.Ed25519.parseKeyFiles(
`${NCTL_USERS_FOLDER_PATH}/user-1/public_key.pem`,
`${NCTL_USERS_FOLDER_PATH}/user-1/secret_key.pem`
);
const recipientKeys = Keys.Ed25519.parseKeyFiles(
`${NCTL_USERS_FOLDER_PATH}/user-2/public_key.pem`,
`${NCTL_USERS_FOLDER_PATH}/user-2/secret_key.pem`
);
const test = async () => {
/** SCHEMA */

const reputationContractSchema = {
entry_points: {
mint: [
{ name: "recipient", cl_type: "Address" },
{ name: "amount", cl_type: "U256" },
],
add_to_whitelist: [{ name: "address", cl_type: "Address" }],
},
named_keys: {
owner: {
named_key: "owner_owner_access_control_contract",
cl_type: [{ name: "Option", inner: "Address" }],
},
total_supply: {
named_key: "total_supply_token_token_contract",
cl_type: "U256",
},
balance: {
named_key: "balances_token_token_contract",
cl_type: [{ name: "Mapping", key: "Address", value: "U256" }],
},
whitelist: {
named_key: "whitelist_whitelist_access_control_contract",
cl_type: [{ name: "Mapping", key: "Address", value: "Bool" }],
},
stakes: {
named_key: "token_stakes",
cl_type: [{ name: "Mapping", key: "Address", value: "U256" }],
},
},
};

const contractHashWithHashPrefix =
"hash-10539a97a58adf60498ecd0e7be1f7284c6dcc01a09154f45f97c8fc5d395a7e";
const contractPackageHash =
"hash-49f5fc30a7888b74508a1ee4365353e858a4fc26ff1cdd3d7a81a7aff36d5276";

// Initialize contract client
const reputationContract = new GenericContractJSClient(
NODE_ADDRESS,
CHAIN_NAME,
EVENT_STREAM_ADDRESS,
contractHashWithHashPrefix,
contractPackageHash,
reputationContractSchema
);

console.log(`\n`);
console.log(`... Testing deploys ...`);

};

test().then(() => {
process.exit(0);
});
Loading

0 comments on commit 7c24f46

Please sign in to comment.