Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ReserveAmount.cs
1using System;
2using System.Threading.Tasks;
3using System.Xml;
6using Waher.Script;
9
11{
16 {
17 private From from;
18 private Amount amount;
19 private Currency currency;
20 private ValidDays validDays;
21 private Reference reference;
22 private Contract contract;
23
28 : base()
29 {
30 }
31
35 public From From => this.from;
36
40 public Amount Amount => this.amount;
41
45 public Currency Currency => this.currency;
46
50 public ValidDays ValidDays => this.validDays;
51
55 public Reference Reference => this.reference;
56
60 public Contract Contract => this.contract;
61
65 [DefaultValueNull]
66 public string TransactionVariable { get; set; }
67
71 public override string LocalName => nameof(ReserveAmount);
72
77 public override IStateMachineNode Create()
78 {
79 return new ReserveAmount();
80 }
81
86 public override async Task Parse(XmlElement Xml)
87 {
88 this.TransactionVariable = XML.Attribute(Xml, "transactionVariable");
89
90 await base.Parse(Xml);
91
93 new Type[]
94 {
95 typeof(From),
96 typeof(Amount),
97 typeof(Currency),
98 typeof(ValidDays),
99 typeof(Reference),
100 typeof(Contract)
101 },
102 new bool[]
103 {
104 true,
105 true,
106 true,
107 true,
108 true,
109 true
110 });
111 }
112
116 protected override void OnChildNodesUpdated()
117 {
118 base.OnChildNodesUpdated();
119
120 this.from = this.GetValueElement<From>();
121 this.amount = this.GetValueElement<Amount>();
122 this.currency = this.GetValueElement<Currency>();
123 this.validDays = this.GetValueElement<ValidDays>();
124 this.reference = this.GetValueElement<Reference>();
125 this.contract = this.GetValueElement<Contract>();
126 }
127
132 public override async Task Execute(EvaluationArguments Arguments)
133 {
134 double ValidDays = Expression.ToDouble(await this.validDays.Evaluate(Arguments));
135 if (ValidDays <= 0)
136 throw new StateMachineException(Arguments.Machine, "ReserveAmounts must be valid a positive number of days.");
137
138 string Contract = (await this.contract.Evaluate(Arguments))?.ToString();
139 if (Contract != (Arguments.Token?.CreationContract ?? Arguments.Machine.DefinitionContractId) &&
140 Contract != Arguments.Token?.OwnershipContract)
141 {
142 throw new StateMachineException(Arguments.Machine, "ReserveAmount contract reference must be either the creation contract, or the current ownership contract.");
143 }
144
145 string From = (await this.from.Evaluate(Arguments))?.ToString();
146
147 if (From == Arguments.Machine.TrustProvider ||
148 From == Arguments.Machine.TrustProviderJid)
149 {
150 throw new StateMachineException(Arguments.Machine, "State-Machine payments from trust provider not permitted.");
151 }
152
153 decimal Amount = Expression.ToDecimal(await this.amount.Evaluate(Arguments));
154 string Currency = (await this.currency.Evaluate(Arguments))?.ToString();
155 string Reference = (await this.reference.Evaluate(Arguments))?.ToString();
156
157 Guid TransactionId = Guid.NewGuid();
158 string ReserveAmountUri = PaiwiseProcessor.GenerateReserveAmountUri(TransactionId,
159 From, Payment.IsLegalId(From), Currency, Amount, Reference, Contract, ValidDays, out _, out _);
160
161 string Msg = await PaiwiseProcessor.ProcessPayment(ReserveAmountUri, Arguments.EDaler);
162 if (!string.IsNullOrEmpty(Msg))
163 throw new StateMachineException(Arguments.Machine, Msg);
164
165 if (!string.IsNullOrEmpty(this.TransactionVariable))
166 Arguments.Variables[this.TransactionVariable] = TransactionId.ToString();
167 }
168
169 }
170}
Helps with common XML-related tasks.
Definition: XML.cs:21
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
Definition: XML.cs:1062
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
static double ToDouble(object Object)
Converts an object to a double value.
Definition: Expression.cs:5110
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.
override async Task Parse(XmlElement Xml)
Parses the State-machine node.
override async Task Execute(EvaluationArguments Arguments)
Evaluates the action node
override IStateMachineNode Create()
Creates a new node of the corresponding type.
Defines number of days payment is valid, before it must be processed.
Definition: ValidDays.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
Definition: ImplTypes.g.cs:58