Giter Site home page Giter Site logo

Comments (4)

6r1d avatar 6r1d commented on May 27, 2024

@a1salimbene, please add:

  • the commit on the stable branch (git rev-parse --short HEAD output)
  • example code without the sugar class, as it adds an unpredictable factor we can not test yet

from iroha-javascript.

a1salimbene avatar a1salimbene commented on May 27, 2024

@6r1d information request below:

> git rev-parse --short HEAD
7acb8a44
> git branch
  iroha2-dev
  iroha2-lts
* iroha2-stable

Here's the code without using the sugar class. I'm getting exact same err:

const transfer = async (from, to, asset, quantity) => {
  logger.info(`[srv] transfer ${from} to ${to} ${quantity} of ${asset} ...`);

  const [senderAccName, senderDomainId] = from.split('@');
  const [receiverAccName, receiverDomainId] = to.split('@');
  const [assetName, assetDomainId] = asset.split('#');

  const {
    AccountId,
    AssetDefinitionId,
    AssetId,
    DomainId,
    InstructionExpr,
    Expression,
    Executable,
    IdBox,
    NumericValue,
    TransferExpr,
    Value,
    VecInstructionExpr,
  } = datamodel;

  const senderDomain = DomainId({
    name: senderDomainId,
  });
  const receiverDomain = DomainId({
    name: receiverDomainId,
  });
  const assetDomain = DomainId({
    name: assetDomainId,
  });

  const assetDefinitionId = AssetDefinitionId({
    name: assetName,
    domain_id: assetDomain,
  });

  const fromAccount = AccountId({
    name: senderAccName,
    domain_id: senderDomain,
  });

  const toAccount = AccountId({
    name: receiverAccName,
    domain_id: receiverDomain,
  });

  const amountToTransfer = Value('Numeric', NumericValue('U32', quantity));

  const evaluatesToAssetId = (assetId) =>
    Expression('Raw', Value('Id', IdBox('AssetId', assetId)));

  const transferAssetInstruction = TransferExpr({
    source_id: evaluatesToAssetId(
      AssetId({
        definition_id: assetDefinitionId,
        account_id: fromAccount,
      })
    ),
    destination_id: evaluatesToAssetId(
      AssetId({
        definition_id: assetDefinitionId,
        account_id: toAccount,
      })
    ),
    object: Expression('Raw', amountToTransfer),
  });

  const instr = InstructionExpr('Transfer', transferAssetInstruction);
  const exec = Executable('Instructions', VecInstructionExpr([instr]));

  const { pre, client } = _clientFactory('matias', 'cognition');
  try {
    const data = await client.submitExecutable(pre, exec);
    logger.info('[srv] transfer completed ...');
    return { data };
  } catch (error) {
    console.log(error);
    return { error: error.message };
  }
};

from iroha-javascript.

a1salimbene avatar a1salimbene commented on May 27, 2024

I found the problem looking at a rust test, see below:

fn transfer_isi_should_be_valid() {
    let _instruction = TransferExpr::new(
        IdBox::AssetId("btc##seller@crypto".parse().expect("Valid")),
        12_u32,
        IdBox::AccountId("buyer@crypto".parse().expect("Valid")),
    );
}

The third param is an AccountId (destination target), but the first param is an AssetId (which already includes the source AccountId.

Thus, this works:

  const toAccount = sugar.accountId(receiverAccName, receiverDomainId);
  const transferIsi = sugar.instruction.transfer(
    datamodel.IdBox('AssetId', sugar.assetId(fromAccount, assetDefinitionId)),
    amountToTransfer,
    datamodel.IdBox('AccountId', toAccount)
  );

but this doesn't

  const transferAsset = sugar.instruction.transfer(
    datamodel.IdBox('AssetId', sugar.assetId(senderAccId, assetDefId)),
    amountToTransfer,
    datamodel.IdBox('AssetId', sugar.assetId(receiverAccId, assetDefId))
  );

Is worth mentioning that the test case from the JS code appears to be wrong then. In all js samples I found, source and destination IDs are AssetId. For instance, this is from the JS docs:

const transferAssetInstruction = Instruction(
  'Transfer',
  TransferBox({
    source_id: evaluatesToAssetId(
      AssetId({
        definition_id: assetDefinitionId,
        account_id: fromAccount,
      }),
    ),
    destination_id: evaluatesToAssetId(
      AssetId({
        definition_id: assetDefinitionId,
        account_id: toAccount,
      }),
    ),
    object: EvaluatesToValue({
      expression: Expression('Raw', amountToTransfer),
    }),
  }),
)

However, this follows the logic from the cli that implements "from" → "to" → "asset", instead of "asset" → "to" like Rust:

iroha_client_cli asset transfer --from matias@cognition --to pepe@cognition --asset-id energy#cognition --quantity 5

So it's all a bit confusing but hopefully, it helps. Now I don't think there's a bug or anything like it. I think this is a case of outdated documentation or lack thereof.

from iroha-javascript.

6r1d avatar 6r1d commented on May 27, 2024

Thank you. I'm closing this issue and will raise the topic with our documentation team.

from iroha-javascript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.