Search in sources :

Example 16 with OTAPI_Func

use of com.moneychanger.core.util.OTAPI_Func in project otapij by FellowTraveler.

the class Market method createOrder.

public static boolean createOrder(String serverID, String nymID, String assetTypeID, String assetAcctID, String currencyTypeID, String currencyAcctID, String scale, String minIncrement, String quantity, String price, int selling) {
    // ----------------------------------------
    OTAPI_Func theRequest = new OTAPI_Func(OTAPI_Func.FT.CREATE_MARKET_OFFER, serverID, nymID, assetTypeID, assetAcctID, currencyTypeID, currencyAcctID, scale, minIncrement, quantity, price, selling == 1 ? true : false, // 86400 seconds == 1 day. TODO: Allow the USER to select the length for the offer...
    new String("86400"));
    String strResponse = OTAPI_Func.SendTransaction(theRequest, "CREATE_MARKET_OFFER");
    if (!Utility.VerifyStringVal(strResponse)) {
        System.out.println("IN createOrder: OTAPI_Func.SendTransaction(() failed. (I give up.) ");
        return false;
    }
    return true;
}
Also used : OTAPI_Func(com.moneychanger.core.util.OTAPI_Func)

Example 17 with OTAPI_Func

use of com.moneychanger.core.util.OTAPI_Func in project otapij by FellowTraveler.

the class NYM method sendMessage.

public boolean sendMessage(String serverID, String nymID, String recipientNymID, String message) throws InterruptedException {
    System.out.println("Send message starts recepientNymID:" + recipientNymID + " nymID:" + nymID + " serverID:" + serverID);
    //
    if (!otapiJNI.OTAPI_Basic_IsNym_RegisteredAtServer(nymID, serverID)) {
        OTAPI_Func theRequest = new OTAPI_Func(OTAPI_Func.FT.CREATE_USER_ACCT, serverID, nymID);
        String strResponse = OTAPI_Func.SendRequest(theRequest, "CREATE_USER_ACCT");
        if (!Utility.VerifyStringVal(strResponse)) {
            System.out.println("IN sendMessage: OTAPI_Func.SendRequest() failed. (I give up.) ");
            return false;
        }
    }
    // -----------------------------------------------
    // Okay the Nym is definitely registered at the server        
    String recipientPubKey = otapiJNI.OTAPI_Basic_LoadPubkey_Encryption(recipientNymID);
    System.out.println("recepientPubKey:" + recipientPubKey);
    // Download the recipient's pubkey since we don't already have it.
    if (!Utility.VerifyStringVal(recipientPubKey)) {
        // ----------------------------------------------------------
        OTAPI_Func theRequest = new OTAPI_Func(OTAPI_Func.FT.CHECK_USER, serverID, nymID, recipientNymID);
        String strResponse = OTAPI_Func.SendRequest(theRequest, "CHECK_USER");
        if (!Utility.VerifyStringVal(strResponse)) {
            System.out.println("IN sendMessage: OTAPI_Func.SendRequest() failed. (I give up.) ");
            return false;
        }
        // ----------------------------------------------------------
        recipientPubKey = otapiJNI.OTAPI_Basic_LoadPubkey_Encryption(recipientNymID);
    }
    if (!Utility.VerifyStringVal(recipientPubKey)) {
        System.out.println("recepientPubKey is null");
        return false;
    }
    System.out.println("Just before api call recepientNymID:" + recipientNymID + " nymID:" + nymID + " serverID:" + serverID);
    // --------------------------------------------------------
    OTAPI_Func theRequest = new OTAPI_Func(OTAPI_Func.FT.SEND_USER_MESSAGE, serverID, nymID, recipientNymID, recipientPubKey, message);
    String strResponse = OTAPI_Func.SendRequest(theRequest, "SEND_USER_MESSAGE");
    if (!Utility.VerifyStringVal(strResponse)) {
        System.out.println("IN sendMessage: OTAPI_Func.SendRequest() failed. (I give up.) ");
        return false;
    }
    return true;
}
Also used : OTAPI_Func(com.moneychanger.core.util.OTAPI_Func)

Example 18 with OTAPI_Func

