Skip to content

Commit 341b734

Browse files
authored
Merge pull request #673 from multiversx/allow-args-as-bytes
Allow args as bytes for SC interactions
2 parents fcbca0c + 191fa74 commit 341b734

File tree

4 files changed

+66
-4
lines changed

4 files changed

+66
-4
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@multiversx/sdk-core",
3-
"version": "15.2.2",
3+
"version": "15.3.0",
44
"description": "MultiversX SDK for JavaScript and TypeScript",
55
"author": "MultiversX",
66
"homepage": "https://multiversx.com",

src/smartContracts/smartContractTransactionsFactory.spec.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { assert } from "chai";
2-
import { Abi, U32Value } from "../abi";
2+
import { Abi, BigUIntValue, U32Value } from "../abi";
33
import { Address, Err, Token, TokenTransfer, TransactionsFactoryConfig } from "../core";
44
import { loadAbiRegistry, loadContractCode } from "../testutils/utils";
55
import { SmartContractTransactionsFactory } from "./smartContractTransactionsFactory";
@@ -44,6 +44,54 @@ describe("test smart contract transactions factory", function () {
4444
}
4545
});
4646

47+
it("should allow args of type 'TypedValue'", async function () {
48+
const sender = Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th");
49+
const gasLimit = 6000000n;
50+
const args = [new BigUIntValue(7)];
51+
52+
const transaction = await factory.createTransactionForDeploy(sender, {
53+
bytecode: bytecode.valueOf(),
54+
gasLimit: gasLimit,
55+
arguments: args,
56+
});
57+
58+
const bytecodeHex = Buffer.from(bytecode).toString("hex");
59+
assert.deepEqual(transaction.data, Buffer.from(`${bytecodeHex}@0500@0504@07`));
60+
});
61+
62+
it("should allow args of type bytes", async function () {
63+
const sender = Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th");
64+
const gasLimit = 6000000n;
65+
let args = [Buffer.from([7]), new Uint8Array([7])];
66+
const bytecodeHex = Buffer.from(bytecode).toString("hex");
67+
68+
let transaction = await factory.createTransactionForDeploy(sender, {
69+
bytecode: bytecode.valueOf(),
70+
gasLimit: gasLimit,
71+
arguments: args,
72+
});
73+
74+
assert.deepEqual(transaction.data, Buffer.from(`${bytecodeHex}@0500@0504@07@07`));
75+
76+
args = [new Uint8Array([7]), Buffer.from("6161626261", "hex")];
77+
transaction = await factory.createTransactionForDeploy(sender, {
78+
bytecode: bytecode.valueOf(),
79+
gasLimit: gasLimit,
80+
arguments: args,
81+
});
82+
83+
assert.deepEqual(transaction.data, Buffer.from(`${bytecodeHex}@0500@0504@07@6161626261`));
84+
85+
args = [new Uint8Array()];
86+
transaction = await factory.createTransactionForDeploy(sender, {
87+
bytecode: bytecode.valueOf(),
88+
gasLimit: gasLimit,
89+
arguments: args,
90+
});
91+
92+
assert.deepEqual(transaction.data, Buffer.from(`${bytecodeHex}@0500@0504@`));
93+
});
94+
4795
it("should create 'Transaction' for deploy", async function () {
4896
const sender = Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th");
4997
const gasLimit = 6000000n;

src/smartContracts/smartContractTransactionsFactory.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,23 @@ export class SmartContractTransactionsFactory extends BaseFactory {
226226
return new ArgSerializer().valuesToStrings(args);
227227
}
228228

229+
if (this.areArgsBuffers(args)) {
230+
return args.map((arg) => Buffer.from(arg).toString("hex"));
231+
}
232+
229233
throw new Err("Can't convert args to TypedValues");
230234
}
231235

236+
private areArgsBuffers(args: any[]): boolean {
237+
for (const arg of args) {
238+
if (!ArrayBuffer.isView(arg)) {
239+
return false;
240+
}
241+
}
242+
243+
return true;
244+
}
245+
232246
private areArgsOfTypedValue(args: any[]): boolean {
233247
return args.every((arg) => isTyped(arg));
234248
}

0 commit comments

Comments
 (0)