Skip to content

Commit

Permalink
Update Lottery logic
Browse files Browse the repository at this point in the history
1. Can buy lottery tickets without go to opensea
2. Can Draw after SoldBp
3. Optimize Lottery open fees
  • Loading branch information
xcaptain committed May 23, 2024
1 parent a579048 commit 4532490
Show file tree
Hide file tree
Showing 9 changed files with 299 additions and 36 deletions.
8 changes: 4 additions & 4 deletions Contracts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,10 @@ public static IList<ERC20Contract> GetERC20Contracts(NetworkCore? network)
};

public static readonly Dictionary<NetworkCore, NFTContract> Lottery = new() {
{ NetworkConfig.EthereumSepolia, new NFTContract("Lottery", "0xF1a162C2d43ea2d1f2Af764cC6E564EaC2A7dC01", LotteryDefaultImage) },
{ NetworkConfig.MoonBaseAlpha, new NFTContract("Lottery", "0x5cAE158176D8c30F7038b127Da9FC845B916a99A", LotteryDefaultImage) },
{ NetworkConfig.MantaPacificSepolia, new NFTContract("Lottery", "0x86cF9996b7a8aF46E74A9FBbd28b149a2f7F33e2", LotteryDefaultImage) },
{ NetworkConfig.MantleSepolia, new NFTContract("Lottery", "0xD40D28361Ef1c9FB6E672E0905548CAdD4d70AA1", LotteryDefaultImage) },
{ NetworkConfig.EthereumSepolia, new NFTContract("Lottery", "0xbeB8F9252843AE522D381F735DB54ee73A18Fb51", LotteryDefaultImage) },
{ NetworkConfig.MoonBaseAlpha, new NFTContract("Lottery", "0x720B8eDf9f9507ae0531e31b09793f291932548c", LotteryDefaultImage) },
{ NetworkConfig.MantaPacificSepolia, new NFTContract("Lottery", "0xcA0f639B825F48413b13195B24CF4ba283703c57", LotteryDefaultImage) },
{ NetworkConfig.MantleSepolia, new NFTContract("Lottery", "0x11462F7860dAd01Cc28dCbcaE2A6b3e3E4697d89", LotteryDefaultImage) },
};

public static readonly Dictionary<NetworkCore, NFTContract> RedEnvelope = new() {
Expand Down
12 changes: 12 additions & 0 deletions Localization/SharedResource.en.resx
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,18 @@
<data name="Lottery" xml:space="preserve">
<value>Lottery</value>
</data>
<data name="Lottery_DefaultPrice" xml:space="preserve">
<value>Default Price(Native Coin)</value>
</data>
<data name="Lottery_SoldPercent" xml:space="preserve">
<value>Sold Percent(%)</value>
</data>
<data name="Buy" xml:space="preserve">
<value>Buy</value>
</data>
<data name="Holds" xml:space="preserve">
<value>Holds</value>
</data>
<data name="RedEnvelope" xml:space="preserve">
<value>RedEnvelope</value>
</data>
Expand Down
12 changes: 12 additions & 0 deletions Localization/SharedResource.zh.resx
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,18 @@
<data name="Lottery" xml:space="preserve">
<value>彩票</value>
</data>
<data name="Lottery_DefaultPrice" xml:space="preserve">
<value>默认售价(原生代币)</value>
</data>
<data name="Lottery_SoldPercent" xml:space="preserve">
<value>售出可开奖比例(%)</value>
</data>
<data name="Buy" xml:space="preserve">
<value>购买</value>
</data>
<data name="Holds" xml:space="preserve">
<value>持有</value>
</data>
<data name="RedEnvelope" xml:space="preserve">
<value>红包</value>
</data>
Expand Down
6 changes: 3 additions & 3 deletions NftApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ public static UserOptionNFT FromStr(long chainId, string contractAddress, long t

public record UserLotteryNFT : UserNftBase
{
public required string Status { get; set; }

public DateTimeOffset DrawTime { get; set; }

public decimal BaseAssetAmount { get; set; }

public required string AssetKind { get; set; }

public required string Writer { get; set; }

public long BaseAssetTokenId { get; set; }

public bool Drawable()
Expand All @@ -112,10 +112,10 @@ public static UserLotteryNFT FromStr(long chainId, string contractAddress, long
TokenId = tokenId,
ChainId = chainId,
Contract = contractAddress,
Status = NftMetadataParser.ParseString(metadataUrl, "status"), // open|close
ImageData = NftMetadataParser.ParseImageSvg(metadataUrl),
DrawTime = NftMetadataParser.ParseMaturityDate(metadataUrl, "drawTime"),
AssetKind = assetKind,
Writer = NftMetadataParser.ParseString(metadataUrl, "writer"),
BaseAssetAmount = assetKind == "ERC20" ? NftMetadataParser.ParseAmount(metadataUrl, "baseAssetAmount") : 0,
BaseAssetTokenId = (assetKind == "ERC721" || assetKind == "ERC1155") ? NftMetadataParser.ParseLong(metadataUrl, "baseAssetTokenId") : 0,
};
Expand Down
112 changes: 109 additions & 3 deletions Pages/LotteryDetail.razor
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,76 @@
}

<div class="flex flex-row items-center justify-center gap-2 p-5">
@if (Token.Drawable())
@if (CanDrawFromChain)
{
<button type="button" class="inline-flex items-center px-3 py-2 text-sm font-medium text-center text-white bg-blue-700 rounded-lg hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800 @DrawButtonDisabledClass"
@onclick="async () => await HandleDraw()"
disabled=@DrawButtonDisabled>
@Loc["Draw_Stmt"]
</button>
}
@if (_metamaskHostProvider.SelectedAccount != Token?.Writer && WriterBalance > 0)
{
<button type="button" class="inline-flex items-center px-3 py-2 text-sm font-medium text-center text-white bg-blue-700 rounded-lg hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800 @DrawButtonDisabledClass"
popovertarget="buy-modal">
@Loc["Buy"] (@Loc["Holds"]: @Balance)
</button>

<div popover id="buy-modal" calss="bg-white dark:bg-gray-700">
<!-- Modal content -->
<div class="rounded-lg bg-white dark:bg-gray-700">
<!-- Modal header -->
<div class="flex items-center justify-between p-4 md:p-5 border-b rounded-t dark:border-gray-600">
<h3 class="text-xl font-semibold text-gray-900 dark:text-white">
@Loc["Buy"]
</h3>
<button type="button" class="end-2.5 text-gray-400 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm w-8 h-8 ms-auto inline-flex justify-center items-center dark:hover:bg-gray-600 dark:hover:text-white" data-modal-hide="authentication-modal"
popovertarget="buy-modal" popovertargetaction="hide">
<svg class="w-3 h-3" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 14 14">
<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m1 1 6 6m0 0 6 6M7 7l6-6M7 7l-6 6" />
</svg>
<span class="sr-only">Close</span>
</button>
</div>
<!-- Modal body -->
<div class="p-4 md:p-5">
<form class="space-y-4">
<div>
<label for="defaultPrice" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">
@Loc["DefaultPrice"]
</label>
<input id="defaultPrice" type="number"
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-600 dark:border-gray-500 dark:placeholder-gray-400 dark:text-white"
@bind-value="DefaultPrice"
disabled=true />
</div>

<div>
<label for="amount" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">
@Loc["Amount"]
</label>
<input id="amount" type="number"
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-600 dark:border-gray-500 dark:placeholder-gray-400 dark:text-white"
@bind-value="BuyAmount"/>
</div>

<button type="button" class="@BuyButtonClass w-full text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800"
disabled=@InProgress
@onclick="HandleBuy">
@if (InProgress)
{
<svg aria-hidden="true" class="inline w-4 h-4 text-gray-200 animate-spin dark:text-gray-600 fill-gray-600 dark:fill-gray-300" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor"/>
<path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentFill"/>
</svg>
}
@Loc["Buy"]
</button>
</form>
</div>
</div>
</div>
}
</div>
}
</div>
Expand Down Expand Up @@ -91,6 +153,8 @@

public int Balance { get; set;}

public int WriterBalance { get; set; } = 0;

public bool IsUserOwner => Balance > 0;

protected override void OnInitialized()
Expand Down Expand Up @@ -119,13 +183,21 @@
await InvokeAsync(() => this.StateHasChanged());
}

