-
Notifications
You must be signed in to change notification settings - Fork 202
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
2881761
commit 7403790
Showing
15 changed files
with
253 additions
and
5 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,126 @@ | ||
import { | ||
Extension, | ||
Wallet, | ||
Dec, | ||
MsgTransfer, | ||
Coin, | ||
LCDClient, | ||
} from '@terra-money/terra.js' | ||
import { Height } from '@terra-money/terra.js/dist/core/ibc/msgs/client/Height' | ||
|
||
export class TerraJsWallet { | ||
private extension: Extension | ||
|
||
private lcdClient: LCDClient | ||
|
||
constructor({ | ||
restEndpoint, | ||
chainId, | ||
}: { | ||
restEndpoint: string | ||
chainId: string | ||
}) { | ||
this.extension = new Extension() | ||
this.lcdClient = new LCDClient({ | ||
URL: restEndpoint, | ||
chainID: chainId, | ||
}) | ||
} | ||
|
||
static isTerraExtensionAvailable(): boolean { | ||
return window.isTerraExtensionAvailable | ||
} | ||
|
||
async connect(): Promise<Wallet> { | ||
await this.extension.request('connect') | ||
|
||
return new Promise((resolve) => { | ||
this.extension.on('connect', (w: Wallet) => { | ||
resolve(w) | ||
}) | ||
}) | ||
} | ||
|
||
async connectWithRequest(): Promise<string> { | ||
const response = await this.extension.request('connect') | ||
|
||
return (response.payload as { address: string }).address | ||
} | ||
|
||
async fetchBalances({ | ||
address, | ||
denom, | ||
}: { | ||
address: string | ||
denom?: string | ||
}): Promise<{ denom: string; amount: string }[]> { | ||
const [coins] = await this.lcdClient.bank.balance(address) | ||
const balances = coins.toArray() | ||
|
||
if (balances.length === 0) { | ||
return [] | ||
} | ||
|
||
if (denom) { | ||
const balance = balances.find( | ||
(balance) => balance.denom.toLowerCase() === denom.toLowerCase(), | ||
) | ||
|
||
if (!balance) { | ||
throw new Error(`No balance found for ${denom}`) | ||
} | ||
|
||
return [{ denom: balance.denom, amount: balance.amount.toString() }] | ||
} | ||
|
||
return balances.map((balance) => ({ | ||
denom: balance.denom, | ||
amount: balance.amount.toString(), | ||
})) | ||
} | ||
|
||
async sendIbcTokens({ | ||
senderAddress, | ||
recipientAddress, | ||
transferAmount, | ||
sourcePort, | ||
sourceChannel, | ||
timeoutHeight, | ||
timeoutTimestamp, | ||
}: { | ||
senderAddress: string | ||
recipientAddress: string | ||
transferAmount: any | ||
sourcePort: string | ||
sourceChannel: string | ||
timeoutHeight: any | ||
timeoutTimestamp: number | ||
}) { | ||
const amount = new Dec(transferAmount.amount) | ||
const coin = new Coin(transferAmount.denom, amount) | ||
const height = new Height( | ||
timeoutHeight?.revision_height, | ||
timeoutHeight?.revision_height, | ||
) | ||
const msgTransfer = new MsgTransfer( | ||
sourcePort, | ||
sourceChannel, | ||
coin, | ||
senderAddress, | ||
recipientAddress, | ||
height, | ||
timeoutTimestamp, | ||
) | ||
|
||
try { | ||
const response = await this.extension.request('post', { | ||
msgs: [msgTransfer.toJSON()], | ||
purgeQueue: true, | ||
}) | ||
|
||
return response.payload | ||
} catch (e: any) { | ||
throw new Error(e.message) | ||
} | ||
} | ||
} |
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,8 @@ | ||
import { TerraChainId } from './types' | ||
|
||
export const TerraEndpoints = { | ||
[TerraChainId.Mainnet]: { | ||
rpc: 'https://tm.terra.injective.network', | ||
rest: 'https://lcd.terra.injective.network', | ||
}, | ||
} as Record<TerraChainId, { rpc: string; rest: string }> |
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 { Erc20TokenMeta, TokenMeta } from '@injectivelabs/token-metadata' | ||
import { TerraChainId } from './types' | ||
|
||
export interface TokenMetaWithDenom extends TokenMeta { | ||
denom: string | ||
} | ||
|
||
export const TerraTokensMeta = { | ||
[TerraChainId.Mainnet]: [ | ||
{ | ||
...Erc20TokenMeta.getMetaBySymbol('LUNA'), | ||
denom: 'uluna', | ||
}, | ||
{ | ||
...Erc20TokenMeta.getMetaBySymbol('UST'), | ||
denom: 'uusd', | ||
}, | ||
], | ||
} as Record<TerraChainId, TokenMetaWithDenom[]> |
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,3 @@ | ||
export enum TerraChainId { | ||
Mainnet = 'columbus-5', | ||
} |
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 |
---|---|---|
@@ -1,4 +1,8 @@ | ||
export * from './KeplrWallet' | ||
export * from './CosmJsWallet' | ||
export * from './types' | ||
export * from './endpoints' | ||
export * from './Keplr/KeplrWallet' | ||
export * from './Keplr/CosmJsWallet' | ||
export * from './Keplr/types' | ||
export * from './Keplr/endpoints' | ||
export * from './Terra/TerraJsWallet' | ||
export * from './Terra/tokensMeta' | ||
export * from './Terra/types' | ||
export * from './Terra/endpoints' |
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