读取用户转账记录

在某些场景下,开发者需要知道用户的转账记录。但是不像mixin messenger的机器人可以收到资产变更的记录,mixin网络的账号不会收到转账通知。为了读取用户的转账记录,开发者需要读取mixin network的浏览器记录,如果记录中的收款账户或者发款账户是开发者创建的,那么该记录会包含更详细信息,包括memo,trace id以及。否则该信息仅仅包含转账金额,时间,以及资产类型。

// cURL Example
curl -i --header "Authorization: Bearer eyJhbGciOiJSUzUxMiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1Mjc1ODYyODQsImlhdCI6MTUyNzU4NjIyNCwianRpIjoiMjZlMjQyM2QtZGUzMC00MTA0LTkyZTQtOTk2MzczOWRkZGE5Iiwic2lkIjoiYWM2ZDFmODYtYTY0Yi00NWRkLTllZmEtN2JmMGVjZjI2MDU2Iiwic2lnIjoiMGIxNGJlZTU5YjE1ODU0MjI1ZTc5ZTU4ZDQwMjZkNDJhYWUyY2Q4ODM4OWE1N2RhNjU4YTRlMjVhNzJlNjRlZSIsInVpZCI6IjMxYjFhMTdjLWFiMzgtNGFhNC05YmM5LWY0NjQyNzEyODExMyJ9.0OUDLd0E1SKslsBJ5nHDE3bC9XKQc_6PPSqBD6Z2E9XYMjQyGht3QWF-uQLohCwbtR_Q7w3_my5MoWM4UyHtWlYh8-mJwg54VFWlhLuFLcWTeG8P971WGVc8oOqNspsEnxDxdBezQVqF1N-XjUtJsVsyJkT6ZEX7VazRm2I2xMM" --header "Content-Type: application/json" --header "Content-length: 0" "https://api.mixin.one/network/snapshots?limit=10&offset=2018-05-29T16:30:24.845515732%2B08:00"
// Sample Response
{
"data":[
{
"amount":"-1688168",
"asset":{
"asset_id":"965e5c6e-434c-3fa9-b780-c50f43cd955c",
"chain_id":"43d61dcd-e413-450d-80b8-101d5e903357",
"icon_url":"https://images.mixin.one/0sQY63dDMkWTURkJVjowWY6Le4ICjAFuu3ANVyZA4uI3UdkbuOT5fjJUT82ArNYmZvVcxDXyNjxoOv0TAYbQTNKS=s128",
"name":"Chui Niu Bi",
"symbol":"CNB",
"type":"asset"
},
"created_at":"2018-05-29T09:31:04.202186212Z",
"data":"",
"snapshot_id":"529934b0-abfd-43ab-9431-1805773000a4",
"source":"TRANSFER_INITIALIZED",
"type":"snapshot"
// Options only for user (or App) who has access.
"user_id":"06aed1e3-bd77-4a59-991a-5bb5ae6fbb09",
"trace_id":"7c67e8e8-b142-488b-80a3-61d4d29c90bf",
"opponent_id":"a465ffdb-4441-4cb9-8b45-00cf79dfbc46",
"data":"Transfer!"
},
...
]
}
Go-lang-function-to-request-snapshot
func (ex *Exchange) requestMixinNetwork(ctx context.Context, checkpoint time.Time, limit int) ([]*Snapshot, error) {
uri := fmt.Sprintf("/network/snapshots?offset=%s&order=ASC&limit=%d", checkpoint.Format(time.RFC3339Nano), limit)
token, err := bot.SignAuthenticationToken(config.ClientId, config.SessionId, config.SessionKey, "GET", uri, "")
if err != nil {
return nil, err
}
body, err := bot.Request(ctx, "GET", uri, nil, token)
if err != nil {
return nil, err
}
var resp struct {
Data []*Snapshot `json:"data"`
Error string `json:"error"`
}
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, err
}
if resp.Error != "" {
return nil, errors.New(resp.Error)
}
return resp.Data, nil
}
Python code to read snapshots
def searchSnapShots(robot, config, offset, limit, order):
finalURL = "/network/snapshots?offset=%s&order=ASC&limit=%d" % (offset, limit)
encoded = robot.genGETJwtToken_extConfig(finalURL, "" , config)
request_header = {"Authorization":"Bearer " + encoded, 'Content-Type': 'application/json', 'Content-length': '0'}
r = requests.get('https://api.mixin.one' + finalURL, headers = request_header)
if r.status_code != 200:
error_body = result_obj['error']
print(error_body)
r.raise_for_status()
result_obj = r.json()
snapshots = result_obj["data"]
for singleSnapShot in snapshots:
if "user_id" in singleSnapShot:
print(singleSnapShot)
print("This snapshot happen to an account")
return snapshots