Skip to content

Commit

Permalink
Fix wrong type of user_id when binding to SQL, open the flag of `pe…
Browse files Browse the repository at this point in the history
…rsist_to_db`, and add a test case.
  • Loading branch information
silathdiir committed Nov 9, 2021
1 parent 3e0fa61 commit be7f6f8
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 10 deletions.
1 change: 1 addition & 0 deletions .github/workflows/integration-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ jobs:
- name: Run trading tests
run: |
cd ./examples/js/
npx ts-node rest_get_user.ts
npx ts-node trade.ts
sleep 5
npx ts-node print_orders.ts
Expand Down
4 changes: 2 additions & 2 deletions examples/js/RESTClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class RESTClient {
});
}

async get_user_by_addr(addr: string): Promise<UserInfo> {
let resp = await this.client.get(`/user/${addr}`);
async get_user(user_id_l1_addr_or_l2_pubkey: string): Promise<UserInfo> {
let resp = await this.client.get(`/user/${user_id_l1_addr_or_l2_pubkey}`);
//console.log('user info', resp.data);
if (resp.data.error) {
console.log("error:", resp.data);
Expand Down
4 changes: 2 additions & 2 deletions examples/js/bots/run_bots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async function initUser(): Promise<number> {
const acc = Account.fromMnemonic(mnemonic3);
//console.log('acc is', acc);
const restClient = defaultRESTClient;
let userInfo = await restClient.get_user_by_addr(acc.ethAddr);
let userInfo = await restClient.get_user(acc.ethAddr);
if (userInfo == null) {
// register
console.log("register new user");
Expand All @@ -41,7 +41,7 @@ async function initUser(): Promise<number> {
const t = Date.now();
console.log("register resp", resp);
await sleep(2000); // FIXME
userInfo = await restClient.get_user_by_addr(acc.ethAddr);
userInfo = await restClient.get_user(acc.ethAddr);
await sleep(2000); // FIXME
await depositAssets({ USDT: "10000.0" }, userInfo.id);
} else {
Expand Down
58 changes: 58 additions & 0 deletions examples/js/rest_get_user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Account } from "fluidex.js";
import { defaultClient as grpcClient } from "./client";
import { defaultRESTClient as restClient } from "./RESTClient";
import { sleep } from "./util";
import { strict as assert } from "assert";

async function initUser() {
console.log("initUser BEGIN");

const mnemonic =
"sound select report rug run cave provide index grief foster bar someone garage donate nominee crew once oil sausage flight tail holiday style afford";
const account = Account.fromMnemonic(mnemonic);
const userInfo = {
user_id: 0,
l1_address: account.ethAddr.toLowerCase(),
l2_pubkey: account.bjjPubKey.toLowerCase(),
};
if (!(await restClient.get_user(userInfo.l1_address))) {
await grpcClient.registerUser(userInfo);
await sleep(2000);
}

console.log("initUser END");

return userInfo;
}

async function testGetUser(userInfo) {
console.log("test get user by l1 address");
let userResult = await restClient.get_user(userInfo.l1_address);
assert.equal(userInfo.l1_address, userResult.l1_address);
assert.equal(userInfo.l2_pubkey, userResult.l2_pubkey);
userInfo.user_id = userResult.id;

console.log("test get user by l2 public key");
userResult = await restClient.get_user(userInfo.l2_pubkey);
assert.equal(userInfo.l1_address, userResult.l1_address);
assert.equal(userInfo.l2_pubkey, userResult.l2_pubkey);
assert.equal(userInfo.user_id, userResult.id);

console.log("test get user by id");
userResult = await restClient.get_user(userInfo.user_id.toString());
assert.equal(userInfo.l1_address, userResult.l1_address);
assert.equal(userInfo.l2_pubkey, userResult.l2_pubkey);
assert.equal(userInfo.user_id, userResult.id);
}

async function main() {
try {
const userInfo = await initUser();
await testGetUser(userInfo);
} catch (error) {
console.error("Caught error:", error);
process.exit(1);
}
}

main();
2 changes: 1 addition & 1 deletion src/matchengine/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl OperationLogConsumer for OperationLogSender {
fn create_persistor(settings: &config::Settings) -> Box<dyn PersistExector> {
let persist_to_mq = true;
let persist_to_mq_full_order = true;
let persist_to_db = false;
let persist_to_db = true;
let persist_to_file = false;
let mut persistor = Box::new(CompositePersistor::default());
if !settings.brokers.is_empty() && persist_to_mq {
Expand Down
15 changes: 10 additions & 5 deletions src/restapi/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ pub async fn get_user(req: HttpRequest, data: web::Data<AppState>) -> Result<Jso
return Ok(Json(user_info.clone()));
}

let sql_query = format!("select * from {} where id = $1 OR l1_address = $1 OR l2_pubkey = $1", ACCOUNT);
let user: AccountDesc = sqlx::query_as(&sql_query).bind(user_id).fetch_one(&data.db).await.map_err(|e| {
log::error!("{:?}", e);
RpcError::bad_request("invalid user ID, l1 address or l2 public key")
})?;
let sql_query = format!("select * from {} where id = $1 OR l1_address = $2 OR l2_pubkey = $2", ACCOUNT);
let user: AccountDesc = sqlx::query_as(&sql_query)
.bind(user_id.parse::<i32>().unwrap_or(-1))
.bind(user_id)
.fetch_one(&data.db)
.await
.map_err(|e| {
log::error!("{:?}", e);
RpcError::bad_request("invalid user ID, l1 address or l2 public key")
})?;

let user_info = AccountDesc {
id: user.id,
Expand Down

0 comments on commit be7f6f8

Please sign in to comment.