Example #1
0
        // NOTE no locks happen here because this callback is called from within a lock
        internal override Hash SettleSwap(Hash sourceHash, Address destination, IToken token, Numerics.BigInteger amount)
        {
            Hash   txHash = Hash.Null;
            string txStr  = null;

            var inProgressMap = new StorageMap(TokenSwapper.InProgressTag, Swapper.Storage);
            var rpcMap        = new StorageMap(TokenSwapper.UsedRpcTag, Swapper.Storage);

            if (inProgressMap.ContainsKey <Hash>(sourceHash))
            {
                txStr = inProgressMap.Get <Hash, string>(sourceHash);

                if (!string.IsNullOrEmpty(txStr))
                {
                    return(VerifyNeoTx(sourceHash, txStr));
                }
            }

            var total = Numerics.UnitConversion.ToDecimal(amount, token.Decimals);

            var neoKeys = NeoKeys.FromWIF(this.WIF);

            var destAddress = NeoWallet.DecodeAddress(destination);

            Logger.Debug($"NEOSWAP: Trying transfer of {total} {token.Symbol} from {neoKeys.Address} to {destAddress}");

            var nonce = sourceHash.ToByteArray();

            Neo.Core.Transaction tx = null;
            string usedRpc          = null;

            try
            {
                if (token.Symbol == "NEO" || token.Symbol == "GAS")
                {
                    tx = neoAPI.SendAsset(neoKeys, destAddress, token.Symbol, total, out usedRpc);
                }
                else
                {
                    var nep5 = neoAPI.GetToken(token.Symbol);
                    tx = nep5.Transfer(neoKeys, destAddress, total, nonce, x => usedRpc = x);
                }

                // persist resulting tx hash as in progress
                inProgressMap.Set <Hash, string>(sourceHash, tx.Hash.ToString());
                rpcMap.Set <Hash, string>(sourceHash, usedRpc);

                Logger.Debug("broadcasted neo tx: " + tx);
            }
            catch (Exception e)
            {
                Logger.Error("Error during transfering {token.Symbol}: " + e);
                return(Hash.Null);
            }

            if (tx == null)
            {
                Logger.Error($"NeoAPI error {neoAPI.LastError} or possible failed neo swap sourceHash: {sourceHash} no transfer happend.");
                return(Hash.Null);
            }

            var strHash = tx.Hash.ToString();

            return(VerifyNeoTx(sourceHash, strHash));
        }