Skip to content
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

[R4R]: Feature/airdrop grpc server #29

Merged
merged 4 commits into from
Mar 13, 2024
Merged
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
10 changes: 7 additions & 3 deletions airdrop/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by Django 4.1.1 on 2024-03-03 03:11


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

Expand All @@ -19,7 +20,9 @@ class Migration(migrations.Migration):
('uuid', models.CharField(blank=True, max_length=100, null=True, unique=True)),
('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
('updated_at', models.DateTimeField(auto_now=True, db_index=True)),
('name', models.CharField(default='unknown', max_length=100, unique=True, verbose_name='用户名')),
('name', models.CharField(default='unknown', max_length=100, verbose_name='用户名')),
('invite_code', models.CharField(default='0000-0000-0000', max_length=100, unique=True, verbose_name='邀请码')),
('invite_me_uuid', models.CharField(default='0000-0000-0000', max_length=100, verbose_name='邀请人')),
('photo', models.ImageField(blank=True, null=True, upload_to='symbol/%Y/%m/%d/')),
('address', models.CharField(max_length=100, unique=True, verbose_name='用户地址')),
('email', models.EmailField(blank=True, max_length=254, null=True)),
Expand All @@ -41,8 +44,9 @@ class Migration(migrations.Migration):
('uuid', models.CharField(blank=True, max_length=100, null=True, unique=True)),
('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
('updated_at', models.DateTimeField(auto_now=True, db_index=True)),
('address', models.CharField(max_length=100, unique=True, verbose_name='用户地址')),
('type', models.CharField(choices=[('BridgeTransfer', 'BridgeTransfer'), ('BridgeStaking', 'BridgeStaking')], default='BridgeTransfer', max_length=100, verbose_name='交易类别')),
('address', models.CharField(max_length=100, verbose_name='用户地址')),
('type', models.CharField(choices=[('Invite', 'Invite'), ('BridgeTransfer', 'BridgeTransfer'), ('BridgeStaking', 'BridgeStaking')], default='BridgeTransfer', max_length=100, verbose_name='交易类别')),

('points', models.PositiveIntegerField(default=0, verbose_name='积分数量')),
('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='airdrop_user', to='airdrop.airdropuser', verbose_name='收藏的商家')),
],
Expand Down
18 changes: 15 additions & 3 deletions airdrop/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,28 @@
from common.models import BaseModel, Asset


TypeChoice = [(x, x) for x in ['BridgeTransfer', 'BridgeStaking']]
TypeChoice = [(x, x) for x in ['Invite', 'BridgeTransfer', 'BridgeStaking']]


class AirdropUser(BaseModel):
name = models.CharField(
default="unknown",
max_length=100,
unique=True,
unique=False,
verbose_name='用户名'
)
invite_code = models.CharField(
default="0000-0000-0000",
max_length=100,
unique=True,
verbose_name='邀请码'
)
invite_me_uuid = models.CharField(
default="0000-0000-0000",
max_length=100,
unique=False,
verbose_name='邀请人'
)
photo = models.ImageField(
upload_to='symbol/%Y/%m/%d/',
blank=True,
Expand Down Expand Up @@ -92,7 +104,7 @@ class PointsRecord(BaseModel):
)
address = models.CharField(
max_length=100,
unique=True,
unique=False,
verbose_name='用户地址'
)
type = models.CharField(
Expand Down
111 changes: 111 additions & 0 deletions airdrop_api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# 空投接口

## 1. 获取邀请码

接口请求
```
curl --location 'http://127.0.0.1:8000/api/get_invite_code_by_address' \
--header 'Content-Type: application/json' \
--data '{
"address": "0xe3b4ECd2EC88026F84cF17fef8bABfD9184C94F0"
}'
```
返回值

```
{
"ok": true,
"code": 200,
"result": {
"invite_code": "0000-0000-0000"
}
}
```


## 2. 提交邀请信息

接口请求
```
curl --location 'http://127.0.0.1:8000/api/submit_invite_info' \
--header 'Content-Type: application/json' \
--data '{
"invite_code": "0000-0000-0000",
"address": "0xe3b4ECd2EC88026F84cF17fef8bABfD9184C94F1"
}'
```
返回值

```
{
"ok": true,
"code": 200,
"result": {}
}
```


## 3. 获取个人信息和积分

接口请求
```
curl --location 'http://127.0.0.1:8000/api/get_points_by_address' \
--header 'Content-Type: application/json' \
--data '{
"address": "0xe3b4ECd2EC88026F84cF17fef8bABfD9184C94F1"
}'
```
返回值

```
{
"ok": true,
"code": 200,
"result": {
"id": 5,
"name": "unknown",
"photo": "",
"address": "0xe3b4ECd2EC88026F84cF17fef8bABfD9184C94F1",
"email": null,
"points": 0,
"x_twitter": "",
"discord": "",
"telegram": "",
"info": ""
}
}
```


## 4. 根据地址获取积分记录

接口请求
```
curl --location 'http://127.0.0.1:8000/api/get_points_record_by_address' \
--header 'Content-Type: application/json' \
--data '{
"address": "0xe3b4ECd2EC88026F84cF17fef8bABfD9184C94F0"
}'
```
返回值

```
{
"ok": true,
"code": 200,
"result": {
"total": 1,
"points": [
{
"id": 1,
"name": "0xe3b4ECd2EC88026F84cF17fef8bABfD9184C94F0",
"type": "BridgeTransfer",
"points": 1
}
]
}
}
```



56 changes: 52 additions & 4 deletions api/airdrop/api_v1.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,63 @@
#encoding=utf-8
# encoding=utf-8

import json
import uuid


from common.helpers import (
ok_json,
error_json
)
from airdrop.models import (
AirdropUser,
PointsRecord
AirdropUser,
PointsRecord
)


# @check_api_token
def get_invite_code_by_address(request):
params = json.loads(request.body.decode())
address = params.get("address", None)
if address is not None:
airdrop_user = AirdropUser.objects.filter(address=address).first()
if airdrop_user is not None:
data = {
"invite_code": airdrop_user.invite_code,
}
return ok_json(data)
else:
return error_json("address is not exist", 4000)
else:
return error_json("address is none", 4000)


# @check_api_token
def submit_invite_info(request):
params = json.loads(request.body.decode())
address = params.get("address", None)
invite_code = params.get("invite_code", None)
if address is None or invite_code is None:
return error_json("address or invite_code params is empty", 4000)
invite_user = AirdropUser.objects.filter(invite_code=invite_code).first()
if invite_user is None:
return error_json("This user is not exist", 4000)
AirdropUser.objects.create(
invite_code=uuid.uuid4(),
invite_me_uuid=invite_user.uuid,
address=address
)
if invite_user.points < 10:
invite_user.points = invite_user.points + 2
invite_user.save()
PointsRecord.objects.create(
user=invite_user,
address=invite_user.address,
type='Invite',
points=2
)
return ok_json({})


# @check_api_token
def get_points_by_address(request):
params = json.loads(request.body.decode())
Expand All @@ -22,6 +70,7 @@ def get_points_by_address(request):
else:
return error_json("No this user address points", 4000)


# @check_api_token
def get_points_record_by_address(request):
params = json.loads(request.body.decode())
Expand All @@ -43,4 +92,3 @@ def get_points_record_by_address(request):
}
return ok_json(data)


4 changes: 4 additions & 0 deletions api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
from api.airdrop.api_v1 import (
get_points_by_address,
get_points_record_by_address,
get_invite_code_by_address,
submit_invite_info
)

urlpatterns = [
Expand Down Expand Up @@ -105,6 +107,8 @@
path(r'get_tokens', get_tokens, name='get_tokens'),

# airdrop
path(r'get_invite_code_by_address', get_invite_code_by_address, name='get_invite_code_by_address'),
path(r'submit_invite_info', submit_invite_info, name='submit_invite_info'),
path(r'get_points_by_address', get_points_by_address, name='get_points_by_address'),
path(r'get_points_record_by_address', get_points_record_by_address, name='get_points_record_by_address'),
]
Loading