-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
758 additions
and
23 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { MNS_CONTRACTS } from '@massalabs/massa-web3'; | ||
|
||
// export const MNS_CONTRACT = | ||
// 'AS1q83qN8Un5UDUkZP3sFi78g1YKVsRKxXMuoA4MKYqywmiuqJWA'; | ||
export const MNS_CONTRACT = MNS_CONTRACTS.mainnet; | ||
export const IS_MAINNET = true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { | ||
getDomains, | ||
getOwner, | ||
getTokenCounter, | ||
getTotalSupply, | ||
initProvider, | ||
} from './utils'; | ||
import { MNS_CONTRACT } from './config'; | ||
|
||
const provider = await initProvider(); | ||
|
||
const owner = await getOwner(provider); | ||
console.log('Contract owner:', owner); | ||
|
||
const count = await getTokenCounter(provider, MNS_CONTRACT); | ||
console.log('COUNTER KEY:', count.toString()); | ||
|
||
const domainKeys = await getDomains(provider); | ||
console.log('Total domains:', domainKeys.length); | ||
|
||
try { | ||
const totalSupply = await getTotalSupply(provider); | ||
console.log('Total supply:', totalSupply.toString()); | ||
} catch (e) { | ||
console.log('No total supply found'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { SmartContract, Mas, Operation, rpcTypes } from '@massalabs/massa-web3'; | ||
import { | ||
getMigrateCounter, | ||
getScByteCode, | ||
getTokenCounter, | ||
initProvider, | ||
} from './utils'; | ||
import { MNS_CONTRACT } from './config'; | ||
|
||
const provider = await initProvider(); | ||
|
||
const byteCode = getScByteCode('build', 'main_mig.wasm'); | ||
const contract = new SmartContract(provider, MNS_CONTRACT); | ||
|
||
console.log( | ||
'Contract initial balance:', | ||
Mas.toString(await provider.client.getBalance(MNS_CONTRACT)), | ||
); | ||
|
||
let op: Operation; | ||
let events: rpcTypes.OutputEvents; | ||
op = await contract.call('upgradeSC', byteCode, { | ||
coins: Mas.fromString('3'), | ||
fee: Mas.fromString('0.1'), | ||
}); | ||
events = await op.getFinalEvents(); | ||
|
||
for (const event of events) { | ||
console.log('upgradeSC Events:', event.data); | ||
} | ||
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'); | ||
} | ||
|
||
while (migrateCount < counter) { | ||
console.log('migrating batch from tokenID', migrateCount.toString()); | ||
op = await contract.call('migrate', undefined, { | ||
coins: Mas.fromString('30'), | ||
fee: Mas.fromString('0.1'), | ||
}); | ||
|
||
events = await op.getFinalEvents(); | ||
|
||
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); | ||
} | ||
console.log('upgradeSC done ! operation:', op.id); | ||
} | ||
|
||
console.log( | ||
'Contract Final balance:', | ||
Mas.toString(await provider.client.getBalance(MNS_CONTRACT)), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { SmartContract } from '@massalabs/massa-web3'; | ||
import { initProvider } from './utils'; | ||
import { MNS_CONTRACT } from './config'; | ||
|
||
const provider = await initProvider(); | ||
|
||
const contract = new SmartContract(provider, MNS_CONTRACT); | ||
|
||
const op = await contract.call('dnsLock', undefined, { coins: 100000000n }); | ||
const events = await op.getSpeculativeEvents(); | ||
|
||
for (const event of events) { | ||
console.log('pause Events:', event.data); | ||
} | ||
|
||
console.log('Contract paused ! operation:', op.id); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { SmartContract } from '@massalabs/massa-web3'; | ||
import { initProvider } from './utils'; | ||
import { MNS_CONTRACT } from './config'; | ||
|
||
const provider = await initProvider(); | ||
const contract = new SmartContract(provider, MNS_CONTRACT); | ||
|
||
const op = await contract.call('dnsUnlock'); | ||
const events = await op.getSpeculativeEvents(); | ||
|
||
for (const event of events) { | ||
console.log('Unpause Events:', event.data); | ||
} | ||
|
||
console.log('Contract unpaused ! operation:', op.id); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,74 @@ | ||
import { readFileSync } from 'fs'; | ||
import { fileURLToPath } from 'url'; | ||
import path from 'path'; | ||
import { | ||
Account, | ||
bytesToStr, | ||
Provider, | ||
SmartContract, | ||
U256, | ||
Web3Provider, | ||
} from '@massalabs/massa-web3'; | ||
import { IS_MAINNET, MNS_CONTRACT } from './config'; | ||
import 'dotenv/config'; | ||
|
||
export function getScByteCode(folderName: string, fileName: string): Buffer { | ||
// Obtain the current file name and directory paths | ||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = path.dirname(path.dirname(__filename)); | ||
return readFileSync(path.join(__dirname, folderName, fileName)); | ||
} | ||
|
||
export async function initProvider() { | ||
const account = await Account.fromEnv(); | ||
return IS_MAINNET | ||
? Web3Provider.mainnet(account) | ||
: Web3Provider.buildnet(account); | ||
} | ||
|
||
export const DOMAIN_SEPARATOR_KEY = [0x42]; | ||
|
||
export async function getTokenCounter( | ||
provider: Provider, | ||
contract: string, | ||
): Promise<bigint> { | ||
const COUNTER_KEY = Uint8Array.from([0x0]); | ||
const counterData = await provider.readStorage(contract, [COUNTER_KEY]); | ||
return U256.fromBytes(counterData[0]); | ||
} | ||
|
||
export async function getMigrateCounter( | ||
provider: Provider, | ||
contract: string, | ||
): Promise<bigint> { | ||
const MIGRATE_COUNTER_KEY_IDX = [0x06]; | ||
const MIGRATE_COUNTER_KEY = Uint8Array.from([ | ||
...DOMAIN_SEPARATOR_KEY, | ||
...MIGRATE_COUNTER_KEY_IDX, | ||
]); | ||
const counterData = await provider.readStorage(contract, [ | ||
MIGRATE_COUNTER_KEY, | ||
]); | ||
return U256.fromBytes(counterData[0]); | ||
} | ||
|
||
export async function getDomains(provider: Provider): Promise<Uint8Array[]> { | ||
const TOKEN_ID_KEY_PREFIX = [0x1]; | ||
const tokenIdsFilter = Uint8Array.from([ | ||
...DOMAIN_SEPARATOR_KEY, | ||
...TOKEN_ID_KEY_PREFIX, | ||
]); | ||
return provider.getStorageKeys(MNS_CONTRACT, tokenIdsFilter); | ||
} | ||
|
||
export async function getTotalSupply(provider: Provider): Promise<bigint> { | ||
const contract = new SmartContract(provider, MNS_CONTRACT); | ||
const { value } = await contract.read('totalSupply'); | ||
return U256.fromBytes(value); | ||
} | ||
|
||
export async function getOwner(provider: Provider): Promise<string> { | ||
const contract = new SmartContract(provider, MNS_CONTRACT); | ||
const { value } = await contract.read('ownerAddress'); | ||
return bytesToStr(value); | ||
} |