Skip to content

Commit

Permalink
added mainnet
Browse files Browse the repository at this point in the history
  • Loading branch information
paulfears committed Aug 14, 2023
1 parent 459dfb2 commit 35f56a2
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 32 deletions.
46 changes: 41 additions & 5 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
color: white !important;

}
.form-check-input:checked {
background-color: #0d6efd !important;
}
.form-check-input{
background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='-4 -4 8 8'%3e%3ccircle r='3' fill='%23fff'/%3e%3c/svg%3e") !important;
}
</style>
</head>

Expand Down Expand Up @@ -67,7 +73,12 @@ <h1>On Metamask!</h1>
<p style="display: none;" id="info">We are funding your testnet account please wait.</p>
</div>
<div id="inputs" style="opacity: 0;">

<div style="padding-left: 35px;" class="container mt-4">
<div class="form-check form-switch">
<input id="networkButton" style="background-color: #0d6efd;" class="form-check-input" type="checkbox" role="switch" checked/>
<label id="networkDisp" class="form-check-label" for="flexSwitchCheckDefault">Testnet</label>
</div>
<br/>
<ul class="nav nav-tabs" id="myTab" role="tablist">
<li class="nav-item" role="presentation">
Expand Down Expand Up @@ -160,12 +171,15 @@ <h6 style="color:white" id="query_output"></h6>
const sendTxnButton = document.getElementById('send');
const balanceButton = document.getElementById('getBalance');
const query_output = document.getElementById('query_output');

const networkButton = document.getElementById('networkButton');
const networkDisp = document.getElementById('networkDisp')
let testnet = true;
connectButton.addEventListener('click', connect)
sendButton.addEventListener('click', send)
getAccountButton.addEventListener('click', getAccount)
sendTxnButton.addEventListener('click', sendTxn)
balanceButton.addEventListener('click', getBalance)
networkButton.addEventListener('click', toggleTestnet)

// here we get permissions to interact with and install the snap
async function connect () {
Expand All @@ -182,13 +196,28 @@ <h6 style="color:white" id="query_output"></h6>
$("#inputs").delay(1000).animate({opacity: "100%"});
}

async function toggleTestnet(){
if(testnet){
testnet = false
networkDisp.innerText = "mainnet"
}
else{
testnet = true
networkDisp.innerText = "testnet"
}

}