use of com.moneychanger.core.util.OTAPI_Func in project otapij by FellowTraveler.

the class Basket method getAssetTypeName.

public static String getAssetTypeName(String assetTypeID, String serverID) {
    String name = otapiJNI.OTAPI_Basic_GetAssetType_Name(assetTypeID);
    if (Utility.VerifyStringVal(name)) {
        return name;
    }
    if (!Utility.VerifyStringVal(otapiJNI.OTAPI_Basic_LoadAssetContract(assetTypeID))) {
        System.out.println("IN getAssetTypeName, OT_API_LoadAssetContract is null");
        // ----------------------------------------
        String nymID = new NYM().registeredNYM(serverID);
        if (!Utility.VerifyStringVal(nymID)) {
            return "Popup Dialog";
        }
        OTAPI_Func theRequest = new OTAPI_Func(OTAPI_Func.FT.GET_CONTRACT, serverID, nymID, assetTypeID);
        String strResponse = OTAPI_Func.SendRequest(theRequest, "GET_CONTRACT");
        if (!Utility.VerifyStringVal(strResponse)) {
            System.out.println("IN getAssetTypeName: OTAPI_Func.SendRequest(() failed. (I give up.) ");
            return null;
        }
    }
    String strAssetID = new String(Utility.VerifyStringVal(otapiJNI.OTAPI_Basic_GetAssetType_Name(assetTypeID)) ? otapiJNI.OTAPI_Basic_GetAssetType_Name(assetTypeID) : "");
    return strAssetID;
}
Also used : OTAPI_Func(com.moneychanger.core.util.OTAPI_Func)

Example 19 with OTAPI_Func

use of com.moneychanger.core.util.OTAPI_Func in project otapij by FellowTraveler.

the class Basket method getRegisteredAssets.

public static List getRegisteredAssets(String serverID, String nymID) {
    System.out.println("getRegisteredAssets - serverID:" + serverID + " nymID:" + nymID);
    List registeredAssetsList = null;
    // output will go here.
    String strEncodedObj = null;
    // we are about to create this object
    StringMap stringMap = null;
    Storable storable = otapi.CreateObject(StoredObjectType.STORED_OBJ_STRING_MAP);
    if (storable != null) {
        stringMap = StringMap.ot_dynamic_cast(storable);
        if (stringMap != null) {
            // ADD ALL THE ASSET IDs HERE (To the string map, so you
            // can ask the server about them...)
            //
            int count = otapiJNI.OTAPI_Basic_GetAssetTypeCount();
            System.out.println(" count:" + count);
            for (int i = 0; i < count; i++) {
                String key = otapiJNI.OTAPI_Basic_GetAssetType_ID(i);
                System.out.println("key:" + key);
                stringMap.SetValue(key, "exists");
            }
            System.out.println(" BEFORE ENCODE,stringMap:" + stringMap);
            strEncodedObj = otapi.EncodeObject(stringMap);
        }
    }
    System.out.println(" fter ENCODE,strEncodedObj:" + strEncodedObj);
    if (!Utility.VerifyStringVal(strEncodedObj)) {
        System.out.println("strEncodedObj is null");
        return null;
    //Error;
    }
    // Then send the server message
    OTAPI_Func theRequest = new OTAPI_Func(OTAPI_Func.FT.QUERY_ASSET_TYPES, serverID, nymID, strEncodedObj);
    System.out.println(" theRequest :" + theRequest);
    String strResponse = theRequest.SendRequest(theRequest, "QUERY_ASSET_TYPES");
    System.out.println(" strResponse:" + strResponse);
    String strReplyMap = null;
    if (Utility.VerifyStringVal(strResponse)) {
        strReplyMap = otapiJNI.OTAPI_Basic_Message_GetPayload(strResponse);
    }
    System.out.println("strResponse is " + strResponse);
    if (Utility.VerifyStringVal(strReplyMap)) {
        StringMap stringMapOutput = null;
        storable = otapi.DecodeObject(StoredObjectType.STORED_OBJ_STRING_MAP, strReplyMap);
        if (storable != null) {
            stringMapOutput = StringMap.ot_dynamic_cast(storable);
            if (stringMapOutput != null) {
                // Loop through string map. For each asset ID key, the value will
                // say either "true" or "false".
                int count = otapiJNI.OTAPI_Basic_GetAssetTypeCount();
                for (int i = 0; i < count; i++) {
                    String key = otapiJNI.OTAPI_Basic_GetAssetType_ID(i);
                    System.out.println("key in output:" + key);
                    String isRegistered = stringMapOutput.GetValue(key);
                    System.out.println("isRegistered in output:" + isRegistered);
                    if ("true".equalsIgnoreCase(isRegistered)) {
                        if (registeredAssetsList == null) {
                            registeredAssetsList = new ArrayList();
                        }
                        registeredAssetsList.add(key);
                    }
                }
            }
        }
    }
    System.out.println("registeredAssetsList is " + registeredAssetsList);
    return registeredAssetsList;
}
Also used : StringMap(org.opentransactions.otapi.StringMap) OTAPI_Func(com.moneychanger.core.util.OTAPI_Func) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Storable(org.opentransactions.otapi.Storable)

