|
| 1 | +import { Backend, Dialect } from '@dialectlabs/sdk'; |
| 2 | +import { CliUx, Command, Flags } from '@oclif/core'; |
| 3 | +import { |
| 4 | + envFlag, |
| 5 | + keypairFlag, |
| 6 | + modeFlag, |
| 7 | + sdkEnvFromEnvFlag, |
| 8 | +} from '../../shared/flags'; |
| 9 | +import { createWalletFromFile } from '../../shared/wallet'; |
| 10 | + |
| 11 | +export default class Broadcast extends Command { |
| 12 | + static description = 'Command sends messages to multiples accounts'; |
| 13 | + |
| 14 | + static examples = ['<%= config.bin %> <%= command.id %>']; |
| 15 | + |
| 16 | + static flags = { |
| 17 | + title: Flags.string({ |
| 18 | + char: 't', |
| 19 | + description: 'Message title', |
| 20 | + required: true, |
| 21 | + }), |
| 22 | + message: Flags.string({ |
| 23 | + char: 'm', |
| 24 | + description: 'Message to send', |
| 25 | + required: true, |
| 26 | + }), |
| 27 | + mode: modeFlag, |
| 28 | + keypair: keypairFlag, |
| 29 | + env: envFlag, |
| 30 | + }; |
| 31 | + |
| 32 | + static args = []; |
| 33 | + |
| 34 | + public async run(): Promise<void> { |
| 35 | + const { flags } = await this.parse(Broadcast); |
| 36 | + |
| 37 | + const backends = []; |
| 38 | + if (flags.mode === 'cloud') { |
| 39 | + backends.push(Backend.DialectCloud); |
| 40 | + } |
| 41 | + |
| 42 | + if (flags.mode === 'solana') { |
| 43 | + backends.push(Backend.Solana); |
| 44 | + } |
| 45 | + |
| 46 | + const wallet = await createWalletFromFile(flags.keypair!); |
| 47 | + |
| 48 | + const sdk = Dialect.sdk({ |
| 49 | + wallet, |
| 50 | + environment: sdkEnvFromEnvFlag(flags.env), |
| 51 | + backends: backends, |
| 52 | + }); |
| 53 | + |
| 54 | + const dapp = await sdk.dapps.find(); |
| 55 | + if (!dapp) { |
| 56 | + throw new Error( |
| 57 | + `You don't have dapp associated with your public key: ${sdk.wallet.publicKey}`, |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + this.log('Dapp public address:', dapp.publicKey.toString()); |
| 62 | + |
| 63 | + CliUx.ux.action.start('fetching addresses'); |
| 64 | + |
| 65 | + const addresses = await dapp.dappAddresses.findAll(); |
| 66 | + |
| 67 | + CliUx.ux.action.stop(`found ${addresses.length} addresses`); |
| 68 | + |
| 69 | + if (addresses.length === 0) { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + const confirmed = await CliUx.ux.confirm( |
| 74 | + `Message preview: |
| 75 | +${flags.title} |
| 76 | +${flags.message} |
| 77 | +to ${addresses.length} addresses? [y/n]`, |
| 78 | + ); |
| 79 | + |
| 80 | + if (!confirmed) { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + CliUx.ux.action.start('sending message'); |
| 85 | + |
| 86 | + await dapp.messages.send({ |
| 87 | + title: flags.title, |
| 88 | + message: flags.message, |
| 89 | + }); |
| 90 | + |
| 91 | + CliUx.ux.action.stop('sending message'); |
| 92 | + } |
| 93 | +} |
0 commit comments