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

Revert interaction error to warning #421

Merged
merged 5 commits into from
Mar 27, 2024
Merged

Conversation

popenta
Copy link
Contributor

@popenta popenta commented Mar 26, 2024

Reverted the error that was throwing when interaction was used without withSender() or with Address.Zero().

Now also the transactions factories use the ArgSerializer. TransferTransactionsFactory has changed a bit, the TransactionComputer is not needed anymore by the constructor.

The nonce parameter is now optional when instantiating a Token. The default value is 0.

@popenta popenta self-assigned this Mar 26, 2024

return new TransactionBuilder({
config: this.config!,
sender: options.sender,
receiver: options.receiver,
dataParts: [data],
dataParts: [Buffer.from(data).toString()],
Copy link
Contributor

Choose a reason for hiding this comment

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

Here might be an issue. Since dataParts wants strings - but Buffer.from(data).toString() is not necessarily a valid / utf8 string. Can we create the transaction without TransactionBuilder? Or perhaps just set the data after creating it with the builder?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think Buffer.toString() returns by default a utf8 string.

this.trueAsHex = utf8ToHex("true");
this.falseAsHex = utf8ToHex("false");
this.argSerializer = new ArgSerializer();
this.trueAsHex = this.argSerializer.valuesToStrings([new StringValue("true")])[0];
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, we don't have valueToString() here. All good.

Comment on lines 69 to 70
new BigUIntValue(options.initialSupply),
new BigUIntValue(options.numDecimals),
Copy link
Contributor

Choose a reason for hiding this comment

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

Can be merged into the call of valuesToStrings() above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed, done!

options.canFreeze ? this.trueAsHex : this.falseAsHex,
utf8ToHex("canWipe"),
this.argSerializer.valuesToStrings([new StringValue("canWipe")])[0],
Copy link
Contributor

Choose a reason for hiding this comment

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

Here, above and below, for easier reading, we can do this:

const args = [
    new StringValue(options.tokenName),
    new StringValue(options.tokenTicker),
    new BigUIntValue(options.numDecimals),
    new StringValue("canFreeze")
    new BooleanValue(options.canFreeze)
    ... and so on
]

const dataParts = ["registerMetaESDT", ...this.argSerializer.valuesToStrings(args)]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

...(options.addRoleNFTBurn ? [utf8ToHex("ESDTRoleNFTBurn")] : []),
...(options.addRoleNFTAddQuantity ? [utf8ToHex("ESDTRoleNFTAddQuantity")] : []),
...(options.addRoleESDTTransferRole ? [utf8ToHex("ESDTTransferRole")] : []),
...this.argSerializer.valuesToStrings([
Copy link
Contributor

Choose a reason for hiding this comment

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

A bit hard to read, here / above / below. Previously, code was easier to read.

Maybe do the args trick as in the comment above?

Also:

if options.addRoleNFTCreate:
    args.push(new StringValue("ESDTRoleNFTCreate"))

That is, ideally, we should only call valuesToStrings() once per factory function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

@@ -146,7 +166,11 @@ export class SmartContractTransactionsFactory {
const args = options.args || [];
const metadata = new CodeMetadata(isUpgradeable, isReadable, isPayable, isPayableBySmartContract);

let parts = ["upgradeContract", byteArrayToHex(options.bytecode), metadata.toString()];
let parts = [
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe here, keep byteArrayToHex, as before (simpler)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reverted the changes.


constructor(options: { config: Config; abi?: IAbi; tokenComputer: TokenComputer }) {
this.config = options.config;
this.abi = options.abi;
this.tokenComputer = options.tokenComputer;
this.dataArgsBuilder = new TokenTransfersDataBuilder();
this.argSerializer = new ArgSerializer();
Copy link
Contributor

Choose a reason for hiding this comment

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

In this file, maybe the code, as it was, was all right.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reverted.

dataParts.push(
...[
options.publicKeys[i].hex(),
this.argSerializer.valuesToStrings([new BytesValue(Buffer.from(options.signedMessages[i]))])[0],
Copy link
Contributor

Choose a reason for hiding this comment

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

We should call valuesToStrings() only once per factory method (ideally).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now it's only used once per method.

Comment on lines 444 to 446
"unPause",
this.argSerializer.valuesToStrings([new StringValue(options.tokenIdentifier)])[0],
];
Copy link
Contributor

Choose a reason for hiding this comment

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

Ideally, without [0]. We can reuse the same pattern as above / below, even if we only have one argument.

const dataParts = [
    "unPause",
     ...this.argSerializer.valuesToStrings([
        new StringValue(options.tokenIdentifier)
    ]),
];

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

];
const args = [new StringValue(options.tokenIdentifier), new AddressValue(options.user)];

options.addRoleNFTCreate ? args.push(new StringValue("ESDTRoleNFTCreate")) : args.push(...[]);
Copy link
Contributor

Choose a reason for hiding this comment

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

args.push(...[]); can be replaced with a no-operation such as 0; or null;.

https://stackoverflow.com/questions/21634886/what-is-the-javascript-convention-for-no-operation

Or, maybe, if you still want a one-liner instead of multi-line if statement:

args.push(...(condition ? [42] : []))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Replaced args.push(...[]); with 0.

const dataParts = ["unsetBurnRoleGlobally", utf8ToHex(options.tokenIdentifier)];
const dataParts = [
"unsetBurnRoleGlobally",
this.argSerializer.valuesToStrings([new StringValue(options.tokenIdentifier)])[0],
Copy link
Contributor

Choose a reason for hiding this comment

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

Preferably, use the same pattern as above / below, even if we have a single argument.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

new StringValue(options.tokenTicker),
new BigUIntValue(options.numDecimals),
new StringValue("canFreeze"),
new StringValue(options.canFreeze ? this.trueAsString : this.falseAsString),
Copy link
Contributor

Choose a reason for hiding this comment

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

A private function such as boolToString() might be useful. E.g.

new StringValue("canFreeze"),
new StringValue(boolToString(options.canFreeze)),
...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

indeed, added.

const dataParts = ["setCheckCapOnReDelegateRewards", utf8ToHex("true")];
const dataParts = [
"setCheckCapOnReDelegateRewards",
this.argSerializer.valuesToStrings([new StringValue("true")])[0],
Copy link
Contributor

Choose a reason for hiding this comment

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

Preferably, without [0] (e.g. reuse the same pattern as when having more arguments).


return new TransactionBuilder({
config: this.config!,
sender: options.sender,
receiver: options.receiver,
dataParts: [data],
dataParts: [Buffer.from(data).toString("utf8")],
Copy link
Contributor

Choose a reason for hiding this comment

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

If the user intends to have the tx.data as Buffer.from([10, 10]):

> Buffer.from([10, 10]).toString("utf-8")
'\n\n'
> 

In this case, however, the final tx.data will be fine:

> Buffer.from([10, 10]).toString("base64")
'Cgo='
> Buffer.from("\n\n").toString("base64")
'Cgo='
> 

But let's take the example when the user wants to send the byte 255.

// What the user wants
> Buffer.from([255]).toString("base64")
'/w=='

// What the user gets
> Buffer.from(Buffer.from([255]).toString("utf-8")).toString("base64")
'77+9'
> 

@bogdan-rosianu bogdan-rosianu self-requested a review March 27, 2024 12:58
@popenta popenta merged commit abe77cf into feat/next Mar 27, 2024
1 check passed
@popenta popenta deleted the revert-error-to-warning branch March 27, 2024 14:17
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