Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ReleaseAmount.cs
1using System;
2using System.Threading.Tasks;
3using System.Xml;
6using Waher.Script;
8
10{
15 {
16 private To to;
17 private Amount amount;
18 private Currency currency;
19 private ValidDays validDays;
20 private Reference reference;
21 private Contract contract;
22
27 : base()
28 {
29 }
30
34 public To To => this.to;
35
39 public Amount Amount => this.amount;
40
44 public Currency Currency => this.currency;
45
49 public ValidDays ValidDays => this.validDays;
50
54 public Reference Reference => this.reference;
55
59 public Contract Contract => this.contract;
60
64 [DefaultValueNull]
65 public string TransactionVariable { get; set; }
66
70 public override string LocalName => nameof(ReleaseAmount);
71
76 public override IStateMachineNode Create()
77 {
78 return new ReleaseAmount();
79 }
80
85 public override async Task Parse(XmlElement Xml)
86 {
87 this.TransactionVariable = XML.Attribute(Xml, "transactionVariable");
88
89 await base.Parse(Xml);
90
92 new Type[]
93 {
94 typeof(To),
95 typeof(Amount),
96 typeof(Currency),
97 typeof(ValidDays),
98 typeof(Reference),
99 typeof(Contract)
100 },
101 new bool[]
102 {
103 true,
104 true,
105 true,
106 true,
107 true,
108 true
109 });
110 }
111
115 protected override void OnChildNodesUpdated()
116 {
117 base.OnChildNodesUpdated();
118
119 this.to = this.GetValueElement<To>();
120 this.amount = this.GetValueElement<Amount>();
121 this.currency = this.GetValueElement<Currency>();
122 this.validDays = this.GetValueElement<ValidDays>();
123 this.reference = this.GetValueElement<Reference>();
124 this.contract = this.GetValueElement<Contract>();
125 }
126
131 public override async Task Execute(EvaluationArguments Arguments)
132 {
133 double ValidDays = Expression.ToDouble(await this.validDays.Evaluate(Arguments));
134 if (ValidDays <= 0)
135 throw new Exception("ReleaseAmounts must be valid a positive number of days.");
136
137 string Contract = (await this.contract.Evaluate(Arguments))?.ToString();
138 if (Contract != (Arguments.Token?.CreationContract ?? Arguments.Machine.DefinitionContractId) &&
139 Contract != Arguments.Token?.OwnershipContract)
140 {
141 throw new Exception("ReleaseAmount contract reference must be either the creation contract, or the current ownership contract.");
142 }
143
144 string To = (await this.to.Evaluate(Arguments))?.ToString();
145
146 if (To == Arguments.Machine.TrustProvider ||
147 To == Arguments.Machine.TrustProviderJid)
148 {
149 throw new Exception("State-Machine releasing of amounts to trust provider not permitted.");
150 }
151
152 decimal Amount = Expression.ToDecimal(await this.amount.Evaluate(Arguments));
153 string Currency = (await this.currency.Evaluate(Arguments))?.ToString();
154 string Reference = (await this.reference.Evaluate(Arguments))?.ToString();
155
156 Guid TransactionId = Guid.NewGuid();
157 string ReleaseAmountUri = PaiwiseProcessor.GenerateReleaseAmountUri(TransactionId,
158 To, Payment.IsLegalId(To), Currency, Amount, Reference, Contract, ValidDays, out _, out _);
159
160 string Msg = await PaiwiseProcessor.ProcessPayment(ReleaseAmountUri, Arguments.EDaler);
161 if (!string.IsNullOrEmpty(Msg))
162 throw new Exception(Msg);
163
164 if (!string.IsNullOrEmpty(this.TransactionVariable))
165 Arguments.Variables[this.TransactionVariable] = TransactionId.ToString();
166 }
167
168 }
169}
Helps with common XML-related tasks.
Definition: XML.cs:19
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
Definition: XML.cs:914
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
static double ToDouble(object Object)
Converts an object to a double value.
Definition: Expression.cs:4824
Paiwise processor, processing payment instructions defined in smart contracts.
Abstract base class for State-Machine action nodes.
Definition: ActionNode.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 to whom a payment (or message) is sent.
Definition: To.cs:9
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: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