Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SellEDaler.cs
1using Paiwise;
2using System;
3using System.Collections.Generic;
4using System.Threading.Tasks;
5using System.Xml;
9using Waher.Script;
14
16{
20 public class SellEDaler : ActionNode
21 {
22 private From from;
23 private Amount amount;
24 private Currency currency;
25 private Reference reference;
26 private Contract contract;
27 private ServiceProvider serviceProvider;
28 private ServiceId serviceId;
29 private Parameter[] parameters;
30
34 public SellEDaler()
35 : base()
36 {
37 }
38
42 public From From => this.from;
43
47 public Amount Amount => this.amount;
48
52 public Currency Currency => this.currency;
53
57 public Reference Reference => this.reference;
58
62 public Contract Contract => this.contract;
63
67 public ServiceProvider ServiceProvider => this.serviceProvider;
68
72 public ServiceId ServiceId => this.serviceId;
73
77 public Parameter[] Parameters => this.parameters;
78
82 public override string LocalName => nameof(SellEDaler);
83
88 public override IStateMachineNode Create()
89 {
90 return new SellEDaler();
91 }
92
97 public override async Task Parse(XmlElement Xml)
98 {
99 await base.Parse(Xml);
100
102 new Type[]
103 {
104 typeof(From),
105 typeof(Amount),
106 typeof(Currency),
107 typeof(Reference),
108 typeof(Contract),
109 typeof(ServiceProvider),
110 typeof(ServiceId)
111 },
112 new bool[]
113 {
114 true,
115 true,
116 true,
117 true,
118 true,
119 true,
120 true
121 });
122 }
123
127 protected override void OnChildNodesUpdated()
128 {
129 base.OnChildNodesUpdated();
130
131 this.from = this.GetValueElement<From>();
132 this.amount = this.GetValueElement<Amount>();
133 this.currency = this.GetValueElement<Currency>();
134 this.reference = this.GetValueElement<Reference>();
135 this.contract = this.GetValueElement<Contract>();
136 this.serviceProvider = this.GetValueElement<ServiceProvider>();
137 this.serviceId = this.GetValueElement<ServiceId>();
138 this.parameters = this.GetChildElements<Parameter>();
139 }
140
145 public override async Task Execute(EvaluationArguments Arguments)
146 {
147 string ContractId = (await this.contract.Evaluate(Arguments))?.ToString();
148 if (ContractId != (Arguments.Token?.CreationContract ?? Arguments.Machine.DefinitionContractId) &&
149 ContractId != Arguments.Token?.OwnershipContract)
150 {
151 throw new Exception("Sell eDaler contract reference must be either the creation contract, or the current ownership contract.");
152 }
153
154 CaseInsensitiveString To = (await this.from.Evaluate(Arguments))?.ToString();
155
156 if (To == Arguments.Machine.TrustProvider ||
157 To == Arguments.Machine.TrustProviderJid)
158 {
159 throw new Exception("Trust Provider not permitted to sell eDaler.");
160 }
161
162 decimal Amount = Expression.ToDecimal(await this.amount.Evaluate(Arguments));
163 string Currency = (await this.currency.Evaluate(Arguments))?.ToString();
164 string Reference = (await this.reference.Evaluate(Arguments))?.ToString();
165 string ServiceProvider = (await this.serviceProvider.Evaluate(Arguments))?.ToString();
166 string ServiceId = (await this.serviceId.Evaluate(Arguments))?.ToString();
167
168 Legal.Contracts.Contract Contract = await XmppServerModule.Legal.GetContract(ContractId)
169 ?? throw new Exception("Unable to get contract: " + ContractId);
170
171 if (Contract.State != ContractState.Signed)
172 throw new Exception("Contract is not in a signed state.");
173
174 if (!await Contract.IsLegallyBinding(true, false, XmppServerModule.Legal))
175 throw new Exception("Contract not legally binding.");
176
177 CaseInsensitiveString PaymentLegalId = null;
178 CaseInsensitiveString PaymentJid = null;
179
180 foreach (ClientSignature Signature in Contract.ClientSignatures)
181 {
182 if (To == Signature.LegalId || To == Signature.BareJid)
183 {
184 PaymentLegalId = Signature.LegalId;
185 PaymentJid = Signature.BareJid;
186 break;
187 }
188 }
189
190 if (PaymentLegalId is null || PaymentJid is null)
191 throw new Exception(To + " is not a part of the contract.");
192
193 if (string.IsNullOrEmpty(ServiceId))
194 throw new Exception("Unable to sell eDaler: Service ID not defined.");
195
196 if (string.IsNullOrEmpty(ServiceProvider))
197 throw new Exception("Unable to sell eDaler: Service Provider not defined.");
198
200 ?? throw new Exception("Unable to sell eDaler: Service Provider " + ServiceProvider + " not found or installed.");
201
202 if (!typeof(ISellEDalerServiceProvider).IsAssignableFrom(T) ||
204 {
205 throw new Exception("Unable to sell eDaler: Service Provider does not support selling of eDaler.");
206 }
207
208 if (string.IsNullOrEmpty(Currency))
209 throw new Exception("Unable to sell eDaler: Currency not defined.");
210
211 if (Amount <= 0)
212 throw new Exception("Unable to sell eDaler: Amount must be positive.");
213
214 XmppAddress PaymentAddress = new XmppAddress(PaymentJid);
215 if (!PaymentAddress.HasAccount)
216 throw new Exception("Unable to sell eDaler: Invalid seller JID.");
217
218 if (!XmppServerModule.Server.IsServerDomain(PaymentAddress.Domain, true))
219 throw new Exception("Unable to sell eDaler: Seller does not have an account on the current broker.");
220
221 LegalIdentity PaymentIdentity = await LegalComponent.GetLocalLegalIdentity(PaymentLegalId)
222 ?? throw new Exception("Unable to get seller legal identity.");
223
224 string Country = PaymentIdentity["COUNTRY"];
225
226 if (string.IsNullOrEmpty(Country))
227 throw new Exception("Seller legal identity lacks country.");
228
229 ISellEDalerService Service = await SellEDalerServiceProvider.GetServiceForSellingEDaler(ServiceId, Currency, Country)
230 ?? throw new Exception("Payment Service ID not found.");
231
232 if (!await Service.CanSellEDaler(PaymentAddress.Account))
233 throw new Exception("Unable to sell eDaler: Selected service provider cannot perform action.");
234
235 if (Service.Supports(Currency) == Grade.NotAtAll)
236 throw new Exception("Unable to sell eDaler: Selected service provider does not support currency.");
237
238 Dictionary<CaseInsensitiveString, object> ContractParameters = new Dictionary<CaseInsensitiveString, object>();
239 Dictionary<CaseInsensitiveString, CaseInsensitiveString> SellerIdParameters = new Dictionary<CaseInsensitiveString, CaseInsensitiveString>();
240
241 foreach (Parameter P in this.parameters)
242 {
243 string Key = (await P.Key.Evaluate(Arguments))?.ToString() ?? string.Empty;
244 object Value = await P.Value.Evaluate(Arguments);
245
246 ContractParameters[Key] = Value;
247 }
248
249 foreach (Property P in PaymentIdentity.Properties)
250 SellerIdParameters[P.Name] = P.Value;
251
252 PaymentResult Result = await PaiwiseProcessor.SellEDaler(Amount, Currency, null, null, null, Service,
253 XmppServerModule.EDaler, PaymentLegalId, ContractId, ContractParameters, SellerIdParameters,
254 Reference, null, null);
255
256 if (!Result.Ok)
257 throw new Exception(Result.Error);
258 }
259
260 }
261}
Result of request payment.
Definition: PaymentResult.cs:7
bool Ok
If payment was successful or not.
string Error
Error message, if payment was not successful.
Contains information about a service provider that users can use to sell eDaler.
Contains information about one XMPP address.
Definition: XmppAddress.cs:9
bool HasAccount
If the address has an account part.
Definition: XmppAddress.cs:167
CaseInsensitiveString Domain
Domain
Definition: XmppAddress.cs:97
CaseInsensitiveString Account
Account
Definition: XmppAddress.cs:124
bool IsServerDomain(CaseInsensitiveString Domain, bool IncludeAlternativeDomains)
Checks if a domain is the server domain, or optionally, an alternative domain.
Definition: XmppServer.cs:861
Represents a case-insensitive string.
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:14
static Type GetType(string FullName)
Gets a type, given its full name.
Definition: Types.cs:41
static object Instantiate(Type Type, params object[] Arguments)
Returns an instance of the type Type . If one needs to be created, it is. If the constructor requires...
Definition: Types.cs:1353
Class managing a script expression.
Definition: Expression.cs:39
static decimal ToDecimal(object Object)
Converts an object to a double value.
Definition: Expression.cs:4883
Paiwise processor, processing payment instructions defined in smart contracts.
Abstract base class for State-Machine action nodes.
Definition: ActionNode.cs:9
Defines from whom a payment (or message) is sent.
Definition: From.cs:9
override void OnChildNodesUpdated()
Method called whenever ChildNodes is updated.
Definition: SellEDaler.cs:127
override async Task Parse(XmlElement Xml)
Parses the State-machine node.
Definition: SellEDaler.cs:97
override async Task Execute(EvaluationArguments Arguments)
Evaluates the action node
Definition: SellEDaler.cs:145
override IStateMachineNode Create()
Creates a new node of the corresponding type.
Definition: SellEDaler.cs:88
Defines to whom a payment (or message) is sent.
Definition: To.cs:9
Contains information required for evaluating script in a state-machine.
StateMachine Machine
Reference to state-machine definition.
void ConvertValueAttributesToElements(XmlElement Xml, Type[] ValueTypes, bool[] Required)
Converts value attributes to parsed elements. The XML definition has to be parsed before,...
Task< object > Evaluate(EvaluationArguments Arguments)
Evaluates the value node.
Definition: Value.cs:146
CaseInsensitiveString TrustProviderJid
JID of Trust Provider
Definition: StateMachine.cs:86
CaseInsensitiveString DefinitionContractId
ID of Definition Contract
Definition: StateMachine.cs:76
CaseInsensitiveString TrustProvider
ID of Trust Provider
Definition: StateMachine.cs:81
Service Module hosting the XMPP broker and its components.
Interface for information about a service provider that users can use to sell eDaler.
Task< bool > CanSellEDaler(CaseInsensitiveString AccountName)
If the service provider can be used to process a request to sell eDaler of a certain amount,...
Interface for information about a service provider that users can use to sell eDaler.
Grade Supports(T Object)
If the interface understands objects such as Object .
Grade
Grade enumeration
Definition: Grade.cs:7