Skip to content

Commit

Permalink
fix showing balances in signPsbt provider and fix send transfers
Browse files Browse the repository at this point in the history
  • Loading branch information
kieled committed Aug 20, 2024
1 parent f7c613e commit ee2a950
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 94 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nintondo-extension",
"version": "0.2.6.1",
"version": "0.2.6.2",
"private": true,
"scripts": {
"dev": "bun build.ts --watch",
Expand Down
78 changes: 15 additions & 63 deletions src/background/controllers/apiController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import type {
ITransaction,
} from "@/shared/interfaces/api";
import {
ApiOrdUTXO,
ContentDetailedInscription,
ContentInscriptionResopnse,
Inscription,
FindInscriptionsByOutpointResponseItem,
} from "@/shared/interfaces/inscriptions";
import { IToken } from "@/shared/interfaces/token";
import { customFetch, fetchProps } from "@/shared/utils";
Expand All @@ -25,31 +24,16 @@ export interface IApiController {
address: string,
params?: UtxoQueryParams
): Promise<ApiUTXO[] | undefined>;
getOrdUtxos(address: string): Promise<ApiOrdUTXO[] | undefined>;
pushTx(rawTx: string): Promise<{ txid: string } | undefined>;
getTransactions(address: string): Promise<ITransaction[] | undefined>;
getPaginatedTransactions(
address: string,
txid: string
): Promise<ITransaction[] | undefined>;
getPaginatedInscriptions(
address: string,
location: string
): Promise<Inscription[] | undefined>;
getBELPrice(): Promise<{ bellscoin?: { usd: number } } | undefined>;
getLastBlockBEL(): Promise<number | undefined>;
getFees(): Promise<{ fast: number; slow: number } | undefined>;
getInscriptions(address: string): Promise<Inscription[] | undefined>;
getAccountStats(address: string): Promise<IAccountStats | undefined>;
getInscription({
inscriptionNumber,
inscriptionId,
address,
}: {
inscriptionNumber?: number;
inscriptionId?: string;
address: string;
}): Promise<Inscription[] | undefined>;
getTokens(address: string): Promise<IToken[] | undefined>;
getTransactionHex(txid: string): Promise<string | undefined>;
getUtxoValues(outpoints: string[]): Promise<number[] | undefined>;
Expand All @@ -67,6 +51,10 @@ export interface IApiController {
getLocationByInscriptionId(
inscriptionId: string
): Promise<{ location: string; owner: string } | undefined>;
findInscriptionsByOutpoint(data: {
outpoint: string;
address: string;
}): Promise<FindInscriptionsByOutpointResponseItem[] | undefined>;
}

type FetchType = <T>(
Expand Down Expand Up @@ -98,14 +86,6 @@ class ApiController implements IApiController {
return data;
}

async getOrdUtxos(address: string) {
const data = await this.fetch<ApiOrdUTXO[]>({
path: `/address/${address}/ords`,
service: "electrs",
});
return data;
}

async getFees() {
const data = await this.fetch<Record<string, number>>({
path: "/fee-estimates",
Expand Down Expand Up @@ -145,13 +125,6 @@ class ApiController implements IApiController {
});
}

async getInscriptions(address: string): Promise<Inscription[] | undefined> {
return await this.fetch<Inscription[]>({
path: `/address/${address}/ords`,
service: "electrs",
});
}

async getPaginatedTransactions(
address: string,
txid: string
Expand All @@ -166,20 +139,6 @@ class ApiController implements IApiController {
}
}

async getPaginatedInscriptions(
address: string,
location: string
): Promise<Inscription[] | undefined> {
try {
return await this.fetch<Inscription[]>({
path: `/address/${address}/ords/chain/${location}`,
service: "electrs",
});
} catch (e) {
return undefined;
}
}

async getLastBlockBEL() {
const data = await this.fetch<string>({
path: "/blocks/tip/height",
Expand Down Expand Up @@ -216,23 +175,6 @@ class ApiController implements IApiController {
}
}

async getInscription({
inscriptionNumber,
inscriptionId,
address,
}: {
inscriptionNumber?: number;
inscriptionId?: string;
address: string;
}): Promise<Inscription[] | undefined> {
return await this.fetch<Inscription[]>({
path: `/address/${address}/ords?search=${
inscriptionId ?? inscriptionNumber
}`,
service: "electrs",
});
}

async getTokens(address: string): Promise<IToken[] | undefined> {
return await this.fetch<IToken[]>({
path: `/address/${address}/tokens`,
Expand Down Expand Up @@ -288,6 +230,16 @@ class ApiController implements IApiController {
service: "history",
});
}

async findInscriptionsByOutpoint(data: {
outpoint: string;
address: string;
}) {
return await this.fetch<FindInscriptionsByOutpointResponseItem[]>({
path: `/find_meta/${data.outpoint}?address=${data.address}`,
service: "electrs",
});
}
}

export default new ApiController();
6 changes: 3 additions & 3 deletions src/background/controllers/keyringController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { keyringService } from "../services";
import type { Hex, SendBEL, SendOrd } from "../services/keyring/types";
import type { IPrivateWallet } from "@/shared/interfaces";
import type { AddressType } from "bellhdw";
import { ApiOrdUTXO } from "@/shared/interfaces/inscriptions";
import { OrdUTXO } from "@/shared/interfaces/inscriptions";
import { ApiUTXO } from "@/shared/interfaces/api";

export interface IKeyringController {
Expand Down Expand Up @@ -35,7 +35,7 @@ export interface IKeyringController {
createSendMultiOrd(
toAddress: string,
feeRate: number,
ordUtxos: ApiOrdUTXO[],
ordUtxos: OrdUTXO[],
utxos: ApiUTXO[],
network: Network
): Promise<string>;
Expand Down Expand Up @@ -133,7 +133,7 @@ class KeyringController implements IKeyringController {
async createSendMultiOrd(
toAddress: string,
feeRate: number,
ordUtxos: ApiOrdUTXO[],
ordUtxos: OrdUTXO[],
utxos: ApiUTXO[],
network: Network
): Promise<string> {
Expand Down
4 changes: 2 additions & 2 deletions src/background/services/keyring/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { SimpleKey, HDPrivateKey, AddressType } from "bellhdw";
import HDSimpleKey from "bellhdw/src/hd/simple";
import type { Keyring } from "bellhdw/src/hd/types";
import { INewWalletProps } from "@/shared/interfaces";
import { ApiOrdUTXO } from "@/shared/interfaces/inscriptions";
import { ApiOrdUTXO, OrdUTXO } from "@/shared/interfaces/inscriptions";
import { ApiUTXO } from "@/shared/interfaces/api";

export const KEYRING_SDK_TYPES = {
Expand Down Expand Up @@ -314,7 +314,7 @@ class KeyringService {
async sendMultiOrd(
toAddress: string,
feeRate: number,
ordUtxos: ApiOrdUTXO[],
ordUtxos: OrdUTXO[],
utxos: ApiUTXO[],
network: Network
) {
Expand Down
2 changes: 1 addition & 1 deletion src/shared/constant/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const HTML_PREVIEW_URL =
export const CONTENT_URL =
process.env.CONTENT_URL ?? "https://content.nintondo.io/api/pub";
export const HISTORY_URL =
process.env.HISTORY_URL ?? "https://history.nintondo.io/api/pub";
process.env.HISTORY_URL ?? "https://history.nintondo.io/pub";

export const NINTONDO_URL = process.env.NINTONDO_URL ?? "https://nintondo.io";

Expand Down
16 changes: 16 additions & 0 deletions src/shared/interfaces/inscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ export interface ApiOrdUTXO extends ApiUTXO {
rawTx?: string;
}

export interface OrdUTXO {
txid: string;
value: number;
hex: string;
vout: number;
inscription_id: string;
offset: number;
}

export interface Inscription extends ApiOrdUTXO {
preview: string;
content: string;
Expand Down Expand Up @@ -44,3 +53,10 @@ export interface ContentDetailedInscription {
creation_block: number;
invalid_token_reason: any;
}

export interface FindInscriptionsByOutpointResponseItem {
number: number;
owner: string;
height: number;
genesis: string;
}
4 changes: 1 addition & 3 deletions src/shared/interfaces/provider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Inscription } from "./inscriptions";

export interface IField {
input: boolean;
label: string;
Expand All @@ -9,7 +7,7 @@ export interface IField {

export interface IFieldValue {
text?: string;
inscriptions?: Inscription[];
inscriptions?: string[];
value?: string;
anyonecanpay?: boolean;
}
Expand Down
8 changes: 3 additions & 5 deletions src/ui/components/sign-psbt-fileds/component.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FC } from "react";

import { IField } from "@/shared/interfaces/provider";
import { PREVIEW_URL, TESTNET_HTML_PREVIEW_URL } from "@/shared/constant";
import { PREVIEW_URL, TESTNET_PREVIEW_URL } from "@/shared/constant";
import { t } from "i18next";
import cn from "classnames";
import { ExclamationTriangleIcon } from "@heroicons/react/24/outline";
Expand Down Expand Up @@ -62,10 +62,8 @@ const SignPsbtFileds: FC<SignPsbtFiledsProps> = ({
>
<img
src={`${
isTestnet(network)
? TESTNET_HTML_PREVIEW_URL
: PREVIEW_URL
}/${k.inscription_id}`}
isTestnet(network) ? TESTNET_PREVIEW_URL : PREVIEW_URL
}/${k}`}
className="object-cover w-full rounded-xl"
/>
<p className="text-xs">
Expand Down
11 changes: 6 additions & 5 deletions src/ui/hooks/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,16 @@ export const useDecodePsbtInputs = () => {
totalInputValue += locationValue[outpoint];

if (psbt.data.inputs[i].sighashType === 131) {
const foundInscriptions = await apiController.getInscription({
address: currentAccount!.address!,
inscriptionId: outpoint.split("i")[0] + "i" + txInput.index,
});
const foundInscriptions =
await apiController.findInscriptionsByOutpoint({
address: currentAccount!.address!,
outpoint: outpoint.replace(":", "i"),
});

if (foundInscriptions && foundInscriptions.length) {
value = {
anyonecanpay: true,
inscriptions: foundInscriptions,
inscriptions: foundInscriptions.map((i) => i.genesis),
value: `${toFixed(inputValue)} BEL`,
};
} else {
Expand Down
27 changes: 16 additions & 11 deletions src/ui/hooks/transactions.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { useGetCurrentAccount, useWalletState } from "../states/walletState";
import { useControllersState } from "../states/controllerState";
import { tidoshisToAmount } from "@/shared/utils/transactions";
import { Psbt } from "belcoinjs-lib";
import { Psbt, Transaction } from "belcoinjs-lib";
import type { Hex } from "@/background/services/keyring/types";
import { t } from "i18next";
import { Inscription } from "@/shared/interfaces/inscriptions";
import { Inscription, OrdUTXO } from "@/shared/interfaces/inscriptions";
import { ITransfer } from "@/shared/interfaces/token";
import toast from "react-hot-toast";
import { gptFeeCalculate, ss } from "../utils";
Expand Down Expand Up @@ -135,17 +135,22 @@ export const useSendTransferTokens = () => {
hex: true,
});
if (!utxos) return;
const inscriptions: Inscription[] = [];
const inscriptions: OrdUTXO[] = [];
for (const transferToken of txIds) {
const foundInscriptons = await apiController.getInscription({
inscriptionId: transferToken.inscription_id,
address: currentAccount.address,
});
if (!foundInscriptons) return;
const txid = foundInscriptons[0].txid;
const hex = await apiController.getTransactionHex(
transferToken.inscription_id.split("i")[0]
);
if (!hex) return;
const tx = Transaction.fromHex(hex);
const vout = Number(transferToken.inscription_id.split("i")[1]);

inscriptions.push({
...foundInscriptons[0],
hex: await apiController.getTransactionHex(txid),
inscription_id: transferToken.inscription_id,
offset: 0,
txid: tx.getId(),
value: tx.outs[vout].value,
vout,
hex,
});
}
const tx = await keyringController.createSendMultiOrd(
Expand Down

0 comments on commit ee2a950

Please sign in to comment.