-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce BalanceController and move APIs
- Loading branch information
Showing
3 changed files
with
68 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using Lib9c; | ||
using Libplanet.Crypto; | ||
using Libplanet.Types.Assets; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Mimir.Models.Assets; | ||
using Mimir.Services; | ||
using Mimir.Util; | ||
|
||
namespace Mimir.Controllers; | ||
|
||
[ApiController] | ||
[Route("{network}/balances/{address}")] | ||
public class BalanceController : ControllerBase | ||
{ | ||
#pragma warning disable CS0618 // Type or member is obsolete | ||
private readonly Currency _ncg = Currency.Legacy( | ||
"NCG", | ||
2, | ||
new Address("0x47D082a115c63E7b58B1532d20E631538eaFADde")); | ||
#pragma warning restore CS0618 // Type or member is obsolete | ||
|
||
[HttpGet("{currency}")] | ||
public async Task<Balance?> GetBalance( | ||
string network, | ||
string address, | ||
string currency, | ||
IStateService stateService) | ||
{ | ||
Address agentAddress; | ||
try | ||
{ | ||
agentAddress = new Address(address); | ||
} | ||
catch (ArgumentException) | ||
{ | ||
Response.StatusCode = StatusCodes.Status400BadRequest; | ||
return null; | ||
} | ||
|
||
Currency? c = currency switch | ||
{ | ||
"NCG" => _ncg, | ||
"CRYSTAL" => Currencies.Crystal, | ||
_ => null, | ||
}; | ||
if (c is null) | ||
{ | ||
try | ||
{ | ||
c = Currencies.GetMinterlessCurrency(currency); | ||
} | ||
catch (ArgumentNullException) | ||
{ | ||
Response.StatusCode = StatusCodes.Status400BadRequest; | ||
return null; | ||
} | ||
catch (ArgumentException) | ||
{ | ||
Response.StatusCode = StatusCodes.Status400BadRequest; | ||
return null; | ||
} | ||
} | ||
|
||
var stateGetter = new StateGetter(stateService); | ||
var balance = await stateGetter.GetBalanceAsync(agentAddress, c.Value); | ||
return new Balance(c.Value, balance); | ||
} | ||
} |