Skip to content

Commit

Permalink
Add deployer raw event (#393)
Browse files Browse the repository at this point in the history
* Improve deployer readability

* Generate powered-by

* Add comments

* fix according to review

* Fix CR

* Fix coins subKey

---------

Co-authored-by: massabot <[email protected]>
Co-authored-by: Grégory Libert <[email protected]>
  • Loading branch information
3 people authored Jun 7, 2024
1 parent 47c0c1a commit df9f3bb
Show file tree
Hide file tree
Showing 6 changed files with 1,070 additions and 850 deletions.
1,770 changes: 970 additions & 800 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@
"dependencies": {
"@massalabs/as-transformer": "^0.3.1",
"@massalabs/massa-web3": "^2.1.0",
"@massalabs/wallet-provider": "^1.2.0",
"eslint": "^8.46.0",
"jest-environment-jsdom": "^29.5.0",
"mkdirp": "^3.0.1",
"path": "^0.12.7",
"re": "^0.1.4",
"regex": "^0.1.1"
Expand Down
128 changes: 92 additions & 36 deletions packages/sc-deployer/assembly/contracts/deployer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
call,
functionExists,
hasOpKey,
generateRawEvent,
Address,
} from '@massalabs/massa-as-sdk';
import { Args } from '@massalabs/as-types';

Expand All @@ -18,47 +20,101 @@ const CONSTRUCTOR = 'constructor';
*
* @param _ - not used
*/
export function main(_: StaticArray<u8>): StaticArray<u8> {
let masterKey = new StaticArray<u8>(1);
masterKey[0] = 0x00;
export function main(_: StaticArray<u8>): void {
let nbSC = getNbSC();

const deployedSC: Address[] = [];

if (!hasOpKey(masterKey)) {
return [];
}
let nbSCSer = getOpData(masterKey);
let nbSC = new Args(nbSCSer).nextU64().unwrap();
for (let i: u64 = 0; i < nbSC; i++) {
let keyBaseArgs = new Args().add(i + 1);
let keyBaseCoins = new Args().add(i + 1);
let key = keyBaseArgs.serialize();
if (!hasOpKey(key)) {
return [];
}
let SCBytecode = getOpData(key);
const contractAddr = createSC(SCBytecode);
const contractAddr = createSC(getScByteCode(i));

if (functionExists(contractAddr, CONSTRUCTOR)) {
let argsIdent = new Uint8Array(1);
argsIdent[0] = 0x00;
let keyArgs = keyBaseArgs.add(argsIdent).serialize();
let coinsIdent = new Uint8Array(1);
coinsIdent[0] = 0x01;
let keyCoins = keyBaseCoins.add(coinsIdent).serialize();
let args: Args;
if (hasOpKey(keyArgs)) {
args = new Args(getOpData(keyArgs));
} else {
args = new Args();
}
let coins: u64;
if (hasOpKey(keyCoins)) {
coins = new Args(getOpData(keyCoins)).nextU64().unwrap();
} else {
coins = 0;
}
call(contractAddr, CONSTRUCTOR, args, coins);
call(contractAddr, CONSTRUCTOR, getConstructorArgs(i), getCoins(i));
}

generateEvent(`Contract deployed at address: ${contractAddr.toString()}`);
deployedSC.push(contractAddr);
}
return [];

generateRawEvent(
new Args().addSerializableObjectArray(deployedSC).serialize(),
);
}

/**
* Get the number of smart contract to deploy.
* @returns The number of smart contract to deploy.
* @throws if the number of smart contract is not defined.
*/
function getNbSC(): u64 {
const key: StaticArray<u8> = [0];

assert(
hasOpKey(key),
'The number of smart contracts to deploy is undefined.',
);
const raw = getOpData(key);
return new Args(raw).mustNext<u64>('nbSC');
}

/**
* Get the bytecode of the smart contract to deploy.
* @param i - The index of the smart contract.
* @returns The bytecode of the smart contract.
* @throws if the bytecode of the smart contract is not defined.
*/
function getScByteCode(i: u64): StaticArray<u8> {
const key = new Args().add(i + 1).serialize();
assert(hasOpKey(key), `No bytecode found for contract number: ${i + 1}`);
return getOpData(key);
}

/**
* Get the arguments key of the constructor function of the smart contract to deploy.
* @param i - The index of the smart contract.
* @returns The arguments key of the constructor function.
*/
function argsKey(i: u64): StaticArray<u8> {
const argsSubKey: StaticArray<u8> = [0];
return new Args()
.add(i + 1)
.add(argsSubKey)
.serialize();
}

/**
* Get the arguments of the constructor function of the smart contract to deploy.
* @param i - The index of the smart contract.
* @returns The arguments of the constructor function.
*/
function getConstructorArgs(i: u64): Args {
const keyArgs = argsKey(i);
return hasOpKey(keyArgs) ? new Args(getOpData(argsKey(i))) : new Args();
}

/**
* Get the coins key of the constructor function of the smart contract to deploy.
* @param i - The index of the smart contract.
* @returns The coins key of the constructor function.
*/
function coinsKey(i: u64): StaticArray<u8> {
let coinsSubKey: StaticArray<u8> = [1];

return new Args()
.add(i + 1)
.add(coinsSubKey)
.serialize();
}

/**
* Get the coins of the constructor function of the smart contract to deploy.
* @param i - The index of the smart contract.
* @returns The coins of the constructor function.
*/
function getCoins(i: u64): u64 {
let keyCoins = coinsKey(i);

return hasOpKey(keyCoins)
? new Args(getOpData(keyCoins)).next<u64>().unwrapOrDefault()
: 0;
}
4 changes: 2 additions & 2 deletions packages/sc-deployer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"author": "",
"license": "ISC",
"dependencies": {
"@massalabs/as-types": "^2.0.0",
"@massalabs/massa-as-sdk": "^2.5.4",
"@massalabs/as-types": "^2.1.0",
"@massalabs/massa-as-sdk": "^2.6.1-dev",
"@massalabs/massa-sc-compiler": "^0.1.0",
"@massalabs/massa-web3": "^3.0.2",
"@types/node": "^18.11.10",
Expand Down
12 changes: 6 additions & 6 deletions packages/sc-deployer/powered-by.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# Dependencies Report

The following is a list of all the dependencies of this project:
## [@massalabs/as-types](https://registry.npmjs.org/@massalabs/as-types/-/as-types-2.0.0.tgz)
## [@massalabs/as-types](https://registry.npmjs.org/@massalabs/as-types/-/as-types-2.1.0.tgz)

**License:** ISC - perpetual

**Used version:** 2.0.0
**Used version:** 2.1.0

**Many thanks to:**

## [@massalabs/massa-as-sdk](git+https://github.com/massalabs/massa-as-sdk.git)

**License:** (MIT AND Apache-2.0) - perpetual

**Used version:** 2.5.4
**Used version:** 2.6.1-dev.20240606144420

**Many thanks to:** [Massa Labs]([email protected])

Expand Down Expand Up @@ -45,23 +45,23 @@ The following is a list of all the dependencies of this project:

**License:** Apache-2.0 - perpetual

**Used version:** 0.27.19
**Used version:** 0.27.27

**Many thanks to:** [Daniel Wirtz]([email protected])

## [rimraf](git://github.com/isaacs/rimraf.git)

**License:** ISC - perpetual

**Used version:** 5.0.5
**Used version:** 5.0.7

**Many thanks to:** [Isaac Z. Schlueter]([email protected]> (http://blog.izs.me/)

## [typescript](git+https://github.com/Microsoft/TypeScript.git)

**License:** Apache-2.0 - perpetual

**Used version:** 5.2.2
**Used version:** 5.4.5

**Many thanks to:** Microsoft Corp.

4 changes: 0 additions & 4 deletions packages/sc-deployer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,10 @@ import {
IEvent,
u64ToBytes,
u8toByte,
strToBytes,
ON_MASSA_EVENT_DATA,
ON_MASSA_EVENT_ERROR,
MASSA_PROTOFILE_KEY,
PROTO_FILE_SEPARATOR,
EventPoller,
IEventFilter,
INodeStatus,
toMAS,
utils,
} from '@massalabs/massa-web3';
Expand Down

0 comments on commit df9f3bb

Please sign in to comment.