Adding Allocation Functionalities

Allocation Functions
const initClick = async () => {
    //Initialize SDK
    await init(config);
};

const listAllocationsClick = async () => {
    //Call listAllocations method
    const allocations = await listAllocations();
    console.log("allocations", allocations);
    setAllocationList(allocations);
};

const createAllocationClick = async () => {
  const expiry = new Date();
  expiry.setDate(expiry.getDate() + 30);

//datashards, parityshards int, size, expiry int64,minReadPrice, maxReadPrice, minWritePrice, maxWritePrice int64, lock int64,preferredBlobberIds []string
const config = {
      datashards: 2,
      parityshards: 2,
      size: 2 * 1073741824,
      expiry: Math.floor(expiry.getTime() / 1000),
      minReadPrice: 0,
      maxReadPrice: 184467440737095516,
      minWritePrice: 0,
      maxWritePrice: 184467440737095516,
      lock: 5000000000,
};

//Call createAllocation method
await createAllocation(config);
  listAllocationsClick();
  };

const createAllocationWithBlobbersClick = async () => {
  const preferredBlobbers = getBlobberListForAllocation();
  const expiry = new Date();
  expiry.setDate(expiry.getDate() + 30);

//datashards, parityshards int, size, expiry int64,minReadPrice, maxReadPrice, minWritePrice, maxWritePrice int64, lock int64,preferredBlobberIds []string
  const config = {
      datashards: 2,
      parityshards: 2,
      size: 2 * 1073741824,
      expiry: Math.floor(expiry.getTime() / 1000),
      minReadPrice: 0,
      maxReadPrice: 184467440737095516,
      minWritePrice: 0,
      maxWritePrice: 184467440737095516,
      lock: 5000000000,
      blobbers: preferredBlobbers,
    };

    //Call createAllocation method
    await createAllocationWithBlobbers(config);
    listAllocationsClick();
  };

  const getAllocationDetailsClick = async (allocationId) => {
    //Call getAllocation method
    const allocation = await getAllocation(allocationId);
    console.log("allocation", allocation);
    setAllocationDetails(allocation);
  };

  const reloadAllocationClick = async (allocationId) => {
    //Call reloadAllocation method
    const allocation = await reloadAllocation(allocationId);
    console.log("allocation", allocation);
    setAllocationDetails(allocation);
  };

  const freezeAllocationClick = async (allocationId) => {
    //Call freezeAllocation method
    await freezeAllocation(allocationId);
  };

  const cancelAllocationClick = async (allocationId) => {
    //Call cancelAllocation method
    await cancelAllocation(allocationId);
  };

  const updateAllocationClick = async () => {
    if (!selectedAllocation) {
      alert("Please select allocation for update");
      return;
    }
    console.log("updating allocation", selectedAllocation.id);

    const expiry = new Date();
    expiry.setDate(expiry.getDate() + 30);

    //allocationId string,size, expiry int64,lock int64, isImmutable, updateTerms bool,addBlobberId, removeBlobberId string
    const size = parseInt(allocationSize),
      expiryVal = Math.floor(expiry.getTime() / 1000),
      lock = 5000000000,
      updateTerms = true,
      addBlobberId = "",
      removeBlobberId = "";

    //Call updateAllocation method
    await updateAllocation(
      selectedAllocation.id,
      size,
      expiryVal,
      lock,
      updateTerms,
      addBlobberId,
      removeBlobberId
    );
  };

const getBlobberListForAllocation = async () => {
    const expiryDate = new Date();
    expiryDate.setDate(expiryDate.getDate() + 30);

    const referredBlobberURLs = [
        "https://dev2.zus.network/blobber02",
        "https://dev1.zus.network/blobber02",
      ],
      dataShards = 2,
      parityShards = 2,
      size = 2 * 1073741824,
      expiry = Math.floor(expiryDate.getTime() / 1000),
      minReadPrice = 0,
      maxReadPrice = 184467440737095516,
      minWritePrice = 0,
      maxWritePrice = 184467440737095516;

    //Call getAllocationBlobbers method
    const blobberList = await getAllocationBlobbers(
      referredBlobberURLs,
      dataShards,
      parityShards,
      size,
      expiry,
      minReadPrice,
      maxReadPrice,
      minWritePrice,
      maxWritePrice
    );
    console.log("blobberList", blobberList);
    return blobberList;
  };

  const getAllocationBlobbersClick = async () => {
    await getBlobberListForAllocation();
  };

  const getBlobberIdsClick = async () => {
    //https://dev1.zus.network/sharder01/v1/screst/6dba10422e368813802877a85039d3985d96760ed844092319743fb3a76712d7/getblobbers
    //const blobberUrls = [];
    const blobberUrls = [
      "https://dev2.zus.network/blobber02",
      "https://dev1.zus.network/blobber02",
    ];
    //Call getBlobberIds method
    const blobberIds = await getBlobberIds(blobberUrls);
    console.log("blobberIds", blobberIds);
  };

  const getAllocationFromAuthTicketClick = async () => {
    //Call getAllocationFromAuthTicket method
    console.log("GetAllocFromAuthTicket", authTicket);
    const allocation = await getAllocationFromAuthTicket(authTicket);
    console.log("allocation", allocation);
  };

