Skip to content

Commit fa4faa7

Browse files
committed
featdapp): purchase name with sdk in tests
1 parent 796cdfa commit fa4faa7

File tree

3 files changed

+59
-21
lines changed

3 files changed

+59
-21
lines changed

dapp/tests/management/management.spec.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
// Copyright (c) 2025 IOTA Stiftung
22
// SPDX-License-Identifier: Apache-2.0
33

4+
import { Ed25519Keypair } from '@iota/iota-sdk/keypairs/ed25519';
45
import { formatAddress } from '@iota/iota-sdk/utils';
56
import { expect } from '@playwright/test';
67

78
import { test } from '../helpers/fixtures';
8-
import { purchaseName } from '../helpers/management';
9-
import { connectWallet, createWallet, requestFaucetTokens } from '../utils';
9+
import { connectWallet, createWallet, purchaseName, requestFaucetTokens } from '../utils';
1010

11-
test.describe.configure({ mode: 'parallel' });
12-
13-
test.describe('Management Name Tests', () => {
11+
test.describe.parallel('Management Name Tests', () => {
1412
test.beforeAll(async ({ appPage, context, extensionPage, extensionName, sharedState }) => {
1513
const { address, mnemonic } = await createWallet(extensionPage);
1614

@@ -29,7 +27,12 @@ test.describe('Management Name Tests', () => {
2927
});
3028

3129
test('purchases a name without errors', async ({ appPage: page, context, sharedState }) => {
32-
await purchaseName(page, context);
30+
const keypair = Ed25519Keypair.deriveKeypair(sharedState.wallet.mnemonic ?? '');
31+
await requestFaucetTokens(keypair.toIotaAddress());
32+
33+
const { response } = await purchaseName(sharedState.wallet.address ?? '', keypair);
34+
expect(response.effects?.status.status).toBe('success');
35+
3336
await page.goto('/my-names');
3437
const nameCards = page.getByTestId('name-card');
3538
await expect(nameCards.first()).toBeVisible({ timeout: 30_000 });

dapp/tests/setup/utils.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ import 'dotenv/config';
55

66
import { exec } from 'child_process';
77
import { IotaClientGraphQLTransport } from '@iota/graphql-transport';
8-
import { IotaNamesClient, IotaNamesTransaction } from '@iota/iota-names-sdk';
8+
import { IotaNamesClient } from '@iota/iota-names-sdk';
99
import { getNetwork, IotaClient } from '@iota/iota-sdk/client';
1010
import { IotaGraphQLClient } from '@iota/iota-sdk/graphql';
11-
import { Transaction } from '@iota/iota-sdk/transactions';
1211

1312
const DEFAULT_NETWORK = process.env.NEXT_PUBLIC_NAMES_DAPP_DEFAULT_NETWORK as string;
1413

@@ -57,17 +56,6 @@ async function getAuthorizedSmartContractTypes() {
5756
isAuctionAuthorized,
5857
};
5958
}
60-
async function purchaseName() {
61-
const tx = new Transaction();
62-
const iotaNamesTx = new IotaNamesTransaction(iotaNamesClient, tx);
63-
const [coin] = iotaNamesTx.transaction.splitCoins(tx.gas, [10_000_000]);
64-
const name = `mycoolname${Math.floor(Math.random() * 1000)}.iota`;
65-
const nft = await iotaNamesTx.register({
66-
name,
67-
coin,
68-
});
69-
return { nft, name };
70-
}
7159

7260
function runCommand(cmd: string, envs: Record<string, string> = {}) {
7361
return new Promise<string>((resolve, reject) => {
@@ -97,5 +85,4 @@ export {
9785
AUCTION_TYPE,
9886
getAuthorizedSmartContractTypes,
9987
runCommand,
100-
purchaseName,
10188
};

dapp/tests/utils.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@ import type { BrowserContext, Page } from '@playwright/test';
77

88
import 'dotenv/config';
99

10+
import { IotaNamesTransaction } from '@iota/iota-names-sdk';
11+
import { Signer } from '@iota/iota-sdk/cryptography';
12+
import { Transaction } from '@iota/iota-sdk/transactions';
13+
1014
import { CONFIG } from '@/config';
1115

1216
import { expect } from './helpers/fixtures';
17+
import { iotaClient, iotaNamesClient } from './setup/utils';
1318

1419
export async function connectWallet(page: Page, context: BrowserContext, extensionName: string) {
1520
await page.getByRole('button', { name: /Connect/i }).click();
@@ -63,11 +68,15 @@ export async function createWallet(page: Page) {
6368

6469
await page.getByText('I saved my mnemonic').click();
6570
await page.getByRole('button', { name: 'Open Wallet' }).click();
71+
6672
await page.getByLabel('Open settings menu').click();
6773
await page.getByText('Network').click();
6874
await page.getByText('Custom RPC').click();
69-
await page.getByPlaceholder('http://localhost:3000/').fill(CONFIG.baseUrl);
75+
const networkId = CONFIG.network;
76+
const networkConfig = getNetwork(networkId);
77+
await page.getByPlaceholder('http://localhost:3000/').fill(networkConfig.url);
7078
await page.getByRole('button', { name: 'Save' }).click();
79+
7180
await page.getByTestId('close-icon').click();
7281

7382
return {
@@ -95,6 +104,40 @@ export async function requestFaucetTokens(recipient: string) {
95104
}
96105
}
97106

107+
export async function purchaseName(address: string, signer: Signer) {
108+
const tx = new Transaction();
109+
const iotaNamesTx = new IotaNamesTransaction(iotaNamesClient, tx);
110+
const [coin] = iotaNamesTx.transaction.splitCoins(tx.gas, [50_000_000_000]);
111+
const name = `mycoolname${Math.floor(Math.random() * 1000)}.iota`;
112+
const nft = await iotaNamesTx.register({
113+
name,
114+
coin,
115+
address,
116+
});
117+
iotaNamesTx.transaction.transferObjects([nft, coin], address);
118+
iotaNamesTx.transaction.setSender(address);
119+
const txBytes = await iotaNamesTx.transaction.build({
120+
client: iotaClient,
121+
});
122+
123+
const txDryRun = await iotaClient.dryRunTransactionBlock({
124+
transactionBlock: txBytes,
125+
});
126+
127+
if (txDryRun.effects.status.status !== 'success') {
128+
throw new Error(txDryRun.effects.status.error || 'Transaction dry run failed');
129+
}
130+
console.log(`Purchased name: ${name} to address: ${address}`);
131+
const response = await iotaClient.signAndExecuteTransaction({
132+
transaction: txBytes,
133+
signer,
134+
options: {
135+
showEffects: true,
136+
},
137+
});
138+
return { nft, name, response };
139+
}
140+
98141
export function deriveAddressFromMnemonic(mnemonic: string, path?: string) {
99142
const keypair = Ed25519Keypair.deriveKeypair(mnemonic, path);
100143
const address = keypair.getPublicKey().toIotaAddress();
@@ -104,3 +147,8 @@ export function deriveAddressFromMnemonic(mnemonic: string, path?: string) {
104147
export function getAddressByIndexPath(mnemonic: string, index: number) {
105148
return deriveAddressFromMnemonic(mnemonic, `m/44'/4218'/0'/0'/${index}'`);
106149
}
150+
151+
export function generateRandomName(name: string) {
152+
const random = Math.floor(Math.random() * 10_000);
153+
return `${name}${random}.iota`;
154+
}

0 commit comments

Comments
 (0)