// here we call the snap's "hello" method
async function send () {
try {
const addr = await ethereum.request({
method: 'wallet_invokeSnap',
params: {snapId:snapId, request:{
method: 'getAddress'
method: 'getAddress',
params:{
testnet: testnet
}
}}
})
console.log(addr)
Expand All @@ -205,7 +234,10 @@ <h6 style="color:white" id="query_output"></h6>
const addr = await ethereum.request({
method: 'wallet_invokeSnap',
params: {snapId:snapId, request:{
method: 'getBalance'
method: 'getBalance',
params:{
testnet: testnet
}
}}
})
console.log(addr)
Expand All @@ -226,7 +258,8 @@ <h6 style="color:white" id="query_output"></h6>
method: 'transfer',
params:{
to: recepient,
amount: amount
amount: amount,
testnet: testnet
}
}}
})
Expand Down Expand Up @@ -254,7 +287,10 @@ <h6 style="color:white" id="query_output"></h6>
const addr = await ethereum.request({
method: 'wallet_invokeSnap',
params: {snapId:snapId, request:{
method: 'getAccountInfo'
method: 'getAccountInfo',
params:{
testnet: testnet
}
}}
})
console.log(addr)
Expand Down
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.2",
"version": "0.0.3",
"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.2",
"version": "0.0.3",
"description": "Stellar on metamask",
"proposedName": "Stellar",
"repository": {
"type": "git",
"url": "https://github.com/paulfears/StellarSnap.git"
},
"source": {
"shasum": "GRwo3IWRDxDIzj2p/412q547VO49p4nlsGDC2BpKYqM=",
"shasum": "SabOqKp44LkCbvXLuqnCsah1jGNjGYvhnFD/EuNeKnY=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
39 changes: 32 additions & 7 deletions src/Client.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,45 @@
import { Transaction } from "stellar-base";

const testNetURL = "https://horizon-testnet.stellar.org"
const mainNetURL = "https://horizon.stellar.org/"

export async function fund(wallet){
const response = await fetch(
`https://friendbot.stellar.org?addr=${encodeURIComponent(
wallet.address,
)}`,
);
console.log(response);
const responseJSON = await response.json();
console.log(responseJSON);
return responseJSON;
}



export class Client{
testNetURL:string
enpoint:string
constructor(){
this.testNetURL = "https://horizon-testnet.stellar.org"
this.enpoint = this.testNetURL;
endPoint:string
testNet: boolean
testNetURL: string
mainNetURL: string
constructor(testnet:boolean=false){
if(testnet){
this.endPoint = testNetURL;
}
else if(!testnet){
this.endPoint = mainNetURL;
}
}
async get(path){
console.log("here")
const response = await fetch(this.enpoint+'/'+path)
console.log(this.endPoint)
const response = await fetch(this.endPoint+'/'+path)
const json = await response.json()
return json
}
async post(path){
console.log("here")
const response = await fetch(this.enpoint+'/'+path, {
const response = await fetch(this.endPoint+'/'+path, {
method: "POST",
headers: {
'Accept': 'application/json'
Expand All @@ -35,6 +48,18 @@ export class Client{
const json = await response.json()
return json
}
setTestnet(testnet:boolean){
if(testnet){
console.log("setting endpoint to Testnet")
this.endPoint = testNetURL
console.log(this.endPoint);
}
else{
console.log("setting endpoint to Testnet")
this.endPoint = mainNetURL
console.log(this.endPoint);
}
}
async getAccount(address: string){
const data = await this.get(`accounts/${address}`);
console.log(data);
Expand Down
4 changes: 2 additions & 2 deletions src/Wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export class Wallet{
console.log(this.publicKey);
}

async getBaseAccount(): Promise<Account>{
const client = new Client();
async getBaseAccount(client: Client): Promise<Account>{
console.log(client);
const sequence = await client.getSequence(this.address)
return new Account(this.address, sequence);
}
Expand Down
11 changes: 2 additions & 9 deletions src/WalletFuncs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TxnBuilder } from "./TxnBuilder";
import { Wallet } from "./Wallet";
import Utils from "./Utils";
import { panel, text, heading, divider, copyable, Panel } from '@metamask/snaps-ui';
import { Screens } from "./screens";

export class WalletFuncs{
account: Account
Expand All @@ -19,15 +20,7 @@ export class WalletFuncs{

async transfer(to:string, amount:string){
const txn = this.builder.buildPaymentTxn(to, amount);
const disp = panel([
heading("Confirm Spend"),
divider(),
text('Amount: '),
text(`${amount}`),
text('Recepient: '),
copyable(to)
])
const confirmed = await Utils.displayPanel(disp);
const confirmed = await Screens.paymentConfirmation(to, amount);
if(!confirmed){
throw Error("User rejected Request");
return;
Expand Down
21 changes: 15 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,28 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ origin, request }) =>
const params = request.params;
let wallet_funded = false;
let baseAccount;

const keyPair = wallet.keyPair;
const client = new Client();
console.log("request.params.testnet");
console.log(params?.testnet);
if(params?.testnet){
console.log("testnet is true");
client.setTestnet(params.testnet);
}
try{
baseAccount = await wallet.getBaseAccount();
console.log("attempting to fund wallet");
baseAccount = await wallet.getBaseAccount(client);
wallet_funded = true;
}
catch(e){
console.log("Account not funded yet")
}
const keyPair = wallet.keyPair;
const client = new Client();
let txnBuilder: TxnBuilder;
let operations: WalletFuncs;
if(wallet_funded){
txnBuilder = new TxnBuilder((await wallet.getBaseAccount()));
console.log("wallet funded");
txnBuilder = new TxnBuilder(baseAccount);
operations = new WalletFuncs(baseAccount, keyPair, txnBuilder, client);
}

Expand All @@ -35,7 +44,7 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ origin, request }) =>
// -------------------------------- Methods That Require a funded Account ------------------------------------------
case 'getAccountInfo':
if(!wallet_funded){
Screens.RequiresFundedWallet(request.method, wallet.address);
await Screens.RequiresFundedWallet(request.method, wallet.address);
throw new Error('Method Requires Account to be funded');
}
return await client.getAccount(wallet.address)
Expand All @@ -46,7 +55,7 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ origin, request }) =>
return await client.getBalance(wallet.address)
case 'transfer':
if(!wallet_funded){
Screens.RequiresFundedWallet(request.method, wallet.address);
await Screens.RequiresFundedWallet(request.method, wallet.address);
throw new Error('Method Requires Account to be funded');
}
return await operations.transfer(params.to, params.amount);
Expand Down
12 changes: 12 additions & 0 deletions src/screens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,16 @@ export class Screens{
await Utils.displayPanel(disp, "alert");
return true;
}

static async paymentConfirmation(to:string, amount:string): Promise<boolean>{
const disp = panel([
heading("Confirm XLM Payment"),
divider(),
text(`Amount: ${amount} XLM`),
text(`Recipient: `),
copyable(to),

])
return await Utils.displayPanel(disp)
}
}

0 comments on commit 35f56a2

Please sign in to comment.