-
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.
190 call updatewebsite index after deploying a deweb sc (#191)
* Add a simple DeWeb index lib * Set a fix massa-web3 version to avoid bugs due to new versions * Remove useless console logs * Call DeWeb index updateWebsite function after a deploySC
- Loading branch information
1 parent
998c184
commit 4696217
Showing
10 changed files
with
211 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,7 @@ | ||
export const BUILDNET_INDEX_ADDRESS = | ||
'AS1TmA4GNpSYBseNNMXpbAp2trUwZxZy3T1sZ9Qd3Qdn9L8wGbMS' | ||
|
||
// TODO: Replace with mainnet address when available | ||
export const MAINNET_INDEX_ADDRESS = '' | ||
|
||
export const updateWebsiteFunctionName = 'updateWebsite' |
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,72 @@ | ||
import { | ||
Args, | ||
Mas, | ||
Operation, | ||
Provider, | ||
SmartContract, | ||
} from '@massalabs/massa-web3' | ||
|
||
import { storageCostForEntry } from '../utils/storage' | ||
import { updateWebsiteFunctionName } from './const' | ||
import { addressToOwnerBaseKey, indexByOwnerBaseKey } from './keys' | ||
import { getWebsiteOwner } from './read' | ||
import { getOwnerFromWebsiteSC, getSCAddress } from './utils' | ||
|
||
/** | ||
* Get the owner of a website using its 'OWNER' storage key | ||
* @param provider - The provider instance | ||
* @param address - The address of the website | ||
*/ | ||
export async function updateWebsite( | ||
provider: Provider, | ||
address: string | ||
): Promise<Operation> { | ||
const args = new Args().addString(address) | ||
|
||
const scAddress = getSCAddress((await provider.networkInfos()).chainId) | ||
const sc = new SmartContract(provider, scAddress) | ||
|
||
const estimatedCost = await estimateCost(sc, address) | ||
|
||
return sc.call(updateWebsiteFunctionName, args, { | ||
coins: estimatedCost, | ||
}) | ||
} | ||
|
||
/** | ||
* Estimate the cost in coins to update a website. | ||
* @param sc - The smart contract instance | ||
*/ | ||
async function estimateCost( | ||
sc: SmartContract, | ||
address: string | ||
): Promise<Mas.Mas> { | ||
return getWebsiteOwner(sc.provider, address) | ||
.then(async (registeredOwner) => { | ||
const scOwner = await getOwnerFromWebsiteSC(sc, address) | ||
|
||
return storageCostForEntry( | ||
BigInt(Math.abs(scOwner.length - registeredOwner.length)), | ||
0n | ||
) | ||
}) | ||
.catch(async () => { | ||
// The website does not exist in the index, we have to create it | ||
const owner = await getOwnerFromWebsiteSC(sc, address) | ||
const addressToOwnerPrefix = addressToOwnerBaseKey(address) | ||
const indexByOwnerPrefix = indexByOwnerBaseKey(owner) | ||
|
||
const addressToOwnerKeyCost = storageCostForEntry( | ||
BigInt(addressToOwnerPrefix.length) + BigInt(owner.length), | ||
0n | ||
) | ||
const indexByOwnerKeyCost = storageCostForEntry( | ||
BigInt(indexByOwnerPrefix.length) + BigInt(address.length), | ||
0n | ||
) | ||
|
||
const totalCost = addressToOwnerKeyCost + indexByOwnerKeyCost | ||
|
||
return totalCost | ||
}) | ||
} |
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,41 @@ | ||
import { I32, strToBytes } from '@massalabs/massa-web3' | ||
|
||
/** | ||
* Returns the base key for the owner's address. | ||
* @param address - The website address | ||
* @returns The base key for the owner's address | ||
*/ | ||
export function addressToOwnerBaseKey(address: string): Uint8Array { | ||
const prefix = strToBytes('\x01') | ||
const lengthBytes = I32.toBytes(BigInt(address.length)) | ||
const addressBytes = strToBytes(address) | ||
|
||
const result = new Uint8Array( | ||
prefix.length + lengthBytes.length + addressBytes.length | ||
) | ||
result.set(prefix, 0) | ||
result.set(lengthBytes, prefix.length) | ||
result.set(addressBytes, prefix.length + lengthBytes.length) | ||
|
||
return result | ||
} | ||
|
||
/** | ||
* Returns the base key for the owner's list of websites. | ||
* @param owner - The owner's address | ||
* @returns The base key for the owner's list of websites | ||
*/ | ||
export function indexByOwnerBaseKey(owner: string): Uint8Array { | ||
const prefix = strToBytes('\x00') | ||
const lengthBytes = I32.toBytes(BigInt(owner.length)) | ||
const ownerBytes = strToBytes(owner) | ||
|
||
const result = new Uint8Array( | ||
prefix.length + lengthBytes.length + ownerBytes.length | ||
) | ||
result.set(prefix, 0) | ||
result.set(lengthBytes, prefix.length) | ||
result.set(ownerBytes, prefix.length + lengthBytes.length) | ||
|
||
return result | ||
} |
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,27 @@ | ||
import { bytesToStr, Provider } from '@massalabs/massa-web3' | ||
|
||
import { addressToOwnerBaseKey } from './keys' | ||
import { getSCAddress } from './utils' | ||
|
||
/** | ||
* Get the owner of a website according to the index smart contract. | ||
* @param sc - The smart contract instance | ||
* @param address - The address of the website | ||
* @returns The owner of the website | ||
*/ | ||
export async function getWebsiteOwner( | ||
provider: Provider, | ||
address: string | ||
): Promise<string> { | ||
const scAddress = getSCAddress((await provider.networkInfos()).chainId) | ||
const prefix = addressToOwnerBaseKey(address) | ||
|
||
const keys = await provider.getStorageKeys(scAddress, prefix) | ||
if (keys.length === 0) { | ||
return '' | ||
} | ||
|
||
const ownerKey = keys[0] | ||
const ownerKeySliced = ownerKey.slice(prefix.length) | ||
return bytesToStr(ownerKeySliced) | ||
} |
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,37 @@ | ||
import { bytesToStr, CHAIN_ID, SmartContract } from '@massalabs/massa-web3' | ||
|
||
import { BUILDNET_INDEX_ADDRESS } from './const' | ||
|
||
/** | ||
* Get the owner of a website using its 'OWNER' storage key | ||
* @param sc - The smart contract instance | ||
* @param address - The address of the website | ||
* @returns The owner of the website | ||
*/ | ||
export async function getOwnerFromWebsiteSC( | ||
sc: SmartContract, | ||
address: string | ||
): Promise<string> { | ||
const ownerAddress = await sc.provider.readStorage(address, ['OWNER'], true) | ||
if (ownerAddress.length === 0) { | ||
throw new Error(`Could not find owner for website ${address}`) | ||
} | ||
|
||
return bytesToStr(ownerAddress[0]) | ||
} | ||
|
||
/** | ||
* Get the index smart contract address for a given chain id | ||
* @param chainId - The chain id of the network to get the index smart contract address for | ||
* @returns The index smart contract address | ||
*/ | ||
export function getSCAddress(chainId: bigint): string { | ||
switch (chainId) { | ||
case CHAIN_ID.Mainnet: | ||
throw new Error('Mainnet is not supported yet') | ||
case CHAIN_ID.Buildnet: | ||
return BUILDNET_INDEX_ADDRESS | ||
default: | ||
throw new Error('Unsupported network') | ||
} | ||
} |
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