Skip to content

Commit

Permalink
seperated funded from unfunded operations
Browse files Browse the repository at this point in the history
  • Loading branch information
paulfears committed Aug 10, 2023
1 parent 3674116 commit 459dfb2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions snap.manifest.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"version": "0.0.1",
"version": "0.0.2",
"description": "Stellar on metamask",
"proposedName": "Stellar",
"repository": {
"type": "git",
"url": "https://github.com/paulfears/StellarSnap.git"
},
"source": {
"shasum": "mQnmqePnCQvcdwVe2nc9Cs2jXJXyf03okFnnFIhJ0zE=",
"shasum": "GRwo3IWRDxDIzj2p/412q547VO49p4nlsGDC2BpKYqM=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
35 changes: 31 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand Down
16 changes: 16 additions & 0 deletions src/screens.ts
Original file line number Diff line number Diff line change
@@ -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<boolean>{
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;
}
}

0 comments on commit 459dfb2

Please sign in to comment.