Skip to content

Commit

Permalink
fix(TRX): add Optional tronJsonRpc to AllbridgeCoreSdkOptions for few…
Browse files Browse the repository at this point in the history
…er HTTP requests
  • Loading branch information
AntonKozAllB committed Jan 23, 2024
1 parent 1d14b6c commit 705b664
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/configs/mainnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const mainnet: AllbridgeCoreSdkOptions = {
wormholeMessengerProgramId: "worm2ZoG2kUd4vFXhvjh93UUH596ayRfgQ2MgjNMTth",
solanaLookUpTable: "2JcBAEVnAwVo4u8d61iqgHPrzZuugur7cVTjWubsVLHj",
sorobanNetworkPassphrase: "Public Global Stellar Network ; September 2015",
tronJsonRpc: "https://api.trongrid.io/jsonrpc",
};

/**
Expand Down
1 change: 1 addition & 0 deletions src/configs/testnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const testnet: AllbridgeCoreSdkOptions = {
wormholeMessengerProgramId: "3u8hJUVTA4jH1wYAyUur7FFZVQ8H635K3tSHHF4ssjQ5",
solanaLookUpTable: "C3jAxHRTZjM2Bs7EqPir4nvrT8zKtpcW7RvGR9R2qKtN",
sorobanNetworkPassphrase: "Test SDF Network ; September 2015",
tronJsonRpc: "https://nile.trongrid.io/jsonrpc",
};

/**
Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ export interface AllbridgeCoreSdkOptions {
wormholeMessengerProgramId: string;
solanaLookUpTable: string;
sorobanNetworkPassphrase: string;
/**
* Optional. Will be used in methods</br>
* {@link AllbridgeCoreSdk.pool.getPoolInfoFromChain} and {@link AllbridgeCoreSdk.pool.getAmountToBeWithdrawn}</br>
* to fetch information from the blockchain with fewer HTTP requests using JSON-RPC API
*/
tronJsonRpc?: string;
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/services/liquidity-pool/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,12 @@ export function getChainPoolService(
}
case ChainType.TRX: {
const nodeRpcUrl = nodeRpcUrlsConfig.getNodeRpcUrl(chainSymbol);
const tronJsonRpc = params.tronJsonRpc;
if (provider) {
return new TronPoolService(provider, api, nodeRpcUrl);
return new TronPoolService(provider, api, tronJsonRpc);
} else {
const tronWeb = new TronWeb({ fullHost: nodeRpcUrl });
return new TronPoolService(tronWeb, api, nodeRpcUrl);
return new TronPoolService(tronWeb, api, tronJsonRpc);
}
}
case ChainType.SOLANA: {
Expand Down
39 changes: 25 additions & 14 deletions src/services/liquidity-pool/trx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,36 @@ export class TronPoolService extends ChainPoolService {
chainType: ChainType.TRX = ChainType.TRX;
private static contracts = new Map<string, any>();
private P = 52;
private web3: Web3;
private web3: Web3 | undefined;

constructor(public tronWeb: typeof TronWeb, public api: AllbridgeCoreClient, tronRpcUrl: string) {
constructor(public tronWeb: typeof TronWeb, public api: AllbridgeCoreClient, tronJsonRpc: string | undefined) {
super();
this.web3 = new Web3(tronRpcUrl + "/jsonrpc");
if (tronJsonRpc) {
this.web3 = new Web3(tronJsonRpc);
}
}

async getUserBalanceInfo(accountAddress: string, token: TokenWithChainDetails): Promise<UserBalanceInfo> {
let userBalanceInfo;
try {
userBalanceInfo = await this.getUserBalanceInfoByBatch(accountAddress, token);
} catch (err) {
if (this.web3) {
try {
userBalanceInfo = await this.getUserBalanceInfoByBatch(this.web3, accountAddress, token);
} catch (err) {
userBalanceInfo = await this.getUserBalanceInfoPerProperty(accountAddress, token);
}
} else {
userBalanceInfo = await this.getUserBalanceInfoPerProperty(accountAddress, token);
}
return userBalanceInfo;
}

private async getUserBalanceInfoByBatch(
web3: Web3,
accountAddress: string,
token: TokenWithChainDetails
): Promise<UserBalanceInfo> {
const batch = new this.web3.BatchRequest();
const contract = new this.web3.eth.Contract(PoolAbi as AbiItem[], tronAddressToEthAddress(token.poolAddress));
const batch = new web3.BatchRequest();
const contract = new web3.eth.Contract(PoolAbi as AbiItem[], tronAddressToEthAddress(token.poolAddress));
const userAccount = tronAddressToEthAddress(accountAddress);
const arr = ["userRewardDebt", "balanceOf"].map((methodName) =>
promisify((cb: any) => batch.add(contract.methods[methodName](userAccount).call.request({}, cb)))()
Expand All @@ -63,17 +70,21 @@ export class TronPoolService extends ChainPoolService {

async getPoolInfoFromChain(token: TokenWithChainDetails): Promise<PoolInfo> {
let poolInfo;
try {
poolInfo = await this.getPoolInfoByBatch(token);
} catch (err) {
if (this.web3) {
try {
poolInfo = await this.getPoolInfoByBatch(this.web3, token);
} catch (err) {
poolInfo = await this.getPoolInfoPerProperty(token);
}
} else {
poolInfo = await this.getPoolInfoPerProperty(token);
}
return poolInfo;
}

private async getPoolInfoByBatch(token: TokenWithChainDetails): Promise<PoolInfo> {
const batch = new this.web3.BatchRequest();
const contract = new this.web3.eth.Contract(PoolAbi as AbiItem[], tronAddressToEthAddress(token.poolAddress));
private async getPoolInfoByBatch(web3: Web3, token: TokenWithChainDetails): Promise<PoolInfo> {
const batch = new web3.BatchRequest();
const contract = new web3.eth.Contract(PoolAbi as AbiItem[], tronAddressToEthAddress(token.poolAddress));
const arr = ["a", "d", "tokenBalance", "vUsdBalance", "totalSupply", "accRewardPerShareP"].map((methodName) =>
promisify((cb: any) => batch.add(contract.methods[methodName]().call.request({}, cb)))()
);
Expand Down

0 comments on commit 705b664

Please sign in to comment.