Skip to content

feat: more robust createOrderlyKey #6

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 3 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
28 changes: 17 additions & 11 deletions packages/core/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,11 @@ export class Account {
);
}

async createOrderlyKey(expiration?: number): Promise<any> {
async createOrderlyKey(
expiration?: number,
scope: string = "trading,read",
shouldMutateAppState: boolean = true
): Promise<any> {
if (this.stateValue.accountId === undefined) {
throw new Error("account id is undefined");
}
Expand All @@ -426,6 +430,7 @@ export class Account {
expiration,
brokerId: this.configStore.get("brokerId"),
timestamp,
scope,
});

const address = this.stateValue.address;
Expand Down Expand Up @@ -455,17 +460,18 @@ export class Account {
//

if (res.success) {
this.keyStore.setKey(address, keyPair);
const nextState = {
...this.stateValue,
status: AccountStatusEnum.EnableTrading,
// accountId: res.data.account_id,
// userId: res.data.user_id,
};

this._ee.emit("change:status", nextState);
if (shouldMutateAppState) {
this.keyStore.setKey(address, keyPair);
const nextState = {
...this.stateValue,
status: AccountStatusEnum.EnableTrading,
// accountId: res.data.account_id,
// userId: res.data.user_id,
};

return res;
this._ee.emit("change:status", nextState);
}
return { publicKey, secretKey: keyPair.secretKey, expiration, scope };
} else {
throw new Error(res.message);
}
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export function generateAddOrderlyKeyMessage(inputs: {
primaryType: keyof typeof definedTypes;
expiration?: number;
timestamp?: number;
scope?: string;
}) {
const {
publicKey,
Expand All @@ -94,13 +95,14 @@ export function generateAddOrderlyKeyMessage(inputs: {
brokerId,
expiration = 365,
timestamp = Date.now(),
scope = "read,trading",
} = inputs;
// const now = Date.now();
// message;
const message = {
brokerId,
orderlyKey: publicKey,
scope: "read,trading",
scope,
chainId,
timestamp,
expiration: timestamp + 1000 * 60 * 60 * 24 * expiration,
Expand Down
12 changes: 10 additions & 2 deletions packages/hooks/src/useAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,16 @@ export const useAccount = () => {
// );

const createOrderlyKey = useCallback(
async (remember: boolean) => {
return account.createOrderlyKey(remember ? 365 : 30);
async (
remember: boolean,
scope?: string,
shouldMutateAppState: boolean = true
) => {
return account.createOrderlyKey(
remember ? 365 : 30,
scope,
shouldMutateAppState
);
},
[account]
);
Expand Down