I am trying to update the virtual wallet, but I keep getting this error:
ResponseError: code: 3, message: json: cannot unmarshal object into Go value of type string
Here is the code on my Flutter frontend:
// Deposit funds
Future<bool> deposit(double amount, {String? phoneNumber}) async {
try {
// Validate amount
if (amount <= 0) {
return false;
}
// Prepare payload
final payload = {
'amount': amount.toString(),
};
// Add phone number if provided
if (phoneNumber != null) {
payload['phoneNumber'] = phoneNumber;
}
final result = await client.rpc(
session: session,
id: rpcProcessDeposit,
payload: jsonEncode(payload),
);
final response = jsonDecode(result!);
if (response['success'] == true) {
// Update local balance
balance.value = (response['balance'] as num).toDouble();
return true;
} else {
print('Deposit failed: ${response['error']}');
return false;
}
} catch (e) {
print('Error processing deposit: $e');
return false;
}
}
Below is my RPC on the server:
let rpcProcessDeposit: nkruntime.RpcFunction = function (ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, payload: string): string {
if (!ctx.userId) {
return JSON.stringify({ success: false, error: "No user ID in context" });
}
try {
const request = JSON.parse(payload);
const amount = Number(request.amount);
const phoneNumber = request.phoneNumber || '';
// Validate amount
if (isNaN(amount) || amount <= 0) {
return JSON.stringify({ success: false, error: "Invalid deposit amount" });
}
// Minimum deposit check
const minDeposit = 10;
if (amount < minDeposit) {
return JSON.stringify({
success: false,
error: `Minimum deposit amount is ${minDeposit} SZL`
});
}
// Validate phone number if provided
if (phoneNumber && !isValidPhoneNumber(phoneNumber)) {
return JSON.stringify({
success: false,
error: "Invalid phone number. Must be 8 digits starting with 76, 78, or 79"
});
}
// Create a wallet update
const changeset = {
"szl": amount // Using the default virtual currency
};
// Include transaction metadata
const metadata = {
"type": "deposit",
"phoneNumber": phoneNumber || "N/A"
};
// Update wallet
nk.walletUpdate(ctx.userId, changeset, metadata, true);
// Get updated balance
const account = nk.accountGetId(ctx.userId);
const updatedBalance = account.wallet["szl"] ? account.wallet["szl"] : 0;
// Log the transaction for records
logger.info("User %s deposited %d SZL via phone %s", ctx.userId, amount, phoneNumber || "N/A");
return JSON.stringify({ success: true, balance: updatedBalance });
} catch (error) {
logger.error("Error processing deposit: %v", error);
return JSON.stringify({ success: false, error: String(error) });
}
}
The RPC works perfectly when I use the API explorer in the server’s admin console. What is causing the mentioned error. Please help.