Skip to content

Commit

Permalink
Merge pull request #4 from michielpost/feature/action-page
Browse files Browse the repository at this point in the history
Feature/action page
  • Loading branch information
michielpost authored May 7, 2024
2 parents 94f9b95 + 590db49 commit 8c7cd9a
Show file tree
Hide file tree
Showing 47 changed files with 2,263 additions and 859 deletions.
17 changes: 17 additions & 0 deletions src/aoWebWallet/Extensions/WalletExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using aoWebWallet.Models;

namespace aoWebWallet.Extensions
{
public static class WalletExtensions
{
public static string ToAutocompleteDisplay(this Wallet wallet)
{
if (string.IsNullOrWhiteSpace(wallet.Name))
{
return wallet.Address;
}

return $"{wallet.Name} ({wallet.Address})";
}
}
}
178 changes: 178 additions & 0 deletions src/aoWebWallet/Models/ActionParam.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@

using System.Text;

namespace aoWebWallet.Models
{
public class AoAction
{
public List<ActionParam> Params { get; set; } = new();

public ActionParam? Target => Params.Where(x => x.ParamType == ActionParamType.Target).FirstOrDefault();
public IEnumerable<ActionParam> AllWithoutTarget => Params.Where(x => x.ParamType != ActionParamType.Target);
public IEnumerable<ActionParam> Filled => Params.Where(x => x.ParamType == ActionParamType.Filled);
public IEnumerable<ActionParam> AllInputs => Params.Where(x =>
x.ParamType != ActionParamType.Filled
&& x.ParamType != ActionParamType.Target);

public string? IsValid()
{
if (Target == null)
return "No Target process specified.";

foreach(var input in AllInputs)
{
if (string.IsNullOrEmpty(input.Value))
return $"Please enter a value for {input.Key}";
}

return null;
}

public List<ArweaveBlazor.Models.Tag> ToEvalTags()
{
return Params.Select(x => new ArweaveBlazor.Models.Tag { Name = x.Key, Value = x.Value ?? string.Empty }).ToList();
}

public List<ArweaveBlazor.Models.Tag> ToTags()
{
return AllWithoutTarget.Select(x => new ArweaveBlazor.Models.Tag { Name = x.Key, Value = x.Value ?? string.Empty }).ToList();
}

public List<ArweaveAO.Models.Tag> ToDryRunTags()
{
return AllWithoutTarget.Select(x => new ArweaveAO.Models.Tag { Name = x.Key, Value = x.Value ?? string.Empty }).ToList();
}


public string ToQueryString()
{
if (Target == null)
return string.Empty;

StringBuilder sb = new StringBuilder();

sb.Append($"{Target.Key}={Target.Value}&");

foreach (var param in this.Filled)
{
sb.Append($"{param.Key}={param.Value}&");
}

foreach (var param in this.AllInputs)
{
var args = string.Join(';', param.Args);
if (args.Length > 0)
{
sb.Append($"X-{param.ParamType}={param.Key};{args}&");
}
else
{
sb.Append($"X-{param.ParamType}={param.Key}&");
}
}

return sb.ToString().TrimEnd('&');
}

public static AoAction CreateFromQueryString(string qstring)
{
// Parsing query string
var queryStringValues = System.Web.HttpUtility.ParseQueryString(qstring);

AoAction action = new AoAction();

foreach (var key in queryStringValues.AllKeys)
{
if (key == null)
continue;

var values = queryStringValues.GetValues(key);
if (values == null || !values.Any())
continue;

foreach (var val in values)
{
string actionKey = key;
string? actionValue = val.ToString();
ActionParamType actionParamType = ActionParamType.Filled;

var actionValueSplit = actionValue.Split(';', StringSplitOptions.RemoveEmptyEntries);
actionValue = actionValueSplit.First();
List<string> args = actionValueSplit.Skip(1).ToList();

if (key.Equals("Target", StringComparison.InvariantCultureIgnoreCase))
actionParamType = ActionParamType.Target;
if (key.Equals("X-Quantity", StringComparison.InvariantCultureIgnoreCase))
actionParamType = ActionParamType.Quantity;
if (key.Equals("X-Balance", StringComparison.InvariantCultureIgnoreCase))
actionParamType = ActionParamType.Balance;
else if (key.Equals("X-Process", StringComparison.InvariantCultureIgnoreCase))
actionParamType = ActionParamType.Process;
else if (key.Equals("X-Integer", StringComparison.InvariantCultureIgnoreCase))
actionParamType = ActionParamType.Integer;
else if (key.Equals("X-Input", StringComparison.InvariantCultureIgnoreCase))
actionParamType = ActionParamType.Input;

if (actionParamType != ActionParamType.Filled
&& actionParamType != ActionParamType.Target)
{
actionKey = actionValue;
actionValue = null;
}

action.Params.Add(new ActionParam
{
Key = actionKey,
Value = actionValue,
Args = args,
ParamType = actionParamType
});

}
}

return action;
}

public static AoAction CreateForTokenTransaction(string tokenId)
{
return new AoAction
{
Params = new List<ActionParam>
{
new ActionParam { Key= "Target", ParamType = ActionParamType.Target, Value= tokenId },
new ActionParam { Key= "Action", ParamType = ActionParamType.Filled, Value= "Transfer" },
new ActionParam { Key= "Recipient", ParamType = ActionParamType.Process },
new ActionParam { Key= "Quantity", ParamType = ActionParamType.Balance, Args = new List<string> { tokenId } }
}

};
}
}

public class ActionParam
{
public required string Key { get; set; }
public string? Value { get; set; }

/// <summary>
/// Arguments (like TokenId)
/// </summary>
public List<string> Args { get; set; } = new();

public ActionParamType ParamType { get; set; }

}

public enum ActionParamType
{
None = 0,
Target,
Filled,
Input,
Integer,
Process,
Balance, //Arg1: TokenId //Must have balance
Quantity, //Arg1: TokenId //Does not care about balance
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace aoWebWallet.ViewModels
namespace aoWebWallet.Models
{
public class Transaction
{
Expand Down
Loading

0 comments on commit 8c7cd9a

Please sign in to comment.