Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update tooling scripts #39

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions smart-contract/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
20
6 changes: 6 additions & 0 deletions smart-contract/src/infos.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
getDomains,
getDomainsFromTarget,
getOwner,
getTokenCounter,
getTotalSupply,
Expand All @@ -24,3 +25,8 @@ try {
} catch (e) {
console.log('No total supply found');
}

// reverse resolve
const target = 'AS1ZTEiyBCyVAdpMfdU7br3xxPSj99kNZTxVXhnuYH7DkDF6h9YK';
const revRes = await getDomainsFromTarget(provider, target);
console.log('target domains:', revRes);
59 changes: 26 additions & 33 deletions smart-contract/src/migrate.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { SmartContract, Mas, Operation, rpcTypes } from '@massalabs/massa-web3';
import {
getMigrateCounter,
getScByteCode,
getTokenCounter,
initProvider,
} from './utils';
SmartContract,
Mas,
Operation,
rpcTypes,
Args,
} from '@massalabs/massa-web3';
import { getScByteCode, initProvider } from './utils';
import { MNS_CONTRACT } from './config';

const provider = await initProvider();

const byteCode = getScByteCode('build', 'main_mig.wasm');
const byteCode = getScByteCode('build', 'main_prev.wasm');
const contract = new SmartContract(provider, MNS_CONTRACT);

console.log(
Expand All @@ -19,6 +20,7 @@ console.log(

let op: Operation;
let events: rpcTypes.OutputEvents;

op = await contract.call('upgradeSC', byteCode, {
coins: Mas.fromString('3'),
fee: Mas.fromString('0.1'),
Expand All @@ -30,18 +32,22 @@ for (const event of events) {
}
console.log('upgradeSC done ! operation:', op.id);

const counter = await getTokenCounter(provider, MNS_CONTRACT);
let migrateCount = 0n;
try {
migrateCount = await getMigrateCounter(provider, MNS_CONTRACT);
} catch (e) {
console.log('no migrate counter found');
console.log('Initialize migration');

op = await contract.call('initMig', byteCode, {
fee: Mas.fromString('0.1'),
});
events = await op.getFinalEvents();

for (const event of events) {
console.log('upgradeSC Events:', event.data);
}
console.log('Initialize done ! operation:', op.id);

while (migrateCount < counter) {
console.log('migrating batch from tokenID', migrateCount.toString());
op = await contract.call('migrate', undefined, {
coins: Mas.fromString('30'),
let batchSize = 3000;
while (true) {
op = await contract.call('migrate', new Args().addI32(BigInt(batchSize)), {
coins: Mas.fromString('25'),
fee: Mas.fromString('0.1'),
});

Expand All @@ -50,24 +56,11 @@ while (migrateCount < counter) {
for (const event of events) {
console.log('migrate Events:', event.data);
}
migrateCount = await getMigrateCounter(provider, MNS_CONTRACT);
console.log('new migrate count:', migrateCount.toString());
}

console.log('Upgrade done ! operation:');

if (migrateCount === counter) {
const cleaned = getScByteCode('build', 'main.wasm');

op = await contract.call('upgradeSC', cleaned, {
fee: Mas.fromString('0.1'),
});
events = await op.getFinalEvents();

for (const event of events) {
console.log('upgradeSC Events:', event.data);
if (events.some((event) => event.data.includes('Migration done'))) {
console.log('Migration done ! yallahh:');
break;
}
console.log('upgradeSC done ! operation:', op.id);
}

console.log(
Expand Down
17 changes: 17 additions & 0 deletions smart-contract/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
bytesToStr,
Provider,
SmartContract,
strToBytes,
U256,
Web3Provider,
} from '@massalabs/massa-web3';
Expand Down Expand Up @@ -72,3 +73,19 @@ export async function getOwner(provider: Provider): Promise<string> {
const { value } = await contract.read('ownerAddress');
return bytesToStr(value);
}

export async function getDomainsFromTarget(
provider: Provider,
target: string,
): Promise<string[]> {
const ADDRESS_KEY_PREFIX_V2 = [0x6];
const targetBytes = strToBytes(target);
const prefix = Uint8Array.from([
...DOMAIN_SEPARATOR_KEY,
...ADDRESS_KEY_PREFIX_V2,
targetBytes.length,
...targetBytes,
]);
const keys = await provider.getStorageKeys(MNS_CONTRACT, prefix);
return keys.map((key) => bytesToStr(key.slice(prefix.length)));
}
Loading