-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
argsKey and coinsKey for populate datastore
- Loading branch information
Showing
3 changed files
with
86 additions
and
59 deletions.
There are no files selected for viewing
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
83 changes: 83 additions & 0 deletions
83
packages/massa-web3/src/experimental/basicElements/dataStore.ts
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,83 @@ | ||
import { u64ToBytes, u8toByte } from '@massalabs/web3-utils' | ||
import { ONE, ZERO } from '../utils' | ||
import { Args } from './args' | ||
|
||
const CONTRACT_LENGTH_KEY = 0x00 | ||
|
||
type DatastoreContract = { | ||
data: Uint8Array | ||
args: Uint8Array | ||
coins: bigint | ||
} | ||
|
||
/** | ||
* Generates a key for coin data in the datastore. | ||
* | ||
* @param offset - The offset to use when generating the key. | ||
* @returns A Uint8Array representing the key. | ||
*/ | ||
function coinsKey(offset: number): Uint8Array { | ||
return new Args() | ||
.addU64(BigInt(offset + ONE)) | ||
.addUint8Array(u8toByte(ONE)) | ||
.serialize() | ||
} | ||
|
||
/** | ||
* Generates a key for args data in the datastore. | ||
* | ||
* @param offset - The offset to use when generating the key. | ||
* @returns A Uint8Array representing the key. | ||
*/ | ||
function argsKey(offset: number): Uint8Array { | ||
return new Args() | ||
.addU64(BigInt(offset + ONE)) | ||
.addUint8Array(u8toByte(ZERO)) | ||
.serialize() | ||
} | ||
|
||
/** | ||
* Generates a key for contract data in the datastore. | ||
* | ||
* @param offset - The offset to use when generating the key. | ||
* @returns A Uint8Array representing the key. | ||
*/ | ||
function contractKey(offset: number): Uint8Array { | ||
return u64ToBytes(BigInt(offset + ONE)) | ||
} | ||
|
||
/** | ||
* Populates the datastore with the contracts. | ||
* | ||
* @remarks | ||
* This function is to be used in conjunction with the deployer smart contract. | ||
* The deployer smart contract expects to have an execution datastore in a specific state. | ||
* This function populates the datastore according to that expectation. | ||
* | ||
* @param contracts - The contracts to populate the datastore with. | ||
* | ||
* @returns The populated datastore. | ||
*/ | ||
export function populateDatastore( | ||
contracts: DatastoreContract[] | ||
): Map<Uint8Array, Uint8Array> { | ||
const datastore = new Map<Uint8Array, Uint8Array>() | ||
|
||
// set the number of contracts in the first key of the datastore | ||
datastore.set( | ||
new Uint8Array([CONTRACT_LENGTH_KEY]), | ||
u64ToBytes(BigInt(contracts.length)) | ||
) | ||
|
||
contracts.forEach((contract, i) => { | ||
datastore.set(contractKey(i), contract.data) | ||
if (contract.args) { | ||
datastore.set(argsKey(i), contract.args) | ||
} | ||
if (contract.coins > ZERO) { | ||
datastore.set(coinsKey(i), u64ToBytes(contract.coins)) | ||
} | ||
}) | ||
|
||
return datastore | ||
} |
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