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;
4using System.Threading.Tasks;
5using System.Xml;
9using Waher.Script;
15
17{
21 public class SellEDaler : ActionNode
22 {
23 private From from;
24 private Amount amount;
25 private Currency currency;
26 private Reference reference;
27 private Contract contract;
28 private ServiceProvider serviceProvider;
29 private ServiceId serviceId;
30 private Parameter[] parameters;
31
35 public SellEDaler()
36 : base()
37 {
38 }
39
43 public From From => this.from;
44
48 public Amount Amount => this.amount;
49
53 public Currency Currency => this.currency;
54
58 public Reference Reference => this.reference;
59
63 public Contract Contract => this.contract;
64
68 public ServiceProvider ServiceProvider => this.serviceProvider;
69
73 public ServiceId ServiceId => this.serviceId;
74
78 public Parameter[] Parameters => this.parameters;
79
83 public override string LocalName => nameof(SellEDaler);
84
89 public override IStateMachineNode Create()
90 {
91 return new SellEDaler();
92 }
93
98 public override async Task Parse(XmlElement Xml)
99 {
100 await base.Parse(Xml);
101
103 new Type[]
104 {
105 typeof(From),
106 typeof(Amount),
107 typeof(Currency),
108 typeof(Reference),
109 typeof(Contract),
110 typeof(ServiceProvider),
111 typeof(ServiceId)
112 },
113 new bool[]
114 {
115 true,
116 true,
117 true,
118 true,
119 true,
120 true,
121 true
122 });
123 }
124
128 protected override void OnChildNodesUpdated()
129 {
130 base.OnChildNodesUpdated();
131
132 this.from = this.GetValueElement<From>();
133 this.amount = this.GetValueElement<Amount>();
134 this.currency = this.GetValueElement<Currency>();
135 this.reference = this.GetValueElement<Reference>();
136 this.contract = this.GetValueElement<Contract>();
137 this.serviceProvider = this.GetValueElement<ServiceProvider>();
138 this.serviceId = this.GetValueElement<ServiceId>();
139 this.parameters = this.GetChildElements<Parameter>();
140 }
141
146 public override async Task Execute(EvaluationArguments Arguments)
147 {
148 string ContractId = (await this.contract.Evaluate(Arguments))?.ToString();
149 if (ContractId != (Arguments.Token?.CreationContract ?? Arguments.Machine.DefinitionContractId) &&
150 ContractId != Arguments.Token?.OwnershipContract)
151 {
152 throw new StateMachineException(Arguments.Machine, "Sell eDaler contract reference must be either the creation contract, or the current ownership contract.");
153 }
154
155 CaseInsensitiveString To = (await this.from.Evaluate(Arguments))?.ToString();
156
157 if (To == Arguments.Machine.TrustProvider ||
158 To == Arguments.Machine.TrustProviderJid)
159 {
160 throw new StateMachineException(Arguments.Machine, "Trust Provider not permitted to sell eDaler.");
161 }
162
163 decimal Amount = Expression.ToDecimal(await this.amount.Evaluate(Arguments));
164 string Currency = (await this.currency.Evaluate(Arguments))?.ToString();
165 string Reference = (await this.reference.Evaluate(Arguments))?.ToString();
166 string ServiceProvider = (await this.serviceProvider.Evaluate(Arguments))?.ToString();
167 string ServiceId = (await this.serviceId.Evaluate(Arguments))?.ToString();
168
169 KeyValuePair<Legal.Contracts.Contract, IqResultEventArgs> P = await XmppServerModule.Legal.GetContract(ContractId);
171
172 if (Contract is null)
173 {
174 if (string.IsNullOrEmpty(P.Value?.ErrorText))
175 throw new StateMachineException(Arguments.Machine, "Unable to get contract: " + ContractId);
176 else
177 throw new StateMachineException(Arguments.Machine, "Unable to get contract " + ContractId + ": " + P.Value.ErrorText);
178 }
179
180 if (Contract.State != ContractState.Signed)
181 throw new StateMachineException(Arguments.Machine, "Contract is not in a signed state.");
182
183 if (!await Contract.IsLegallyBinding(true, false, XmppServerModule.Legal))
184 throw new StateMachineException(Arguments.Machine, "Contract not legally binding.");
185
186 CaseInsensitiveString PaymentLegalId = null;
187 CaseInsensitiveString PaymentJid = null;
188
189 foreach (ClientSignature Signature in Contract.ClientSignatures)
190 {
191 if (To == Signature.LegalId || To == Signature.BareJid)
192 {
193 PaymentLegalId = Signature.LegalId;
194 PaymentJid = Signature.BareJid;
195 break;
196 }
197 }
198
199 if (PaymentLegalId is null || PaymentJid is null)
200 throw new StateMachineException(Arguments.Machine, To + " is not a part of the contract.");
201
202 if (string.IsNullOrEmpty(ServiceId))
203 throw new StateMachineException(Arguments.Machine, "Unable to sell eDaler: Service ID not defined.");
204
205 if (string.IsNullOrEmpty(ServiceProvider))
206 throw new StateMachineException(Arguments.Machine, "Unable to sell eDaler: Service Provider not defined.");
207
209 ?? throw new StateMachineException(Arguments.Machine, "Unable to sell eDaler: Service Provider " + ServiceProvider + " not found or installed.");
210
211 if (!typeof(ISellEDalerServiceProvider).IsAssignableFrom(T) ||
213 {
214 throw new StateMachineException(Arguments.Machine, "Unable to sell eDaler: Service Provider does not support selling of eDaler.");
215 }
216
217 if (string.IsNullOrEmpty(Currency))
218 throw new StateMachineException(Arguments.Machine, "Unable to sell eDaler: Currency not defined.");
219
220 if (Amount <= 0)
221 throw new StateMachineException(Arguments.Machine, "Unable to sell eDaler: Amount must be positive.");
222
223 XmppAddress PaymentAddress = new XmppAddress(PaymentJid);
224 if (!PaymentAddress.HasAccount)
225 throw new StateMachineException(Arguments.Machine, "Unable to sell eDaler: Invalid seller JID.");
226
227 if (!XmppServerModule.Server.IsServerDomain(PaymentAddress.Domain, true))
228 throw new StateMachineException(Arguments.Machine, "Unable to sell eDaler: Seller does not have an account on the current broker.");
229
230 LegalIdentity PaymentIdentity = await LegalComponent.GetLocalLegalIdentity(PaymentLegalId)
231 ?? throw new StateMachineException(Arguments.Machine, "Unable to get seller legal identity.");
232
233 string Country = PaymentIdentity[PersonalInformation.CountryTag];
234
235 if (string.IsNullOrEmpty(Country))
236 throw new StateMachineException(Arguments.Machine, "Seller legal identity lacks country.");
237
238 ISellEDalerService Service = await SellEDalerServiceProvider.GetServiceForSellingEDaler(ServiceId, Currency, Country)
239 ?? throw new StateMachineException(Arguments.Machine, "Payment Service ID not found.");
240
241 if (!await Service.CanSellEDaler(PaymentAddress.Account))
242 throw new StateMachineException(Arguments.Machine, "Unable to sell eDaler: Selected service provider cannot perform action.");
243
244 if (Service.Supports(Currency) == Grade.NotAtAll)
245 throw new StateMachineException(Arguments.Machine, "Unable to sell eDaler: Selected service provider does not support currency.");
246
247 Dictionary<CaseInsensitiveString, object> ContractParameters = new Dictionary<CaseInsensitiveString, object>();
248 Dictionary<CaseInsensitiveString, CaseInsensitiveString> SellerIdParameters = new Dictionary<CaseInsensitiveString, CaseInsensitiveString>();
249
250 foreach (Parameter P2 in this.parameters)
251 {
252 string Key = (await P2.Key.Evaluate(Arguments))?.ToString() ?? string.Empty;
253 object Value = await P2.Value.Evaluate(Arguments);
254
255 ContractParameters[Key] = Value;
256 }
257
258 foreach (Property P2 in PaymentIdentity.Properties)
259 SellerIdParameters[P2.Name] = P2.Value;
260
261 PaymentResult Result = await PaiwiseProcessor.SellEDaler(Amount, Currency, null, null, null, Service,
262 XmppServerModule.EDaler, PaymentLegalId, ContractId, ContractParameters, SellerIdParameters,
263 Reference, null, null);
264
265 if (!Result.Ok)
266 throw new StateMachineException(Arguments.Machine, Result.Error);
267 }
268
269 }
270}
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 personal information found in a legal identity.
const string CountryTag
COUNTRY
Contains information about a service provider that users can use to sell eDaler.
Event arguments for responses to IQ queries.
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:898
Represents a case-insensitive string.
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:15
static Type GetType(string FullName)
Gets a type, given its full name.
Definition: Types.cs:42
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:1454
Class managing a script expression.
Definition: Expression.cs:41
static decimal ToDecimal(object Object)
Converts an object to a double value.
Definition: Expression.cs:5169
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:128
override async Task Parse(XmlElement Xml)
Parses the State-machine node.
Definition: SellEDaler.cs:98
override async Task Execute(EvaluationArguments Arguments)
Evaluates the action node
Definition: SellEDaler.cs:146
override IStateMachineNode Create()
Creates a new node of the corresponding type.
Definition: SellEDaler.cs:89
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:147
CaseInsensitiveString TrustProviderJid
JID of Trust Provider
Definition: StateMachine.cs:92
CaseInsensitiveString DefinitionContractId
ID of Definition Contract
Definition: StateMachine.cs:82
CaseInsensitiveString TrustProvider
ID of Trust Provider
Definition: StateMachine.cs:87
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