@mysten/sui
Version:
Sui TypeScript API
1 lines • 3.13 kB
Source Map (JSON)
{"version":3,"file":"faucet.mjs","names":[],"sources":["../../src/faucet/faucet.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nexport class FaucetRateLimitError extends Error {}\n\ntype FaucetCoinInfo = {\n\tamount: number;\n\tid: string;\n\ttransferTxDigest: string;\n};\n\ntype FaucetResponseV2 = {\n\tstatus: 'Success' | FaucetFailure;\n\tcoins_sent: FaucetCoinInfo[] | null;\n};\n\ntype FaucetFailure = {\n\tFailure: {\n\t\tinternal: string;\n\t};\n};\n\ntype FaucetRequest = {\n\thost: string;\n\tpath: string;\n\tbody?: Record<string, any>;\n\theaders?: HeadersInit;\n\tmethod: 'GET' | 'POST';\n};\n\nasync function faucetRequest<T>({ host, path, body, headers, method }: FaucetRequest): Promise<T> {\n\tconst endpoint = new URL(path, host).toString();\n\tconst res = await fetch(endpoint, {\n\t\tmethod,\n\t\tbody: body ? JSON.stringify(body) : undefined,\n\t\theaders: {\n\t\t\t'Content-Type': 'application/json',\n\t\t\t...headers,\n\t\t},\n\t});\n\n\tif (res.status === 429) {\n\t\tthrow new FaucetRateLimitError(\n\t\t\t`Too many requests from this client have been sent to the faucet. Please retry later`,\n\t\t);\n\t}\n\n\ttry {\n\t\tconst parsed = await res.json();\n\t\treturn parsed as T;\n\t} catch (e) {\n\t\tthrow new Error(\n\t\t\t`Encountered error when parsing response from faucet, error: ${e}, status ${res.status}, response ${res}`,\n\t\t);\n\t}\n}\n\nexport async function requestSuiFromFaucetV2(input: {\n\thost: string;\n\trecipient: string;\n\theaders?: HeadersInit;\n}) {\n\tconst response = await faucetRequest<FaucetResponseV2>({\n\t\thost: input.host,\n\t\tpath: '/v2/gas',\n\t\tbody: {\n\t\t\tFixedAmountRequest: {\n\t\t\t\trecipient: input.recipient,\n\t\t\t},\n\t\t},\n\t\theaders: input.headers,\n\t\tmethod: 'POST',\n\t});\n\n\tif (response.status !== 'Success') {\n\t\tthrow new Error(`Faucet request failed: ${response.status.Failure.internal}`);\n\t}\n\n\treturn response;\n}\n\nexport function getFaucetHost(network: 'testnet' | 'devnet' | 'localnet') {\n\tswitch (network) {\n\t\tcase 'testnet':\n\t\t\treturn 'https://faucet.testnet.sui.io';\n\t\tcase 'devnet':\n\t\t\treturn 'https://faucet.devnet.sui.io';\n\t\tcase 'localnet':\n\t\t\treturn 'http://127.0.0.1:9123';\n\t\tdefault:\n\t\t\tthrow new Error(`Unknown network: ${network}`);\n\t}\n}\n"],"mappings":";AAGA,IAAa,uBAAb,cAA0C,MAAM;AA2BhD,eAAe,cAAiB,EAAE,MAAM,MAAM,MAAM,SAAS,UAAqC;CACjG,MAAM,WAAW,IAAI,IAAI,MAAM,KAAK,CAAC,UAAU;CAC/C,MAAM,MAAM,MAAM,MAAM,UAAU;EACjC;EACA,MAAM,OAAO,KAAK,UAAU,KAAK,GAAG;EACpC,SAAS;GACR,gBAAgB;GAChB,GAAG;GACH;EACD,CAAC;AAEF,KAAI,IAAI,WAAW,IAClB,OAAM,IAAI,qBACT,sFACA;AAGF,KAAI;AAEH,SADe,MAAM,IAAI,MAAM;UAEvB,GAAG;AACX,QAAM,IAAI,MACT,+DAA+D,EAAE,WAAW,IAAI,OAAO,aAAa,MACpG;;;AAIH,eAAsB,uBAAuB,OAI1C;CACF,MAAM,WAAW,MAAM,cAAgC;EACtD,MAAM,MAAM;EACZ,MAAM;EACN,MAAM,EACL,oBAAoB,EACnB,WAAW,MAAM,WACjB,EACD;EACD,SAAS,MAAM;EACf,QAAQ;EACR,CAAC;AAEF,KAAI,SAAS,WAAW,UACvB,OAAM,IAAI,MAAM,0BAA0B,SAAS,OAAO,QAAQ,WAAW;AAG9E,QAAO;;AAGR,SAAgB,cAAc,SAA4C;AACzE,SAAQ,SAAR;EACC,KAAK,UACJ,QAAO;EACR,KAAK,SACJ,QAAO;EACR,KAAK,WACJ,QAAO;EACR,QACC,OAAM,IAAI,MAAM,oBAAoB,UAAU"}