public override Empty Sell(SellInput input)
        {
            var fromConnector = State.Connectors[input.Symbol];

            Assert(fromConnector != null, "[Sell]Can't find from connector.");
            Assert(fromConnector.IsPurchaseEnabled, "can't purchase");
            var toConnector = State.Connectors[fromConnector.RelatedSymbol];

            Assert(toConnector != null, "[Sell]Can't find to connector.");
            var amountToReceive = BancorHelper.GetReturnFromPaid(
                GetSelfBalance(fromConnector), GetWeight(fromConnector),
                GetSelfBalance(toConnector), GetWeight(toConnector),
                input.Amount
                );

            var fee = Convert.ToInt64(amountToReceive * GetFeeRate());

            if (Context.Sender == Context.GetContractAddressByName(SmartContractConstants.TreasuryContractSystemName))
            {
                fee = 0;
            }

            var amountToReceiveLessFee = amountToReceive.Sub(fee);

            Assert(input.ReceiveLimit == 0 || amountToReceiveLessFee >= input.ReceiveLimit, "Price not good.");

            // Pay fee
            if (fee > 0)
            {
                HandleFee(fee);
            }

            // Transfer base token
            State.TokenContract.Transfer.Send(
                new TransferInput
            {
                Symbol = State.BaseTokenSymbol.Value,
                To     = Context.Sender,
                Amount = amountToReceive
            });
            State.DepositBalance[toConnector.Symbol] =
                State.DepositBalance[toConnector.Symbol].Sub(amountToReceive);
            // Transfer sold token
            State.TokenContract.TransferFrom.Send(
                new TransferFromInput
            {
                Symbol = input.Symbol,
                From   = Context.Sender,
                To     = Context.Self,
                Amount = input.Amount
            });
            Context.Fire(new TokenSold
            {
                Symbol     = input.Symbol,
                SoldAmount = input.Amount,
                BaseAmount = amountToReceive,
                FeeAmount  = fee
            });
            return(new Empty());
        }
        public void Pow_Test()
        {
            var result1 = BancorHelper.Pow(1.5m, 1);

            result1.ShouldBe(1.5m);

            BancorHelper.Pow(1.5m, 2);
        }
        private long SellOperation(long paidRes)
        {
            var getReturnFromPaid = BancorHelper.GetReturnFromPaid(
                _writeConnector.VirtualBalance, Decimal.Parse(_writeConnector.Weight),
                _elfConnector.VirtualBalance, Decimal.Parse(_elfConnector.Weight),
                paidRes);

            return(getReturnFromPaid);
        }
        private long BuyOperation(long paidElf)
        {
            var getAmountToPayout = BancorHelper.GetAmountToPayFromReturn(
                _elfConnector.VirtualBalance, Decimal.Parse(_elfConnector.Weight),
                _writeConnector.VirtualBalance, Decimal.Parse(_writeConnector.Weight),
                paidElf);

            return(getAmountToPayout);
        }
Пример #5
0
        public override Empty Buy(BuyInput input)
        {
            Assert(IsValidSymbol(input.Symbol), "Invalid symbol.");
            var fromConnector = State.Connectors[State.BaseTokenSymbol.Value];
            var toConnector   = State.Connectors[input.Symbol];

            Assert(toConnector != null, "Can't find connector.");
            var amountToPay = BancorHelper.GetAmountToPayFromReturn(
                GetSelfBalance(fromConnector), GetWeight(fromConnector),
                GetSelfBalance(toConnector), GetWeight(toConnector),
                input.Amount);
            var fee = Convert.ToInt64(amountToPay * GetFeeRate());

            var amountToPayPlusFee = amountToPay.Add(fee);

            Assert(input.PayLimit == 0 || amountToPayPlusFee <= input.PayLimit, "Price not good.");

            // Pay fee
            if (fee > 0)
            {
                State.TokenContract.TransferFrom.Send(
                    new TransferFromInput()
                {
                    Symbol = State.BaseTokenSymbol.Value,
                    From   = Context.Sender,
                    To     = State.FeeReceiverAddress.Value,
                    Amount = fee
                });
            }

            // Transfer base token
            State.TokenContract.TransferFrom.Send(
                new TransferFromInput()
            {
                Symbol = State.BaseTokenSymbol.Value,
                From   = Context.Sender,
                To     = Context.Self,
                Amount = amountToPay
            });

            // Transfer bought token
            State.TokenContract.Transfer.Send(
                new TransferInput
            {
                Symbol = input.Symbol,
                To     = Context.Sender,
                Amount = input.Amount
            });
            Context.Fire(new TokenBought
            {
                Symbol       = input.Symbol,
                BoughtAmount = input.Amount,
                BaseAmount   = amountToPay,
                FeeAmount    = fee
            });
            return(new Empty());
        }
