Skip to content

Commit

Permalink
added test directory
Browse files Browse the repository at this point in the history
  • Loading branch information
paulfears committed Oct 21, 2024
1 parent 91d2bc6 commit 2a0798c
Show file tree
Hide file tree
Showing 5 changed files with 11,492 additions and 8,072 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"lint:fix": "yarn lint:eslint --fix && yarn lint:misc --write",
"lint:misc": "prettier '**/*.json' '**/*.md' '!CHANGELOG.md' --ignore-path .gitignore",
"serve": "mm-snap serve",
"test": "echo 'TODO'",
"test": "mm-snap build && concurrently \"(mm-snap serve)\" \"http-server ./test\"",
"start": "npx mm-snap build && concurrently \"(npx mm-snap serve)\" \"npm install --prefix ./site/ && npm run --prefix ./site/ dev\""
},
"devDependencies": {
Expand Down Expand Up @@ -52,6 +52,7 @@
"eslint-plugin-jest": "^24.4.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.4.0",
"http-server": "^14.1.1",
"prettier": "^2.3.2",
"prettier-plugin-packagejson": "^2.2.11",
"rimraf": "^3.0.2",
Expand Down
20 changes: 11 additions & 9 deletions site/src/routes/wallet/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import {connected, dataPacket, isTestnet} from '$lib/wallet-store';
import type {DataPacket} from '$lib/wallet-store';
import {onMount} from 'svelte';
import {FileCopyAltOutline} from 'flowbite-svelte-icons';
import { env } from "$lib/env";
Expand Down Expand Up @@ -45,14 +45,16 @@
}
//Fund the testnet Account if not Funded
if($dataPacket.testnetXLMBalance === "0"){
callMetaStellar('fund', {testnet:true}).then(
()=>{
callMetaStellar('getDataPacket', {}).then((dp:DataPacket)=>{$dataPacket = dp});
}
);
}
onMount(()=>{
if($dataPacket.testnetXLMBalance === "0"){
callMetaStellar('fund', {testnet:true}).then(
()=>{
callMetaStellar('getDataPacket', {}).then((dp:DataPacket)=>{$dataPacket = dp});
}
);
}
});
</script>
{#if $connected}
<div>
Expand Down
25 changes: 25 additions & 0 deletions test/functions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

const supportedFunctions = [
{name:'getAddress', params:{}, desc:'Get the current account address'},
{name:'getDataPacket', params:{}, desc:'Get the data packet for the current account'},
{name:'setCurrentAccount', params:{address:'string'}, desc:'Set the current account to the address provided'},
{name:'showAddress', params:{}, desc:'Show the current account address'},
{name:'createAccount', params:{name:'string'}, desc:'Create a new account with the name provided'},
{name:'listAccounts', params:{}, desc:'List all accounts'},
{name:'renameAccount', params:{address:'string', name:'string'}, desc:'Rename an account'},
{name:'importAccount', params:{}, desc:'Import an account'},
{name:'fund', params:{}, desc:'Fund the current account with testnet lumens'},
{name:'getFederationName', params:{}, desc:'Get the federation name for the current account'},
{name:'lookUpFedAccountByAddress', params:{address:'string'}, desc:'Get the federation address for the current account'},
{name:'lookUpFedAccountByName', params:{url:'string'}, desc:'Get the federation address for an account by its name*metastellar.io fed name'},
{name:'getBalance', params:{testnet:'boolean'}, desc:'Get the balance of the current account'},
{name:'getAssets', params:{}, desc:'Get the assets of the current account'},
{name:'signStr', params:{challenge:'string'}, desc:'Sign a string'},
{name:'dispPrivateKey', params:{}, desc:'Display the private key of the current account'},
{name: 'getAccountInfo', params:{}, desc:'Get the account info of the current account from horizon'},
{name: 'transfer', params:{to:'string', amount:'string', testnet:'boolean'}, desc:'Transfer lumens to another account, will create an account if it does not exist'},
{name: 'signTransaction', params:{transaction:'XDR as string', testnet:'boolean'}, desc:'Sign a transaction'},
{name: 'signAndSubmitTransaction', params:{transaction:'XDR as string', testnet:'boolean'}, desc:'Sign and submit a transaction'},
{name: 'createFederationAccount', params:{}, desc:'Opens the create federation account dialog'},
{name: 'openSendXLM', params:{}, desc:'Opens the send XLM dialog'},
]
138 changes: 138 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<!doctype html>
<html>
</head>
<title>Hello, Snaps!</title>
<link rel="icon" type="image/svg" href="./images/icon.svg"/>
<script src="./functions.js"></script>
</head>

<body>
<h1>Hello, Snaps!</h1>
<details>
<summary>Instructions</summary>
<ul>
<li>First, click "Connect". Then, try out the other buttons!</li>
<li>Please note that:</li>
<ul>
<li>
The <code>snap.manifest.json</code> and <code>package.json</code> must be located in the server root directory...
</li>
<li>
The Snap bundle must be hosted at the location specified by the <code>location</code> field of <code>snap.manifest.json</code>.
</li>
</ul>
</ul>
</details>
<br/>

<button class="connect">Connect</button>

</body>

<script>
const snapId = `local:${window.location.href}`;



async function callMetaStellar(method, params){
if (typeof window !== 'undefined' && typeof window.ethereum !== undefined) {
//You Can Delete this section after offical launch
const isFlask = (
await window.ethereum?.request({ method: "web3_clientVersion" })
)?.includes("flask");
if(!isFlask){
alert("install Metamask Flask")
}
// ------------------------------------------------
try{
if(method === 'connect'){
//This will also install stellar if the user has metamask
return await window.ethereum.request({
method: 'wallet_requestSnaps',
params: {
['npm:stellar-snap']: {}
},
});
}
const rpcPacket = {
method: 'wallet_invokeSnap',
params:{
snapId:'npm:stellar-snap',
request: {'method':method, params:params}
}
}
return await window.ethereum.request(rpcPacket);
}
catch(e){
alert(e.message);
}
}
}



const connectButton = document.querySelector('button.connect')


connectButton.addEventListener('click', connect)


// here we get permissions to interact with and install the snap
async function connect () {
await callMetaStellar('connect');
}

function createTest(FunctionItem){
const test = document.createElement('div');
test.style = `display: flex; flex-direction: column; margin:50px;`;
test.innerHTML = FunctionItem.name;
let description = document.createElement('p');
description.innerHTML = FunctionItem.desc;
test.appendChild(description);
for(let i = 0; i < Object.keys(FunctionItem.params).length; i++){
let key = Object.keys(FunctionItem.params)[i];
let value = Object.values(FunctionItem.params)[i];
let input = document.createElement('input');
if(value === "string"){
const input = document.createElement('input');
input.id = FunctionItem.name+"-"+key;
input.placeholder = key;
}
if(value === "boolean"){
const input = document.createElement('input');
input.id = FunctionItem.name+"-"+key;
input.type = "checkbox";
input.checked = true;
}
test.appendChild(input);
}
buildRunFunction = (FunctionItem)=>{
return ()=>{
let params = {};
for(let i = 0; i < Object.keys(FunctionItem.params).length; i++){
let key = Object.keys(FunctionItem.params)[i];
let value = Object.values(FunctionItem.params)[i];
if(value === "string"){
params[key] = document.getElementById(FunctionItem.name+"-"+key).value;
}
if(value === "boolean"){
params[key] = document.getElementById(FunctionItem.name+"-"+key).checked;
}
}
callMetaStellar(FunctionItem.name, params).then(console.log);
}
};
const button = document.createElement('button');
button.innerHTML = "Run";
button.addEventListener('click', buildRunFunction(FunctionItem));
test.appendChild(button);
document.body.appendChild(test);
}

for(let i = 0; i<supportedFunctions.length; i++){
createTest(supportedFunctions[i]);
}


</script>
</html>
Loading

0 comments on commit 2a0798c

Please sign in to comment.