cpu_frac and net_frac calculation

Open
#4 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
25/100
Issue type
Bug
Clarity
Needs clarification
Activity status
Stale
Tech stack
typescript
Domain
blockchain

Research direction

Start at the rentResources entry point and trace powerup.cpu.frac_by_ms, powerup.net.frac_by_kb, and the POWER_UP action parameters. Reproduce the cases where either fraction is zero, then verify the resource calculation and action behavior for CPU-only, NET-only, and combined rentals.

Written by the indexing model from the issue text.

Description

i'm trying to rent cpu and net resources, but find out very strange behavior. when im trying to buy net, so my cpu_frac is 0, it only purchase cpu resource, i discovered the other way is also true. in my case, how should i adjust my code to get rid of this behavior of power up model and rent resources correctly?

briefly, i want to know how to calculate cpu and net frac correctly, thanks!

  async rentResources(params: RentResourcesDto): Promise<AccountResource> {
    const { payer, reciver, cpuAmountMs, netAmountKb } = params;

    const apiClient = getClient().apiClient;
    const eosResources = new Resources({
      api: apiClient,
    });

    const [powerup, rexState, ramState, sampleUsage] =
      await Promise.all([
        eosResources.v1.powerup.get_state(),
        eosResources.v1.rex.get_state(),
        eosResources.v1.ram.get_state(),
        eosResources.getSampledUsage(),
      ]);

    const cpuPrice = powerup.cpu.price_per_ms(sampleUsage, cpuAmountMs);
    const netPrice = powerup.net.price_per_kb(sampleUsage, netAmountKb);

    const totalCost = cpuPrice + netPrice;

    const days = powerup.powerup_days;

    // weird problematic part?
    const cpu_frac = powerup.cpu.frac_by_ms(sampleUsage, cpuAmountMs);
    const net_frac = powerup.net.frac_by_kb(sampleUsage, netAmountKb);

    const powerUpAction = contract.action(
      EOS_ACTIONS.POWER_UP,
      {
        payer,
        receiver: reciver,
        days,
        net_frac: net_frac,
        cpu_frac: cpu_frac,
        max_payment: `${totalCost.toFixed(NATIVE_DECIMALS)} ${EOS_SYMBOL}`,
      },
      {
        authorization: [
          {
            actor: payer,
            permission: ACTIVE_PERMISSION,
          },
        ],
      },
    );
...
Dominant language
TypeScript
Stars
9
Forks
7
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from wharfkit/resources

All issues in wharfkit/resources

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.