refactored
This commit is contained in:
0
src/sol/operator.ts
Normal file
0
src/sol/operator.ts
Normal file
82
src/sol/reader.ts
Normal file
82
src/sol/reader.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
import { PublicKey, Connection } from '@solana/web3.js';
|
||||
import {connection, TICKETS_MINT, TOKENS_MINT} from './shared';
|
||||
|
||||
|
||||
|
||||
export async function GetSolBalance(wallet){
|
||||
const balance = await connection.getBalance(new PublicKey(wallet));
|
||||
|
||||
return balance;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
export async function GetTokenAccount(ownerAddress){
|
||||
const accountPublicKey = new PublicKey(ownerAddress);
|
||||
const account = await connection.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 connection.getTokenAccountBalance(tokenAccount);
|
||||
if (info.value.uiAmount == null){
|
||||
return -1;
|
||||
}
|
||||
console.log('Token Balance (using connection-Web3.js): ', info.value.uiAmount);
|
||||
return info.value.uiAmount;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
export async function GetTicketsAccount(ownerAddress){
|
||||
const accountPublicKey = new PublicKey(ownerAddress);
|
||||
const account = await connection.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 connection.getTokenAccountBalance(tokenAccount);
|
||||
if (info.value.uiAmount == null){
|
||||
return -1;
|
||||
}
|
||||
console.log('Ticket Balance (using Solana-Web3.js): ', info.value.uiAmount);
|
||||
return info.value.uiAmount;
|
||||
}
|
||||
6
src/sol/shared.ts
Normal file
6
src/sol/shared.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { Connection, PublicKey } from "@solana/web3.js";
|
||||
|
||||
export const TOKENS_MINT = new PublicKey('vcHyeKhk67CVumjzYV3m8cf6V8xcE85N9ay48pcyUpB');
|
||||
export const TICKETS_MINT = new PublicKey('tktUDLZhFGb9VW9zDxZ7HYDFuBooEf8daZEvPbBY7at');
|
||||
|
||||
export const connection = new Connection("https://api.devnet.solana.com")
|
||||
Reference in New Issue
Block a user