-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathworld-config.js
More file actions
71 lines (63 loc) · 2.91 KB
/
Copy pathworld-config.js
File metadata and controls
71 lines (63 loc) · 2.91 KB
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
// GET /api/bnb/world-config?network=testnet|mainnet
// ---------------------------------------------------------------------------
// Public (non-secret) config for prompt 16's on-chain presence mode
// (src/agora/onchain-presence.js). A deployed contract address is not a
// secret — only the deployer key is — so this is a plain cached GET, no auth,
// mirroring api/bnb/babt-check.js's shape. Exists so the browser never needs
// its own copy of WORLD_MOVES_ADDRESS_TESTNET/MAINNET (which live in the
// SERVER's env, unreachable from client code) and so flipping the env var the
// moment a real public deploy lands (contracts/DEPLOYMENTS.md) takes effect
// for every open browser tab without a frontend rebuild.
//
// Response: { network, chainId, address, deployed, explorer, rpcs, worldId }
// `address: null, deployed: false` is a normal, honest state (00-CONTEXT:
// never fabricate a live-looking address) — the client renders "not deployed
// yet" and never fires a wallet prompt for a contract that doesn't exist.
import { cors, json, method, wrap, error, rateLimited } from '../_lib/http.js';
import { limits, clientIp } from '../_lib/rate-limit.js';
import { BNB_CHAINS } from '../_lib/bnb/chains.js';
import { worldMovesAddress, WorldMovesError } from '../_lib/bnb/world-moves.js';
// Single logical "world" for the Agora Commons on-chain presence layer today
// (see src/agora/onchain-presence.js) — multi-world routing is out of scope
// for this campaign; bumping this would fragment presence for every existing
// player, so treat it as a stable public constant, not a per-request input.
const COMMONS_WORLD_ID = 1;
function normalizeNetworkParam(raw) {
const v = String(raw || '').trim().toLowerCase();
if (v === 'testnet' || v === '97' || v === 'bsctestnet') return 'bscTestnet';
if (v === '' || v === 'mainnet' || v === '56' || v === 'bscmainnet') return 'bscMainnet';
return null;
}
export default wrap(async (req, res) => {
if (cors(req, res, { methods: 'GET,OPTIONS', origins: '*' })) return;
if (!method(req, res, ['GET'])) return;
const rl = await limits.publicIp(clientIp(req));
if (!rl.success) return rateLimited(res, rl);
const params = new URL(req.url, 'http://x').searchParams;
const network = normalizeNetworkParam(params.get('network'));
if (network === null) {
return error(res, 400, 'bad_request', `unknown network "${params.get('network')}" — use "mainnet" or "testnet"`);
}
const meta = BNB_CHAINS[network];
let address = null;
try {
address = worldMovesAddress(network);
} catch (err) {
if (!(err instanceof WorldMovesError)) throw err;
// no_deployment is the expected, honest state until a public deploy lands.
}
return json(
res,
200,
{
network,
chainId: meta.id,
address,
deployed: !!address,
explorer: meta.explorer,
rpcs: meta.rpcs,
worldId: COMMONS_WORLD_ID,
},
{ 'cache-control': 'public, max-age=30, s-maxage=60, stale-while-revalidate=300' },
);
});