@@ -30,6 +30,7 @@ import {
3030 type PeerAndOfferNameFlags ,
3131 CC_IDS_FLAG_NAME ,
3232 FINISH_COMMITMENT_FLAG_NAME ,
33+ FLT_SYMBOL ,
3334} from "../const.js" ;
3435import { dbg } from "../dbg.js" ;
3536import {
@@ -40,6 +41,8 @@ import {
4041 sign ,
4142 multicallRead ,
4243 type MulticallReadItem ,
44+ ensureJsonRpcProvider ,
45+ sendRawTransaction ,
4346} from "../dealClient.js" ;
4447import { getCCDetails , getCCIdsByHexPeerIds } from "../gql/gql.js" ;
4548import type {
@@ -50,7 +53,7 @@ import { secondsToDate } from "../helpers/bigintOps.js";
5053import { stringifyUnknown } from "../helpers/stringifyUnknown.js" ;
5154import { bigintToStr , numToStr } from "../helpers/typesafeStringify.js" ;
5255import { splitErrorsAndResults , commaSepStrToArr } from "../helpers/utils.js" ;
53- import { input } from "../prompt.js" ;
56+ import { confirm , input } from "../prompt.js" ;
5457import {
5558 resolveComputePeersByNames ,
5659 type ResolvedComputePeer ,
@@ -61,7 +64,7 @@ import {
6164 peerIdBase58ToHexString ,
6265 peerIdHexStringToBase58String ,
6366} from "./conversions.js" ;
64- import { fltFormatWithSymbol } from "./currencies.js" ;
67+ import { fltFormatWithSymbol , fltParse } from "./currencies.js" ;
6568
6669const HUNDRED_PERCENT = 100 ;
6770
@@ -246,8 +249,83 @@ async function getCommitmentsIds(
246249 return getComputePeersWithCCIds ( await resolveComputePeersByNames ( { flags } ) ) ;
247250}
248251
252+ const MIN_PEER_TOKENS_FLT_STR = "10" ;
253+
254+ async function ensurePeersHaveEnoughTokens (
255+ computePeers : ResolvedComputePeer [ ] ,
256+ ) {
257+ const MIN_PEER_TOKENS = await fltParse ( MIN_PEER_TOKENS_FLT_STR ) ;
258+ const jsonRpcProvider = await ensureJsonRpcProvider ( ) ;
259+
260+ const peersWithoutEnoughTokens = (
261+ await Promise . all (
262+ computePeers . map ( async ( cp ) => {
263+ const balance = await jsonRpcProvider . getBalance ( cp . walletAddress ) ;
264+ return { ...cp , balance } ;
265+ } ) ,
266+ )
267+ ) . filter ( ( { balance } ) => {
268+ return balance < MIN_PEER_TOKENS ;
269+ } ) ;
270+
271+ if (
272+ peersWithoutEnoughTokens . length > 0 &&
273+ ( await confirm ( {
274+ message : `The following peers don't have enough tokens (${ await fltFormatWithSymbol ( MIN_PEER_TOKENS ) } ) in their wallets to work properly:\n${ (
275+ await Promise . all (
276+ peersWithoutEnoughTokens . map ( async ( { name, balance } ) => {
277+ return `${ name } : ${ await fltFormatWithSymbol ( balance ) } ` ;
278+ } ) ,
279+ )
280+ ) . join ( "\n" ) } \nDo you want to ensure they do?`,
281+ default : true ,
282+ } ) )
283+ ) {
284+ const targetTokens = await fltParse (
285+ await input ( {
286+ message : `Enter the amount of ${ FLT_SYMBOL } tokens (min: ${ await fltFormatWithSymbol (
287+ MIN_PEER_TOKENS ,
288+ ) } ) that you want to have on the wallets of peers: ${ peersWithoutEnoughTokens
289+ . map ( ( { name } ) => {
290+ return name ;
291+ } )
292+ . join ( ", " ) } `,
293+ default : MIN_PEER_TOKENS_FLT_STR ,
294+ async validate ( val : string ) {
295+ let parsedVal : bigint ;
296+
297+ try {
298+ parsedVal = await fltParse ( val ) ;
299+ } catch {
300+ return "Amount must be a positive number" ;
301+ }
302+
303+ return parsedVal > 0 || "Amount must be a positive number" ;
304+ } ,
305+ } ) ,
306+ ) ;
307+
308+ for ( const { balance, walletAddress, name } of peersWithoutEnoughTokens ) {
309+ const tokensToDistribute = targetTokens - balance ;
310+ const formattedAmount = await fltFormatWithSymbol ( tokensToDistribute ) ;
311+
312+ const txReceipt = await sendRawTransaction (
313+ `Distribute ${ formattedAmount } to ${ name } (${ walletAddress } )` ,
314+ { to : walletAddress , value : tokensToDistribute } ,
315+ ) ;
316+
317+ commandObj . logToStderr (
318+ `Successfully distributed ${ color . yellow ( formattedAmount ) } to ${ color . yellow (
319+ name ,
320+ ) } with tx hash: ${ color . yellow ( txReceipt . hash ) } `,
321+ ) ;
322+ }
323+ }
324+ }
325+
249326export async function createCommitments ( flags : PeerAndOfferNameFlags ) {
250327 const computePeers = await resolveComputePeersByNames ( { flags } ) ;
328+ await ensurePeersHaveEnoughTokens ( computePeers ) ;
251329 const { contracts } = await getContracts ( ) ;
252330 const precision = await contracts . diamond . precision ( ) ;
253331 const { ZeroAddress } = await import ( "ethers" ) ;
0 commit comments