Skip to content

Commit

Permalink
Fix/sdk error msg (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
rustin01 authored Aug 14, 2024
1 parent a617b5e commit bb72192
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
18 changes: 15 additions & 3 deletions apps/wallet/src/utils/chain/chain-wallets/ton/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import { TonClient, WalletContractV4, internal, Address, toNano, fromNano, Opene
import { KeyPair, mnemonicToPrivateKey } from "@ton/crypto";
import nacl from "tweetnacl";

const JETTON_TRANSFER_AMOUNT = new BigNumber(0.1)
const JETTON_FORWARD_AMOUNT = new BigNumber(0.0001)

export class TonChainWallet extends ChainWallet {
private keyPair: KeyPair | null = null
private client: TonClient | null = null
Expand Down Expand Up @@ -117,6 +120,15 @@ export class TonChainWallet extends ChainWallet {
// jetton
if (assetInfo.chainAssetType.equals(ChainAssetType.Jetton)) {
const ownerAddress = (await this.getAccount()).address

// gas check
const gasBalance = await this.client!.getBalance(Address.parse(ownerAddress));
const gasBn = new BigNumber(fromNano(gasBalance))
const minGas = JETTON_TRANSFER_AMOUNT.plus(JETTON_FORWARD_AMOUNT)
if (gasBn.lt(minGas)) {
throw new Error(`Insufficient gas balance (at least ${minGas.toString()} Ton)`)
}

const jettonWallet = await this.getJettonWallet(ownerAddress, assetInfo.contractAddress)

const forwardPayload = beginCell()
Expand All @@ -131,14 +143,14 @@ export class TonChainWallet extends ChainWallet {
.storeAddress(Address.parse(toAddress))
.storeAddress(Address.parse(ownerAddress)) // response destination
.storeBit(0) // no custom payload
.storeCoins(toNano('0.0001')) // forward amount - if >0, will send notification message
.storeCoins(toNano(JETTON_FORWARD_AMOUNT.toString())) // forward amount - if >0, will send notification message
.storeBit(1) // we store forwardPayload as a reference
.storeRef(forwardPayload)
.endCell();

const internalMessage = internal({
to: jettonWallet.address,
value: toNano('0.1'),
value: toNano(JETTON_TRANSFER_AMOUNT.toString()),
bounce: true,
body: messageBody,
});
Expand All @@ -163,7 +175,7 @@ export class TonChainWallet extends ChainWallet {
if (e.response?.status === 500) {
const message: string = e.response?.data?.error || ''
if (/^LITE_SERVER_UNKNOWN:[.\s\S]*inbound external message rejected by transaction[.\s\S]*$/i.test(message)) {
throw new Error('Insufficient Ton gas')
throw new Error(`Insufficient gas balance (at least ${minGas} Ton)`)
}
}
throw e
Expand Down
12 changes: 8 additions & 4 deletions packages/sdk/src/lib/wallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class HibitIdWallet {
}
throw new Error('User manually canceled')
} catch (e: any) {
throw new Error(`Connect failed: ${e.message || e}`)
throw new Error(`Connect failed: ${this.getRpcErrorMessage(e)}`)
}
}

Expand Down Expand Up @@ -117,7 +117,7 @@ export class HibitIdWallet {
})
return res?.signature ?? null
} catch (e: any) {
throw new Error(`Sign message failed: ${e.message || e}`)
throw new Error(`Sign message failed: ${this.getRpcErrorMessage(e)}`)
}
}

Expand All @@ -129,7 +129,7 @@ export class HibitIdWallet {
const res = await this._rpc?.call<GetBalanceResponse>(HibitIdExposeRPCMethod.GET_BALANCE, request)
return res?.balance ?? null
} catch (e: any) {
throw new Error(`Get balance failed: ${e.message || e}`)
throw new Error(`Get balance failed: ${this.getRpcErrorMessage(e)}`)
}
}

Expand All @@ -141,7 +141,7 @@ export class HibitIdWallet {
return res?.txHash ?? null
} catch (e: any) {
console.error(e, JSON.stringify(e))
throw new Error(`Transfer failed: ${e.message || e}`)
throw new Error(`Transfer failed: ${this.getRpcErrorMessage(e)}`)
}
}

Expand Down Expand Up @@ -326,4 +326,8 @@ export class HibitIdWallet {
this._disconnectedPromise?.resolve(true)
this._disconnectedPromise = null
}

private getRpcErrorMessage = (e: any) => {
return (e.message as string)?.split('\n')[0].replace('Error: ', '') || String(e)
}
}

0 comments on commit bb72192

Please sign in to comment.