internal CryptoTransferTransactionBody(Address token, Address fromAddress, Address toAddress, long amount) : this()
 {
     TokenTransfers.Add(new TokenTransferList(token, fromAddress, toAddress, amount));
 }
        internal CryptoTransferTransactionBody(TransferParams transfers) : this()
        {
            if (transfers == null)
            {
                throw new ArgumentNullException(nameof(transfers), "The transfer parametes cannot not be null.");
            }
            var missingTransfers = true;

            if (transfers.CryptoTransfers is not null)
            {
                long sum         = 0;
                var  netRequests = new Dictionary <Hashgraph.Address, long>();
                foreach (var transfer in transfers.CryptoTransfers)
                {
                    if (transfer.Value == 0)
                    {
                        throw new ArgumentOutOfRangeException(nameof(transfers.CryptoTransfers), $"The amount to transfer crypto to/from {transfer.Key.ShardNum}.{transfer.Key.RealmNum}.{transfer.Key.AccountNum} must be a value, negative for transfers out, and positive for transfers in. A value of zero is not allowed.");
                    }
                    if (netRequests.TryGetValue(transfer.Key, out long value))
                    {
                        netRequests[transfer.Key] = value + transfer.Value;
                    }
                    else
                    {
                        netRequests[transfer.Key] = transfer.Value;
                    }
                    sum += transfer.Value;
                }
                if (netRequests.Count == 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(transfers.CryptoTransfers), "The dictionary of crypto transfers can not be empty.");
                }
                if (sum != 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(transfers.CryptoTransfers), "The sum of crypto sends and receives does not balance.");
                }
                var xferList = new TransferList();
                foreach (var transfer in netRequests)
                {
                    if (transfer.Value != 0)
                    {
                        xferList.AccountAmounts.Add(new AccountAmount(transfer.Key, transfer.Value));
                    }
                }
                missingTransfers = xferList.AccountAmounts.Count == 0;
                Transfers        = xferList;
            }
            if (transfers.TokenTransfers is not null)
            {
                foreach (var tokenGroup in transfers.TokenTransfers.GroupBy(txfer => txfer.Token))
                {
                    if (tokenGroup.Key.IsNullOrNone())
                    {
                        throw new ArgumentException("Token", "The list of token transfers cannot contain a null or empty Token value.");
                    }
                    long sum         = 0;
                    var  netRequests = new Dictionary <Address, long>();
                    foreach (var xfer in tokenGroup)
                    {
                        if (xfer.Address.IsNullOrNone())
                        {
                            throw new ArgumentException(nameof(xfer.Address), "The list of token transfers cannot contain a null or empty account value.");
                        }
                        if (xfer.Amount == 0)
                        {
                            throw new ArgumentOutOfRangeException(nameof(xfer.Amount), $"The amount to transfer tokens to/from {xfer.Address.ShardNum}.{xfer.Address.RealmNum}.{xfer.Address.AccountNum} must be a value, negative for transfers out, and positive for transfers in. A value of zero is not allowed.");
                        }
                        if (netRequests.TryGetValue(xfer.Address, out long value))
                        {
                            netRequests[xfer.Address] = value + xfer.Amount;
                        }
                        else
                        {
                            netRequests[xfer.Address] = xfer.Amount;
                        }
                        sum += xfer.Amount;
                    }
                    if (sum != 0)
                    {
                        throw new ArgumentOutOfRangeException(nameof(transfers.TokenTransfers), $"The sum of token sends and receives for {tokenGroup.Key.ShardNum}.{tokenGroup.Key.RealmNum}.{tokenGroup.Key.AccountNum} does not balance.");
                    }
                    var xferList = new TokenTransferList
                    {
                        Token = new TokenID(tokenGroup.Key)
                    };
                    foreach (var netTransfer in netRequests)
                    {
                        if (netTransfer.Value != 0)
                        {
                            xferList.Transfers.Add(new AccountAmount(netTransfer.Key, netTransfer.Value));
                        }
                    }
                    if (xferList.Transfers.Count > 0)
                    {
                        TokenTransfers.Add(xferList);
                    }
                }
                missingTransfers &= TokenTransfers.Count == 0;
            }
            if (missingTransfers)
            {
                throw new ArgumentException(nameof(transfers), "Both crypto and token transfer lists are null or empty.  At least one must include net transfers.");
            }
        }
Example #3
0
 internal CryptoTransferTransactionBody(Asset asset, AddressOrAlias fromAddress, AddressOrAlias toAddress) : this()
 {
     TokenTransfers.Add(new TokenTransferList(asset, fromAddress, toAddress));
 }