Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cpu_frac and net_frac calculation #4

Open
dev-dantealighieri opened this issue Jan 22, 2024 · 1 comment
Open

cpu_frac and net_frac calculation #4

dev-dantealighieri opened this issue Jan 22, 2024 · 1 comment

Comments

@dev-dantealighieri
Copy link

dev-dantealighieri commented Jan 22, 2024

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,
          },
        ],
      },
    );
...
@dev-dantealighieri
Copy link
Author

dev-dantealighieri commented Jan 22, 2024

i made a typo, somewhat fixed the problem, but i still want to hear and learn more from you, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant