Closed yuriescl closed 1 year ago
By looking at the Python Soroban SDK Enum implementation, it seems that enums can be converted into vectors:
def to_xdr_sc_val(self) -> stellar_xdr.SCVal:
vec: List[stellar_xdr.SCVal] = [
stellar_xdr.SCVal(
stellar_xdr.SCValType.SCV_SYMBOL,
sym=stellar_xdr.SCSymbol(self.key.encode()),
),
]
if self.value is not None:
vec.append(self.value)
return stellar_xdr.SCVal.from_scv_vec(stellar_xdr.SCVec(vec))
So I was able to get it working by using a vector to represent the enum:
SorobanClient.xdr.ScVal.scvVec([
SorobanClient.xdr.ScVal.scvSymbol(
ethereumjs.Buffer.Buffer.from("Identifier", "utf-8")
),
SorobanClient.xdr.ScVal.scvSymbol("IDENT1"),
])
Updated code:
SorobanClient.TransactionBuilder(account, {
fee: config.fee,
networkPassphrase: config.networkPassphrase,
})
.addOperation(
contract.call(
"myfunc",
SorobanClient.xdr.ScVal.scvU32(param1),
SorobanClient.xdr.ScVal.scvSymbol(param2),
SorobanClient.xdr.ScVal.scvVec([
SorobanClient.xdr.ScVal.scvSymbol(
ethereumjs.Buffer.Buffer.from("Identifier", "utf-8")
),
SorobanClient.xdr.ScVal.scvSymbol("IDENT1"),
])
)
)
.setTimeout(30)
.build();
It would be nice to have built-in support for enums in JS SDK, similar to the Python SDK.
In the Browser (not Node.js) I'm trying to invoke a contract function but one of parameters is a
Enum
in Rust. How do I build and pass an instance ofMyEnum
to the function?I'm using this to import the JS SDK:
This is the the code I'm trying to fix:
In the above code, the function
myfunc
receives two parameters, the first one is of typeu32
, and second one is of typeSymbol
. But in my contract, there's a third parameter of typeMyEnum
, which is anEnum
:Also, if
param3
wasstruct
instead ofEnum
, how would I build and pass the struct to the function?