Skip to content

Chainstate #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Django 区块链钱包应用
# 应用

```
pip install Django>=4.0.4,<5
# 构建本地数据库
python manage.py migrate
# 创建超级用户
Expand Down
5 changes: 2 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
Django>=4.0.4,<5

ecdsa>=0.13,<1
mnemonic>=0.19,<1
pysha3>=1.0.2,<2
base58>=2.0.1,<3
base58>=2.0.1,<3
requests>=2.27.1,<3
1 change: 1 addition & 0 deletions start.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python manage.py runserver
14 changes: 14 additions & 0 deletions wallet.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@
"explorer_url":"https://etherscan.io/"
}
},
{
"model":"wallet.rpc",
"fields":{
"created_at":"2022-05-21T03:11:20.347Z",
"updated_at":"2022-05-21T03:11:20.347Z",
"chain":2,
"company":"infura",
"alias":"test",
"endpoint":"https://mainnet.infura.io/v3/8a5284b35807498aa04f95fc580a76ec",
"username":null,
"password":null,
"auth":"NORMAL"
}
},
{
"model":"wallet.token",
"fields":{
Expand Down
Empty file added wallet/chainstate/__init__.py
Empty file.
15 changes: 15 additions & 0 deletions wallet/chainstate/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from django.contrib import admin

# Register your models here.
from wallet.chainstate.models import *

class RPCAdmin(admin.ModelAdmin):

list_display = ("chain","company","alias","endpoint","username","password","auth",)

class StateAdmin(admin.ModelAdmin):

list_display = ("address","balance","next_time","is_active","rpc","next_time")

admin.site.register(RPC,RPCAdmin)
admin.site.register(State,StateAdmin)
6 changes: 6 additions & 0 deletions wallet/chainstate/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class ChainstateConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'wallet.chainstate'
7 changes: 7 additions & 0 deletions wallet/chainstate/constant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.utils.translation import gettext_lazy as _


NORMAL="NORMAL"
AUTH_TYPE = (
(NORMAL, _("normal")),
)
53 changes: 53 additions & 0 deletions wallet/chainstate/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Generated by Django 4.0.4 on 2022-05-21 12:27

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
('wallet', '0009_remove_state_address_remove_state_rpc_delete_rpc_and_more'),
]

operations = [
migrations.CreateModel(
name='RPC',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_at', models.DateTimeField(auto_now_add=True, verbose_name='Created Time')),
('updated_at', models.DateTimeField(auto_now=True, verbose_name='Updated Time')),
('company', models.CharField(max_length=128, verbose_name='company')),
('alias', models.CharField(blank=True, max_length=128, null=True, verbose_name='alias')),
('endpoint', models.CharField(max_length=128, verbose_name='endpoint')),
('username', models.CharField(blank=True, max_length=128, null=True, verbose_name='username')),
('password', models.CharField(blank=True, max_length=128, null=True, verbose_name='password')),
('auth', models.CharField(choices=[('NORMAL', 'normal')], default='NORMAL', max_length=128, verbose_name='auth')),
('chain', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='wallet_rpc', to='wallet.chain')),
],
options={
'verbose_name': 'RPC',
'verbose_name_plural': 'RPC',
},
),
migrations.CreateModel(
name='State',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_at', models.DateTimeField(auto_now_add=True, verbose_name='Created Time')),
('updated_at', models.DateTimeField(auto_now=True, verbose_name='Updated Time')),
('balance', models.JSONField(verbose_name='balance')),
('next_time', models.DateTimeField(auto_now=True, help_text='When to run the next test', verbose_name='Created Time')),
('is_active', models.BooleanField(default=True, help_text='Whether it is necessary to detect the operating status of the address', verbose_name='active')),
('is_update', models.BooleanField(default=False, help_text='Status of address update', verbose_name='update')),
('address', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='wallet_state', to='wallet.address')),
('rpc', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='wallet_state', to='chainstate.rpc')),
],
options={
'verbose_name': 'State',
'verbose_name_plural': 'Status',
},
),
]
Empty file.
60 changes: 60 additions & 0 deletions wallet/chainstate/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from django.db import models
from django.utils.translation import gettext_lazy as _

# Create your models here.
from wallet.models import Chain,CreateUpdateTracker,Address
from wallet.chainstate.constant import *

class RPC(CreateUpdateTracker):
chain = models.ForeignKey(Chain, related_name='wallet_rpc', on_delete=models.CASCADE)
company = models.CharField(verbose_name=_("company"),max_length=128)
alias = models.CharField(verbose_name=_("alias"),max_length=128,blank=True,null=True,)
endpoint = models.CharField(verbose_name=_("endpoint"),max_length=128)
username = models.CharField(verbose_name=_("username"),max_length=128,blank=True,null=True,)
password = models.CharField(verbose_name=_("password"),max_length=128,blank=True,null=True,)
auth = models.CharField(verbose_name=_("auth"),max_length=128,choices=AUTH_TYPE,default=NORMAL)

def __str__(self):
return f"{self.alias}-[{self.company}]"

class Meta:
verbose_name = _('RPC')
verbose_name_plural = _('RPC')


class StateManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(is_active=True)

class State(CreateUpdateTracker):
objects = StateManager()

address = models.ForeignKey(Address, related_name="wallet_state",on_delete=models.CASCADE)
balance = models.JSONField(verbose_name=_('balance'))
next_time = models.DateTimeField(
_("Created Time"),editable=False,
auto_now=True,
help_text=_(
"When to run the next test"
)
)
is_active = models.BooleanField(
verbose_name=_('active'),default=True,
help_text=_(
"Whether it is necessary to detect the operating status of the address"
)
)
is_update = models.BooleanField(
verbose_name=_('update'),default=False,
help_text=_(
"Status of address update"
)
)
rpc = models.ForeignKey(RPC, related_name="wallet_state", on_delete=models.CASCADE)

def __str__(self):
return f"{self.is_update}"

class Meta:
verbose_name = _('State')
verbose_name_plural = _('Status')
3 changes: 3 additions & 0 deletions wallet/chainstate/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
3 changes: 3 additions & 0 deletions wallet/chainstate/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.
3 changes: 2 additions & 1 deletion wallet/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
ADDRESS_TYPE = (
(DEPOSIT,_("deposit")),
(SYSTEM_LEASE,_("system lease")),
)
)

4 changes: 1 addition & 3 deletions wallet/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def generate_address(user, chain_symbol,index:int=None, type=None, new_address=T
chain=chain,
public_key= hdwallet.xpublic_key(),
)
# user = pubkey.user

# 确认最新的地址下标
if index is None:
Expand All @@ -50,7 +49,6 @@ def generate_address(user, chain_symbol,index:int=None, type=None, new_address=T
if new_address:
index = index + 1
except Address.DoesNotExist as e:
# index=0
pass

# 若已经存在该地址则返回,没有则创建
Expand All @@ -72,4 +70,4 @@ def generate_address(user, chain_symbol,index:int=None, type=None, new_address=T
address=address
)
except Exception as e:
logger.error(f"wallet.generate_address:{e.args}")
logger.error(msg="Exception while generating wallet address:", exc_info=e)
10 changes: 5 additions & 5 deletions wallet/management/commands/create_deposit_address.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@

import os
# os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'lender.settings')

from django.core.management.base import BaseCommand

from logging import getLogger
logger = getLogger(__name__)


class Command(BaseCommand):
help = "Create Deposit Address"
help = """
Create Deposit Address:

python manage.py create_deposit_address -u [username or uid]
"""

def add_arguments(self, parser):
self.username_help = 'User Unique username (Required)'
Expand Down
23 changes: 23 additions & 0 deletions wallet/management/commands/runchainstate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from django.core.management.base import BaseCommand

from logging import getLogger
logger = getLogger(__name__)


class Command(BaseCommand):
help = """
Create Deposit Address:

python manage.py create_deposit_address -u [username or uid]
"""

def add_arguments(self, parser):
pass

def handle(self, *args, **options):
pass





51 changes: 51 additions & 0 deletions wallet/migrations/0003_rpc_state.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Generated by Django 4.0.4 on 2022-05-21 02:24

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('wallet', '0002_alter_address_chain_alter_pubkey_chain_and_more'),
]

operations = [
migrations.CreateModel(
name='RPC',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_at', models.DateTimeField(auto_now_add=True, verbose_name='Created Time')),
('updated_at', models.DateTimeField(auto_now=True, verbose_name='Updated Time')),
('company', models.CharField(max_length=128, verbose_name='company')),
('alias', models.CharField(blank=True, max_length=128, null=True, verbose_name='alias')),
('endpoint', models.CharField(max_length=128, verbose_name='endpoint')),
('username', models.CharField(blank=True, max_length=128, null=True, verbose_name='username')),
('password', models.CharField(blank=True, max_length=128, null=True, verbose_name='password')),
('auth', models.CharField(max_length=128, verbose_name='auth')),
('chain', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='wallet_rpc', to='wallet.chain')),
],
options={
'verbose_name': 'RPC',
'verbose_name_plural': 'RPC',
},
),
migrations.CreateModel(
name='State',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_at', models.DateTimeField(auto_now_add=True, verbose_name='Created Time')),
('updated_at', models.DateTimeField(auto_now=True, verbose_name='Updated Time')),
('balance', models.JSONField(verbose_name='balance')),
('next_time', models.DateTimeField(auto_now_add=True, verbose_name='Created Time')),
('is_active', models.BooleanField(default=False, verbose_name='active')),
('address', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='wallet_state', to='wallet.address')),
('rpc', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='wallet_state', to='wallet.rpc')),
('token', models.ManyToManyField(related_name='wallet_state', to='wallet.token')),
],
options={
'ordering': ('-created_at',),
'abstract': False,
},
),
]
18 changes: 18 additions & 0 deletions wallet/migrations/0004_alter_rpc_auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.0.4 on 2022-05-21 03:09

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('wallet', '0003_rpc_state'),
]

operations = [
migrations.AlterField(
model_name='rpc',
name='auth',
field=models.CharField(choices=[('NORMAL', 'normal')], default='NORMAL', max_length=128, verbose_name='auth'),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 4.0.4 on 2022-05-21 03:26

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('wallet', '0004_alter_rpc_auth'),
]

operations = [
migrations.AddField(
model_name='state',
name='is_update',
field=models.BooleanField(default=False, help_text='Status of address update', verbose_name='update'),
),
migrations.AlterField(
model_name='state',
name='is_active',
field=models.BooleanField(default=True, help_text='Whether it is necessary to detect the operating status of the address', verbose_name='active'),
),
migrations.AlterField(
model_name='state',
name='next_time',
field=models.DateTimeField(editable=False, help_text='When to run the next test', verbose_name='Created Time'),
),
]
17 changes: 17 additions & 0 deletions wallet/migrations/0006_remove_state_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.0.4 on 2022-05-21 03:28

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('wallet', '0005_state_is_update_alter_state_is_active_and_more'),
]

operations = [
migrations.RemoveField(
model_name='state',
name='token',
),
]
Loading