Skip to content

Commit 7c24f46

Browse files
authored
Added generic contract JS client with e2e-tests and documentation (#72)
* Finished generic client with e2e-tests and documentation * Extended CLTypeDict
1 parent aba3ed1 commit 7c24f46

13 files changed

+811
-72
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ CARGO_TEST = cargo test --features=test-support --no-default-features
44

55
prepare:
66
rustup target add wasm32-unknown-unknown
7-
cargo install cargo-expand --version 1.0.17
7+
# cargo install cargo-expand
88

99
build-proxy-getter:
1010
$(CARGO_BUILD) -p casper-dao-utils --bin getter_proxy

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Reusable smart contracts for building DAOs on top of Casper.
44

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

client/README.md

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# `dao-contracts-js-client`
22

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

55
## Installation
66

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

1717
```ts
18-
const installDeployHash = await installReputationContract(
18+
// create deploy
19+
const deploy = createInstallReputationContractDeploy(
1920
CHAIN_NAME,
2021
NODE_ADDRESS,
21-
KEYS, // Key pair used for signing
2222
200000000000, // Payment amount
2323
"../target/wasm32-unknown-unknown/release/reputation_contract.wasm" // Path to WASM file
24+
OWNER_KEYS, // Key pair used for signing deploy
2425
);
26+
27+
// send deploy to network
28+
const installDeployHash = await installDeploy.send(NODE_ADDRESS);
2529
```
2630

27-
### Create an instance to interact with the contract
31+
### Create a client instance to interact with the contract
2832

2933
```ts
30-
const reputationContract = new ReputationContractJSClient(
34+
const reputationContract = new GenericContractJSClient(
3135
http://localhost:11101, // RPC address
3236
"casper-net-1", // Network name
3337
"http://localhost:18101/events/main", // Event stream address
3438
"hash-XXXXXXXXXXXXXXXXXXXXx", // contractPackageHash
3539
"hash-XXXXXXXXXXXXXXXXXXXXx", // contractHash
40+
'path-to-contract-yaml-schema-file'
3641
);
3742
```
3843

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

4550
```ts
46-
const owner = await reputationContract.getOwner();
47-
const total_supply = await reputationContract.getTotalSupply();
51+
const total_supply =
52+
await reputationContract.getNamedKey("total_supply");
53+
54+
const isWhitelisted =
55+
await reputationContract.getNamedKey("whitelist", publicKey);
4856
```
4957

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

57-
const deployHashMint = await reputationContract.mint(
65+
const mintResult: Result<string, string> = await reputationContract.callEntryPoint(
66+
"mint",
5867
ownerKeys,
59-
ownerKeys.publicKey,
60-
mintAmount,
61-
DEPLOY_PAYMENT_AMOUNT
68+
DEPLOY_PAYMENT_AMOUNT,
69+
createRecipientAddress(ownerKeys.publicKey), // import { createRecipientAddress } from "casper-js-client-helper/dist/helpers/lib";
70+
CLValueBuilder.u256(mintAmount)
6271
);
72+
73+
if (mintResult.ok) {
74+
// handle success
75+
mintResult.val
76+
} else {
77+
// handle error
78+
mintResult.val
79+
}
6380
```
6481

6582
## Development
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import BigNumber from "bignumber.js";
2+
import { utils } from "casper-js-client-helper";
3+
import { createRecipientAddress } from "casper-js-client-helper/dist/helpers/lib";
4+
import {
5+
CLKey,
6+
CLKeyParameters,
7+
CLPublicKey,
8+
CLU256,
9+
CLValue,
10+
CLValueBuilder,
11+
EventName,
12+
EventStream,
13+
Keys,
14+
} from "casper-js-sdk";
15+
import { Option, Result } from "ts-results";
16+
17+
if (process.env.NODE_ENV !== "ci") {
18+
require("dotenv").config({ path: "./.env", debug: true });
19+
}
20+
21+
import {
22+
createInstallReputationContractDeploy,
23+
ReputationContractEventParser,
24+
ReputationContractEvents,
25+
GenericContractJSClient,
26+
} from "../src";
27+
import { createRpcClient } from "../src/common/rpc-client";
28+
import {
29+
getAccountInfo,
30+
getAccountNamedKeyValue,
31+
waitForDeploy,
32+
assert,
33+
} from "./utils";
34+
35+
const {
36+
NODE_ENV,
37+
CHAIN_NAME,
38+
NODE_ADDRESS,
39+
EVENT_STREAM_ADDRESS,
40+
WASM_RELEASE_PATH,
41+
NCTL_USERS_FOLDER_PATH,
42+
INSTALL_PAYMENT_AMOUNT,
43+
DEPLOY_PAYMENT_AMOUNT,
44+
} = process.env;
45+
46+
console.log("testing env variables", {
47+
NODE_ENV,
48+
CHAIN_NAME,
49+
NODE_ADDRESS,
50+
EVENT_STREAM_ADDRESS,
51+
WASM_RELEASE_PATH,
52+
NCTL_USERS_FOLDER_PATH,
53+
INSTALL_PAYMENT_AMOUNT,
54+
DEPLOY_PAYMENT_AMOUNT,
55+
});
56+
57+
const ownerKeys = Keys.Ed25519.parseKeyFiles(
58+
`${NCTL_USERS_FOLDER_PATH}/user-1/public_key.pem`,
59+
`${NCTL_USERS_FOLDER_PATH}/user-1/secret_key.pem`
60+
);
61+
const recipientKeys = Keys.Ed25519.parseKeyFiles(
62+
`${NCTL_USERS_FOLDER_PATH}/user-2/public_key.pem`,
63+
`${NCTL_USERS_FOLDER_PATH}/user-2/secret_key.pem`
64+
);
65+
const test = async () => {
66+
/** SCHEMA */
67+
68+
const reputationContractSchema = {
69+
entry_points: {
70+
mint: [
71+
{ name: "recipient", cl_type: "Address" },
72+
{ name: "amount", cl_type: "U256" },
73+
],
74+
add_to_whitelist: [{ name: "address", cl_type: "Address" }],
75+
},
76+
named_keys: {
77+
owner: {
78+
named_key: "owner_owner_access_control_contract",
79+
cl_type: [{ name: "Option", inner: "Address" }],
80+
},
81+
total_supply: {
82+
named_key: "total_supply_token_token_contract",
83+
cl_type: "U256",
84+
},
85+
balance: {
86+
named_key: "balances_token_token_contract",
87+
cl_type: [{ name: "Mapping", key: "Address", value: "U256" }],
88+
},
89+
whitelist: {
90+
named_key: "whitelist_whitelist_access_control_contract",
91+
cl_type: [{ name: "Mapping", key: "Address", value: "Bool" }],
92+
},
93+
stakes: {
94+
named_key: "token_stakes",
95+
cl_type: [{ name: "Mapping", key: "Address", value: "U256" }],
96+
},
97+
},
98+
};
99+
100+
const contractHashWithHashPrefix =
101+
"hash-10539a97a58adf60498ecd0e7be1f7284c6dcc01a09154f45f97c8fc5d395a7e";
102+
const contractPackageHash =
103+
"hash-49f5fc30a7888b74508a1ee4365353e858a4fc26ff1cdd3d7a81a7aff36d5276";
104+
105+
// Initialize contract client
106+
const reputationContract = new GenericContractJSClient(
107+
NODE_ADDRESS,
108+
CHAIN_NAME,
109+
EVENT_STREAM_ADDRESS,
110+
contractHashWithHashPrefix,
111+
contractPackageHash,
112+
reputationContractSchema
113+
);
114+
115+
console.log(`\n`);
116+
console.log(`... Testing deploys ...`);
117+
118+
};
119+
120+
test().then(() => {
121+
process.exit(0);
122+
});

0 commit comments

Comments
 (0)