Skip to content

Commit 1c2265d

Browse files
authored
feat: extend setPayableAmount (#3144)
Signed-off-by: Ivan Ivanov <[email protected]>
1 parent 17762e0 commit 1c2265d

File tree

4 files changed

+66
-3
lines changed

4 files changed

+66
-3
lines changed

src/Hbar.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export default class Hbar {
4949
}
5050

5151
/**
52-
* @param {number | Long | BigNumber} amount
52+
* @param {number | Long | string | BigNumber} amount
5353
* @param {HbarUnit} unit
5454
* @returns {Hbar}
5555
*/

src/contract/ContractExecuteTransaction.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Transaction, {
77
import ContractId from "./ContractId.js";
88
import ContractFunctionParameters from "./ContractFunctionParameters.js";
99
import Long from "long";
10+
import HbarUnit from "../HbarUnit.js";
1011

1112
/**
1213
* @namespace proto
@@ -204,11 +205,13 @@ export default class ContractExecuteTransaction extends Transaction {
204205
* Sets the number of hbars to be sent with this function call.
205206
*
206207
* @param {number | string | Long | BigNumber | Hbar} amount
208+
* @param {HbarUnit} unit
207209
* @returns {ContractExecuteTransaction}
208210
*/
209-
setPayableAmount(amount) {
211+
setPayableAmount(amount, unit = HbarUnit.Hbar) {
210212
this._requireNotFrozen();
211-
this._amount = amount instanceof Hbar ? amount : new Hbar(amount);
213+
this._amount =
214+
amount instanceof Hbar ? amount : Hbar.from(amount, unit);
212215

213216
return this;
214217
}

test/integration/ContractExecuteIntegrationTest.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ import {
55
ContractFunctionParameters,
66
FileCreateTransaction,
77
FileDeleteTransaction,
8+
Hbar,
9+
HbarUnit,
810
Status,
911
} from "../../src/exports.js";
1012
import IntegrationTestEnv from "./client/NodeIntegrationTestEnv.js";
13+
import { createAccount } from "./utils/Fixtures.js";
1114

1215
let smartContractBytecode =
1316
"608060405234801561001057600080fd5b506040516104d73803806104d78339818101604052602081101561003357600080fd5b810190808051604051939291908464010000000082111561005357600080fd5b90830190602082018581111561006857600080fd5b825164010000000081118282018810171561008257600080fd5b82525081516020918201929091019080838360005b838110156100af578181015183820152602001610097565b50505050905090810190601f1680156100dc5780820380516001836020036101000a031916815260200191505b506040525050600080546001600160a01b0319163317905550805161010890600190602084019061010f565b50506101aa565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061015057805160ff191683800117855561017d565b8280016001018555821561017d579182015b8281111561017d578251825591602001919060010190610162565b5061018992915061018d565b5090565b6101a791905b808211156101895760008155600101610193565b90565b61031e806101b96000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c8063368b87721461004657806341c0e1b5146100ee578063ce6d41de146100f6575b600080fd5b6100ec6004803603602081101561005c57600080fd5b81019060208101813564010000000081111561007757600080fd5b82018360208201111561008957600080fd5b803590602001918460018302840111640100000000831117156100ab57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250929550610173945050505050565b005b6100ec6101a2565b6100fe6101ba565b6040805160208082528351818301528351919283929083019185019080838360005b83811015610138578181015183820152602001610120565b50505050905090810190601f1680156101655780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000546001600160a01b0316331461018a5761019f565b805161019d906001906020840190610250565b505b50565b6000546001600160a01b03163314156101b85733ff5b565b60018054604080516020601f600260001961010087891615020190951694909404938401819004810282018101909252828152606093909290918301828280156102455780601f1061021a57610100808354040283529160200191610245565b820191906000526020600020905b81548152906001019060200180831161022857829003601f168201915b505050505090505b90565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061029157805160ff19168380011785556102be565b828001600101855582156102be579182015b828111156102be5782518255916020019190600101906102a3565b506102ca9291506102ce565b5090565b61024d91905b808211156102ca57600081556001016102d456fea264697066735822122084964d4c3f6bc912a9d20e14e449721012d625aa3c8a12de41ae5519752fc89064736f6c63430006000033";
@@ -359,6 +362,41 @@ describe("ContractExecute", function () {
359362
}
360363
});
361364

365+
it("should execute with payable amount", async function () {
366+
const BYTECODE =
367+
"6080604052348015600e575f80fd5b50335f806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506104a38061005b5f395ff3fe608060405260043610610033575f3560e01c8063607a4427146100375780637065cb4814610053578063893d20e81461007b575b5f80fd5b610051600480360381019061004c919061033c565b6100a5565b005b34801561005e575f80fd5b50610079600480360381019061007491906103a2565b610215565b005b348015610086575f80fd5b5061008f6102b7565b60405161009c91906103dc565b60405180910390f35b3373ffffffffffffffffffffffffffffffffffffffff165f8054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16146100fb575f80fd5b805f806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550600181908060018154018082558091505060019003905f5260205f20015f9091909190916101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505f8173ffffffffffffffffffffffffffffffffffffffff166108fc3490811502906040515f60405180830381858888f19350505050905080610211576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016102089061044f565b60405180910390fd5b5050565b805f806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550600181908060018154018082558091505060019003905f5260205f20015f9091909190916101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050565b5f805f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905090565b5f80fd5b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f61030b826102e2565b9050919050565b61031b81610301565b8114610325575f80fd5b50565b5f8135905061033681610312565b92915050565b5f60208284031215610351576103506102de565b5b5f61035e84828501610328565b91505092915050565b5f610371826102e2565b9050919050565b61038181610367565b811461038b575f80fd5b50565b5f8135905061039c81610378565b92915050565b5f602082840312156103b7576103b66102de565b5b5f6103c48482850161038e565b91505092915050565b6103d681610367565b82525050565b5f6020820190506103ef5f8301846103cd565b92915050565b5f82825260208201905092915050565b7f5472616e73666572206661696c656400000000000000000000000000000000005f82015250565b5f610439600f836103f5565b915061044482610405565b602082019050919050565b5f6020820190508181035f8301526104668161042d565b905091905056fea26469706673582212206c46ddb2acdbcc4290e15be83eb90cd0b2ce5bd82b9bfe58a0709c5aec96305564736f6c634300081a0033";
368+
const { fileId } = await (
369+
await new FileCreateTransaction()
370+
.setContents(BYTECODE)
371+
.execute(env.client)
372+
).getReceipt(env.client);
373+
374+
const { accountId } = await createAccount(env.client);
375+
376+
const newOwnerSolidityAddress = accountId.toSolidityAddress();
377+
378+
const contractId = (
379+
await (
380+
await new ContractCreateTransaction()
381+
.setBytecodeFileId(fileId)
382+
.setGas(400_000)
383+
.execute(env.client)
384+
).getReceipt(env.client)
385+
).contractId;
386+
387+
await new ContractExecuteTransaction()
388+
.setContractId(contractId)
389+
.setFunction(
390+
"addOwnerAndTransfer",
391+
new ContractFunctionParameters().addAddress(
392+
newOwnerSolidityAddress,
393+
),
394+
)
395+
.setGas(60_000)
396+
.setPayableAmount(new Hbar(100), HbarUnit.Kilobar)
397+
.execute(env.client);
398+
});
399+
362400
afterAll(async function () {
363401
await env.close();
364402
});

test/unit/ContractExecuteTransaction.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
PrivateKey,
88
ContractFunctionParameters,
99
Transaction,
10+
HbarUnit,
1011
} from "../../src/index.js";
1112
import Long from "long";
1213

@@ -139,6 +140,27 @@ describe("ContractExecuteTransaction", function () {
139140
);
140141
});
141142

143+
it("should set payable amount from number with unit", function () {
144+
const amount = 10;
145+
let transaction = new ContractExecuteTransaction().setPayableAmount(
146+
amount,
147+
HbarUnit.Tinybar,
148+
);
149+
150+
expect(transaction.payableAmount.toString()).to.equal(
151+
Hbar.from(amount, HbarUnit.Tinybar).toString(),
152+
);
153+
154+
transaction = new ContractExecuteTransaction().setPayableAmount(
155+
amount,
156+
HbarUnit.Megabar,
157+
);
158+
159+
expect(transaction.payableAmount.toString()).to.equal(
160+
Hbar.from(amount, HbarUnit.Megabar).toString(),
161+
);
162+
});
163+
142164
it("should set function parameters", function () {
143165
const transaction =
144166
new ContractExecuteTransaction().setFunctionParameters(

0 commit comments

Comments
 (0)