Skip to content

Commit

Permalink
fix: get it working
Browse files Browse the repository at this point in the history
  • Loading branch information
chadoh committed Oct 29, 2024
1 parent a16fadf commit 9f4bad3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
10 changes: 6 additions & 4 deletions src/contract/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Server } from '../rpc';
import { AssembledTransaction } from "./assembled_transaction";
import type { ClientOptions, MethodOptions } from "./types";
import { processSpecEntryStream } from './utils';
import randomBytes from "randombytes";

const CONSTRUCTOR_FUNC = "__constructor";

Expand Down Expand Up @@ -62,7 +63,7 @@ export class Client {
options: MethodOptions &
Omit<ClientOptions, "contractId"> & {
wasmHash: Buffer | string;
salt: Buffer;
salt?: Buffer;
format?: "hex" | "base64";
}
): Promise<AssembledTransaction<T>> {
Expand All @@ -82,7 +83,7 @@ export class Client {
let contractIdPreimage =
xdr.ContractIdPreimage.contractIdPreimageFromAddress(
new xdr.ContractIdPreimageFromAddress({
salt: options.salt,
salt: options.salt ?? hash(randomBytes(48)),
address: address.toScAddress(),
})
);
Expand All @@ -106,8 +107,9 @@ export class Client {
...options,
contractId,
method: CONSTRUCTOR_FUNC,
parseResultXdr: () =>
new Client(spec, { ...options, contractId }),
parseResultXdr: (result) =>
new Client(spec, { ...options, contractId: Address.fromScVal(result).toString() })

}) as unknown as AssembledTransaction<T>;
}

Expand Down
30 changes: 21 additions & 9 deletions test/e2e/src/test-constructor-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,40 @@ const { Address, contract, hash } = require("../../../lib");
const { installContract, rpcUrl, networkPassphrase } = require("./util");
const { basicNodeSigner } = require("../../../lib/contract");

describe("Constructor Args", function () {
const INIT_VALUE = 42;

describe("contract with constructor args", function () {
before(async function () {
const { wasmHash, keypair } = await installContract("constructorArgs");
this.context = { wasmHash, keypair };
});

it("does things", async function () {
expect(this.context.wasmHash).not.to.be.empty;
it("can be instantiated when deployed", async function () {
const tx = await contract.Client.deploy(
{ counter: 42 },
{ counter: INIT_VALUE },
{
networkPassphrase,
rpcUrl,
allowHttp: true,
wasmHash: this.context.wasmHash,
salt: hash(Buffer.from("salt")),
publicKey: this.context.keypair.publicKey(),
...basicNodeSigner(this.context.keypair),
...basicNodeSigner(this.context.keypair, networkPassphrase),
},
);
console.log(tx);
const { result } = await tx.signAndSend();
console.log(result);
const { result: client } = await tx.signAndSend();
const t = await client.counter();
expect(t.result, INIT_VALUE);
});

it("fails deploy if not given arguments", async function () {
const tx = await contract.Client.deploy(null, {
networkPassphrase,
rpcUrl,
allowHttp: true,
wasmHash: this.context.wasmHash,
publicKey: this.context.keypair.publicKey(),
...basicNodeSigner(this.context.keypair, networkPassphrase),
});
expect(tx.signAndSend().then((t) => t.result)).to.eventually.throw();
});
});

0 comments on commit 9f4bad3

Please sign in to comment.