🍁Asset Management Of Share Holders using with PSce

This scenario presents that 2 different share holders have been created at first stage. Then these people can sell their share holder percentage to other people.

//####PIRICHAIN Smart Scenario v.1.0########
// PiriChain Smart Scenario Code Blocks Area 

 // All Functions must be return promise non-blocking async block or await prefix delimitier..  

const myProductName='Ferrari';
const myProductWorth=1000000;
const contractExplanation='This Product can be sold with shareholders how much they have. And All of Shareholders must accept before operations!';

async function initializeDeliverShareHolders()
{

    if (PSDATA["shareHolders"]==undefined)
    {
            PSDATA['totalPerson']=2;
            PSDATA["shareHolders"]=new Array();
            PSDATA["shareHolders"].push({name:'Alex',holderpercent:50,address:'PRTMRKWZ56v4D8mV1oj813bqTgdfUm9J4woVTDMpGxF'});
            PSDATA["shareHolders"].push({name:'Barbara',holderpercent:50,address:'PRTMPp6wo6zGAcStmhmBGQErnomzhFCoaTfLuXKTvo2'});
            await Map.saveMap(JSON.stringify(PSDATA));       
    }
    return PSDATA["shareHolders"];
}
async function getHoldersWithAuthorized()
{
        initializeDeliverShareHolders();
        
        if (PSDATA["shareHolders"].find(r=>r.address==EXECUTER_ADDRESS)!=undefined)
            return PSDATA;
        else
            return {error:1,message:'You dont have right to execute this scenario. You are not shareholder in this scenario!'};
}

async function sellYourShareHolder(acceptText,buyerName,buyerAddress,buyerHolderPercent)
{
    initializeDeliverShareHolders();

    if (acceptText!=='I accept to sell my holder')
            return {error:1,message:'You need to write for accepting your holder!'};

    if (buyerAddress===EXECUTER_ADDRESS)
        return {error:1,message:'You cannot transfer to your self!'};

    if (parseFloat(buyerHolderPercent)<=0 || isNaN(parseFloat(buyerHolderPercent)))
        return {error:1,message:'Percent is not true format!'};

    const myHolder=PSDATA["shareHolders"].find(r=>r.address===EXECUTER_ADDRESS);
    if (myHolder==undefined)
        return {error:1,message:'Your holder has not been found!'};

    if (myHolder.holderpercent<buyerHolderPercent)
        return {error:1,message:'Your Percent is not enough that you wanted to sell! Please decrease the holder to sell'};
    
    myHolder.holderpercent-=buyerHolderPercent;

    const isExistsHolder=PSDATA["shareHolders"].find(r=>r.address===buyerAddress);
    if (isExistsHolder==undefined)
    {
        PSDATA['totalPerson']+=1;
        PSDATA["shareHolders"].push({name:buyerName,holderpercent:buyerHolderPercent,address:buyerAddress});
    }
    else
    {
        isExistsHolder.holderpercent+=buyerHolderPercent;
    }
    
    return await Map.saveMap(JSON.stringify(PSDATA));       
}
async function getTotalPersonCount()
{
    initializeDeliverShareHolders();
    return PSDATA["totalPerson"];
}

async function listHolderTransactions(skip,limit)
{
    if (parseInt(skip)<0 || parseInt(limit)<=0)
    return {error:1,message:'Skip or Limit Param(s) are not true format!'};
    return await Transaction.listTransactions(EXECUTER_SCENARIOADDRESS,skip,limit);
}

async function getMyShareHolderPercentage()
{
    initializeDeliverShareHolders();
    const myHolder= PSDATA["shareHolders"].find(f=>f.address==EXECUTER_ADDRESS);
    if (myHolder===undefined)
        return 0;
    else
       return myHolder.holderpercent;
}

// Auto generated Withdraw Function.
// Please DO NOT Edit and Remove WithdrawAllAssetToOwnerWallet function!
/// Withdraw all amount of defined asset_ID to scenario owner address
async function WithdrawAllAssetToOwnerWallet(asset_ID)
{
    if (OWNER_ADDRESS!==EXECUTER_ADDRESS)
    return {error:1,data:'Only scenario owner can execute this method!'};
    if (asset_ID)
    {
        if (!isNaN(parseInt(asset_ID)))
        {
            const myBalanceObject=await Transaction.getBalance(EXECUTER_SCENARIOADDRESS,-1);
            let myBalance=parseFloat(myBalanceObject.balance);
            if (asset_ID===-1)
            {
                if (myBalance<1)
                    return {error:1,data:'Your Piri Coin is not enough to transfer!'};
                myBalance-=1; // Fee
                    return await Transaction.sendPIRI(OWNER_ADDRESS,myBalance);
            }
            else
            {
                if (myBalance<1)
                    return {error:1,data:'Your Piri Coin is not enough to transfer!'};             
                const myTokenBalanceObject=await Transaction.getBalance(EXECUTER_SCENARIOADDRESS,asset_ID);
                let myTokenBalance=parseFloat(myTokenBalanceObject.balance);
                if (myTokenBalance>0)
                    return await Transaction.sendToken(OWNER_ADDRESS,myTokenBalance,asset_ID);
                else
                return {error:1,data:'You dont have enough token balance!'};
            }

        }
    }
}

Last updated