update payout logic

migrate-new-models
lza_menace 2 years ago
parent 159504f703
commit 90377f8d0a

@ -34,15 +34,19 @@ class User(Model):
def get_wow_received(self):
tips = TipReceived.select().join(Post).where(Post.user == self)
return sum(tip.amount for tip in tips)
return sum([tip.amount for tip in tips])
def get_wow_sent(self):
tips = TipSent.select().where(TipSent.from_user == self)
return sum(tip.amount for tip in tips) + sum(tip.fee for tip in tips)
return sum([tip.amount + tip.fee for tip in tips])
def get_post_count(self):
posts = Post.select().where(Post.user == self)
return posts.count()
def get_post_addresses(self):
posts = Post.select().where(Post.user == self)
return [i.address_index for i in posts]
class Meta:
database = db
@ -157,7 +161,8 @@ class Post(Model):
'approved_by': self.approved_by,
'received_wow': self.get_wow_received(),
'hours_elapsed': self.hours_elapsed(),
'tips_received': [tip for tip in TipReceived.select().where(TipReceived.post == self)]
'user_tips_received': wownero.from_atomic(self.user.get_wow_received()),
'user_tips_sent': wownero.from_atomic(self.user.get_wow_sent())
}
class Meta:

@ -86,6 +86,7 @@ def process_tips():
post.id, post.user.username
))
@bp.cli.command('payout_users')
def payout_users():
wallet = wownero.Wallet()
@ -93,15 +94,53 @@ def payout_users():
print('Wallet balances are {} locked, {} unlocked'.format(
wownero.from_atomic(balances[0]), wownero.from_atomic(balances[1])
))
for user in User.select():
for user in User.select().join(Post, on=Post.user).distinct().order_by(User.id.asc()):
rcvd = user.get_wow_received()
sent = user.get_wow_sent()
if rcvd == 0:
continue
to_send = rcvd - sent
if to_send >= 1:
if to_send >= wownero.to_atomic(.5):
print('{} has received {} atomic WOW but sent {} atomic WOW. Sending {} atomic WOW'.format(
user.username, wownero.from_atomic(rcvd),
wownero.from_atomic(sent), wownero.from_atomic(to_send)
))
if balances[1] >= to_send:
tx_data = {
'account_index': config.WALLET_ACCOUNT,
'destinations': [{
'address': user.address,
'amount': to_send
}],
'subaddress_indices': user.get_post_addresses(),
'priority': 0,
'unlock_time': 0,
'get_tx_key': True,
'do_not_relay': True,
'ring_size': 22
}
transfer = wallet.make_wallet_rpc('transfer', tx_data)
print(transfer)
if 'code' in transfer:
return
tx_data['destinations'][0]['amount'] = to_send - transfer['fee']
tx_data['do_not_relay'] = False
transfer = wallet.make_wallet_rpc('transfer', tx_data)
print(tx_data)
print(transfer)
if 'code' in transfer:
return
TipSent.create(
from_user=user,
to_user=user,
txid=transfer['tx_hash'],
timestamp=datetime.utcnow(),
amount=transfer['amount'],
fee=transfer['fee'],
)
print(f'Sent tip of {wownero.from_atomic(transfer["amount"])} WOW to {user} in tx_hash {transfer["tx_hash"]}')
wallet.make_wallet_rpc('store')
@bp.cli.command('fix_image')
@click.argument('post_id')

Loading…
Cancel
Save