Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Orderbookv1.1 #35

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
30 changes: 30 additions & 0 deletions Swap/flamingo-contract-swap/FlamingoSwapOrderBook/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Numerics;
using Neo;
using Neo.SmartContract.Framework;
using Neo.SmartContract.Framework.Attributes;

namespace FlamingoSwapOrderBook
{
public static class Extensions
{
/// <summary>
/// uint160 转为正整数,用于合约地址排序,其它场景勿用
/// </summary>
/// <param name="val">合约地址</param>
/// <returns></returns>
[OpCode(OpCode.PUSHDATA1, "0100")]
[OpCode(OpCode.CAT)]
[OpCode(OpCode.CONVERT, "21")]
public static extern BigInteger ToUInteger(this UInt160 val);

/// <summary>
/// Is Valid and not Zero address
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public static bool IsAddress(this UInt160 address)
{
return address.IsValid && !address.IsZero;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Neo3.Compiler.CSharp.Dev" Version="3.1.0" />
<PackageReference Include="Neo.SmartContract.Framework" Version="3.1.0" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="$(neon3) $(ProjectDir)" />
</Target>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using Neo;
using Neo.SmartContract;
using Neo.SmartContract.Framework;
using Neo.SmartContract.Framework.Attributes;
using Neo.SmartContract.Framework.Native;
using Neo.SmartContract.Framework.Services;

namespace FlamingoSwapOrderBook
{
public partial class FlamingoSwapOrderBookContract
{
#region Admin

#warning Update the admin address if necessary
[InitialValue("NdrUjmLFCmr6RjM52njho5sFUeeTdKPxG9", ContractParameterType.Hash160)]
static readonly UInt160 superAdmin = default;

[InitialValue("0xc0695bdb8a87a40aff33c73ff6349ccc05fa9f01", ContractParameterType.Hash160)]
static readonly UInt160 Factory = default;

[InitialValue("0x48c40d4666f93408be1bef038b6722404d9a4c2a", ContractParameterType.Hash160)]
static readonly UInt160 bNEO = default;

private const string AdminKey = nameof(superAdmin);
private const string GASAdminKey = nameof(GASAdminKey);
private const string FundAddresskey = nameof(FundAddresskey);

private static readonly byte[] OrderIDKey = new byte[] { 0x00 };
private static readonly byte[] BookMapPrefix = new byte[] { 0x01 };
private static readonly byte[] OrderMapPrefix = new byte[] { 0x02 };
private static readonly byte[] ReceiptMapPrefix = new byte[] { 0x03 };
private static readonly byte[] PauseMapPrefix = new byte[] { 0x04 };
private static readonly byte[] FeeMapPrefix = new byte[] { 0x05 };

// When this contract address is included in the transaction signature,
// this method will be triggered as a VerificationTrigger to verify that the signature is correct.
// For example, this method needs to be called when withdrawing token from the contract.
[Safe]
public static bool Verify() => Runtime.CheckWitness(GetAdmin());

[Safe]
public static UInt160 GetAdmin()
{
var admin = Storage.Get(Storage.CurrentReadOnlyContext, AdminKey);
return admin?.Length == 20 ? (UInt160)admin : superAdmin;
}

public static bool SetAdmin(UInt160 admin)
{
Assert(Verify(), "No Authorization");
Assert(admin.IsAddress(), "Invalid Address");
Storage.Put(Storage.CurrentContext, AdminKey, admin);
return true;
}

public static void ClaimGASFrombNEO(UInt160 receiveAddress)
{
Assert(Runtime.CheckWitness(GetGASAdmin()), "Forbidden");
var me = Runtime.ExecutingScriptHash;
var beforeBalance = GAS.BalanceOf(me);
Assert((bool)Contract.Call(bNEO, "transfer", CallFlags.All, Runtime.ExecutingScriptHash, bNEO, 0, null), "claim fail");
var afterBalance = GAS.BalanceOf(me);

GAS.Transfer(me, receiveAddress, afterBalance - beforeBalance);
}

[Safe]
public static UInt160 GetGASAdmin()
{
var address = Storage.Get(Storage.CurrentReadOnlyContext, GASAdminKey);
return address?.Length == 20 ? (UInt160)address : null;
}

public static bool SetGASAdmin(UInt160 GASAdmin)
{
Assert(GASAdmin.IsAddress(), "Invalid Address");
Assert(Verify(), "No Authorization");
Storage.Put(Storage.CurrentContext, GASAdminKey, GASAdmin);
return true;
}
#endregion

#region FundFee

[Safe]
public static UInt160 GetFundAddress()
{
var address = Storage.Get(Storage.CurrentReadOnlyContext, FundAddresskey);
return address?.Length == 20 ? (UInt160)address : null;
}

public static bool SetFundAddress(UInt160 address)
{
Assert(address.IsAddress(), "Invalid Address");
Assert(Verify(), "No Authorization");
Storage.Put(Storage.CurrentContext, FundAddresskey, address);
return true;
}
#endregion

#region Upgrade

public static void Update(ByteString nefFile, string manifest)
{
Assert(Verify(), "No Authorization");
ContractManagement.Update(nefFile, manifest, null);
}
#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.ComponentModel;
using System.Numerics;
using Neo;
using Neo.SmartContract.Framework;

namespace FlamingoSwapOrderBook
{
public partial class FlamingoSwapOrderBookContract
{
/// <summary>
/// When orderbook status changed
/// </summary>
[DisplayName("BookStatusChanged")]
public static event BookStatusChangedEvent onBookStatusChanged;
public delegate void BookStatusChangedEvent(UInt160 baseToken, UInt160 quoteToken, BigInteger quoteScale, BigInteger minOrderAmount, BigInteger maxOrderAmount, bool isPaused);

/// <summary>
/// When order status changed
/// </summary>
[DisplayName("OrderStatusChanged")]
public static event OrderStatusChangedEvent onOrderStatusChanged;
public delegate void OrderStatusChangedEvent(UInt160 baseToken, UInt160 quoteToken, ByteString id, bool isBuy, UInt160 maker, BigInteger price, BigInteger leftAmount);
}
}
Loading