Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
BuyEDaler.cs
1using Paiwise;
2using System;
4using System.Threading.Tasks;
5using System.Xml;
9using Waher.Script;
15
17{
21 public class BuyEDaler : ActionNode
22 {
23 private To to;
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 BuyEDaler()
36 : base()
37 {
38 }
39
43 public To To => this.to;
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(BuyEDaler);
84
89 public override IStateMachineNode Create()
90 {
91 return new BuyEDaler();
92 }
93
98 public override async Task Parse(XmlElement Xml)
99 {
100 await base.Parse(Xml);
101
103 new Type[]
104 {
105 typeof(To),
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.to = this.GetValueElement<To>();
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, "Buy eDaler contract reference must be either the creation contract, or the current ownership contract.");
153 }
154
155 CaseInsensitiveString To = (await this.to.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 buy 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);
170
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 {
178 throw new StateMachineException(Arguments.Machine, "Unable to get contract " + ContractId +
179 ": " + P.Value.ErrorText);
180 }
181 }
182
183 if (Contract.State != ContractState.Signed)
184 throw new StateMachineException(Arguments.Machine, "Contract is not in a signed state.");
185
186 if (!await Contract.IsLegallyBinding(true, false, XmppServerModule.Legal))
187 throw new StateMachineException(Arguments.Machine, "Contract not legally binding.");
188
189 CaseInsensitiveString PaymentLegalId = null;
190 CaseInsensitiveString PaymentJid = null;
191
192 foreach (ClientSignature Signature in Contract.ClientSignatures)
193 {
194 if (To == Signature.LegalId || To == Signature.BareJid)
195 {
196 PaymentLegalId = Signature.LegalId;
197 PaymentJid = Signature.BareJid;
198 break;
199 }
200 }
201
202 if (PaymentLegalId is null || PaymentJid is null)
203 throw new StateMachineException(Arguments.Machine, To + " is not a part of the contract.");
204
205 if (string.IsNullOrEmpty(ServiceId))
206 throw new StateMachineException(Arguments.Machine, "Unable to buy eDaler: Service ID not defined.");
207
208 if (string.IsNullOrEmpty(ServiceProvider))
209 throw new StateMachineException(Arguments.Machine, "Unable to buy eDaler: Service Provider not defined.");
210
212 ?? throw new StateMachineException(Arguments.Machine, "Unable to buy eDaler: Service Provider " + ServiceProvider + " not found or installed.");
213
214 if (!typeof(IBuyEDalerServiceProvider).IsAssignableFrom(T) ||
216 {
217 throw new StateMachineException(Arguments.Machine, "Unable to buy eDaler: Service Provider does not support buying of eDaler.");
218 }
219
220 if (string.IsNullOrEmpty(Currency))
221 throw new StateMachineException(Arguments.Machine, "Unable to buy eDaler: Currency not defined.");
222
223 if (Amount <= 0)
224 throw new StateMachineException(Arguments.Machine, "Unable to buy eDaler: Amount must be positive.");
225
226 XmppAddress PaymentAddress = new XmppAddress(PaymentJid);
227 if (!PaymentAddress.HasAccount)
228 throw new StateMachineException(Arguments.Machine, "Unable to buy eDaler: Invalid buyer JID.");
229
230 if (!XmppServerModule.Server.IsServerDomain(PaymentAddress.Domain, true))
231 throw new StateMachineException(Arguments.Machine, "Unable to buy eDaler: Buyer does not have an account on the current broker.");
232
233 LegalIdentity PaymentIdentity = await LegalComponent.GetLocalLegalIdentity(PaymentLegalId)
234 ?? throw new StateMachineException(Arguments.Machine, "Unable to get buyer legal identity.");
235
236 string Country = PaymentIdentity[PersonalInformation.CountryTag];
237
238 if (string.IsNullOrEmpty(Country))
239 throw new StateMachineException(Arguments.Machine, "Buyer legal identity lacks country.");
240
241 IBuyEDalerService Service = await BuyEDalerServiceProvider.GetServiceForBuyingEDaler(ServiceId, Currency, Country)
242 ?? throw new StateMachineException(Arguments.Machine, "Payment Service ID not found.");
243
244 if (!await Service.CanBuyEDaler(PaymentAddress.Account))
245 throw new StateMachineException(Arguments.Machine, "Unable to buy eDaler: Selected service provider cannot perform action.");
246
247 if (Service.Supports(Currency) == Grade.NotAtAll)
248 throw new StateMachineException(Arguments.Machine, "Unable to buy eDaler: Selected service provider does not support currency.");
249
250 Dictionary<CaseInsensitiveString, object> ContractParameters = new Dictionary<CaseInsensitiveString, object>();
251 Dictionary<CaseInsensitiveString, CaseInsensitiveString> BuyerIdParameters = new Dictionary<CaseInsensitiveString, CaseInsensitiveString>();
252
253 foreach (Parameter P2 in this.parameters)
254 {
255 string Key = (await P2.Key.Evaluate(Arguments))?.ToString() ?? string.Empty;
256 object Value = await P2.Value.Evaluate(Arguments);
257
258 ContractParameters[Key] = Value;
259 }
260
261 foreach (Property P2 in PaymentIdentity.Properties)
262 BuyerIdParameters[P2.Name] = P2.Value;
263
264 PaymentResult Result = await PaiwiseProcessor.BuyEDaler(Amount, Currency, null, null, null, Service, XmppServerModule.EDaler,
265 PaymentLegalId, ContractId, ContractParameters, BuyerIdParameters, Reference, null, null);
266
267 if (!Result.Ok)
268 throw new StateMachineException(Arguments.Machine, Result.Error);
269 }
270
271 }
272}
Contains information about a service provider that users can use to buy eDaler.
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
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
override IStateMachineNode Create()
Creates a new node of the corresponding type.
Definition: BuyEDaler.cs:89
override async Task Execute(EvaluationArguments Arguments)
Evaluates the action node
Definition: BuyEDaler.cs:146
override async Task Parse(XmlElement Xml)
Parses the State-machine node.
Definition: BuyEDaler.cs:98
override void OnChildNodesUpdated()
Method called whenever ChildNodes is updated.
Definition: BuyEDaler.cs:128
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 buy eDaler.
Task< bool > CanBuyEDaler(CaseInsensitiveString AccountName)
If the service provider can be used to process a request to buy eDaler of a certain amount,...
Interface for information about a service provider that users can use to buy eDaler.
Grade Supports(T Object)
If the interface understands objects such as Object .
Grade
Grade enumeration
Definition: Grade.cs:7