Skip to content

Commit

Permalink
add little helper to compute alloc cost
Browse files Browse the repository at this point in the history
  • Loading branch information
AurelienFT committed May 14, 2024
1 parent 3a1f7c4 commit c6ab4b1
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
17 changes: 17 additions & 0 deletions smart-contract/assembly/contracts/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
bytesToU256,
stringToBytes,
u256ToBytes,
u64ToBytes,
} from '@massalabs/as-types';
import {
_approve,
Expand Down Expand Up @@ -113,6 +114,7 @@ export function isValidDomain(domain: string): bool {
return true;
}


function buildTokenIdKey(domain: string): StaticArray<u8> {
return TOKEN_ID_KEY_PREFIX.concat(stringToBytes(domain));
}
Expand All @@ -129,6 +131,21 @@ function buildAddressKey(address: string): StaticArray<u8> {
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<u8>): StaticArray<u8> {
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)
Expand Down
1 change: 1 addition & 0 deletions smart-contract/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 .",
Expand Down
68 changes: 68 additions & 0 deletions smart-contract/src/test.ts
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit c6ab4b1

Please sign in to comment.