> For the complete documentation index, see [llms.txt](https://smartscenarios.pirichain.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://smartscenarios.pirichain.com/psce-functions/examples/token-example-with-ticket-operation.md).

# Token Example with Ticket Operation

```javascript
//####PIRICHAIN Smart Scenario v.1.0########
// TICKET Token Sample Scenario

// System first fetches data from external environment , 
//checks the received data where is real or fake user infos
//and then
// controls to send one TICKET Token to users and cut 10 PIRI coin for each TICKET.

 async function TryToBuyTicket(name,surname)
{
    // Checking Amount Price
    if (parseInt(AMOUNT)<10 )
        return {error:1,message:'You must send 10 PIRI Coin for buy ticket!'};

    const userPiriBalance=await Transaction.getBalance(EXECUTER_ADDRESS,-1);

    if (parseFloat(userPiriBalance.balance)<10)
       return {error:1,message:'You dont have enough Piri Coin to buy ticket!'};

    let result=await Tools.getData('https://dummyjson.com/users');
    if (!result) return {error:1,message:'Result Object not fetched!'};
    if (!result.data) return {error:1,message:'Result Data not fetched!'};
    if (!result.data.users) return {error:1,message:'Users not fetched!'};
    try
    {
        var user=result.data.users.find((user)=> 
                        user.firstName==name 
                        && user.lastName==surname);
        if (user)
        {
            const isUserBoughtBeforeTicket=await Transaction.getBalance(EXECUTER_ADDRESS,10006);
            if (parseInt(isUserBoughtBeforeTicket.balance)==0)
            {
                // Send to Ticket Revelant Address
                const transactionResult=await Transaction.sendToken(EXECUTER_ADDRESS,1,10006);
                if (transactionResult.tx)
                {
                    // Inform to ticket organizator using with their service 
                    //for completed new ticket operation
                    await Tools.postData('https://ticketorganizatorservice.xyz/newTicket/',{tx:transactionResult.tx,address:EXECUTER_ADDRESS});
                    return {error:0,message:'Congratulations! Ticket has been transferred to your wallet Operation Number:'+transactionResult.tx};
                }
                else
                return {error:1,message:'There was an error while processing!'};
            }
            else
                return {error:1,message:'You have already a ticket! '+
                                         'You cannot buy twice this address!'};
        }
        else
            return {error:1,message:'User not found for this ticket organization!'};
    }
    catch(e)
    {
        return {error:1,message:e.message};
    }
}

async function getAllUsers()
{
        let result=await Tools.getData('https://dummyjson.com/users');
        return result;

}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://smartscenarios.pirichain.com/psce-functions/examples/token-example-with-ticket-operation.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
