@noves/noves-sdk
Version:
Noves Developer Kit
91 lines (80 loc) • 3.69 kB
text/typescript
/**
* 1.5.0 Release Test — TVM viewAsAccountAddress Support (NEW)
*
* Tests the new viewAsAccountAddress parameter on TVM getTransaction():
* - getTransaction without viewAs (baseline)
* - getTransaction with viewAsAccountAddress (using the actual accountAddress from the tx)
*/
import { Translate } from '../dist/index';
import { section, pass, fail, runTest } from './helpers';
const API_KEY = process.env.NOVES_API_KEY!;
export async function testTVMViewAs() {
section('NEW: TVM viewAsAccountAddress Support');
const translate = Translate.tvm(API_KEY);
const chain = 'tron';
// 1. Get chains first to verify TVM works
await runTest('TVM.getChains()', async () => {
const chains = await translate.getChains();
if (!chains || (Array.isArray(chains) && chains.length === 0)) {
throw new Error('TVM getChains() returned empty');
}
pass('TVM.getChains()', `Chains: ${JSON.stringify(chains).substring(0, 120)}`);
});
// 2. Get some transactions to find a valid hash and addresses
let txHash: string | null = null;
let accountAddress: string | null = null;
const knownAddress = 'TLa2f6VPqDgRE67v1736s7bJ8Ray5wYjU7'; // Active Tron address
await runTest('TVM.getTransactions() — find test tx', async () => {
const page = await translate.getTransactions(chain, knownAddress, { pageSize: 2 });
const txs = page.getTransactions();
if (!txs || txs.length === 0) {
throw new Error('No TVM transactions found');
}
const tx = txs[0] as any;
txHash = tx.rawTransactionData?.transactionHash || tx.txHash || tx.transactionHash;
accountAddress = tx.accountAddress || knownAddress;
pass('TVM.getTransactions()', `Found ${txs.length} txs, hash=${txHash?.substring(0, 16)}...`);
});
// 3. getTransaction without viewAs (default v5)
if (txHash) {
await runTest('TVM.getTransaction(v5, no viewAs)', async () => {
const tx = await translate.getTransaction(chain, txHash!, 'v5');
if (!tx) throw new Error('Returned null');
pass('TVM.getTransaction(v5)', `keys: ${Object.keys(tx).join(', ')}`);
});
// 4. getTransaction WITH viewAsAccountAddress
// Use the known address that owns the transactions
await runTest('TVM.getTransaction(v5, viewAsAccountAddress)', async () => {
// Get transactions specifically for our known address to ensure viewAs works
const page2 = await translate.getTransactions(chain, knownAddress, { pageSize: 5 });
const txs2 = page2.getTransactions();
if (!txs2 || txs2.length === 0) throw new Error('No txs for viewAs test');
// Find a tx where the knownAddress is involved
let foundWorkingViewAs = false;
for (const t of txs2) {
const h = (t as any).rawTransactionData?.transactionHash || (t as any).txHash;
if (!h) continue;
try {
const tx = await translate.getTransaction(chain, h, 'v5', knownAddress);
if (tx) {
pass('TVM.getTransaction(v5 + viewAs)', `keys: ${Object.keys(tx).join(', ')}`);
foundWorkingViewAs = true;
break;
}
} catch (e: any) {
// Some txs may not support viewAs, try next one
continue;
}
}
if (!foundWorkingViewAs) {
throw new Error('viewAsAccountAddress failed on all attempted transactions');
}
});
// 5. getTransaction with v2 format
await runTest('TVM.getTransaction(v2)', async () => {
const tx = await translate.getTransaction(chain, txHash!, 'v2');
if (!tx) throw new Error('Returned null');
pass('TVM.getTransaction(v2)', `keys: ${Object.keys(tx).join(', ')}`);
});
}
}