|
| 1 | +// @ts-ignore |
| 2 | +if (typeof BigInt.prototype.toJSON !== "function") { |
| 3 | + // @ts-ignore |
| 4 | + BigInt.prototype.toJSON = function () { |
| 5 | + return this.toString() |
| 6 | + } |
| 7 | +} |
| 8 | + |
| 9 | +import { Effect, Logger } from "effect" |
| 10 | +import { getFullnodeUrl } from "@mysten/sui/client" |
| 11 | + |
| 12 | +import { |
| 13 | + PublicClient, |
| 14 | + readCoinMetadata, |
| 15 | + readCoinBalances, |
| 16 | + readTotalCoinBalance, |
| 17 | + getAllCoinsUnique, |
| 18 | + getCoinName, |
| 19 | + getCoinDecimals, |
| 20 | + readCoinSymbol, |
| 21 | +} from "../src/Sui.js" |
| 22 | + |
| 23 | +const ADDRESS = |
| 24 | + process.env.ADDRESS ?? |
| 25 | + "0x03ff9dd9e093387bdd4432c6a3eb6a1bd5a8f39a530042ac7efe576f18d3232b" |
| 26 | + |
| 27 | +const COIN_TYPE = "0x2::sui::SUI" as any |
| 28 | + |
| 29 | +const program = Effect.gen(function* () { |
| 30 | + const { client } = yield* PublicClient |
| 31 | + yield* Effect.log("Sui public client initialized", client.network) |
| 32 | + |
| 33 | + const meta = yield* readCoinMetadata(COIN_TYPE) |
| 34 | + yield* Effect.log("SUI metadata", meta) |
| 35 | + |
| 36 | + const [name, symbol, decimals] = yield* Effect.all([ |
| 37 | + getCoinName(COIN_TYPE), |
| 38 | + readCoinSymbol(COIN_TYPE), |
| 39 | + getCoinDecimals(COIN_TYPE), |
| 40 | + ]) |
| 41 | + yield* Effect.log("SUI meta (granular)", { name, symbol, decimals }) |
| 42 | + |
| 43 | + yield* Effect.log("Address", ADDRESS) |
| 44 | + const coins = yield* readCoinBalances(COIN_TYPE, ADDRESS as any) |
| 45 | + yield* Effect.log("SUI coins (objects)", coins) |
| 46 | + |
| 47 | + const total = yield* readTotalCoinBalance(COIN_TYPE, ADDRESS as any) |
| 48 | + yield* Effect.log("SUI total balance (mist as BigInt)", total.toString()) |
| 49 | + |
| 50 | + |
| 51 | + const unique = yield* getAllCoinsUnique(ADDRESS as any) |
| 52 | + |
| 53 | + yield* Effect.log("All coins (unique, summed)", unique) |
| 54 | +}).pipe( |
| 55 | + Effect.provide(PublicClient.Live({ url: getFullnodeUrl("testnet") })), |
| 56 | + Effect.provide(Logger.replace(Logger.defaultLogger, Logger.prettyLoggerDefault)), |
| 57 | +) |
| 58 | + |
| 59 | +Effect.runPromise(program).catch(console.error) |
0 commit comments