Skip to content

Commit 356a845

Browse files
committed
add getBalance()
1 parent 85f5fb2 commit 356a845

6 files changed

Lines changed: 172 additions & 14 deletions

File tree

README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,59 @@
11
# Bridge SDK
2+
3+
## Basic usage
4+
5+
Connect station:
6+
7+
```js
8+
import { StationWallet, ChainType, BridgeType } from 'bridge-sdk'
9+
10+
const wallet = new StationWallet()
11+
12+
if (!wallet.isSupported()) {
13+
console.log(
14+
`${wallet.description.name} is not supported on your device, please try from a different wallet.`,
15+
)
16+
} else if (!wallet.isInstalled()) {
17+
console.log(`You can install ${wallet.description.name} here: ${wallet.description.installLink}`)
18+
}
19+
20+
wallet
21+
.connect(ChainType.terra)
22+
.then(({ address }) => console.log(address))
23+
```
24+
25+
Get the balance (wallet must be already connected):
26+
```js
27+
wallet
28+
.getBalance('uluna')
29+
.then((res) => {
30+
res.success
31+
? console.log(`Balance: ${res.data} uluna`)
32+
: console.log(`Error: ${res.error}`
33+
})
34+
```
35+
36+
Send tx from station (wallet must be already connected):
37+
38+
```js
39+
wallet
40+
.transfer({
41+
src: ChainType.terra,
42+
dst: ChainType.osmosis,
43+
bridge: BridgeType.ibc,
44+
address: 'osmo1...',
45+
coin: {
46+
amount: 100_000,
47+
denom: 'uluna',
48+
},
49+
})
50+
.then((res) => {
51+
res.success
52+
? console.log(`TX hash: ${res.txhash}`)
53+
: console.log(`Error: ${res.error}`
54+
})
55+
```
56+
57+
> You can use the same functions on the `KeplrWallet` and `MetaMaskWallet` to send a tx from those wallets
58+
>
59+
> You can find more info about the available functions on the [Wallet inteface](/src/wallets/Wallet.ts#25)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bridge-sdk",
3-
"version": "1.0.0",
3+
"version": "1.0.0-beta.1",
44
"description": "",
55
"main": "dist/index.js",
66
"typings": "dist/index.d.ts",

src/wallets/Wallet.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,23 @@ export type TxResult =
2222
error: string
2323
}
2424

