TRS

Access Guide To access TRS-SDK development, developers need to follow these steps:

  1. Build front/back end projects that support TypeScript.

  2. Install Solana dependencies, Solana network connections, and Solana wallet connections.

  3. Copy TRS-SDK files to the project.

  4. Implement business logic according to interface document. These four steps are described in detail below. (This document code is based on the development of TypeScript+Vue project.)

Step 1:Build front/back end projects that support TypeScript.

Front-end projects support the three major front-end frameworks Vue, React, and Angular, while front/back end projects must support TypeScript.

Step 2: Install Solana dependencies, Solana network connections, and Solana wallet connections.

1. Install Solana Dependencie

Yarn

$ yarn add @solana/web3.js
$ yarn add @solana/spl-token
$ yarn add bn.js

npm

$ npm install --save @solana/web3.js
$ npm install --save @solana/spl-token
$ npm install --save bn.js

2. Solana Connections

Create a new tool file solana.ts, which contains network connections, wallet connections, methods to get USDC address , and other components that can be imported to call methods. Solana Wallet is based on Phantom Wallet, and the token supported is USDC.The API reference address of Phantom Wallet: https://docs.phantom.app/integrating/establishing-a-connection Store is a state management library based on the Vuex and allows multiple components to share state.

import {clusterApiUrl, Connection,PublicKey} from "@solana/web3.js";
import { TOKEN_PROGRAM_ID } from "@solana/spl-token";
import store from '@/store'
//Network connection
//"devnet" |"testnet" | "mainnet-beta"
export const connection = new Connection(clusterApiUrl("testnet"));
//wallet connection 
export const connectEffect = () => {
    if(window.solana&&window.solana.isPhantom){
        const solanaProvider = window.solana;
        store.dispatch('updateSolanaProvider',solanaProvider);

        solanaProvider.on("connect", async (connectInfo) =>{
            console.log("Connected to wallet")
            store.dispatch('updateAccount',connectInfo.toString());
        })
        solanaProvider.on("disconnect", () => {
            store.dispatch('updateAccount',null)
            console.log("Disconnected from wallet");
        })
        solanaProvider.on("accountChanged", (connectInfo) => {
            console.log("Update wallet");
            store.dispatch('updateAccount',connectInfo.toString());
        })
        // try to eagerly connect
        solanaProvider.connect({ onlyIfTrusted: true }).catch(() => {
            // fail silently
        });
    }else{
        console.log("Could not find a provider")
    }
}
//Get USDC Address 
const ASSOCIATED_TOKEN_PROGRAM_ID = new PublicKey(
    "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"
);
const USDC_MINT_ADDRESS = new PublicKey(
    "CpMah17kQEL2wqyMKt3mZBdTnZbkbfx4nqmQMFDP5vwp"
);
export const findAssociatedTokenAddress = async () => {
    return (
        await PublicKey.findProgramAddress(
            [
                store.state.solanaProvider.publicKey.toBuffer(),
                TOKEN_PROGRAM_ID.toBuffer(),
                USDC_MINT_ADDRESS.toBuffer(),
            ],
            ASSOCIATED_TOKEN_PROGRAM_ID
        )
    )[0];
};
import { buyTrs,addProduct } from "@/utils/solana/trs/trs";
import { connection,findAssociatedTokenAddress } from "@/utils/solana/solana";
import store from '@/store'

export const addProductTest = async () => {
/**
     * Creat project
     * @param solanaProvider  Wallet provider
     * @param connection      Wallet provider
     * @param token_a_name    Name of a token
     * @param token_b_name    Name of b token
     * @param period          Period(day)
     * @param raise_time      Raise time
     * @param found_time      Established time(Accurate to seconds)
     * @param price           Unit price(u)
     * @param token_share     Share
     * @returns
     */
	 addProduct(store.state.solanaProvider,connection,'BTC','ETH',2,1641254400,1641600000,1,1000)
	.then(()=>{
	 	console.log("addProduct success")
	 }).catch((err)=>{
	 	console.log(err)
    });

}
export const buyTrsTest = async () => {
      /**
     *User buy product
     * @param solanaProvider Wallet provider
     * @param connection    Wallet provider
     * @param usdcPayPubKey  Usdc address of user
     * @param buyTokenName   Name of token of purchase 
     * @param buyShare       Share of purchase
     * @param productPubKey  Product address of purchase
     * @returns
     */
	 const usdcPayPubKey = await findAssociatedTokenAddress();
	 buyTrs(store.state.solanaProvider,connection,usdcPayPubKey,'BTC',10,"B6Piu8TL4TggDhk4HZYjLq3KFH165pPsFNvdLqWaPa1Q")
	 .then(()=>{
	 	console.log("transaction success")
	 }).catch((err)=>{
	 	console.log(err)
    })

Common Utility Methods

Document Address: utils/solana/utils.ts

- Currency Address Exchange

For example:"DJW6f4ZVqCnpYNN9rNuzqUcCvkVtBgixo8mq9FKSsCbJ"转化为BTC

import { boinNames_test, boinAddresses_test } from '@/utils/solana/utils';
const address = "DJW6f4ZVqCnpYNN9rNuzqUcCvkVtBgixo8mq9FKSsCbJ";
const addressName = boinNames_test[boinAddresses_test.indexOf(address)]//BTC

Last updated