Skip to content

Commit 717873b

Browse files
feat: add ContractExecute endpoint (#3317)
Signed-off-by: Mario Dimitrov <[email protected]>
1 parent 8776e7b commit 717873b

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

src/contract/ContractExecuteTransaction.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,11 @@ export default class ContractExecuteTransaction extends Transaction {
189189
*/
190190
setGas(gas) {
191191
this._requireNotFrozen();
192-
this._gas = gas instanceof Long ? gas : Long.fromValue(gas);
192+
const gasLong = gas instanceof Long ? gas : Long.fromValue(gas);
193+
if (gasLong.lt(0)) {
194+
throw new Error("Gas must be greater than 0");
195+
}
196+
this._gas = gasLong;
193197

194198
return this;
195199
}

tck/methods/contract.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
ContractCreateTransaction,
33
ContractDeleteTransaction,
4+
ContractExecuteTransaction,
45
ContractUpdateTransaction,
56
Hbar,
67
Timestamp,
@@ -9,6 +10,7 @@ import {
910
import {
1011
CreateContractParams,
1112
DeleteContractParams,
13+
ExecuteContractParams,
1214
UpdateContractParams,
1315
} from "../params/contract";
1416
import { ContractResponse } from "../response/contract";
@@ -233,3 +235,46 @@ export const deleteContract = async ({
233235
status: receipt.status.toString(),
234236
};
235237
};
238+
239+
export const executeContract = async ({
240+
contractId,
241+
gas,
242+
amount,
243+
functionParameters,
244+
commonTransactionParams,
245+
}: ExecuteContractParams): Promise<ContractResponse> => {
246+
const transaction = new ContractExecuteTransaction().setGrpcDeadline(
247+
DEFAULT_GRPC_DEADLINE,
248+
);
249+
250+
if (contractId != null) {
251+
transaction.setContractId(contractId);
252+
}
253+
254+
if (gas != null) {
255+
transaction.setGas(Long.fromString(gas));
256+
}
257+
258+
if (amount != null) {
259+
transaction.setPayableAmount(Hbar.fromTinybars(amount));
260+
}
261+
262+
if (functionParameters != null) {
263+
const functionParams = decode(functionParameters);
264+
transaction.setFunctionParameters(functionParams);
265+
}
266+
267+
if (commonTransactionParams != null) {
268+
applyCommonTransactionParams(
269+
commonTransactionParams,
270+
transaction,
271+
sdk.getClient(),
272+
);
273+
}
274+
const response = await transaction.execute(sdk.getClient());
275+
const receipt = await response.getReceipt(sdk.getClient());
276+
277+
return {
278+
status: receipt.status.toString(),
279+
};
280+
};

tck/params/contract.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,11 @@ export interface DeleteContractParams {
3737
readonly permanentRemoval?: boolean;
3838
readonly commonTransactionParams?: Record<string, any>;
3939
}
40+
41+
export interface ExecuteContractParams {
42+
readonly contractId: string;
43+
readonly gas?: string;
44+
readonly amount?: string;
45+
readonly functionParameters?: string;
46+
readonly commonTransactionParams?: Record<string, any>;
47+
}

test/unit/ContractExecuteTransaction.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ describe("ContractExecuteTransaction", function () {
119119
expect(transaction.gas.toNumber()).to.equal(gasNumber);
120120
});
121121

122+
it("should throw error if gas is negative", function () {
123+
const gasNumber = -10;
124+
expect(() => {
125+
new ContractExecuteTransaction().setGas(gasNumber);
126+
}).to.throw("Gas must be greater than 0");
127+
});
128+
122129
it("should set payable amount", function () {
123130
const transaction =
124131
new ContractExecuteTransaction().setPayableAmount(

0 commit comments

Comments
 (0)