Skip to content

Commit

Permalink
Tweaked 'only-receipt' behavior to not apply to contract-queries & ad…
Browse files Browse the repository at this point in the history
…ded tests & updated docs. Refers to #56
  • Loading branch information
3Nigma committed Apr 12, 2022
1 parent 2ccb9b8 commit 21cfbb3
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ HEDERAS_OPERATOR_KEY=
# If missing, it's assumed to be in the "contracts" subfolder
HEDERAS_CONTRACTS_RELATIVE_PATH=

# (OPTIONAL) A fine cost-control config option which, when set to true (default), returns only the receipt of the contract-transaction call (for live-contracts)
# Set this to false if you want to also do a record-query and get the transaction contract-function-result
# Note: this only impacts the calling of mutating/transactions contract functions. Queries (non-mutating) requests are not affected by it and will always
# return the function result.
HEDERAS_DEFAULT_CONTRACT_REQUESTS_RETURN_ONLY_RECEIPTS=

# (OPTIONAL) A list of coma-separated paths (relative or absolute) that are tried out in the following format
# current working directory > relative path > included prefix > requested file import (when non-absolute) and
# included prefix > requested file import (when absolute)
Expand Down
2 changes: 1 addition & 1 deletion lib.docs/markdown/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ title: Changelog
* Implemented [`LiveAddress.equals(AccountId)` functionality](https://github.com/buidler-labs/hedera-strato-js/issues/34)
* Implemented [`StratoAddress.toLiveAccount()`](https://github.com/buidler-labs/hedera-strato-js/issues/49)
* [Auto arrayfying hex string](https://github.com/buidler-labs/hedera-strato-js/issues/40) if `bytes32` arguments are expected by the `LiveContract` call
* 💥 *Braking change!* Added [`HEDERAS_DEFAULT_CONTRACT_REQUESTS_RETURN_ONLY_RECEIPTS` config](./configuration.md) option to have [finer cost-control over contract-requests](https://github.com/buidler-labs/hedera-strato-js/issues/48). Set it to `false` to revert to v0.7.3 behavior
* 💥 *Potentially braking change!* Added [`HEDERAS_DEFAULT_CONTRACT_REQUESTS_RETURN_ONLY_RECEIPTS` config](./configuration.md) option to have [finer cost-control over contract-requests](https://github.com/buidler-labs/hedera-strato-js/issues/48). Set it to `false` to revert to v0.7.3 behavior. This only affects state-mutating contract-calls. Non-mutating (query) calls are not affected by this parameter.
* Fixed [recursive loading of ABIs into `ContractRegistry`s](https://github.com/buidler-labs/hedera-strato-js/issues/50) at bundling time
* Allow [`ContractRegistry`s to be created from abstract](https://github.com/buidler-labs/hedera-strato-js/issues/54) solidity contracts
* A lot of tweaks on docs, visual and others
Expand Down
2 changes: 1 addition & 1 deletion lib.docs/markdown/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Following is a table detailing all the object-parameters along with their enviro
| HEDERAS_CONTRACTS_INCLUDED_PREFIXES | - | No | [^relative-path-prefixes] | `contracts` | The places to search for imported contract paths from within a solidity code. The contract's parent folder is the first one being searched, followed by the project's `node_modules` and then, if nothing matches, the rest of the included prefixes are looked at in the order in which they are defined
| HEDERAS_CONTRACTS_RELATIVE_PATH | - | No | path | `contracts` | The name of the folder relative to the project root directory where all the solidity contracts are expected to reside. This is used when Strato is doing the contract compiling of a given relative-pathed contract
| HEDERAS_DEFAULT_CONTRACT_CREATION_GAS | session.defaults.contractCreationGas | No | number | 150000 | The default amount spent for creating a contract on the network
| HEDERAS_DEFAULT_CONTRACT_REQUESTS_RETURN_ONLY_RECEIPTS | session.defaults.onlyReceiptsFromContractRequests | No | boolean | true | `true` to return _only_ receipts for all live-contract call, `false` otherwise. If desired, can be tweaked by the `onlyReceipt` contract-call meta-argument at an individual level.
| HEDERAS_DEFAULT_CONTRACT_REQUESTS_RETURN_ONLY_RECEIPTS | session.defaults.onlyReceiptsFromContractRequests | No | boolean | true | `true` to return _only_ receipts for all state-mutating/transactions live-contract call, `false` otherwise. Contract queries (non-mutating calls) are not affected by this param. If desired, can be tweaked by the `onlyReceipt` contract-call meta-argument at an individual level.
| HEDERAS_DEFAULT_CONTRACT_TRANSACTION_GAS | session.defaults.contractTransactionGas | No | number | 169000 | The default amount given when executing a contract transaction
| HEDERAS_DEFAULT_EMIT_CONSTRUCTOR_LOGS | session.defaults.emitConstructorLogs | No | boolean | `true` | `true` to emit the constructor logs at contract-creation time, `false` otherwise
| HEDERAS_DEFAULT_EMIT_LIVE_CONTRACT_RECEIPTS | session.defaults.emitLiveContractReceipts | No | boolean | `false` | `true` to ask for and emit the receipts originating from live-contract calls, `false` otherwise
Expand Down
3 changes: 2 additions & 1 deletion lib/live/LiveContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ export class LiveContract extends BaseLiveEntityWithBalance<ContractId, Contract
enumerable: true,
value: (async function (this: LiveContract, fDescription: FunctionFragment, ...args: any[]) {
const { request, meta } = await this.createContractRequestFor({ fDescription, args });
const executionResultType = meta.onlyReceipt ? TypeOfExecutionReturn.Receipt : TypeOfExecutionReturn.Result;
const isNonQuery = !(request instanceof ContractCallQuery);
const executionResultType = isNonQuery && meta.onlyReceipt ? TypeOfExecutionReturn.Receipt : TypeOfExecutionReturn.Result;
const callResponse = await this.session.execute(request, executionResultType, meta.emitReceipt);

if (executionResultType == TypeOfExecutionReturn.Result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('ApiSession.Solidity-by-Example.Receipts', () => {
expect((spiedReceiptCallback.mock.calls[0][0] as any).transaction).toBeInstanceOf(ContractExecuteTransaction);
});

it('executing a live-contract function in a default-session environment that does not return only receipts when calling such functions should do so if runtime requests it', async () => {
it('executing a live-contract mutating function in a default-session environment that does not return only receipts when calling such functions should do so if runtime requests it', async () => {
const { session } = await ApiSession.default({
session: {
defaults: {
Expand All @@ -90,7 +90,7 @@ describe('ApiSession.Solidity-by-Example.Receipts', () => {
expect(sessionExecutionSpy.mock.calls[0][1]).toEqual(TypeOfExecutionReturn.Receipt);
});

it('executing a live-contract function in a default-session environment that does return only receipts when calling such functions should behave accordingly and, by default, return that receipt', async () => {
it('executing a live-contract mutating function in a default-session environment that does return only receipts when calling such functions should behave accordingly and, by default, return that receipt', async () => {
const { session } = await ApiSession.default({
session: {
defaults: {
Expand All @@ -107,4 +107,40 @@ describe('ApiSession.Solidity-by-Example.Receipts', () => {
expect(sessionExecutionSpy.mock.calls).toHaveLength(1);
expect(sessionExecutionSpy.mock.calls[0][1]).toEqual(TypeOfExecutionReturn.Receipt);
});

it('executing a live-contract non-mutating function in a default-session environment that does return only receipts when calling such a function should return that query result', async () => {
const { session } = await ApiSession.default({
session: {
defaults: {
onlyReceiptsFromContractRequests: true,
},
},
});
const solContract = await Contract.newFrom({ code: read({ contract: 'hello_world' }) });
const liveContract = await session.upload(solContract);
const sessionExecutionSpy = jest.spyOn(session, "execute");
const queryResult = await liveContract.greet();

expect(queryResult).toEqual("Hello World!");
expect(sessionExecutionSpy.mock.calls).toHaveLength(1);
expect(sessionExecutionSpy.mock.calls[0][1]).toEqual(TypeOfExecutionReturn.Result);
});

it('executing a live-contract non-mutating function in a default-session environment that does not return only receipts, yet only receipts is requested, when calling such a function should return that query result', async () => {
const { session } = await ApiSession.default({
session: {
defaults: {
onlyReceiptsFromContractRequests: false,
},
},
});
const solContract = await Contract.newFrom({ code: read({ contract: 'hello_world' }) });
const liveContract = await session.upload(solContract);
const sessionExecutionSpy = jest.spyOn(session, "execute");
const queryResult = await liveContract.greet({ onlyReceipt: true });

expect(queryResult).toEqual("Hello World!");
expect(sessionExecutionSpy.mock.calls).toHaveLength(1);
expect(sessionExecutionSpy.mock.calls[0][1]).toEqual(TypeOfExecutionReturn.Result);
});
});

0 comments on commit 21cfbb3

Please sign in to comment.