private bool CanDrawFromChain = false;

private async Task initTokens()
{
if (Token is null && _metamaskHostProvider.SelectedNetworkChainId > 0)
{
string metadataUri = await _web3Service.GetUri(ContractAddress, TokenId);
Token = UserLotteryNFT.FromStr(_metamaskHostProvider.SelectedNetworkChainId, ContractAddress, TokenId, metadataUri);
Balance = await _web3Service.Erc1155BalanceOf(_metamaskHostProvider.SelectedAccount, ContractAddress, TokenId);
CanDrawFromChain = await _web3Service.CanDrawLottery(TokenId, ContractAddress);
WriterBalance = await _web3Service.Erc1155BalanceOf(Token.Writer, ContractAddress, TokenId);
// 取得 Token 的价格
var metadata = await _web3Service.GetLotteryTokenMetadata(TokenId, ContractAddress);
DefaultPrice = Web3.Convert.FromWei(metadata.DefaultPrice, 18); // Hardcode to 18 decimals
Logger.LogInformation("init tokens, defaultPrice: {}, writerBalance: {}", DefaultPrice, WriterBalance);
}
}

Expand Down Expand Up @@ -153,8 +225,8 @@
DrawButtonDisabled = true;
try
{
var txHash = await _web3Service.DrawLottery(Token!.TokenId, Token!.Contract);
toastService.ShowToast($"Token: {Token.TokenId} {Loc["MintSuccess_Stmt"]}", ToastLevel.Success);
var winner = await _web3Service.DrawLottery(Token!.TokenId, Token!.Contract);
toastService.ShowToast($"Winner: {winner}", ToastLevel.Success);
}
catch (Nethereum.JsonRpc.Client.RpcResponseException e)
{
Expand All @@ -168,4 +240,38 @@
DrawButtonDisabled = false;
}
}

private bool InProgress = false;

private string BuyButtonClass => InProgress ? "cursor-not-allowed" : "";

private int BuyAmount = 1;

private decimal DefaultPrice = 0;

private async Task HandleBuy()
{
if (BuyAmount > WriterBalance)
{
toastService.ShowToast($"At most {WriterBalance}", ToastLevel.Error);
return;
}
InProgress = true;
try
{
var txHash = await _web3Service.BuyLottery(Token!.TokenId, BuyAmount, DefaultPrice, Token!.Contract);
toastService.ShowToast($"Token: {Token.TokenId} {Loc["MintSuccess_Stmt"]}", ToastLevel.Success);
}
catch (Nethereum.JsonRpc.Client.RpcResponseException e)
{
if (e.Message.Contains("User denied transaction signature"))
{
toastService.ShowToast(Loc["TxCancelled_Stmt"], ToastLevel.Error);
}
}
finally
{
InProgress = false;
}
}
}
Loading

0 comments on commit 4532490

Please sign in to comment.