25+
export type QueryResult<T> =
26+
| {
27+
success: true
28+
data: T
29+
}
30+
| {
31+
success: false
32+
error: string
33+
}
34+
2535
export interface Wallet {
2636
isSupported(): boolean
2737
isInstalled(): boolean
2838
connect(chain: ChainType): Promise<{ address: string }>
39+
getBalance(
40+
token: string,
41+
): Promise<QueryResult<number>>
2942
transfer(tx: Tx): Promise<TxResult>
3043

3144
supportedChains: ChainType[]

src/wallets/keplr/KeplrWallet.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import { SigningStargateClient } from '@cosmjs/stargate'
22
import { TxRaw } from 'cosmjs-types/cosmos/tx/v1beta1/tx.js'
33
import { BridgeType } from '../../const/bridges'
44
import { ChainType, chainIDs, ibcChannels } from '../../const/chains'
5-
import { Tx, TxResult, Wallet } from '../Wallet'
5+
import { QueryResult, Tx, TxResult, Wallet } from '../Wallet'
66
import { getAxelarDepositAddress } from '../../packages/axelar'
77

88
type KeplrChain = ChainType.cosmos | ChainType.osmosis
99

1010
const keplrRpc: Record<KeplrChain, string> = {
11-
[ChainType.cosmos]: 'https://rpc-cosmoshub-ia.notional.ventures/',
12-
[ChainType.osmosis]: '',
11+
[ChainType.cosmos]: 'https://cosmos-mainnet-rpc.allthatnode.com:26657/',
12+
[ChainType.osmosis]: 'https://rpc.osmosis.zone/',
1313
}
1414

1515
declare global {
@@ -21,6 +21,7 @@ declare global {
2121
export class KeplrWallet implements Wallet {
2222
private address: string = ''
2323
private signer: SigningStargateClient | null = null
24+
private chain: ChainType | null = null
2425

2526
isSupported(): boolean {
2627
// supported on chrome, edge and firefox (only on desktop)
@@ -46,6 +47,7 @@ export class KeplrWallet implements Wallet {
4647
const accounts = await keplrOfflineSigner.getAccounts()
4748

4849
this.address = accounts[0].address
50+
this.chain = chain
4951
this.signer = await SigningStargateClient.connectWithSigner(
5052
rpc || keplrRpc[chain as KeplrChain],
5153
keplrOfflineSigner,
@@ -61,17 +63,34 @@ export class KeplrWallet implements Wallet {
6163
return { address: accounts[0].address }
6264
}
6365

66+
async getBalance(
67+
token: string,
68+
): Promise<QueryResult<number>> {
69+
if (!this.signer) {
70+
return {
71+
success: false,
72+
error: `You must connect the wallet before the query`,
73+
}
74+
}
75+
76+
const res = await this.signer.getBalance(this.address, token)
77+
return {
78+
success: true,
79+
data: parseInt(res.amount),
80+
}
81+
}
82+
6483
async transfer(tx: Tx): Promise<TxResult> {
6584
if (!this.address || !this.signer) {
6685
return {
6786
success: false,
6887
error: `You must connect the wallet before the transfer`,
6988
}
7089
}
71-
if (!this.supportedChains.includes(tx.src)) {
90+
if (tx.src !== this.chain) {
7291
return {
7392
success: false,
74-
error: `${tx.src} is not supported by ${this.description.name}`,
93+
error: `You must connect to ${tx.src} before the transfer`,
7594
}
7695
}
7796
if (tx.src === tx.dst) {

src/wallets/metamask/MetaMaskWallet.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import MetaMaskOnboarding from '@metamask/onboarding'
22
import { BridgeType } from '../../const/bridges'
33
import { chainIDs, ChainType } from '../../const/chains'
44
import { getAxelarDepositAddress } from '../../packages/axelar'
5-
import { Tx, TxResult, Wallet } from '../Wallet'
5+
import { QueryResult, Tx, TxResult, Wallet } from '../Wallet'
66
import { ethers } from 'ethers'
77
import abi from './abi'
88

@@ -27,6 +27,26 @@ export class MetaMaskWallet implements Wallet {
2727
return MetaMaskOnboarding.isMetaMaskInstalled()
2828
}
2929

30+
async getBalance(
31+
token: string,
32+
): Promise<QueryResult<number>> {
33+
if (!this.address) {
34+
return {
35+
success: false,
36+
error: `You must connect the wallet before the query`,
37+
}
38+
}
39+
40+
const contract = new ethers.Contract(token, abi, window.ethereum)
41+
42+
const result = await contract.balanceOf(this.address)
43+
44+
return {
45+
success: true,
46+
data: result.toNumber(),
47+
}
48+
}
49+
3050
async connect(chain: ChainType): Promise<{ address: string }> {
3151
if (!this.supportedChains.includes(chain)) {
3252
throw new Error(`${chain} is not supported by ${this.description.name}`)

src/wallets/station/StationWallet.ts

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ import {
22
Coin,
33
CreateTxOptions,
44
Extension,
5+
LCDClient,
56
MsgTransfer,
67
} from '@terra-money/terra.js'
78
import { BridgeType } from '../../const/bridges'
89
import { ChainType, ibcChannels } from '../../const/chains'
910
import { getAxelarDepositAddress } from '../../packages/axelar'
10-
import { Tx, TxResult, Wallet } from '../Wallet'
11+
import { QueryResult, Tx, TxResult, Wallet } from '../Wallet'
1112

1213
const ext = new Extension()
1314

@@ -37,6 +38,47 @@ export class StationWallet implements Wallet {
3738
return res
3839
}
3940

41+
async getBalance(
42+
token: string,
43+
): Promise<QueryResult<number>> {
44+
if (!this.address) {
45+
return {
46+
success: false,
47+
error: `You must connect the wallet before the query`,
48+
}
49+
}
50+
51+
const lcd = new LCDClient({
52+
URL: 'https://phoenix-lcd.terra.dev',
53+
chainID: 'phoenix-1',
54+
})
55+
56+
if (token.startsWith('terra1')) {
57+
const result = (await lcd.wasm.contractQuery(token, {
58+
balance: {
59+
address: this.address,
60+
},
61+
})) as {
62+
data: {
63+
balance: string
64+
}
65+
}
66+
67+
return {
68+
success: true,
69+
data: parseInt(result.data.balance),
70+
}
71+
} else {
72+
const result = await lcd.bank.balance(this.address)
73+
const coin = result[0].get(token)
74+
75+
return {
76+
success: true,
77+
data: coin?.amount?.toNumber() || 0,
78+
}
79+
}
80+
}
81+
4082
async transfer(tx: Tx): Promise<TxResult> {
4183
if (!this.address) {
4284
return {
@@ -93,12 +135,18 @@ export class StationWallet implements Wallet {
93135
}
94136

95137
case BridgeType.axelar:
96-
const depositAddress = await getAxelarDepositAddress(tx.address, tx.src, tx.dst, tx.coin.denom)
97-
98-
if(!depositAddress) return {
99-
success: false,
100-
error: 'Can\'t generate the Axelar deposit address'
101-
}
138+
const depositAddress = await getAxelarDepositAddress(
139+
tx.address,
140+
tx.src,
141+
tx.dst,
142+
tx.coin.denom,
143+
)
144+
145+
if (!depositAddress)
146+
return {
147+
success: false,
148+
error: "Can't generate the Axelar deposit address",
149+
}
102150

103151
const axlTx: CreateTxOptions = {
104152
msgs: [

0 commit comments

Comments
 (0)