How to transfer XEM using C#

Hi.
I use https://github.com/NemProject/nem1-sdk-csharp library

Transaction #1 (created in Nano wallet),
to: TBY6F5-WE6QUG-RAFV7K-DUYZFJ-OJAHLP-2UZBDJ-OY6L
hash: d9c82f65028a0bb6112966d4ceee9dbd211c9fce248629aa336bba6c5a193614
There are 2 fields: amount and fee.

Transaction #2 (created using .net library):
from: TBY6F5-WE6QUG-RAFV7K-DUYZFJ-OJAHLP-2UZBDJ-OY6L
to: TBDF4L-NGW7RN-BWRMJ4-VNXCXO-SMJZ2E-F2ESQ5-KHUQ
hash: cb618492947128ae0c344a687724b50fa716ce2986062709141d99947180e40e
There is only 1 field: fee, and instead of amount I see mosaic: nem:xem with correct amount.

see screenshot https://www.screencast.com/t/NyhIN8ANWxi

To create transaction #2 I use code:

var mosaic = new Xem((ulong)(amount * 1000000));
var transaction = TransferTransaction.Create(
        account.Address.NetworkByte,
        Deadline.CreateHours(24),
        (ulong)(fee * 1000000m),
        Address.CreateFromEncoded(destAddress),
        new List<Mosaic> { mosaic }, null)
    .SignWith(account.KeyPair);
var responseTask = new TransactionHttp(Settings.Host).Announce(transaction);

Is there any other method to create TransferTransaction with fee and amount and without mosaic? Have I done something wrong?
Thanks.

Is there any other method to create TransferTransaction with fee and amount and without mosaic? Have I done something wrong?

no. this is by design. you also do not need to include the fee if you dont want. that is optional. if you do not include the fee it will automatically calculate the minimum fee for the transfer.

build the transaction without fee:

TransferTransaction transaction = TransferTransaction.Create(
NetworkType.Types.TEST_NET,
Deadline.CreateHours(2),
Address.CreateFromEncoded(“TAVPDJ-DR3R3X-4FJUKN-PY2IQB-NNRFV2-QR5NJZ-J3WR”),
new List {Mosaic.CreateFromIdentifier(“nem:xem”, 1000000)}
);

OR build transaction with fee:

TransferTransaction transaction = TransferTransaction.Create(
NetworkType.Types.TEST_NET,
Deadline.CreateHours(2),
10000,
Address.CreateFromEncoded(“TAVPDJ-DR3R3X-4FJUKN-PY2IQB-NNRFV2-QR5NJZ-J3WR”),
new List {Mosaic.CreateFromIdentifier(“nem:xem”, 1000000)});

if you want to include a message:

TransferTransaction transaction = TransferTransaction.Create(
NetworkType.Types.TEST_NET,
Deadline.CreateHours(2),
Address.CreateFromEncoded(“TAVPDJ-DR3R3X-4FJUKN-PY2IQB-NNRFV2-QR5NJZ-J3WR”),
new List {Mosaic.CreateFromIdentifier(“nem:xem”, 1000000)},
PlainMessage.Create(“hello”));

Sign transaction:

SignedTransaction signedTransaction = transaction.SignWith(keyPair);

Send the transaction:

TransactionResponse response = await new TransactionHttp(“http://127.0.0.1:7890”).Announce(signedTransaction);

Ok. Thanks.

  1. If I set Fee = 0, then in TransferTransaction constructor, fee will be calculated automatically, see CalculateFee private method, so it looks like I can not set 0 fee.
  2. Default fee for transaction < 10000 xem is 0.05, see https://nemproject.github.io/#transaction-fees
    If I add Mosaic (nem:xem) then CalculateFee return 0.1 (default 0.05 + 1 mosaic * 0.05 = 0.1). thats why I set fee manually.

P.S. I use .net framework 4.6, and visual studio 2013, not standard 2.0, but this is not a problem

integer types are 0 value by default in c#, so even if you dont set the fee, its still 0 and will auto calculate. there is no need to set a fee value of 0. the only time you need a 0 fee is for mijin, and that is accounted for when network type is NetworkType.Types.MIJIN or NetworkType.Types.MIJIN_TEST

the fee is correctly calculated as a minimum 0.05 fee when sending 0.05 xem as per your screen shot: https://www.screencast.com/t/NyhIN8ANWxi so im a little unsure what the issue with autocalculated fee is?

I understand 0 means Auto calculate fee, transaction on my screenshot created without fee autocalc.
I set 0.05 (manually, const value) because of in my case fee calculates incorrectly (should be 0.05, but I see 0.1)

Look at my first message, I asked about transactions. My transactions contains mosaic, but transaction from NanoWallet does not. It looks like there is a difference (structure, fields, or something else). I’m trying to figure out how to send transactions like in NanoWallet.

@kodtycoon your library don’t allow send v1 transactions (where xem is not send as mosaic)?

no. when it reads a transaction, it converts V1 txs to V2 in the sdk as well.

its not that it doesnt support them, you just cant send V1. if its an issue, i can change it but didnt think it would be an issue. the intention is to bridge the gap between nis1 and catapult. catapult only transfers xem as a mosaic as you see in nis1 sdk. so when people get used to nis1-sdk-csharp, they wont see much difference when they switch to nem2-sdk-csharp. sending a transaction actually only requires changing the node, without any code changes bar updating the namespace in .cs files

is there a reason you need to send it like nanowallet? both methods create the same result. if there is some reason why V1 transactions are required i can add it.

seems your right. min fee is 50000 too much. will fix

fixed

I guess it could be a problem. Couple major exchanges don’t support mosaic transfers but only v1. That’s why would be nice allow send XEM as v1. Not sure when Catapult will be on public chain but it looks like still long way.

I’m newbie :slight_smile: I saw difference, so I asked: why?
Thanks for hotfix.

@CryptoBeliever, what exchanges?

For example Cryptopia and Livecoin :slight_smile:

@CryptoBeliever, thanks.

@kodtycoon, let say I want to transfer total balance to another wallet, in this case I have to calc fee before creating transaction, tx amount = current balance - fee. So I would be nice to see CalculateFee as public method.

will look into it but may not get to it for a while.

it would be nicer if exchanges just updated to support mosaics natively :wink: i recently worked with an exchange that had no issue in doing so using this c# library. so its not a case where they cant, they just dont because its easier. if supporting mosaics natively is not only easy but also the only option, more exchanges would just do it without question.

Yes. I know. Is pretty easy with new exchanges but hard to ask old one to change they implementation.
And I also know a lot of trauma caused when someone sent (for example from dimcoin depotwallet) xem as mosaic and after that long time waiting for exchange support answer :slight_smile:

Hello.

Just of curiosity @kodtycoon can you tell me if your API can be used on linux with mono
or is exclusively possible to use it on windows?

Thanks.

Yes it is compatible :slight_smile: .netStandard can be used in .netCore 2.0 applications or .netFramework4.6(.1?)