Init
This commit is contained in:
41
.gitignore
vendored
Normal file
41
.gitignore
vendored
Normal file
@@ -0,0 +1,41 @@
|
||||
lib-cov
|
||||
*.seed
|
||||
*.log
|
||||
*.csv
|
||||
*.dat
|
||||
*.out
|
||||
*.pid
|
||||
*.gz
|
||||
*.swp
|
||||
|
||||
pids
|
||||
logs
|
||||
results
|
||||
tmp
|
||||
|
||||
# Build
|
||||
public/css/main.css
|
||||
|
||||
# Coverage reports
|
||||
coverage
|
||||
|
||||
# API keys and secrets
|
||||
.env
|
||||
|
||||
# Dependency directory
|
||||
node_modules
|
||||
bower_components
|
||||
|
||||
# Editors
|
||||
.idea
|
||||
*.iml
|
||||
|
||||
# OS metadata
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
|
||||
# Ignore built ts files
|
||||
dist/**/*
|
||||
|
||||
# ignore yarn.lock
|
||||
yarn.lock
|
||||
11
eslint.config.mjs
Normal file
11
eslint.config.mjs
Normal file
@@ -0,0 +1,11 @@
|
||||
import globals from "globals";
|
||||
import pluginJs from "@eslint/js";
|
||||
import tseslint from "typescript-eslint";
|
||||
|
||||
|
||||
export default [
|
||||
{files: ["**/*.{ts}"]},
|
||||
{languageOptions: { globals: globals.browser }},
|
||||
pluginJs.configs.recommended,
|
||||
...tseslint.configs.recommended,
|
||||
];
|
||||
2947
package-lock.json
generated
Normal file
2947
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
27
package.json
Normal file
27
package.json
Normal file
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"name": "sobridge",
|
||||
"version": "1.0.0",
|
||||
"main": "dist/app.js",
|
||||
"scripts": {
|
||||
"start": "tsc && node dist/app.js",
|
||||
"lint": "eslint .",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"description": "",
|
||||
"devDependencies": {
|
||||
"@eslint/js": "^9.13.0",
|
||||
"@types/express": "^4.17.1",
|
||||
"eslint": "^9.13.0",
|
||||
"globals": "^15.11.0",
|
||||
"typescript": "^5.6.3",
|
||||
"typescript-eslint": "^8.12.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"@solana/web3.js": "^1.95.4",
|
||||
"dotenv": "^16.4.5",
|
||||
"express": "^4.17.1"
|
||||
}
|
||||
}
|
||||
72
src/app.ts
Normal file
72
src/app.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import express from 'express';
|
||||
import dotenv from 'dotenv';
|
||||
|
||||
import { GetSolBalance, GetTicketsAccount, GetTicketsBalanceByTA, GetTokenAccount, GetTokenBalanceByTA } from "./sol";
|
||||
|
||||
|
||||
dotenv.config();
|
||||
|
||||
const app = express();
|
||||
const port = process.env.EXPRESS_PORT;
|
||||
|
||||
app.get("/status", (req,res)=>{
|
||||
res.status(200).json({status:"Working"})
|
||||
})
|
||||
|
||||
app.get("/getSolBalance", async(req,res)=>{
|
||||
const {wallet} = req.query;
|
||||
|
||||
if(!wallet){res.status(403).json({error:"Wallet should be present"}); return;}
|
||||
|
||||
const balance = await GetSolBalance(wallet);
|
||||
|
||||
const response = {
|
||||
address: wallet,
|
||||
balance: balance
|
||||
};
|
||||
|
||||
res.status(200).json(response);
|
||||
})
|
||||
|
||||
app.get("/getTokenBalance", async(req,res)=>{
|
||||
const{ wallet } = req.query;
|
||||
if(!wallet){res.status(403).json({error:"Wallet should be present"}); return;}
|
||||
|
||||
const tokenAddress = await GetTokenAccount(wallet);
|
||||
const result = await GetTokenBalanceByTA(tokenAddress);
|
||||
const balance = result <0 ? 0 : result;
|
||||
|
||||
const response = {
|
||||
tokenAccount:tokenAddress,
|
||||
balance:balance
|
||||
}
|
||||
|
||||
res.status(200).json(response);
|
||||
})
|
||||
|
||||
app.get("/getTicketsBalance", async(req,res)=>{
|
||||
const{ wallet } = req.query;
|
||||
if(!wallet){res.status(403).json({error:"Wallet should be present"}); return;}
|
||||
|
||||
const tokenAddress = await GetTicketsAccount(wallet);
|
||||
const result = await GetTicketsBalanceByTA(tokenAddress);
|
||||
const balance = result <0 ? 0 : result;
|
||||
|
||||
const response = {
|
||||
tokenAccount:tokenAddress,
|
||||
balance:balance
|
||||
}
|
||||
|
||||
res.status(200).json(response);
|
||||
})
|
||||
|
||||
app.get("/purchaseTickets", async(req,res)=>{
|
||||
const {amount} = req.query;
|
||||
if(!amount){
|
||||
res.status(403).json({error:"Enter an amount"}); return;
|
||||
}
|
||||
})
|
||||
|
||||
app.listen(port, ()=>{
|
||||
console.log(`Mhunt Solana Brdige listening to port ${port}`)
|
||||
})
|
||||
88
src/sol.ts
Normal file
88
src/sol.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
import { PublicKey, Connection } from '@solana/web3.js';
|
||||
|
||||
const TOKENS_MINT = new PublicKey('vcHyeKhk67CVumjzYV3m8cf6V8xcE85N9ay48pcyUpB');
|
||||
const TICKETS_MINT = new PublicKey('tktUDLZhFGb9VW9zDxZ7HYDFuBooEf8daZEvPbBY7at');
|
||||
|
||||
const solana = new Connection("https://api.devnet.solana.com")
|
||||
|
||||
|
||||
|
||||
export async function GetSolBalance(wallet){
|
||||
const balance = await solana.getBalance(new PublicKey(wallet));
|
||||
|
||||
return balance;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
export async function GetTokenAccount(ownerAddress){
|
||||
const accountPublicKey = new PublicKey(ownerAddress);
|
||||
const account = await solana.getTokenAccountsByOwner(accountPublicKey, {mint: TOKENS_MINT});
|
||||
try{
|
||||
const tokenAddress = account.value[0].pubkey.toString();
|
||||
return tokenAddress;
|
||||
}catch{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function GetTokenBalanceByOwner(owner){
|
||||
const tokenAccount = await GetTokenAccount(owner);
|
||||
if(tokenAccount==null){
|
||||
return 0;
|
||||
}
|
||||
console.log(tokenAccount);
|
||||
return await GetTokenBalanceByTA(tokenAccount);
|
||||
}
|
||||
|
||||
export async function GetTokenBalanceByTA(tokenAccountAddress){
|
||||
if(tokenAccountAddress==null){
|
||||
return 0;
|
||||
}
|
||||
const tokenAccount = new PublicKey(tokenAccountAddress);
|
||||
const info = await solana.getTokenAccountBalance(tokenAccount);
|
||||
if (info.value.uiAmount == null){
|
||||
return -1;
|
||||
}
|
||||
console.log('Token Balance (using Solana-Web3.js): ', info.value.uiAmount);
|
||||
return info.value.uiAmount;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
export async function GetTicketsAccount(ownerAddress){
|
||||
const accountPublicKey = new PublicKey(ownerAddress);
|
||||
const account = await solana.getTokenAccountsByOwner(accountPublicKey, {mint: TICKETS_MINT});
|
||||
|
||||
try{
|
||||
const tokenAddress = account.value[0].pubkey.toString();
|
||||
return tokenAddress;
|
||||
}catch{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function GetTicketsBalanceByOwner(owner){
|
||||
const tokenAccount = await GetTicketsAccount(owner);
|
||||
if(tokenAccount==null){
|
||||
return 0;
|
||||
}
|
||||
console.log(tokenAccount);
|
||||
return await GetTicketsBalanceByTA(tokenAccount);
|
||||
}
|
||||
|
||||
export async function GetTicketsBalanceByTA(tokenAccountAddress){
|
||||
if(tokenAccountAddress==null){
|
||||
return 0;
|
||||
}
|
||||
const tokenAccount = new PublicKey(tokenAccountAddress);
|
||||
const info = await solana.getTokenAccountBalance(tokenAccount);
|
||||
if (info.value.uiAmount == null){
|
||||
return -1;
|
||||
}
|
||||
console.log('Ticket Balance (using Solana-Web3.js): ', info.value.uiAmount);
|
||||
return info.value.uiAmount;
|
||||
}
|
||||
11
tsconfig.json
Normal file
11
tsconfig.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"esModuleInterop": true,
|
||||
"target": "es6",
|
||||
"moduleResolution": "node",
|
||||
"sourceMap": true,
|
||||
"outDir": "dist"
|
||||
},
|
||||
"lib": ["es2015"]
|
||||
}
|
||||
Reference in New Issue
Block a user