Search code examples
blockchainsmartcontractswavewavesplatformride

How to create a whitelist as a smart-asset?


I need someone to help me create a simple smart asset. I just want that you can not trade under any pair and that it can only be sent to 2 specific Waves addresses. Can someone help me with this?


Solution

  • You can add the list of addresses to the sender account by using Data transaction in Waves Console (here I added two addresses to the Whitelist) and then broadcast the transaction to the network:

    const DataTx = 
    data(
    {
    data: [
    {key: "3Mt2yEuqDZVSfN7jqzvtkresLRah329k2ya", value: 12},
    {key: "3N17vWKRThx5eKkPLC18UjyUuFr4X3sSKCD", value: 10}], 
    fee: 1500000
    }
       )
    broadcast(DataTx)
    

    For the smart contract, you can use the pattern matching mechanism to allow transfer transaction only by checking if the recipient address is defined in the sender whitelist account, otherwise the transfer transaction will not be allowed:

    let whiteListAccount = tx.sender //In this line, we just defined the sender (in our case Bob).
    match tx 
    {  
    case tx : TransferTransaction => let recipient = toBase58String(addressFromRecipient(tx.recipient).bytes)
    isDefined(getInteger(whiteListAccount, recipient))
    case _ => true 
    }
    

    After that you will need to compile the smart contract and set the script by Waves Console as following:

    const Tx = setScript
     (
    {
    Script: compile(contract()),
    senderPublicKey:publicKey(),
    Fee:1400000
    }
     )
    
    broadcast(Tx)