Skip to content

Commit a310a7c

Browse files
committed
add round method choosing for minting
1 parent 69d7199 commit a310a7c

File tree

4 files changed

+63
-6
lines changed

4 files changed

+63
-6
lines changed

docs/guardian/standard-registry/policies/policy-creation/introduction/mintdocumentblock.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ This block is responsible for adding configurations on calculating the amount of
66

77
<table><thead><tr><th width="208">Block Property</th><th>Definition</th><th>Example Input</th><th>Status</th></tr></thead><tbody><tr><td>tag</td><td>Unique name for the logic block.</td><td><strong>mintDocumentBlock</strong></td><td></td></tr><tr><td>permissions</td><td>Which entity has rights to interact at this part of the workflow.</td><td>VVB</td><td></td></tr><tr><td>defaultActive</td><td>Shows whether this block is active at this time and whether it needs to be shown.</td><td>Checked or unchecked.</td><td></td></tr><tr><td>On errors</td><td>Called if the system error has occurs in the Block</td><td><ul><li>No action</li><li>Retry</li><li>Go to step</li><li>Go to tag</li></ul></td><td></td></tr><tr><td>Stop Propagation</td><td>End processing here, don't pass control to the next block.</td><td>Checked or unchecked.</td><td></td></tr></tbody></table>
88

9+
### Additional Properties
10+
11+
<table><thead><tr><th width="208">Block Property</th><th>Definition</th><th>Example Input</th><th>Status</th></tr></thead><tbody><tr><td>Round Method</td><td>The method which is using for rounding token number.</td><td>Choose option from dropdown(`Round to nearest` by default):<ul><li>Round up</li><li>Round down</li><li>Round to nearest</li></ul></td><td></td></tr><tr></tbody></table>
12+
913
### UI Properties
1014

1115
| UI Property | Definition | Example Input |

policy-service/src/policy-engine/blocks/mint-block.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { HederaDidDocument, MessageAction, MessageMemo, MessageServer, Token as
88
import { PolicyUtils } from '../helpers/utils.js';
99
import { AnyBlockType, IPolicyDocument, IPolicyEventState, IPolicyTokenBlock } from '../policy-engine.interface.js';
1010
import { IPolicyEvent, PolicyInputEventType, PolicyOutputEventType } from '../interfaces/index.js';
11-
import { ChildrenType, ControlType } from '../interfaces/block-about.js';
11+
import { ChildrenType, ControlType, PropertyType } from '../interfaces/block-about.js';
1212
import { PolicyUser, UserCredentials } from '../policy-user.js';
1313
import { ExternalDocuments, ExternalEvent, ExternalEventType } from '../interfaces/external-event.js';
1414
import { MintService } from '../mint/mint-service.js';
@@ -38,6 +38,27 @@ import { RecordActionStep } from '../record-action-step.js';
3838
PolicyOutputEventType.RefreshEvent,
3939
PolicyOutputEventType.ErrorEvent
4040
],
41+
properties: [{
42+
name: 'roundMethod',
43+
label: 'Round Method',
44+
title: 'Round Method',
45+
type: PropertyType.Select,
46+
items: [
47+
{
48+
label: 'Round up',
49+
value: 'ceil'
50+
},
51+
{
52+
label: 'Round down',
53+
value: 'floor'
54+
},
55+
{
56+
label: 'Round to nearest',
57+
value: 'round'
58+
}
59+
],
60+
default: 'round'
61+
}],
4162
defaultEvent: true
4263
},
4364
variables: [
@@ -296,7 +317,7 @@ export class MintBlock {
296317
if (Number.isNaN(amount) || !Number.isFinite(amount) || amount < 0) {
297318
throw new BlockActionError(`Invalid token value: ${amount}`, ref.blockType, ref.uuid);
298319
}
299-
const [tokenValue, tokenAmount] = PolicyUtils.tokenAmount(token, amount);
320+
const [tokenValue, tokenAmount] = PolicyUtils.tokenAmount(token, amount, ref.options.roundMethod);
300321

301322
const policyOwnerCred = await PolicyUtils.getUserCredentials(ref, ref.policyOwner, userId);
302323
const policyOwnerDid = await policyOwnerCred.loadDidDocument(ref, userId);

policy-service/src/policy-engine/blocks/retirement-block.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Token as TokenCollection, VcHelper, VcDocumentDefinition as VcDocument,
77
import { PolicyUtils } from '../helpers/utils.js';
88
import { AnyBlockType, IPolicyDocument, IPolicyEventState } from '../policy-engine.interface.js';
99
import { IPolicyEvent, PolicyInputEventType, PolicyOutputEventType } from '../interfaces/index.js';
10-
import { ChildrenType, ControlType } from '../interfaces/block-about.js';
10+
import { ChildrenType, ControlType, PropertyType } from '../interfaces/block-about.js';
1111
import { PolicyUser, UserCredentials } from '../policy-user.js';
1212
import { ExternalDocuments, ExternalEvent, ExternalEventType } from '../interfaces/external-event.js';
1313
import { MintService } from '../mint/mint-service.js';
@@ -35,6 +35,27 @@ import { RecordActionStep } from '../record-action-step.js';
3535
PolicyOutputEventType.RefreshEvent,
3636
PolicyOutputEventType.ErrorEvent
3737
],
38+
properties: [{
39+
name: 'roundMethod',
40+
label: 'Round Method',
41+
title: 'Round Method',
42+
type: PropertyType.Select,
43+
items: [
44+
{
45+
label: 'Round up',
46+
value: 'ceil'
47+
},
48+
{
49+
label: 'Round down',
50+
value: 'floor'
51+
},
52+
{
53+
label: 'Round to nearest',
54+
value: 'round'
55+
}
56+
],
57+
default: 'round'
58+
}],
3859
defaultEvent: true
3960
},
4061
variables: [
@@ -194,7 +215,7 @@ export class RetirementBlock {
194215
throw new Error('For FUNGIBLE tokens, Rule is required');
195216
}
196217
const amount = PolicyUtils.aggregate(ref.options.rule, documents);
197-
[tokenValue, tokenAmount] = PolicyUtils.tokenAmount(token, amount);
218+
[tokenValue, tokenAmount] = PolicyUtils.tokenAmount(token, amount, ref.options.roundMethod);
198219
}
199220

200221
const wipeVC = await this.createWipeVC(policyOwnerDidDocument, token, tokenAmount, ref, serialNumbers, actionStatus?.id);

policy-service/src/policy-engine/helpers/utils.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,21 @@ export class PolicyUtils {
188188
* @param token
189189
* @param amount
190190
*/
191-
public static tokenAmount(token: Token, amount: number): [number, string] {
191+
public static tokenAmount(token: Token, amount: number, method: string): [number, string] {
192192
const decimals = parseFloat(token.decimals) || 0;
193193
const _decimals = Math.pow(10, decimals);
194-
const tokenValue = Math.round(amount * _decimals);
194+
let tokenValue: number;
195+
switch (method) {
196+
case 'ceil':
197+
tokenValue = Math.ceil(amount * _decimals);
198+
break;
199+
case 'floor':
200+
tokenValue = Math.floor(amount * _decimals);
201+
break;
202+
case 'round':
203+
default:
204+
tokenValue = Math.round(amount * _decimals);
205+
}
195206
const tokenAmount = (tokenValue / _decimals).toFixed(decimals);
196207
return [tokenValue, tokenAmount];
197208
}

0 commit comments

Comments
 (0)