update ss

This commit is contained in:
Kukks
2023-07-13 09:14:17 +02:00
parent af1bacea6a
commit 0df2fb8907
10 changed files with 262 additions and 81 deletions

View File

@@ -3,9 +3,12 @@
@using Newtonsoft.Json.Linq
@inject BTCPayServer.Security.ContentSecurityPolicies csp
@inject SideShiftService SideShiftService
@model BTCPayServer.Models.InvoicingModels.PaymentModel
@{
var storeId = ((JObject)JObject.Parse(JsonConvert.SerializeObject(Model)))["StoreId"].Value<string>();
var storeId = Model.StoreId;
var settings = await SideShiftService.GetSideShiftForStore(storeId);
var preferredTargetPaymentMethodId = string.IsNullOrEmpty(settings?.PreferredTargetPaymentMethodId) ? null : Model.AvailableCryptos.Any(crypto => crypto.PaymentMethodId == settings.PreferredTargetPaymentMethodId) ? settings.PreferredTargetPaymentMethodId : null;
}
@if (settings?.Enabled is true)
{
@@ -22,6 +25,21 @@
Vue.component("SideShiftCheckout", {
template: "#side-shift-checkout-template",
props: ["model"],
data: function() {
return {
explicitId: "",
preferredToCurrency: @Json.Serialize(preferredTargetPaymentMethodId),
}
},
created () {
const self = this;
setInterval(function() {
if ( self.explicitId === window.ssExplicitId) {
return;
}
self.explicitId = window.ssExplicitId;
},200)
},
computed: {
content () {
return this.$i18n.i18next.t("conversion_body", this.model).replace(/\n/ig, '<br>');
@@ -55,13 +73,20 @@
openDialog () {
window.__SIDESHIFT__ = {
parentAffiliateId: "qg0OrfHJV",
defaultDepositMethodId: this.explicitId || undefined,
defaultSettleMethodId: this.settleMethodId,
settleAddress: this.model.btcAddress,
settleAmount: this.amountDue,
type: this.type
};
console.log(window.__SIDESHIFT__);
window.sideshift.show();
}
},
watch: {
explicitId (val) {
this.openDialog();
}
}
});
</script>

View File

@@ -1,15 +1,36 @@
@using BTCPayServer.Plugins.SideShift
@using Newtonsoft.Json
@using Newtonsoft.Json.Linq
@inject SideShiftService SideShiftService
@model BTCPayServer.Models.InvoicingModels.PaymentModel
@{
const string id = "SideShift";
var storeId = ((JObject)JObject.Parse(JsonConvert.SerializeObject(Model)))["StoreId"].Value<string>();
var storeId = Model.StoreId;
var settings = await SideShiftService.GetSideShiftForStore(storeId);
if (settings?.Enabled is true)
{
<a href="#@id" class="btcpay-pill m-0 payment-method" :class="{ active: pmId === '@id' }" v-on:click.prevent="changePaymentMethod('@id')">
@id
</a>
var coins = await SideShiftService.GetDepositOptions();
if (settings.ExplicitMethods?.Any() is true)
{
foreach (var explicitMethod in settings.ExplicitMethods)
{
var s = explicitMethod.Split("_");
var coin = s[0];
var network = s[1];
var coinInfo = coins.FirstOrDefault(c => c.CryptoCode == coin && c.Network == network);
if(coinInfo is null)
continue;
<a href="#@id" class="btcpay-pill m-0 payment-method" :class="{ active: pmId === '@id' && window.ssExplicitId === '@coinInfo.Id'}" v-on:click.prevent="()=>{ window.ssExplicitId = '@coinInfo.Id'; changePaymentMethod('@id'); }">
@coinInfo.DisplayName @(coinInfo.DisplayName.Equals(coinInfo.Network, StringComparison.InvariantCultureIgnoreCase)? string.Empty: $"({coinInfo.Network})")
</a>
}
}
if (!settings.OnlyShowExplicitMethods || settings.ExplicitMethods?.Any() is not true)
{
<a href="#@id" class="btcpay-pill m-0 payment-method" :class="{ active: pmId === '@id' && !window.ssExplicitId }" v-on:click.prevent="()=>{ window.ssExplicitId = null; changePaymentMethod('@id'); }">
@id
</a>
}
}
}

View File

@@ -1,31 +1,24 @@
@using System.Net.Http
@using BTCPayServer.Abstractions.TagHelpers
@using BTCPayServer.Abstractions.TagHelpers
@using BTCPayServer.Plugins.SideShift
@using Microsoft.AspNetCore.Mvc.TagHelpers
@using Newtonsoft.Json
@using Newtonsoft.Json.Linq
@inject IHttpClientFactory HttpClientFactory
@inject SideShiftService SideShiftService
@{
var client = HttpClientFactory.CreateClient("sideshift");
var request = new HttpRequestMessage(HttpMethod.Get, "https://sideshift.ai/api/v2/coins");
var response = await client.SendAsync(request);
if (!response.IsSuccessStatusCode)
var coins = await SideShiftService.GetSettleCoins();
coins = coins.Where(tuple => new[] {SideShiftService.CoinType.VariableOnly, SideShiftService.CoinType.Both}.Contains(tuple.Type)).ToList();
if(coins.Any() is not true)
{
return;
}
var coins = await response.Content.ReadAsStringAsync().ContinueWith(t => JsonConvert.DeserializeObject<List<SideShiftAvailableCoin>>(t.Result));
var availableCoins = coins.SelectMany(coin => coin.networks.Select(s => (Coin: coin, Network: s)))
.Where(tuple => (tuple.Coin.fixedOnly.Type == JTokenType.Boolean && !tuple.Coin.fixedOnly.Value<bool>()) || (
tuple.Coin.fixedOnly is JArray varOnlyArray && varOnlyArray.All(v => v.Value<string>() != tuple.Network))).ToList();
}
<button type="button" class="btn btn-primary btn-sm mt-4" data-bs-toggle="modal" data-bs-target="#sideshiftModal" >Generate SideShift destination</button>
<script>
const ssAvailableCoins = @Json.Serialize(availableCoins.ToDictionary(tuple=> $"{tuple.Coin.coin}_{tuple.Network}",tuple =>
const ssAvailableCoins = @Json.Serialize(coins.ToDictionary(tuple=> $"{tuple.CryptoCode}_{tuple.Network}",tuple =>
new {
coin = tuple.Coin.name,
code = tuple.Coin.coin,
memo = tuple.Coin.hasMemo,
coin = tuple.DisplayName,
code = tuple.CryptoCode,
memo = tuple.HasMemo,
network = tuple.Network
}));
document.addEventListener('DOMContentLoaded', (event) => {
@@ -156,9 +149,9 @@ document.addEventListener('DOMContentLoaded', (event) => {
<div class="form-group">
<label class="form-label">Which coin should Sideshift send you</label>
<select id="sscoin" class="form-select">
@foreach (var opt in availableCoins)
@foreach (var opt in coins)
{
<option value="@(opt.Coin.coin)_@(opt.Network)">@opt.Coin.name (@opt.Network)</option>
<option value="@($"{opt.CryptoCode}_{opt.Network}")">@opt.ToString()</option>
}
</select>
</div>

View File

@@ -1,7 +1,5 @@
@using System.Net.Http
@using BTCPayServer.Plugins.SideShift
@using Newtonsoft.Json
@using Newtonsoft.Json.Linq
@model BTCPayServer.Models.ViewPullPaymentModel
@inject IHttpClientFactory HttpClientFactory
@inject SideShiftService SideShiftService
@@ -11,28 +9,23 @@
{
return;
}
var client = HttpClientFactory.CreateClient("sideshift");
var request = new HttpRequestMessage(HttpMethod.Get, "https://sideshift.ai/api/v2/coins");
var response = await client.SendAsync(request);
if (!response.IsSuccessStatusCode)
var coins = await SideShiftService.GetSettleCoins();
coins = coins.Where(tuple => new[] {SideShiftService.CoinType.FixedOnly, SideShiftService.CoinType.Both}.Contains(tuple.Type)).ToList();
if(coins.Any() is not true)
{
return;
}
var coins = await response.Content.ReadAsStringAsync().ContinueWith(t => JsonConvert.DeserializeObject<List<SideShiftAvailableCoin>>(t.Result));
var availableCoins = coins.SelectMany(coin => coin.networks.Select(s => (Coin: coin, Network: s)))
.Where(tuple => (tuple.Coin.fixedOnly.Type == JTokenType.Boolean && !tuple.Coin.fixedOnly.Value<bool>()) || (
tuple.Coin.fixedOnly is JArray varOnlyArray && varOnlyArray.All(v => v.Value<string>() != tuple.Network))).ToList();
var potentialPaymentMethods = Model.PaymentMethods;//.Where(id => id.CryptoCode.Equals(Model.Currency, StringComparison.OrdinalIgnoreCase)).ToList();
if (Model.IsPending && potentialPaymentMethods.Any() && availableCoins.Any())
if (Model.IsPending && potentialPaymentMethods.Any())
{
<script>
const url = @Json.Serialize(Url.Action("CreatePayout", "SideShift", new {pullPaymentId = Model.Id }))
const ssAvailableCoins = @Json.Serialize(availableCoins.ToDictionary(tuple=> $"{tuple.Coin.coin}_{tuple.Network}",tuple =>
const ssAvailableCoins = @Json.Serialize(coins.ToDictionary(tuple=> $"{tuple.CryptoCode}_{tuple.Network}",tuple =>
new {
coin = tuple.Coin.name,
code = tuple.Coin.coin,
memo = tuple.Coin.hasMemo,
coin = tuple.DisplayName,
code = tuple.CryptoCode,
memo = tuple.HasMemo,
network = tuple.Network
}));
const ssPaymentMethods = @Json.Serialize(potentialPaymentMethods.Select(id => new { id = id.ToString(), name= id.ToPrettyString()}));
@@ -159,9 +152,9 @@
<div class="form-group">
<label class="form-label">Which coin should Sideshift send you</label>
<select id="sscoin" class="form-select">
@foreach (var opt in availableCoins)
@foreach (var opt in coins)
{
<option value="@(opt.Coin.coin)_@(opt.Network)">@opt.Coin.name (@opt.Network)</option>
<option value="@($"{opt.CryptoCode}_{opt.Network}")">@opt.ToString()</option>
}
</select>
</div>