Skip to content

Commit

Permalink
introduce BalanceController and move APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
boscohyun committed May 21, 2024
1 parent 06223e3 commit 10fe8f6
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 86 deletions.
47 changes: 0 additions & 47 deletions Mimir/Controllers/AgentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,53 +47,6 @@ public class AgentController : ControllerBase
};
}

[HttpGet("balances/{currency}")]
public async Task<Balance?> GetBalances(
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);
}

[HttpGet("avatars")]
public async Task<AvatarsResponse> GetAvatars(
string network,
Expand Down
39 changes: 0 additions & 39 deletions Mimir/Controllers/AvatarController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,45 +136,6 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer
return new Avatar(avatarState);
}

[HttpGet("balances/{currency}")]
public async Task<Balance?> GetBalances(
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;
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);
return new Balance(c, balance);
}

[HttpGet("inventory")]
public async Task<Inventory?> GetInventory(
string network,
Expand Down
68 changes: 68 additions & 0 deletions Mimir/Controllers/BalanceController.cs
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);
}
}

0 comments on commit 10fe8f6

Please sign in to comment.