-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c66e830
commit cceff72
Showing
10 changed files
with
307 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export * from './queries' | ||
export * from './transformer' | ||
export * from './types' | ||
export * from './service' | ||
|
||
export const NEPTUNE_PRICE_CONTRACT = 'inj1u6cclz0qh5tep9m2qayry9k97dm46pnlqf8nre' |
19 changes: 19 additions & 0 deletions
19
packages/sdk-ts/src/client/wasm/neptune/queries/QueryGetPrices.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { BaseWasmQuery } from '../../BaseWasmQuery' | ||
import { toBase64 } from '../../../../utils' | ||
import { AssetInfo } from '../types' | ||
|
||
export declare namespace QueryGetPrices { | ||
export interface Params { | ||
assets: AssetInfo[] | ||
} | ||
} | ||
|
||
export class QueryGetPrices extends BaseWasmQuery<QueryGetPrices.Params> { | ||
toPayload() { | ||
return toBase64({ | ||
get_prices: { | ||
assets: this.params.assets, | ||
}, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { QueryGetPrices } from './QueryGetPrices' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
import { | ||
Network, | ||
isMainnet, | ||
getNetworkEndpoints, | ||
} from '@injectivelabs/networks' | ||
import { AssetInfo, NEPTUNE_USDT_CW20_CONTRACT, AssetInfoWithPrice } from './types' | ||
import { ChainGrpcWasmApi } from '../../chain' | ||
import { QueryGetPrices } from './queries' | ||
import { PriceQueryTransformer } from './transformer' | ||
import ExecArgNeptuneDeposit from '../../../core/modules/wasm/exec-args/ExecArgNeptuneDeposit' | ||
import ExecArgNeptuneWithdraw from '../../../core/modules/wasm/exec-args/ExecArgNeptuneWithdraw' | ||
|
||
import MsgExecuteContractCompat from '../../../core/modules/wasm/msgs/MsgExecuteContractCompat' | ||
|
||
import { GeneralException } from '@injectivelabs/exceptions' | ||
import { NEPTUNE_PRICE_CONTRACT } from './index' | ||
|
||
const NEPTUNE_USDT_MARKET_CONTRACT = 'inj1nc7gjkf2mhp34a6gquhurg8qahnw5kxs5u3s4u' | ||
|
||
export class NeptuneService { | ||
private client: ChainGrpcWasmApi | ||
private priceOracleContract: string | ||
|
||
/** | ||
* Constructs a new NeptuneService instan ce. | ||
* @param network The network to use (default: Mainnet). | ||
* @param endpoints Optional custom network endpoints. | ||
*/ | ||
constructor( | ||
network: Network = Network.Mainnet, | ||
endpoints?: any // Replace `any` with the appropriate type if available | ||
) { | ||
if (network !== Network.Mainnet) { | ||
throw new GeneralException(new Error('Please switch to mainnet network')) | ||
} | ||
|
||
const networkEndpoints = endpoints || getNetworkEndpoints(network) | ||
this.client = new ChainGrpcWasmApi(networkEndpoints.grpc) | ||
this.priceOracleContract = isMainnet(network) ? NEPTUNE_PRICE_CONTRACT : '' | ||
} | ||
|
||
/** | ||
* Fetch prices for given assets from the Neptune Price Oracle contract. | ||
* @param assets Array of AssetInfo objects. | ||
* @returns Array of Price objects. | ||
*/ | ||
async fetchPrices(assets: AssetInfo[]): Promise<AssetInfoWithPrice[]> { | ||
const queryGetPricesPayload = new QueryGetPrices({ assets }).toPayload() | ||
|
||
try { | ||
const response = await this.client.fetchSmartContractState( | ||
this.priceOracleContract, | ||
queryGetPricesPayload | ||
) | ||
|
||
const prices = PriceQueryTransformer.contractPricesResponseToPrices(response) | ||
|
||
return prices | ||
} catch (error) { | ||
console.error('Error fetching prices:', error) | ||
throw new GeneralException(new Error('Failed to fetch prices')) | ||
} | ||
} | ||
|
||
/** | ||
* Fetch the redemption ratio based on CW20 and native asset prices. | ||
* @param cw20Asset AssetInfo for the CW20 token. | ||
* @param nativeAsset AssetInfo for the native token. | ||
* @returns Redemption ratio as a number. | ||
*/ | ||
async fetchRedemptionRatio({ cw20Asset, nativeAsset }: { | ||
cw20Asset: AssetInfo, | ||
nativeAsset: AssetInfo | ||
}): Promise<number> { | ||
const prices = await this.fetchPrices([cw20Asset, nativeAsset]) | ||
|
||
const [cw20Price] = prices | ||
const [nativePrice] = prices.reverse() | ||
|
||
if (!cw20Price || !nativePrice) { | ||
throw new GeneralException(new Error('Failed to compute redemption ratio')) | ||
} | ||
|
||
return Number(cw20Price.price) / Number(nativePrice.price) | ||
} | ||
|
||
/** | ||
* Convert CW20 nUSDT to bank nUSDT using the redemption ratio. | ||
* @param amountCW20 Amount in CW20 nUSDT. | ||
* @param redemptionRatio Redemption ratio. | ||
* @returns Amount in bank nUSDT. | ||
*/ | ||
calculateBankAmount(amountCW20: number, redemptionRatio: number): number { | ||
return amountCW20 * redemptionRatio | ||
} | ||
|
||
/** | ||
* Convert bank nUSDT to CW20 nUSDT using the redemption ratio. | ||
* @param amountBank Amount in bank nUSDT. | ||
* @param redemptionRatio Redemption ratio. | ||
* @returns Amount in CW20 nUSDT. | ||
*/ | ||
calculateCw20Amount(amountBank: number, redemptionRatio: number): number { | ||
return amountBank / redemptionRatio | ||
} | ||
|
||
/** | ||
* Create a deposit message. | ||
* @param sender Sender's Injective address. | ||
* @param contractAddress USDT market contract address. | ||
* @param denom Denomination of the asset. | ||
* @param amount Amount to deposit as a string. | ||
* @returns MsgExecuteContractCompat message. | ||
*/ | ||
createDepositMsg( | ||
denom: string, | ||
amount: string, | ||
sender: string, | ||
contractAddress: string = NEPTUNE_USDT_MARKET_CONTRACT, | ||
): MsgExecuteContractCompat { | ||
return MsgExecuteContractCompat.fromJSON({ | ||
sender, | ||
contractAddress, | ||
execArgs: ExecArgNeptuneDeposit.fromJSON({}), | ||
funds: { | ||
denom, | ||
amount, | ||
}, | ||
}) | ||
} | ||
|
||
/** | ||
* Create a withdraw message. | ||
* @param sender Sender's Injective address. | ||
* @param contractAddress nUSDT contract address. | ||
* @param amount Amount to withdraw as a string. | ||
* @returns MsgExecuteContractCompat message. | ||
*/ | ||
createWithdrawMsg({ | ||
amount, | ||
sender, | ||
cw20ContractAddress = NEPTUNE_USDT_CW20_CONTRACT, | ||
marketContractAddress = NEPTUNE_USDT_MARKET_CONTRACT, | ||
}: { | ||
amount: string, | ||
sender: string, | ||
cw20ContractAddress?: string, | ||
marketContractAddress?: string, | ||
}): MsgExecuteContractCompat { | ||
return MsgExecuteContractCompat.fromJSON({ | ||
sender, | ||
contractAddress: cw20ContractAddress, | ||
execArgs: ExecArgNeptuneWithdraw.fromJSON({ | ||
amount, | ||
contract: marketContractAddress, | ||
}), | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { WasmContractQueryResponse } from '../types' | ||
import { toUtf8 } from '../../../utils' | ||
import { AssetInfo, PriceResponse } from './types' | ||
|
||
export class PriceQueryTransformer { | ||
static contractPricesResponseToPrices( | ||
response: WasmContractQueryResponse, | ||
): Array<{ assetInfo: AssetInfo; price: string }> { | ||
const data = JSON.parse(toUtf8(response.data)) as PriceResponse | ||
|
||
return data.map(([assetInfo, priceInfo]) => ({ | ||
assetInfo, | ||
price: priceInfo.price, | ||
})) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export type AssetInfo = | ||
| { | ||
token: { | ||
contract_addr: string | ||
} | ||
} | ||
| { | ||
native_token: { | ||
denom: string | ||
} | ||
} | ||
|
||
export type AssetInfoWithPrice = {assetInfo: AssetInfo, price: string } | ||
|
||
export type PriceResponse = Array<[AssetInfo, { price: string }]> | ||
|
||
export const NEPTUNE_USDT_CW20_CONTRACT = | ||
'inj1cy9hes20vww2yr6crvs75gxy5hpycya2hmjg9s' |
33 changes: 33 additions & 0 deletions
33
packages/sdk-ts/src/core/modules/wasm/exec-args/ExecArgNeptuneDeposit.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { | ||
dataToExecData, | ||
ExecArgBase, | ||
ExecDataRepresentation, | ||
} from '../ExecArgBase' | ||
|
||
export declare namespace ExecArgNeptuneDeposit { | ||
export interface Params {} | ||
|
||
export interface Data {} | ||
} | ||
|
||
/** | ||
* @category Contract Exec Arguments | ||
*/ | ||
export default class ExecArgNeptuneDeposit extends ExecArgBase< | ||
ExecArgNeptuneDeposit.Params, | ||
ExecArgNeptuneDeposit.Data | ||
> { | ||
static fromJSON( | ||
params: ExecArgNeptuneDeposit.Params, | ||
): ExecArgNeptuneDeposit { | ||
return new ExecArgNeptuneDeposit(params) | ||
} | ||
|
||
public toData(): ExecArgNeptuneDeposit.Data { | ||
return {} | ||
} | ||
|
||
public toExecData(): ExecDataRepresentation<ExecArgNeptuneDeposit.Data> { | ||
return dataToExecData('lend', this.toData()) | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
packages/sdk-ts/src/core/modules/wasm/exec-args/ExecArgNeptuneWithdraw.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { | ||
ExecArgBase, | ||
dataToExecData, | ||
ExecDataRepresentation, | ||
} from '../ExecArgBase' | ||
import { toBase64 } from '../../../../utils' | ||
|
||
|
||
export declare namespace ExecArgNeptuneWithdraw { | ||
export interface Params { | ||
amount: string | ||
contract: string | ||
} | ||
|
||
export interface Data { | ||
amount: string | ||
contract: string | ||
msg: string | ||
} | ||
} | ||
|
||
/** | ||
* @category Contract Exec Arguments | ||
*/ | ||
export default class ExecArgNeptuneWithdraw extends ExecArgBase< | ||
ExecArgNeptuneWithdraw.Params, | ||
ExecArgNeptuneWithdraw.Data | ||
> { | ||
static fromJSON( | ||
params: ExecArgNeptuneWithdraw.Params, | ||
): ExecArgNeptuneWithdraw { | ||
return new ExecArgNeptuneWithdraw(params) | ||
} | ||
|
||
toData(): ExecArgNeptuneWithdraw.Data { | ||
const { params } = this | ||
const innerMsg = { redeem: {} } | ||
const encodedMsg = toBase64(innerMsg) | ||
|
||
return { | ||
amount: params.amount, | ||
contract: params.contract, | ||
msg: encodedMsg, | ||
} | ||
} | ||
|
||
toExecData(): ExecDataRepresentation<ExecArgNeptuneWithdraw.Data> { | ||
return dataToExecData('send', this.toData()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters