-
Notifications
You must be signed in to change notification settings - Fork 89
Description
Hi team.
I try to buy my items on smart contract :
Chain id : 16602 | RPC : https://evmrpc-testnet.0g.ai/ | contract address : 0xdbb716eb769df16c224ab094e509b8f980a317ab
use both the code : contract.Write and : thirdwebTransaction.SendAndWaitForTransactionReceipt(transaction);
contract = await ThirdwebManager.Instance.GetContract(contractAddress, ActiveChainId,contractAbi);
` public async void BuyLife(GameLivesEnum livesEnum, int quantity )
{
txtLog.text = "Vao";
BigInteger itemIdToBuy = (int)livesEnum;
if (contract == null)
{
txtLog.text = "Contract not initialized.";
Debug.LogError("Contract not initialized."); // It's good practice to keep the error log for debugging
return;
}
txtLog.text = $"⏳ Fetching price for Item {itemIdToBuy}...";
try
{
// 2. Fetch Item Info (Price) from Smart Contract
// We read the 'items' mapping. We use the DTO we created in Step 1.
var itemData = await contract.Read<ShopItem>("items", itemIdToBuy);
Debug.Log($"[ITEM DATA] Name: {itemData.name} | Active: {itemData.active} | Price: {itemData.price} | Stock: {itemData.quantity}");
// CHECK 1: Is it active?
if (!itemData.active) {
txtLog.text = "❌ Transaction prevented: Item is INACTIVE.";
return;
}
// CHECK 2: Do we have enough Stock? (Common Cause of JSON-RPC Error)
if (itemData.quantity < quantity) {
txtLog.text = $"❌ Transaction prevented: Out of Stock! (Has: {itemData.quantity}, Need: {quantity})";
return;
}
// 3. Calculate Total Price (Price * Quantity)
BigInteger totalPrice = itemData.price * quantity;
// txtLog.text = $"💰 Buying {quantity} x {itemData.name} for {totalPrice} Wei...";
object[] args = { itemIdToBuy, quantity };
// 4. Execute Buy Transaction
// Syntax: contract.Write("functionName", valueToSend, param1, param2...)
// IMPORTANT: 'totalPrice' is passed as the "WeiValue" (the native token sent with tx)
ThirdwebTransaction transaction = await contract.Prepare(
wallet,
"buy",
weiValue : totalPrice,
parameters:args
);
transaction = transaction
.SetGasPrice(100000)
.SetGasLimit(250000);
var receipt = await ThirdwebTransaction.SendAndWaitForTransactionReceipt(transaction);
/*var receipt = await contract.Write(
wallet: wallet,
method: "buy",
weiValue: totalPrice,
args
);*/
txtLog.text = $"✅ Purchase Successful! Tx Hash: {receipt.TransactionHash}";
}
catch (System.Exception e)
{
txtLog.text = $"❌ Purchase Failed: {e.Message}";
}`
ABI :
public const string ABI = @"[ { ""type"": ""function"", ""name"": ""items"", ""inputs"": [{""name"": """", ""type"": ""uint256""}], ""outputs"": [ {""name"": ""price"", ""type"": ""uint256""}, {""name"": ""nftTokenId"", ""type"": ""uint256""}, {""name"": ""paymentToken"", ""type"": ""address""}, {""name"": ""quantity"", ""type"": ""uint88""}, {""name"": ""itemType"", ""type"": ""uint8""}, {""name"": ""nftContract"", ""type"": ""address""}, {""name"": ""active"", ""type"": ""bool""}, {""name"": ""name"", ""type"": ""string""} ], ""stateMutability"": ""view"" }, { ""type"": ""function"", ""name"": ""buy"", ""inputs"": [ {""name"": ""itemId"", ""type"": ""uint256""}, {""name"": ""quantity"", ""type"": ""uint88""} ], ""outputs"": [], ""stateMutability"": ""payable"" } ]";
but none of them are work , both fail with internal json-rpc error