-
Notifications
You must be signed in to change notification settings - Fork 105
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
5dc4f19
commit f46e58f
Showing
16 changed files
with
398 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
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
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,113 @@ | ||
import { Sdk } from "@heliax/namada-sdk/web"; | ||
import { | ||
Account, | ||
AccountType, | ||
TxMsgValue, | ||
TxProps, | ||
WrapperTxProps, | ||
} from "@namada/types"; | ||
import { getIndexerApi } from "atoms/api"; | ||
import invariant from "invariant"; | ||
import { Address, ChainSettings, GasConfig } from "types"; | ||
|
||
export type EncodedTxData<T> = { | ||
type: string; | ||
txs: TxProps[] & | ||
{ | ||
innerTxHashes: string[]; | ||
}[]; | ||
wrapperTxProps: WrapperTxProps; | ||
meta?: { | ||
props: T[]; | ||
}; | ||
}; | ||
|
||
export const isPublicKeyRevealed = async ( | ||
address: Address | ||
): Promise<boolean> => { | ||
const api = getIndexerApi(); | ||
console.log("address", address); | ||
let publicKey: string | undefined; | ||
try { | ||
publicKey = (await api.apiV1RevealedPublicKeyAddressGet(address)).data | ||
?.publicKey; | ||
console.log("publicKey", publicKey); | ||
} catch {} | ||
return Boolean(publicKey); | ||
}; | ||
|
||
const getTxProps = ( | ||
account: Account, | ||
gasConfig: GasConfig, | ||
chain: ChainSettings | ||
): WrapperTxProps => { | ||
invariant( | ||
!!account.publicKey, | ||
"Account doesn't contain a publicKey attached to it" | ||
); | ||
|
||
return { | ||
token: chain.nativeTokenAddress, | ||
feeAmount: gasConfig.gasPrice, | ||
gasLimit: gasConfig.gasLimit, | ||
chainId: chain.chainId, | ||
publicKey: account.publicKey!, | ||
memo: "", | ||
}; | ||
}; | ||
|
||
export const buildTx2 = async <T>( | ||
sdk: Sdk, | ||
account: Account, | ||
gasConfig: GasConfig, | ||
chain: ChainSettings, | ||
queryProps: T[], | ||
txFn: (wrapperTxProps: WrapperTxProps, props: T) => Promise<TxMsgValue> | ||
): Promise<EncodedTxData<T>> => { | ||
const { tx } = sdk; | ||
const wrapperTxProps = getTxProps(account, gasConfig, chain); | ||
const txs: TxMsgValue[] = []; | ||
const txProps: TxProps[] = []; | ||
console.log("wrapperTxProps", wrapperTxProps); | ||
|
||
// Determine if RevealPK is needed: | ||
// const publicKeyRevealed = await isPublicKeyRevealed(account.address); | ||
// console.log("publicKeyRevealed", publicKeyRevealed, account.address); | ||
// if (!publicKeyRevealed) { | ||
// const revealPkTx = await tx.buildRevealPk(wrapperTxProps); | ||
// txs.push(revealPkTx); | ||
// } | ||
|
||
console.log("queryProps", queryProps); | ||
|
||
const encodedTxs = await Promise.all( | ||
queryProps.map((props) => txFn.apply(tx, [wrapperTxProps, props])) | ||
); | ||
console.log("encodedTxs", encodedTxs); | ||
|
||
txs.push(...encodedTxs); | ||
|
||
if (account.type === AccountType.Ledger) { | ||
txProps.push(...txs); | ||
} else { | ||
txProps.push(tx.buildBatch(txs)); | ||
} | ||
|
||
return { | ||
txs: txProps.map(({ args, hash, bytes, signingData }) => { | ||
const innerTxHashes = tx.getInnerTxHashes(bytes); | ||
return { | ||
args, | ||
hash, | ||
bytes, | ||
signingData, | ||
innerTxHashes, | ||
}; | ||
}), | ||
wrapperTxProps, | ||
type: txFn.name, | ||
meta: { | ||
props: queryProps, | ||
}, | ||
}; | ||
}; |
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,38 @@ | ||
import initSdk from "@heliax/namada-sdk/inline-init"; | ||
import { getSdk, Sdk } from "@heliax/namada-sdk/web"; | ||
import { nativeTokenAddressAtom } from "atoms/chain"; | ||
import { maspIndexerUrlAtom, rpcUrlAtom } from "atoms/settings"; | ||
import { getDefaultStore } from "jotai"; | ||
|
||
// TODO: temp import fix | ||
const initializeSdk = async (): Promise<Sdk> => { | ||
const { cryptoMemory } = await initSdk(); | ||
const store = getDefaultStore(); | ||
const rpcUrl = store.get(rpcUrlAtom); | ||
const maspIndexerUrl = store.get(maspIndexerUrlAtom); | ||
const nativeToken = store.get(nativeTokenAddressAtom); | ||
|
||
if (!nativeToken.isSuccess) { | ||
throw "Native token not loaded"; | ||
} | ||
|
||
const sdk = getSdk( | ||
cryptoMemory, | ||
rpcUrl, | ||
maspIndexerUrl, | ||
"", | ||
nativeToken.data | ||
); | ||
return sdk; | ||
}; | ||
|
||
// Global instance of initialized SDK | ||
let sdkInstance: Promise<Sdk>; | ||
|
||
// Helper to access SDK instance | ||
export const getSdkInstance = async (): Promise<Sdk> => { | ||
if (!sdkInstance) { | ||
sdkInstance = initializeSdk(); | ||
} | ||
return sdkInstance; | ||
}; |
Oops, something went wrong.