# BankController

The bank controller's purpose is to allow for custom currency integrations.

All functions shown below are required and the data you provide is completely up to you.&#x20;

(Usually currency integrations will use [Websockets](https://docs.streamavatars.com/lua-scripting-api/api-reference-and-tips/events/websockets) or [WWWRequests ](https://docs.streamavatars.com/lua-scripting-api/api-reference-and-tips/global-functions/webrequest-get-post)to be the middle-man for talking between applications)

First you have to enable the LuaScript Integration found in: Shop Editing > Currency Settings.

<div><figure><img src="https://2994430787-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYvA0UlM6GCcOd8hKhafT%2Fuploads%2F4o5Wb3uNnHqNyYiZuX1T%2FRs84Z51nih.png?alt=media&#x26;token=b3d6d927-a098-4f5d-9a9c-36c0de6faf16" alt=""><figcaption></figcaption></figure> <figure><img src="https://2994430787-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYvA0UlM6GCcOd8hKhafT%2Fuploads%2FS5IXHgkE4qtUkIEVqkVY%2F3KV9sRSFax.png?alt=media&#x26;token=c0552a53-3f06-47bd-95c3-62f876db1713" alt=""><figcaption></figcaption></figure></div>

<pre class="language-lua"><code class="lang-lua">script_trigger_type = 'On Connect';

--note:
<strong>--the withdraw, deposit, getBalance, getRichest, fixedAdjustment 
</strong><strong>--MUST return value(s);
</strong>


function fixedAdjustment(viewerId, amount)

    --code to set the viewerId's currency to the exact amount
    
    
    return amount; --return the new balance
end


function getRichest(howMany)
    --this is used for extension leaderboard data. the data returned here will be displayed on the extension

    --try to grab the top howMany requested and put it into a richest array like so...
    --this array will be sorted correctly so order doesnt matter, just ensure it's the richest users.

    local richestArray = {
        { displayName = 'clonzeh', points = 1234 },
        { displayName = 'Person2', points = 442 },
        { displayName = 'Person3', points = 14532 }
    };

    return richestArray; --return the richest users array
end


function getBalance(viewerId)
    --code to check lua balance of viewerId;
    return 1234; --return the balance of the viewer
end

function deposit(viewerId, amount)
    --code to add points to the viewerid
    local newBalance = 1234 + amount;
    --finish code
    
    return newBalance; --return the new balance of the viewer
end

function withdraw(viewerId, amount)

    local balanceFound = 1234;
    local success = false;
    
    if balanceFound > amount then
        --code to subtract points from the viewerId
        local newBalance = balanceFound - amount;
        --if the subtraction works then
        --set success to true...
        success = true;
        return success, newBalance;
    end

    return success, balanceFound; --return the new balance of the user
end

function massDeposit(viewerIds, amounts)

    --efficiently deposit large amounts points to users...
    --this is useful if a custom bot currency system has an endpoint to adjust multiple users at once.


    --or just do it one at a time inside a loop the deposit(viewerId, amount); function
    for key, value in ipairs(viewerIds) do
        deposit(value, amounts[key]);
    end
end

return function()
    setupBankController(); --adds all required events for the bank controlling system.
    --NOTE: All functions above must exist with the same parameters specificed.
    
    keepAlive();
end
</code></pre>

{% hint style="info" %}
Events are asynchronous coroutines.
{% endhint %}