Example 20 with OTAPI_Func

use of com.moneychanger.core.util.OTAPI_Func in project otapij by FellowTraveler.

the class CashPurseAccount method exportCashPurse.

// Input: server ID, assetID, Nym of current owner, existing purse, list of selected tokens, Nym of Recipient, and bool isPasted.
// Returns: "new Purse"
//
public String exportCashPurse(String serverID, String assetID, String nymID, String oldPurse, ArrayList selectedTokens, String recepientNymID, boolean isPasted) {
    System.out.println("exportCashPurse starts, selectedTokens:" + selectedTokens);
    Helpers.setObj(null);
    //
    if (!Utility.VerifyStringVal(recepientNymID) || recepientNymID.length() == 0) {
        System.out.println("exportCashPurse: recepientNym empty--using NymID for recipient instead: " + nymID);
        recepientNymID = nymID;
    }
    //
    if (isPasted && !recepientNymID.equals(nymID)) {
        String recepientPubKey = otapiJNI.OTAPI_Basic_LoadPubkey_Encryption(recepientNymID);
        System.out.println("recepientPubKey:" + recepientPubKey);
        //
        if (!Utility.VerifyStringVal(recepientPubKey)) {
            // ----------------------------------------------------------
            OTAPI_Func theRequest = new OTAPI_Func(OTAPI_Func.FT.CHECK_USER, serverID, nymID, recepientNymID);
            String strResponse = OTAPI_Func.SendRequest(theRequest, "CHECK_USER");
            if (!Utility.VerifyStringVal(strResponse)) {
                System.out.println("IN exportCashPurse: OTAPI_Func.SendRequest() failed. (I give up.) ");
                return null;
            }
            // ----------------------------------------------------------
            recepientPubKey = otapiJNI.OTAPI_Basic_LoadPubkey_Encryption(recepientNymID);
        }
        // Still?
        if (!Utility.VerifyStringVal(recepientPubKey)) {
            System.out.println("exportCashPurse: recepientPubKey is null");
            return null;
        }
    }
    // --------------------------------------------------
    // By this point, we have verified that we can load the public key for the recipient.
    // (At least, as long as "isPasted" is true.)
    String token = null;
    String exportedToken = null;
    //
    if (selectedTokens.size() == 1) {
        // New Purse is created with Nym as the owner (NOT!!!!!! recipient.)
        //
        String newPurse = otapiJNI.OTAPI_Basic_CreatePurse(serverID, assetID, nymID, nymID);
        if (!Utility.VerifyStringVal(newPurse)) {
            System.out.println("OT_API_CreatePurse returned null");
            return null;
        }
        // "int count" starts out containing the count of the tokens in OLD PURSE.
        //
        int count = otapiJNI.OTAPI_Basic_Purse_Count(serverID, assetID, oldPurse);
        // tempPurse now contains a COPY of OLD PURSE. (From old owner...)
        String tempPurse = oldPurse;
        for (// iterate through tempPurse, all the tokens.
        int i = 0; // iterate through tempPurse, all the tokens.
        i < count; // iterate through tempPurse, all the tokens.
        i++) {
            // "token" now contains the token for this iteration.
            // DOESN'T POP!! Only peeks.
            token = otapiJNI.OTAPI_Basic_Purse_Peek(serverID, assetID, nymID, tempPurse);
            // Here we explicitly "pop" as well, so we are positioned in case of any "continue"s.
            //
            String returnStringVal = otapiJNI.OTAPI_Basic_Purse_Pop(serverID, assetID, nymID, tempPurse);
            if (// this should never happen
            !Utility.VerifyStringVal(token) || !Utility.VerifyStringVal(returnStringVal)) {
                System.out.println("IN export cash, OT_API_Purse_Peek returned null... (should never happen.) ");
                return null;
            }
            // tempPurse now contains its former contents, MINUS the token being EXPORTED.
            //
            tempPurse = returnStringVal;
            // -----------------------------------
            // We read the token's ID, so we can see if it is on the list of SELECTED tokens (to be exported...)
            String tokenID = otapiJNI.OTAPI_Basic_Token_GetID(serverID, assetID, token);
            //
            if (selectedTokens.contains(tokenID)) {
                exportedToken = otapiJNI.OTAPI_Basic_Token_ChangeOwner(serverID, assetID, token, nymID, nymID, recepientNymID);
            // Normally I'd "break" here, since we were only looking to export a single token.
            // However, I still need to iterate the rest of the tokens onto the NEW PURSE, and then SAVE IT.
            // That's why I don't break here, and instead allow the loop to continue.
            } else // If the token was NOT selected for export, just push it onto the new purse (for OLD owner, since he still owns it.)
            // This will happen with ALL TOKENS except ONE.
            {
                returnStringVal = otapiJNI.OTAPI_Basic_Purse_Push(serverID, assetID, nymID, nymID, newPurse, token);
                if (// this should never happen
                !Utility.VerifyStringVal(returnStringVal)) {
                    System.out.println("IN export cash, OT_API_Purse_Push returned null... (should never happen.) Returning null.");
                    return null;
                }
                newPurse = returnStringVal;
            }
        }
        //
        if (!otapiJNI.OTAPI_Basic_SavePurse(serverID, assetID, nymID, newPurse)) {
            // If SAVE FAILS (==0) then set Utility obj to OLD PURSE.
            // Displays on screen??
            Helpers.setObj(oldPurse);
            return null;
        }
        // This means a token is sometimes returned (here), and sometimes a purse (below.)
        return exportedToken;
    }
    // By this point, we know that "selected tokens" has a size of 0, or MORE THAN ONE.
    // Next I create another "newPurse" by calling this function.
    // I pass it the server, asset, NYM ID, OLD PURSE, the selected tokens, and the RECIPIENT NYM.
    //
    String newPurse = processCashPurse(serverID, assetID, nymID, oldPurse, selectedTokens, recepientNymID);
    //
    return newPurse;
}
Also used : OTAPI_Func(com.moneychanger.core.util.OTAPI_Func)

Aggregations

OTAPI_Func (com.moneychanger.core.util.OTAPI_Func)30 HashMap (java.util.HashMap)3 Map (java.util.Map)3 MarketData (org.opentransactions.otapi.MarketData)3 MarketList (org.opentransactions.otapi.MarketList)3 MarketTicker (com.moneychanger.core.dataobjects.MarketTicker)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 OfferDataNym (org.opentransactions.otapi.OfferDataNym)2 OfferListNym (org.opentransactions.otapi.OfferListNym)2 MarketDetails (com.moneychanger.core.dataobjects.MarketDetails)1 Date (java.util.Date)1 AskData (org.opentransactions.otapi.AskData)1 BidData (org.opentransactions.otapi.BidData)1 OfferListMarket (org.opentransactions.otapi.OfferListMarket)1 Storable (org.opentransactions.otapi.Storable)1 StringMap (org.opentransactions.otapi.StringMap)1 TradeDataMarket (org.opentransactions.otapi.TradeDataMarket)1 TradeListMarket (org.opentransactions.otapi.TradeListMarket)1 Utility (org.opentransactions.otjavalib.util.Utility)1