Пример #6
0
        public override Empty Buy(BuyInput input)
        {
            Assert(IsValidSymbol(input.Symbol), "Invalid symbol.");
            var toConnector = State.Connectors[input.Symbol];

            Assert(toConnector != null, "[Buy]Can't find to connector.");
            Assert(toConnector.IsPurchaseEnabled, "can't purchase");
            Assert(!string.IsNullOrEmpty(toConnector.RelatedSymbol), "can't find related symbol'");
            var fromConnector = State.Connectors[toConnector.RelatedSymbol];

            Assert(fromConnector != null, "[Buy]Can't find from connector.");
            var amountToPay = BancorHelper.GetAmountToPayFromReturn(
                GetSelfBalance(fromConnector), GetWeight(fromConnector),
                GetSelfBalance(toConnector), GetWeight(toConnector),
                input.Amount);
            var fee = Convert.ToInt64(amountToPay * GetFeeRate());

            var amountToPayPlusFee = amountToPay.Add(fee);

            Assert(input.PayLimit == 0 || amountToPayPlusFee <= input.PayLimit, "Price not good.");

            // Pay fee
            if (fee > 0)
            {
                HandleFee(fee);
            }

            // Transfer base token
            State.TokenContract.TransferFrom.Send(
                new TransferFromInput()
            {
                Symbol = State.BaseTokenSymbol.Value,
                From   = Context.Sender,
                To     = Context.Self,
                Amount = amountToPay,
            });
            State.DepositBalance[fromConnector.Symbol] = State.DepositBalance[fromConnector.Symbol].Add(amountToPay);
            // Transfer bought token
            State.TokenContract.Transfer.Send(
                new TransferInput
            {
                Symbol = input.Symbol,
                To     = Context.Sender,
                Amount = input.Amount
            });

            Context.Fire(new TokenBought
            {
                Symbol       = input.Symbol,
                BoughtAmount = input.Amount,
                BaseAmount   = amountToPay,
                FeeAmount    = fee
            });
            return(new Empty());
        }
 public void GetAmountToPay_GetReturnFromPaid_Failed()
 {
     //fromConnectorBalance <= 0
     Should.Throw <InvalidValueException>(() => BancorHelper.GetAmountToPayFromReturn(0, 1000, 1000, 1000, 1000));
     //paidAmount <= 0
     Should.Throw <InvalidValueException>(() => BancorHelper.GetAmountToPayFromReturn(1000, 1000, 1000, 1000, 0));
     //toConnectorBalance <= 0
     Should.Throw <InvalidValueException>(() => BancorHelper.GetReturnFromPaid(1000, 1000, 0, 1000, 1000));
     //amountToReceive <= 0
     Should.Throw <InvalidValueException>(() => BancorHelper.GetReturnFromPaid(1000, 1000, 1000, 1000, 0));
 }
Пример #8
0
        public override DepositInfo GetNeededDeposit(ToBeConnectedTokenInfo input)
        {
            Assert(IsValidSymbol(input.TokenSymbol), "Invalid symbol.");

            var toConnector = State.Connectors[input.TokenSymbol];

            Assert(toConnector != null, "[GetNeededDeposit]Can't find to connector.");
            Assert(!string.IsNullOrEmpty(toConnector.RelatedSymbol), "can't find related symbol'");
            var fromConnector = State.Connectors[toConnector.RelatedSymbol];

            Assert(fromConnector != null, "[GetNeededDeposit]Can't find from connector.");
            var tokenInfo = State.TokenContract.GetTokenInfo.Call(
                new GetTokenInfoInput
            {
                Symbol = input.TokenSymbol,
            });
            var balance = State.TokenContract.GetBalance.Call(
                new GetBalanceInput
            {
                Owner  = Context.Self,
                Symbol = input.TokenSymbol
            }).Balance;
            var  amountOutOfTokenConvert = tokenInfo.TotalSupply - balance - input.AmountToTokenConvert;
            long needDeposit             = 0;

            if (amountOutOfTokenConvert > 0)
            {
                var fb = fromConnector.VirtualBalance;

                var tb = toConnector.IsVirtualBalanceEnabled
                    ? toConnector.VirtualBalance.Add(tokenInfo.TotalSupply)
                    : tokenInfo.TotalSupply;
                needDeposit =
                    BancorHelper.GetAmountToPayFromReturn(fb, GetWeight(fromConnector),
                                                          tb, GetWeight(toConnector), amountOutOfTokenConvert);
            }

            return(new DepositInfo
            {
                NeedAmount = needDeposit,
                AmountOutOfTokenConvert = amountOutOfTokenConvert
            });
        }
Пример #9
0
        public override Empty Sell(SellInput input)
        {
            Assert(IsValidSymbol(input.Symbol), "Invalid symbol.");
            var fromConnector = State.Connectors[input.Symbol];

            Assert(fromConnector != null, "Can't find connector.");
            var toConnector     = State.Connectors[State.BaseTokenSymbol.Value];
            var amountToReceive = BancorHelper.GetReturnFromPaid(
                GetSelfBalance(fromConnector), GetWeight(fromConnector),
                GetSelfBalance(toConnector), GetWeight(toConnector),
                input.Amount
                );

            var fee = Convert.ToInt64(amountToReceive * GetFeeRate());

            if (Context.Sender == Context.GetContractAddressByName(SmartContractConstants.TreasuryContractSystemName))
            {
                fee = 0;
            }

            var amountToReceiveLessFee = amountToReceive.Sub(fee);

            Assert(input.ReceiveLimit == 0 || amountToReceiveLessFee >= input.ReceiveLimit, "Price not good.");

            // Pay fee
            if (fee > 0)
            {
                State.TokenContract.Transfer.Send(
                    new TransferInput()
                {
                    Symbol = State.BaseTokenSymbol.Value,
                    To     = State.FeeReceiverAddress.Value,
                    Amount = fee
                });
            }

            // Transfer base token
            State.TokenContract.Transfer.Send(
                new TransferInput()
            {
                Symbol = State.BaseTokenSymbol.Value,
                To     = Context.Sender,
                Amount = amountToReceiveLessFee
            });

            // Transfer sold token
            State.TokenContract.TransferFrom.Send(
                new TransferFromInput()
            {
                Symbol = input.Symbol,
                From   = Context.Sender,
                To     = Context.Self,
                Amount = input.Amount
            });
            Context.Fire(new TokenSold()
            {
                Symbol     = input.Symbol,
                SoldAmount = input.Amount,
                BaseAmount = amountToReceive,
                FeeAmount  = fee
            });
            return(new Empty());
        }