|
| 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 | +import { Effect, Logger } from "effect" |
| 9 | +import { getFullnodeUrl } from "@mysten/sui/client" |
| 10 | +import { PublicClient, WalletClient, writeContract, readCoinMetadata, readCoinBalances } from "../src/Sui.js" |
| 11 | +import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519" |
| 12 | +import { Transaction } from "@mysten/sui/transactions" |
| 13 | + |
| 14 | +const MNEMONIC = process.env.MNEMONIC ?? "fix auto gallery heart practice drip joke nice decline lift attend bread" |
| 15 | +const RECIPIENT = process.env.RECIPIENT ?? "0x03ff9dd9e093387bdd4432c6a3eb6a1bd5a8f39a530042ac7efe576f18d3232b" |
| 16 | + |
| 17 | +const keypair = Ed25519Keypair.deriveKeypair(MNEMONIC) |
| 18 | + |
| 19 | + |
| 20 | +const program = Effect.gen(function* () { |
| 21 | + const { client } = yield* PublicClient |
| 22 | + yield* Effect.log("Sui public client initialized", client.network ) |
| 23 | + const meta = yield* readCoinMetadata("0x2::sui::SUI" as any) |
| 24 | + yield* Effect.log("SUI metadata", meta) |
| 25 | + |
| 26 | + yield* Effect.log("keypair.getPublicKey().toSuiAddress()", keypair.getPublicKey().toSuiAddress()) |
| 27 | + const balances = yield* readCoinBalances("0x2::sui::SUI" as any, keypair.getPublicKey().toSuiAddress() as any) |
| 28 | + yield* Effect.log("SUI balances", balances) |
| 29 | + |
| 30 | + |
| 31 | + const wallet = yield* WalletClient |
| 32 | + const amountMist = 10_000_000n // 0.01 SUI |
| 33 | + |
| 34 | + const tx = new Transaction() |
| 35 | + const coin = tx.splitCoins(tx.gas, [tx.pure.u64(amountMist)]) |
| 36 | + const recipient = tx.pure.address(RECIPIENT) |
| 37 | + |
| 38 | + const res = yield* writeContract( |
| 39 | + client, |
| 40 | + keypair, |
| 41 | + "0x2", // packageId: Sui framework |
| 42 | + "transfer", // module: sui::transfer |
| 43 | + "public_transfer", // function |
| 44 | + ["0x2::coin::Coin<0x2::sui::SUI>"], // type arg T |
| 45 | + [coin, recipient], // (obj: T, recipient: address) |
| 46 | + tx, |
| 47 | + ) |
| 48 | + |
| 49 | + yield* Effect.log("Transfer submitted", res) |
| 50 | + |
| 51 | + |
| 52 | +}).pipe( |
| 53 | + Effect.provide(PublicClient.Live({ url: getFullnodeUrl("testnet") })), |
| 54 | + Effect.provide( |
| 55 | + WalletClient.Live({ |
| 56 | + url: getFullnodeUrl("testnet"), |
| 57 | + account: keypair, // signer |
| 58 | + chain: "sui-testnet" as any, // placeholder; not used internally |
| 59 | + }), |
| 60 | + ), |
| 61 | + |
| 62 | + Effect.provide(Logger.replace(Logger.defaultLogger, Logger.prettyLoggerDefault)), |
| 63 | +) |
| 64 | + |
| 65 | +Effect.runPromise(program).catch(console.error) |
0 commit comments