diff --git a/smart-contract/assembly/contracts/main.ts b/smart-contract/assembly/contracts/main.ts index ecce023..8b7d3c4 100644 --- a/smart-contract/assembly/contracts/main.ts +++ b/smart-contract/assembly/contracts/main.ts @@ -15,6 +15,7 @@ import { bytesToU256, stringToBytes, u256ToBytes, + u64ToBytes, } from '@massalabs/as-types'; import { _approve, @@ -113,6 +114,7 @@ export function isValidDomain(domain: string): bool { return true; } + function buildTokenIdKey(domain: string): StaticArray { return TOKEN_ID_KEY_PREFIX.concat(stringToBytes(domain)); } @@ -129,6 +131,21 @@ function buildAddressKey(address: string): StaticArray { return ADDRESS_KEY_PREFIX.concat(stringToBytes(address)); } +/** + * Calculate the cost of the dns allocation + * @param binaryArgs - (domain: string, target: string) + * + * @returns cost of the dns allocation as u64 + */ +export function dnsAllocCost(binaryArgs: StaticArray): StaticArray { + const args = new Args(binaryArgs); + const domain = args + .nextString() + .expect('domain argument is missing or invalid'); + assert(isValidDomain(domain), 'Invalid domain'); + return u64ToBytes(calculateCreationCost(domain.length) + 10_000_000); +} + /** * Register domain * @param binaryArgs - (domain: string, target: string) diff --git a/smart-contract/package.json b/smart-contract/package.json index e137ede..fba9447 100644 --- a/smart-contract/package.json +++ b/smart-contract/package.json @@ -8,6 +8,7 @@ "build": "npx massa-as-compile", "clean": "rimraf build", "deploy": "npm run build && tsx src/deploy.ts", + "testDns": "tsx src/test.ts", "prettier": "prettier '**/src/**/*.ts' --check && as-prettier --check assembly", "prettier:fix": "prettier '**/src/**/*.ts' --write && as-prettier --write assembly", "lint": "eslint .", diff --git a/smart-contract/src/test.ts b/smart-contract/src/test.ts new file mode 100644 index 0000000..3807197 --- /dev/null +++ b/smart-contract/src/test.ts @@ -0,0 +1,68 @@ +import * as dotenv from 'dotenv'; +import path from 'path'; +import { readFileSync } from 'fs'; +import { fileURLToPath } from 'url'; +import { getEnvVariable } from './utils'; +import { deploySC, WalletClient, ISCData } from '@massalabs/massa-sc-deployer'; +import { + Args, + fromMAS, + MAX_GAS_DEPLOYMENT, + CHAIN_ID, + ClientFactory, + DefaultProviderUrls, + EOperationStatus, + bytesToStr, +} from '@massalabs/massa-web3'; + +// Load .env file content into process.env +dotenv.config(); + +// Get environment variables +const publicApi = getEnvVariable('JSON_RPC_URL_PUBLIC'); +const secretKey = getEnvVariable('WALLET_SECRET_KEY'); +// Define deployment parameters +const chainId = CHAIN_ID.BuildNet; // Choose the chain ID corresponding to the network you want to deploy to + +const testAccount = await WalletClient.getAccountFromSecretKey(secretKey); +const client = await ClientFactory.createDefaultClient( + DefaultProviderUrls.BUILDNET, + chainId, + true, + testAccount, +); +// Claim dns +const domain = 'testaurelien'; +const opId = await client.smartContracts().callSmartContract({ + coins: fromMAS(10), + targetAddress: "AS1Xxfwr9pzTXBKfkLKkZNjmJMWimgvpESRv5GdNhmeuc1mqB2JL", + targetFunction: "dnsAlloc", + parameter: new Args().addString(domain).addString(testAccount.address!), + fee: fromMAS(0.01), +}); +const status = await client.smartContracts().awaitMultipleRequiredOperationStatus(opId, [EOperationStatus.SPECULATIVE_SUCCESS, EOperationStatus.SPECULATIVE_ERROR]); +if (status === EOperationStatus.SPECULATIVE_SUCCESS) { + console.log('DNS claimed'); +} else { + const error = await client.smartContracts().getFilteredScOutputEvents({ + start: null, + end: null, + original_operation_id: opId, + original_caller_address: null, + emitter_address: null, + is_final: null + }); + console.error('Error claiming DNS', error); +} + +const addressFetched = bytesToStr( + ( + await client.smartContracts().readSmartContract({ + targetAddress: 'AS1Xxfwr9pzTXBKfkLKkZNjmJMWimgvpESRv5GdNhmeuc1mqB2JL', + targetFunction: 'dnsResolve', + parameter: new Args().addString(domain), + }) + ).returnValue, +); + +console.log('domain', addressFetched);