-
Notifications
You must be signed in to change notification settings - Fork 5
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
4937d9b
commit 9b342af
Showing
13 changed files
with
302 additions
and
86 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Get some specific parts of bns-v2-sdk package until it gets fixed. |
119 changes: 119 additions & 0 deletions
119
packages/query/src/stacks/bns/bns-v2-sdk/callers-helper.ts
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,119 @@ | ||
import { StacksMainnet, StacksTestnet } from '@stacks/network'; | ||
import { ClarityValue, callReadOnlyFunction } from '@stacks/transactions'; | ||
|
||
import { BnsContractName, NetworkType, getBnsContractAddress } from './config'; | ||
import { debug } from './debug'; | ||
import { BnsReadOnlyOptions } from './interfaces'; | ||
import { getFallbackUrl, getNetwork } from './network'; | ||
|
||
async function executeReadOnlyCall( | ||
options: BnsReadOnlyOptions, | ||
contractAddress: string, | ||
contractName: string, | ||
network: StacksMainnet | StacksTestnet, | ||
isZonefile: boolean = false | ||
): Promise<ClarityValue> { | ||
const networkType = network instanceof StacksMainnet ? 'mainnet' : 'testnet'; | ||
const fallbackUrl = getFallbackUrl(networkType); | ||
|
||
debug.log('executeReadOnlyCall initiated:', { | ||
networkType, | ||
contractAddress, | ||
contractName, | ||
functionName: options.functionName, | ||
coreApiUrl: network.coreApiUrl, | ||
fallbackUrl, | ||
isZonefile, | ||
}); | ||
|
||
async function attemptCall(url: string): Promise<ClarityValue> { | ||
const currentNetwork = | ||
networkType === 'mainnet' ? new StacksMainnet({ url }) : new StacksTestnet({ url }); | ||
|
||
debug.log('Attempting call with:', { | ||
url, | ||
networkType: networkType, | ||
currentNetworkType: currentNetwork instanceof StacksMainnet ? 'mainnet' : 'testnet', | ||
}); | ||
|
||
try { | ||
const response = await callReadOnlyFunction({ | ||
contractAddress, | ||
contractName, | ||
functionName: options.functionName, | ||
functionArgs: options.functionArgs, | ||
senderAddress: options.senderAddress, | ||
network: currentNetwork, | ||
}); | ||
|
||
if ((response as any).error) { | ||
debug.error('Response contains error:', (response as any).error); | ||
throw new Error((response as any).error); | ||
} | ||
|
||
debug.log('Call successful'); | ||
return response; | ||
} catch (error: any) { | ||
debug.error('Call failed:', { | ||
error: error.message, | ||
url, | ||
networkType, | ||
}); | ||
throw error; | ||
} | ||
} | ||
|
||
// For zonefile calls, always use fallback URL if available | ||
if (isZonefile && fallbackUrl) { | ||
debug.log('Using fallback URL for zonefile call:', fallbackUrl); | ||
try { | ||
return await attemptCall(fallbackUrl); | ||
} catch (error) { | ||
debug.error('Fallback attempt failed for zonefile:', error); | ||
throw error; | ||
} | ||
} | ||
|
||
// For non-zonefile calls or if no fallback URL, try primary first then fallback | ||
try { | ||
return await attemptCall(network.coreApiUrl); | ||
} catch (error) { | ||
debug.log('Primary URL failed, checking fallback availability:', { | ||
hasFallback: !!fallbackUrl, | ||
fallbackUrl, | ||
isSameAsPrimary: fallbackUrl === network.coreApiUrl, | ||
}); | ||
|
||
if (fallbackUrl && fallbackUrl !== network.coreApiUrl) { | ||
try { | ||
return await attemptCall(fallbackUrl); | ||
} catch (fallbackError) { | ||
debug.error('Fallback attempt failed:', fallbackError); | ||
throw fallbackError; | ||
} | ||
} else { | ||
throw error; | ||
} | ||
} | ||
} | ||
|
||
export async function bnsV2ReadOnlyCall( | ||
options: Omit<BnsReadOnlyOptions, 'network'> & { network: NetworkType } | ||
): Promise<ClarityValue> { | ||
debug.log('bnsV2ReadOnlyCall initiated:', { | ||
network: options.network, | ||
functionName: options.functionName, | ||
}); | ||
|
||
const network = getNetwork(options.network); | ||
const contractAddress = getBnsContractAddress(options.network); | ||
|
||
debug.log('Network configuration:', { | ||
networkType: options.network, | ||
contractAddress, | ||
isMainnet: network instanceof StacksMainnet, | ||
apiUrl: network.coreApiUrl, | ||
}); | ||
|
||
return executeReadOnlyCall(options, contractAddress, BnsContractName, network, false); | ||
} |
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,12 @@ | ||
export const BnsContractName = 'BNS-V2'; | ||
|
||
enum BnsContractAddress { | ||
Mainnet = 'SP2QEZ06AGJ3RKJPBV14SY1V5BBFNAW33D96YPGZF', | ||
Testnet = 'ST2QEZ06AGJ3RKJPBV14SY1V5BBFNAW33D9SZJQ0M', | ||
} | ||
|
||
export function getBnsContractAddress(network: NetworkType): string { | ||
return network === 'mainnet' ? BnsContractAddress.Mainnet : BnsContractAddress.Testnet; | ||
} | ||
|
||
export type NetworkType = 'mainnet' | 'testnet'; |
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,22 @@ | ||
let isDebugEnabled = false; | ||
|
||
export const debug = { | ||
enable: () => { | ||
isDebugEnabled = true; | ||
}, | ||
disable: () => { | ||
isDebugEnabled = false; | ||
}, | ||
log: (...args: any[]) => { | ||
if (isDebugEnabled) { | ||
// eslint-disable-next-line no-console | ||
console.log('[BNS-V2-SDK]:', ...args); | ||
} | ||
}, | ||
error: (...args: any[]) => { | ||
if (isDebugEnabled) { | ||
// eslint-disable-next-line no-console | ||
console.error('[BNS-V2-SDK]:', ...args); | ||
} | ||
}, | ||
}; |
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 @@ | ||
export { getPrimaryName } from './read-only-calls'; |
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,14 @@ | ||
import { ClarityValue } from '@stacks/transactions'; | ||
|
||
import { NetworkType } from './config'; | ||
|
||
export interface BnsReadOnlyOptions { | ||
functionName: string; | ||
functionArgs: ClarityValue[]; | ||
senderAddress: string; | ||
network: NetworkType; | ||
} | ||
export interface GetPrimaryNameOptions { | ||
address: string; | ||
network: NetworkType; | ||
} |
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,44 @@ | ||
import { StacksMainnet, StacksTestnet } from '@stacks/network'; | ||
|
||
import { NetworkType } from './config'; | ||
import { debug } from './debug'; | ||
|
||
export function getNetwork(networkType: NetworkType) { | ||
debug.log('Getting network for type:', networkType); | ||
|
||
if (networkType === 'mainnet') { | ||
const network = new StacksMainnet(); | ||
debug.log('Created mainnet network:', { | ||
apiUrl: network.coreApiUrl, | ||
}); | ||
return network; | ||
} else { | ||
const network = new StacksTestnet(); | ||
debug.log('Created testnet network:', { | ||
apiUrl: network.coreApiUrl, | ||
}); | ||
return network; | ||
} | ||
} | ||
|
||
export function getFallbackUrl(networkType?: NetworkType): string { | ||
debug.log('Getting fallback URL for network type:', networkType); | ||
|
||
if (networkType === 'testnet') { | ||
const testnetFallbackUrl = process.env.NEXT_PUBLIC_BNS_FALLBACK_URL_TESTNET; | ||
debug.log('Testnet fallback URL:', testnetFallbackUrl); | ||
if (!testnetFallbackUrl) { | ||
debug.log('No testnet fallback URL configured'); | ||
return ''; | ||
} | ||
return testnetFallbackUrl; | ||
} | ||
|
||
const mainnetFallbackUrl = process.env.NEXT_PUBLIC_BNS_FALLBACK_URL; | ||
debug.log('Mainnet fallback URL:', mainnetFallbackUrl); | ||
if (!mainnetFallbackUrl) { | ||
debug.log('No mainnet fallback URL configured'); | ||
return ''; | ||
} | ||
return mainnetFallbackUrl; | ||
} |
55 changes: 55 additions & 0 deletions
55
packages/query/src/stacks/bns/bns-v2-sdk/read-only-calls.ts
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,55 @@ | ||
import { | ||
BufferCV, | ||
ClarityType, | ||
ClarityValue, | ||
cvToString, | ||
getCVTypeString, | ||
standardPrincipalCV, | ||
} from '@stacks/transactions'; | ||
|
||
import { bnsV2ReadOnlyCall } from './callers-helper'; | ||
import { GetPrimaryNameOptions } from './interfaces'; | ||
import { generateRandomAddress } from './utils'; | ||
|
||
export async function getPrimaryName({ | ||
address, | ||
network, | ||
}: GetPrimaryNameOptions): Promise<{ name: string; namespace: string } | null> { | ||
const bnsFunctionName = 'get-primary'; | ||
const randomAddress = generateRandomAddress(); | ||
return bnsV2ReadOnlyCall({ | ||
functionName: bnsFunctionName, | ||
senderAddress: randomAddress, | ||
functionArgs: [standardPrincipalCV(address)], | ||
network, | ||
}).then((responseCV: ClarityValue) => { | ||
if (responseCV.type === ClarityType.ResponseOk) { | ||
if (responseCV.value.type === ClarityType.Tuple) { | ||
const nameCV = responseCV.value.data['name'] as BufferCV; | ||
const namespaceCV = responseCV.value.data['namespace'] as BufferCV; | ||
return { | ||
name: Buffer.from(nameCV.buffer).toString(), | ||
namespace: Buffer.from(namespaceCV.buffer).toString(), | ||
}; | ||
} else if (responseCV.value.type === ClarityType.OptionalSome) { | ||
const innerValue = responseCV.value.value; | ||
if (innerValue.type === ClarityType.Tuple) { | ||
const nameCV = innerValue.data['name'] as BufferCV; | ||
const namespaceCV = innerValue.data['namespace'] as BufferCV; | ||
return { | ||
name: Buffer.from(nameCV.buffer).toString(), | ||
namespace: Buffer.from(namespaceCV.buffer).toString(), | ||
}; | ||
} | ||
} | ||
throw new Error('Unexpected response structure'); | ||
} else if (responseCV.type === ClarityType.ResponseErr) { | ||
if (cvToString(responseCV.value) === 'u131') { | ||
return null; | ||
} | ||
throw new Error(cvToString(responseCV.value)); | ||
} else { | ||
throw new Error(`Unexpected Clarity Value type: ${getCVTypeString(responseCV)}`); | ||
} | ||
}); | ||
} |
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,13 @@ | ||
import { | ||
getAddressFromPrivateKey, | ||
makeRandomPrivKey, | ||
privateKeyToString, | ||
} from '@stacks/transactions'; | ||
|
||
export function generateRandomAddress() { | ||
const randomPrivateKey = makeRandomPrivKey(); | ||
const privateKeyString = privateKeyToString(randomPrivateKey); | ||
const randomAddress = getAddressFromPrivateKey(privateKeyString); | ||
|
||
return randomAddress; | ||
} |
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
Oops, something went wrong.