Skip to content

Commit f33008a

Browse files
authored
Merge pull request #62 from skalenetwork/fix-decimals-distributor
Fix payment wei value - credit distributor
2 parents ecbcb4a + 713fd5a commit f33008a

3 files changed

Lines changed: 7 additions & 3 deletions

File tree

credit-distributor/config.toml.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ state_file = 'state.json' # optional
66
[destination]
77
endpoint = 'https://example.com' # required - destination schain RPC
88
contract = '0x0' # required - Ledger contract address on destination schain
9+
credit_decimals = 18 # optional - decimals to scale credit count to native wei (default 18)
910

1011
# One [[sources]] entry per CreditStation deployment to monitor.
1112
# Each deployment must have been initialized with a unique on-chain `sourceId`

credit-distributor/src/configs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class Source(BaseModel):
4545
class Destination(BaseModel):
4646
endpoint: str
4747
contract: str
48+
credit_decimals: int = 18
4849

4950

5051
class Agent(BaseModel):

credit-distributor/src/main.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def distribute_credits_for_source(
9393
chunk_size=config.agent.events_chunk_size,
9494
)
9595
for event in all_events:
96-
fulfill_payment(source, event, schain_cs)
96+
fulfill_payment(source, event, schain_cs, config.destination.credit_decimals)
9797

9898
if all_events:
9999
state.from_blocks[source.name] = all_events[-1]['block_number'] + 1
@@ -106,13 +106,15 @@ def fulfill_payment(
106106
source: Source,
107107
event: PaymentReceivedEvent,
108108
schain_cs: SchainCreditStation,
109+
credit_decimals: int,
109110
) -> None:
110111
payment_id = event['payment_id']
111112
logger.info(f'[{source.name}] Checking payment: {payment_id}')
112113
is_fulfilled = schain_cs.ledger.is_fulfilled(payment_id)
113114
if not is_fulfilled:
114-
logger.info(f'[{source.name}] Fulfilling payment: {payment_id}')
115-
schain_cs.ledger.fulfill(payment_id, event['to_address'], value=event['value'])
115+
amount = event['value'] * (10 ** credit_decimals)
116+
logger.info(f'[{source.name}] Fulfilling payment: {payment_id} with amount {amount} wei')
117+
schain_cs.ledger.fulfill(payment_id, event['to_address'], value=amount)
116118
logger.info(f'[{source.name}] Payment {payment_id} fulfilled successfully.')
117119
else:
118120
logger.debug(f'[{source.name}] Payment {payment_id} is already fulfilled.')

0 commit comments

Comments
 (0)