diff --git a/package.json b/package.json index c732a95..716750c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "stellar-snap", - "version": "0.0.1", + "version": "0.0.2", "description": "A non custodial Stellar Wallet built for metamask", "repository": { "type": "git", diff --git a/snap.manifest.json b/snap.manifest.json index 51e3620..249daf9 100644 --- a/snap.manifest.json +++ b/snap.manifest.json @@ -1,5 +1,5 @@ { - "version": "0.0.1", + "version": "0.0.2", "description": "Stellar on metamask", "proposedName": "Stellar", "repository": { @@ -7,7 +7,7 @@ "url": "https://github.com/paulfears/StellarSnap.git" }, "source": { - "shasum": "mQnmqePnCQvcdwVe2nc9Cs2jXJXyf03okFnnFIhJ0zE=", + "shasum": "GRwo3IWRDxDIzj2p/412q547VO49p4nlsGDC2BpKYqM=", "location": { "npm": { "filePath": "dist/bundle.js", diff --git a/src/index.ts b/src/index.ts index 6d01fce..980cc8c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,24 +4,51 @@ import { getWallet } from './Wallet'; import { fund, Client } from './Client'; import { TxnBuilder } from './TxnBuilder'; import { WalletFuncs } from './WalletFuncs'; +import { Screens } from './screens'; export const onRpcRequest: OnRpcRequestHandler = async ({ origin, request }) => { const wallet = await getWallet(); - const baseAccount = await wallet.getBaseAccount() + const params = request.params; + let wallet_funded = false; + let baseAccount; + try{ + baseAccount = await wallet.getBaseAccount(); + wallet_funded = true; + } + catch(e){ + console.log("Account not funded yet") + } const keyPair = wallet.keyPair; const client = new Client(); - const txnBuilder = new TxnBuilder((await wallet.getBaseAccount())); - const operations = new WalletFuncs(baseAccount, keyPair, txnBuilder, client); - const params = request.params; + let txnBuilder: TxnBuilder; + let operations: WalletFuncs; + if(wallet_funded){ + txnBuilder = new TxnBuilder((await wallet.getBaseAccount())); + operations = new WalletFuncs(baseAccount, keyPair, txnBuilder, client); + } + switch (request.method) { + // ------------------------------- Methods That Do not Require A funded Account --------------------------------- case 'getAddress': return wallet.address case 'fund': return await fund(wallet); + // -------------------------------- Methods That Require a funded Account ------------------------------------------ case 'getAccountInfo': + if(!wallet_funded){ + Screens.RequiresFundedWallet(request.method, wallet.address); + throw new Error('Method Requires Account to be funded'); + } return await client.getAccount(wallet.address) case 'getBalance': + if(!wallet_funded){ + return '0'; + } return await client.getBalance(wallet.address) case 'transfer': + if(!wallet_funded){ + Screens.RequiresFundedWallet(request.method, wallet.address); + throw new Error('Method Requires Account to be funded'); + } return await operations.transfer(params.to, params.amount); default: throw new Error('Method not found.'); diff --git a/src/screens.ts b/src/screens.ts new file mode 100644 index 0000000..a61e90e --- /dev/null +++ b/src/screens.ts @@ -0,0 +1,16 @@ + +import { panel, text, heading, divider, copyable, Panel } from '@metamask/snaps-ui'; +import Utils from './Utils'; +export class Screens{ + + static async RequiresFundedWallet(method:string, address:string):Promise{ + const disp = panel([ + heading(`${method} Requires A Funded Account`), + divider(), + text(`To fund an account send XLM to`), + copyable(address) + ]) + await Utils.displayPanel(disp, "alert"); + return true; + } +} \ No newline at end of file