Skip to content

Commit

Permalink
added support for wasm files in npm package
Browse files Browse the repository at this point in the history
  • Loading branch information
hoffmannjan committed Oct 23, 2022
1 parent 963a5b8 commit e2cfcab
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 42 deletions.
70 changes: 32 additions & 38 deletions client-js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from "casper-js-sdk";
import { concat } from "@ethersproject/bytes";
import { Some } from "ts-results";
import * as fs from "fs";

const { Contract, toCLMap, fromCLMap } = Contracts;

Expand Down Expand Up @@ -53,22 +54,33 @@ const convertHashStrToHashBuff = (hashStr: string) => {
return Buffer.from(hashHex, "hex");
};

// TODO: In future, when we want to support also
// browser version - we will need to polyfill this
// and move to some separate module.
export const getBinary = (pathToBinary: string) => {
return new Uint8Array(fs.readFileSync(pathToBinary, null).buffer);
};

export class CEP78Client {
casperClient: CasperClient;
contractClient: Contracts.Contract;
contractHashKey: CLKey;

constructor(public nodeAddress: string, public networkName: string) {
this.casperClient = new CasperClient(nodeAddress);
this.contractClient = new Contract(this.casperClient);
}

public install(
wasm: Uint8Array,
args: CEP78InstallArgs,
paymentAmount: string,
deploySender: CLPublicKey,
keys?: Keys.AsymmetricKey[]
keys?: Keys.AsymmetricKey[],
wasm?: Uint8Array
) {
const wasmToInstall =
wasm || getBinary(`${__dirname}/../wasm/contract.wasm`);

const runtimeArgs = RuntimeArgs.fromMap({
collection_name: CLValueBuilder.string(args.collectionName),
collection_symbol: CLValueBuilder.string(args.collectionSymbol),
Expand Down Expand Up @@ -118,7 +130,7 @@ export class CEP78Client {
}

return this.contractClient.install(
wasm,
wasmToInstall,
runtimeArgs,
paymentAmount,
deploySender,
Expand All @@ -133,6 +145,9 @@ export class CEP78Client {
bootstrap?: boolean
) {
this.contractClient.setContractHash(contractHash, contractPackageHash);
this.contractHashKey = CLValueBuilder.key(
CLValueBuilder.byteArray(convertHashStrToHashBuff(contractHash))
);

if (bootstrap) {
// TODO: Set all possible config options inside the client and validate every client call.
Expand Down Expand Up @@ -235,44 +250,22 @@ export class CEP78Client {
wasm?: Uint8Array
) {
// TODO: Add metadata validation
const wasmToCall = wasm || getBinary(`${__dirname}/../wasm/contract.wasm`);

const runtimeArgs = RuntimeArgs.fromMap({
token_owner: CLValueBuilder.key(owner),
token_meta_data: CLValueBuilder.string(JSON.stringify(meta)),
nft_contracy_hash: this.contractHashKey,
});

let preparedDeploy: DeployUtil.Deploy;

if (!wasm) {
preparedDeploy = this.contractClient.callEntrypoint(
"mint",
runtimeArgs,
deploySender,
this.networkName,
paymentAmount,
keys
);
} else {
if (!this.contractClient?.contractHash) {
throw Error("Missing contractHash");
}
runtimeArgs.insert(
"nft_contract_hash",
CLValueBuilder.key(
CLValueBuilder.byteArray(
convertHashStrToHashBuff(this.contractClient?.contractHash)
)
)
);
preparedDeploy = this.contractClient.install(
wasm,
runtimeArgs,
paymentAmount,
deploySender,
this.networkName,
keys
);
}
const preparedDeploy = this.contractClient.install(
wasmToCall,
runtimeArgs,
paymentAmount,
deploySender,
this.networkName,
keys
);

return preparedDeploy;
}
Expand All @@ -281,20 +274,21 @@ export class CEP78Client {
tokenId: string,
paymentAmount: string,
deploySender: CLPublicKey,
keys?: Keys.AsymmetricKey[],
wasm?: Uint8Array
keys?: Keys.AsymmetricKey[]
) {
const runtimeArgs = RuntimeArgs.fromMap({
token_id: CLValueBuilder.u64(tokenId),
});

return this.contractClient.callEntrypoint(
const preparedDeploy = this.contractClient.callEntrypoint(
"burn",
runtimeArgs,
deploySender,
this.networkName,
paymentAmount,
keys
);

return preparedDeploy;
}
}
3 changes: 0 additions & 3 deletions client-js/test/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ const install = async () => {
const cc = new CEP78Client(process.env.NODE_URL!, process.env.NETWORK_NAME!);

const installDeploy = await cc.install(
getBinary(
"../contract/target/wasm32-unknown-unknown/release/contract.wasm"
),
{
collectionName: "my-collection",
collectionSymbol: "AMAG-ASSETS",
Expand Down
3 changes: 2 additions & 1 deletion client-js/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
]
},
"include": [
"./src/**/*"
"./src/**/*",
"./src/wasm/**/*"
],
"exclude": ["./test/**/*", "*/*.test.ts"]
}
Binary file added client/balance_of_session/.DS_Store
Binary file not shown.
Binary file added client/mint_session/.DS_Store
Binary file not shown.

0 comments on commit e2cfcab

Please sign in to comment.