Skip to content

Commit 3c19e3d

Browse files
committed
Update ExchangeContract.cs
[Added] Take Offer functionality (Implemented) [Added] CancelOTCOrder for canceling an OTC order. [Added] CreateOTC added verification and removing token from user to the contract.
1 parent bd2bc6a commit 3c19e3d

File tree

1 file changed

+50
-3
lines changed

1 file changed

+50
-3
lines changed

Phantasma.Blockchain/Contracts/ExchangeContract.cs

+50-3
Original file line numberDiff line numberDiff line change
@@ -388,9 +388,9 @@ public void OpenLimitOrder(Address from, Address provider, string baseSymbol, st
388388
OpenOrder(from, provider, baseSymbol, quoteSymbol, side, IoC ? ImmediateOrCancel : Limit, orderSize, price);
389389
}
390390

391-
public void OpenOTCOrder(Address from, Address provider, string baseSymbol, string quoteSymbol, BigInteger ammount, BigInteger price)
391+
public void OpenOTCOrder(Address from, string baseSymbol, string quoteSymbol, BigInteger ammount, BigInteger price)
392392
{
393-
OpenOrder(from, provider, baseSymbol, quoteSymbol, ExchangeOrderSide.Sell, ExchangeOrderType.OTC, ammount, price);
393+
OpenOrder(from, Address.Null, baseSymbol, quoteSymbol, ExchangeOrderSide.Sell, ExchangeOrderType.OTC, ammount, price);
394394
}
395395

396396
public void CancelOrder(BigInteger uid)
@@ -531,6 +531,22 @@ private void CreateOTC(Address from, string baseSymbol, string quoteSymbol, BigI
531531
{
532532
var uid = Runtime.GenerateUID();
533533

534+
var count = _otcBook.Count();
535+
ExchangeOrder lockUpOrder;
536+
for (int i = 0; i < count; i++)
537+
{
538+
lockUpOrder = _otcBook.Get<ExchangeOrder>(i);
539+
if(lockUpOrder.Creator == from)
540+
{
541+
throw new Exception("Already have an offer created");
542+
return;
543+
}
544+
}
545+
546+
var baseBalance = Runtime.GetBalance(baseSymbol, from);
547+
Runtime.Expect(baseBalance >= amount, "invalid seller amount");
548+
Runtime.TransferTokens(baseSymbol, from, this.Address, price);
549+
534550
var order = new ExchangeOrder(uid, Runtime.Time, from, this.Address, amount, baseSymbol, price, quoteSymbol, ExchangeOrderSide.Sell, ExchangeOrderType.OTC);
535551
_otcBook.Add<ExchangeOrder>(order);
536552
}
@@ -545,15 +561,46 @@ public void TakeOrder(Address from, BigInteger uid)
545561
var order = _otcBook.Get<ExchangeOrder>(i);
546562
if (order.Uid == uid)
547563
{
548-
throw new NotImplementedException();
564+
var baseBalance = Runtime.GetBalance(order.BaseSymbol, this.Address);
565+
Runtime.Expect(baseBalance >= order.Price, "invalid seller amount");
566+
567+
var quoteBalance = Runtime.GetBalance(order.QuoteSymbol, from);
568+
Runtime.Expect(quoteBalance >= order.Amount, "invalid buyer amount");
549569

570+
Runtime.TransferTokens(order.BaseSymbol, this.Address, from, order.Price);
571+
Runtime.TransferTokens(order.QuoteSymbol, from, order.Creator, order.Amount);
572+
_otcBook.RemoveAt(i);
550573
return;
551574
}
552575
}
553576

554577
Runtime.Expect(false, "order not found");
555578
}
556579

580+
public void CancelOTCOrder(Address from, BigInteger uid)
581+
{
582+
var count = _otcBook.Count();
583+
ExchangeOrder order;
584+
for (int i = 0; i < count; i++)
585+
{
586+
order = _otcBook.Get<ExchangeOrder>(i);
587+
if (order.Uid == uid)
588+
{
589+
Runtime.Expect(Runtime.IsWitness(from), "invalid witness");
590+
Runtime.Expect(Runtime.IsWitness(order.Creator), "invalid witness");
591+
Runtime.Expect(from == order.Creator, "invalid owner");
592+
_otcBook.RemoveAt(i);
593+
594+
Runtime.TransferTokens(order.BaseSymbol, this.Address, order.Creator, order.Price);
595+
Runtime.Notify(EventKind.TokenReceive, order.Creator, new TokenEventData(order.BaseSymbol, order.Amount, Runtime.Chain.Name));
596+
return;
597+
}
598+
}
599+
600+
// if it reaches here, it means it not found nothing in previous part
601+
throw new Exception("order not found");
602+
}
603+
557604
/*public void SwapTokens(Address buyer, Address seller, string baseSymbol, string quoteSymbol, BigInteger amount, BigInteger price, byte[] signature)
558605
{
559606
Runtime.Expect(Runtime.IsWitness(buyer), "invalid witness");

0 commit comments

Comments
 (0)