const createReadPoolClick = async () => {
    //Call createReadPool method
    const result = await createReadPool();
    console.log("result", result);
  };

  const getReadPoolInfoClick = async () => {
    //Call getReadPoolInfo method
    console.log("GetReadPoolInfo", clientId);
    const result = await getReadPoolInfo(clientId);
    console.log("result", result);
  };

  const lockWritePoolClick = async () => {
    //Call lockWritePool method
    const allocationId = selectedAllocation.id;
    console.log("LockWritePool", allocationId);
    //allocationId string, tokens string, fee string
    const result = await lockWritePool(allocationId, 1000, 10);
    console.log("result", result);
  };

  const decodeAuthTicketClick = async () => {
    //Call decodeAuthTicket method
    console.log("DecodeAuthTicket", authTicket);
    const result = await decodeAuthTicket(authTicket);
    console.log("result", result);
  };

  const greetClick = async () => {
    //Call Greeter method
    const greetMessage = Greeter("john doe");
    setMessage(greetMessage);
  };

  const selectAllocation = (allocation) => {
    setSelectedAllocation(allocation);
  };

const selectFile = (file) => {
    setSelectedFile(file);
    console.log("selected file", file);
  };

const showLogsClick = async () => {
    await showLogs();
  };

  const hideLogsClick = async () => {
    await hideLogs();
  };

Describing Code

Line 1 to 219 defines the following allocation functionalities for the web app :

  • initClick:Initialize SDK functionality on click.js-sdk init function is called. Here await keyword tells that the async function stops the execution until the config data is fetched.

  • listAllocationsClick: List Allocations on mouse click . Retrieved via js-sdk listAllocations function.

  • createAllocationClick :Create allocation on click.Allocation is created via js-sdk createAllocation function. Hereawait keyword tells that the async create allocation function to stop the execution until the allocation config is provided . config constant provides configuration for the allocation.

  • createAllocationWithBlobbersClick: Create allocation on a specific set /preferred blobbers.The blobber list is fetched via getBlobberListforAllocation function .Js-sdk createAllocationWithBlobbers method is called.

  • getAllocationDetailsClick :Get Allocation details in web app by calling listAllocationsClick function defined above.

  • reloadAllocationClick : Reloads the allocation in web app by calling js-sdk reloadAllocation method.

  • freezeAllocationClick : Freeze Allocation on click in web app by calling js-sdk freezeAllocation method.Await execution until allocation id is provided.

  • cancelAllocationClick :Cancel Allocation on click .Functionality provided by js-sdk cancelAllocation function.

  • updateAllocationClick : Update Allocation configuration.Functionality provided by js-sdk updateAllocation method .Execution of updateAllocation method is awaited until updated allocation config is provided in term of allocation expiry or size.

  • getBlobberListForAllocation: Get list of blobbers available to host allocation.Calls js-sdk getAllocationBlobbers method.

  • getAllocationBlobbersClick :Get Blobbers for Allocation .awaits execution of getBlobberListForAllocation function specified above.

  • getBlobberIdsClick :Get blobber ID's on click.Calls js-sdk getBlobberIds method .

  • getAllocationFromAuthTicketClick : Get Allocation with AuthTicket.Calls js-sdk getAllocationFromAuthTicket .

  • createReadPoolClick : Create Read Pool for Storage SC. Calls js-sdk createReadPool method

  • getReadPoolInfoClick: Get Read pool information.Calls js-sdk getReadPoolInfo method.

  • lockWritePoolClick: Lock Tokens into Write Pool. Calls js-sdk lockWritePool method.

  • getBlobbersClick: Get Blobbers for honoring allocation. Calls js-sdk getBlobbers method.

  • decodeAuthTicket: A function for decoding AuthTicket generated by file share. Calls js-sdk decodeAuthTicket method.

  • greetClick: A function for displaying a greeting message.

  • selectAllocation: A helper function for setting allocation.

  • selectFile: A helper function for selecting file for operations.

  • showLogs: Show logs for web app functionalities.Calls js-sdk showLogs method.

  • hideLogs: Hide logs for web app functionalities. Calls js-sdk hideLogs method

Last updated