-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
123 lines (108 loc) · 3.53 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// ==== CONFIG ====
// get your credentials here from your Orderly admin wallet:
// https://orderlynetwork.github.io/broker-registration/
// Your Orderly account ID from the admin wallet
const adminAddress = "your-admin-evm-or-sol-address-here";
// Your Orderly secret key from the admin wallet
const orderlyAdminSecret = "ed25519:your-admin-secret-key-here";
// Your broker ID
const brokerId = "your-broker-id-here";
// set your fees here. Fees are set as decimal numbers,
// e.g. 0.0001 => 0.01% fee
// 0.00005 => 0.005% fee etc
// you cannot go below 0.03% taker fee, because this is what Orderly charges
// the maker fee rate you want to set for the list of accounts
const makerFeeRate = 0;
// the taker fee rate you want to set for the list of accounts
const takerFeeRate = 0.0003;
// the list of addresses as array of strings
// you can add/delete addresses as you see fit
const addresses = [
"0xfirst-address-here",
"0xsecond-address-here",
"0xthird-address-here",
// ...
];
// network: "testnet" or "mainnet"
const network = "testnet";
// ==== DO NOT CHANGE ANYTHING BELOW ====
import { getPublicKeyAsync, signAsync } from "@noble/ed25519";
import bs58 from "bs58";
const baseUrl =
network === "testnet"
? "https://testnet-api-evm.orderly.org"
: "https://api-evm.orderly.org";
async function main() {
const orderlyKey = bs58.decode(orderlyAdminSecret.substring(8));
const res = await signAndSendRequest(
await getAccountId(adminAddress, brokerId),
orderlyKey,
`${baseUrl}/v1/broker/fee_rate/set`,
{
method: "POST",
body: JSON.stringify({
maker_fee_rate: makerFeeRate,
taker_fee_rate: takerFeeRate,
account_ids: await Promise.all(
addresses.map((address) => getAccountId(address, brokerId))
),
}),
}
);
const response = await res.json();
console.log(JSON.stringify(response, undefined, 2));
}
main();
async function signAndSendRequest(
orderlyAccountId: string,
privateKey: Uint8Array | string,
input: URL | string,
init?: RequestInit | undefined
): Promise<Response> {
const timestamp = Date.now();
const encoder = new TextEncoder();
const url = new URL(input);
let message = `${String(timestamp)}${init?.method ?? "GET"}${url.pathname}${
url.search
}`;
if (init?.body) {
message += init.body;
}
const orderlySignature = await signAsync(encoder.encode(message), privateKey);
return fetch(input, {
headers: {
"Content-Type":
init?.method !== "GET" && init?.method !== "DELETE"
? "application/json"
: "application/x-www-form-urlencoded",
"orderly-timestamp": String(timestamp),
"orderly-account-id": orderlyAccountId,
"orderly-key": `ed25519:${bs58.encode(
await getPublicKeyAsync(privateKey)
)}`,
"orderly-signature": Buffer.from(orderlySignature).toString("base64url"),
...(init?.headers ?? {}),
},
...(init ?? {}),
});
}
async function getAccountId(address: string, brokerId: string) {
const isEvm = isEvmAddress(address);
const isSol = isSolAddress(address);
if (!isEvm && !isSol) {
throw new Error("Invalid address format");
}
const res = await fetch(
`${baseUrl}/v1/get_account?address=${address}&broker_id=${brokerId}${
isSol ? "&chain_type=SOL" : ""
}`
);
const response = await res.json();
return response.data.account_id as string;
}
function isEvmAddress(address: string) {
return address.match(/^0x[0-9a-fA-F]{40}$/);
}
function isSolAddress(address: string) {
return address.match(/^[0-9a-zA-Z]{44}$/);
}