Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate the SC Intents Factory in the SmartContract Class #330

Merged
merged 7 commits into from
Sep 25, 2023

Conversation

popenta
Copy link
Contributor

@popenta popenta commented Sep 12, 2023

Integrated the SC transaction intents factory in the smart contract module.

@popenta popenta self-assigned this Sep 12, 2023
@popenta popenta marked this pull request as draft September 12, 2023 14:25
@popenta popenta marked this pull request as ready for review September 13, 2023 10:58
bogdan-rosianu
bogdan-rosianu previously approved these changes Sep 13, 2023
Copy link
Contributor

@andreibancioiu andreibancioiu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should further discuss about using / not using the exact gas limit provided by the user.

Comment on lines +27 to +29
if (bytes.length !== this.codeMetadataLength) {
return new CodeMetadata();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps raising an exception would have worked as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I've done it how it's done in vm-common.

@@ -22,6 +23,22 @@ export class CodeMetadata {
this.payableBySc = payableBySc
}

static fromBytes(bytes: Uint8Array): CodeMetadata {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be unit tested.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added unit test.

@@ -41,7 +41,7 @@ describe("test smart contract interactor", function () {
assert.deepEqual(transaction.getReceiver(), dummyAddress);
assert.equal(transaction.getValue().toString(), "1000000000000000000");
assert.equal(transaction.getNonce(), 7);
assert.equal(transaction.getGasLimit().valueOf(), 20000000);
assert.equal(transaction.getGasLimit().valueOf(), 20057500);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is somehow a behavioral change. I think, in the smart contracts factory, we should simply use the provided gas limit as the "final" one, without adding an extra gas limit for data movement (the computed one). Debatable, though (we should specify this in the specs).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed, and already done, we are not modifying the gas limit that was set by the user for smart contract transactions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will also fix the unit tests.

@@ -31,7 +31,7 @@ export interface ISmartContract {
export interface DeployArguments {
code: ICode;
codeMetadata?: ICodeMetadata;
initArguments?: TypedValue[];
initArguments?: any[];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. Not a breaking change.

@@ -188,7 +188,7 @@ describe("test smart contract interactor", function () {
assert.equal(transaction.getData().toString(), "getUltimateAnswer");
assert.equal(
transaction.getHash().toString(),
"60d0956a8902c1179dce92d91bd9670e31b9a9cd07c1d620edb7754a315b4818"
"3ff6309bfe87ecc4ab541db649d57187f3a012291570552ac003866f77212c19"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, gas limit should stay the same (and the transaction hashes, as well).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

.setCodeMetadata(codeMetadata)
.setInitArgs(initArguments)
.build();
const bytecode = Uint8Array.from(Buffer.from(code.toString(), 'hex'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const bytecode = Buffer.from(code.toString(), 'hex');

Should have worked, as well, since Buffer implements Uint8Array.

@@ -15,16 +15,23 @@ interface Config {
gasLimitPerByte: BigNumber.Value;
}

interface Abi {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


const preparedArgs = this.argsToDataParts(args, this.abiRegistry?.constructorDefinition)
const preparedArgs = this.argsToDataParts(args, this.abiRegistry?.getEndpoint(options.functionName));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

const isReadable = options.isReadable || true;
const isPayable = options.isPayable || false;
const isPayableBySmartContract = options.isPayableBySmartContract || true;
const isUpgradeable = options.isUpgradeable ?? true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now I see, we should define some constants in the specs, such as:

DEFAULT_CODE_METADATA_IS_READABLE = true;

else {
metadata = new CodeMetadata();
}
const metadataAsJson = metadata.toJSON() as {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, we can directly make the fields of CodeMetadata as public readonly:

https://github.com/multiversx/mx-sdk-js-core/blob/main/src/smartcontracts/codeMetadata.ts

Then use those, without an extra mapping to JSON.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really good if someone uses the toggle... methods.

@popenta popenta merged commit 17e4312 into feat/factories Sep 25, 2023
1 check passed
@popenta popenta deleted the sc-intents-factory branch September 25, 2023 14:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants