Skip to content

Commit

Permalink
Clean repo (#525)
Browse files Browse the repository at this point in the history
* remove assemblyscript dep

* update wallet-provider

* cleanup formating config

* format code

* fix ci lint warning

* prettier

---------

Co-authored-by: BenRey <[email protected]>
  • Loading branch information
peterjah and Ben-Rey authored Feb 2, 2024
1 parent 90667ff commit 3325ff5
Show file tree
Hide file tree
Showing 113 changed files with 4,883 additions and 5,651 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module.exports = {
extends: ["@massalabs"],
extends: ["@massalabs","prettier"],
rules: {
"tsdoc/syntax": "warn",
"max-len": ["error", 200],
camelcase: "off",
"@typescript-eslint/no-unused-vars": "error",
'no-console': 'warn',
},
};
6 changes: 4 additions & 2 deletions .lintstagedrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"*.{js,ts,jsx,tsx,json,cjs}": ["npx eslint"],
"*.ts": ["npx as-prettier --check"]
"*.{js,ts,json,cjs,md}": [
"prettier --check",
"eslint"
]
}
9 changes: 9 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/** @type {import("prettier").Config} */
const config = {
trailingComma: 'es5',
tabWidth: 2,
semi: false,
singleQuote: true,
}

module.exports = config
6 changes: 3 additions & 3 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Config } from '@jest/types';
import type { Config } from '@jest/types'

const config: Config.InitialOptions = {
transform: {
Expand All @@ -18,6 +18,6 @@ const config: Config.InitialOptions = {
'<rootDir>/node_modules/',
'<rootDir>/packages/*/dist/',
],
};
}

export default config;
export default config
1,057 changes: 164 additions & 893 deletions package-lock.json

Large diffs are not rendered by default.

28 changes: 10 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,20 @@
"lint:fix": "eslint . --fix && npm run check-types --ws",
"prettier": "prettier --check .",
"prettier:fix": "prettier --write .",
"fmt": "npm run prettier:fix && npm run lint:fix",
"fmt:check": "npm run prettier && npm run lint"
"fmt": "npm run lint:fix && npm run prettier:fix",
"fmt:check": "npm run lint && npm run prettier"
},
"devDependencies": {
"@massalabs/eslint-config": "^0.0.9",
"@massalabs/prettier-config-as": "^0.0.2",
"@massalabs/eslint-config": "^0.0.10",
"@types/jest": "^29.5.5",
"@typescript-eslint/eslint-plugin": "^6.7.5",
"@typescript-eslint/eslint-plugin": "^6.20.0",
"bignumber.js": "^9.1.1",
"assemblyscript": "^0.27.22",
"eslint": "^8.51.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-standard": "^17.0.0",
"eslint-config-standard-with-typescript": "^39.1.1",
"eslint-plugin-import": "^2.28.1",
"eslint-plugin-jsdoc": "^40.0.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^6.1.1",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-jsdoc": "^48.0.4",
"husky": "^8.0.0",
"lint-staged": "^15.0.1",
"prettier": "^2.8.4",
"prettier": "^3.2.4",
"typescript": "^4.9.5"
},
"workspaces": [
Expand All @@ -45,6 +38,5 @@
"bugs": {
"url": "https://github.com/massalabs/massa-web3/issues"
},
"homepage": "https://github.com/massalabs/massa-web3#readme",
"prettier": "@massalabs/prettier-config-as"
}
"homepage": "https://github.com/massalabs/massa-web3#readme"
}
2 changes: 1 addition & 1 deletion packages/massa-web3/babel.config.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = { presets: ['@babel/preset-env'] };
module.exports = { presets: ['@babel/preset-env'] }
24 changes: 11 additions & 13 deletions packages/massa-web3/code-snippets/walletGenerateNewAccount.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { WalletClient } from '@massalabs/massa-web3';
import { WalletClient } from '@massalabs/massa-web3'

(async () => {
const newAccount = await WalletClient.walletGenerateNewAccount();
const newAccount = await WalletClient.walletGenerateNewAccount()

// print the account address, public and private keys
console.log(
'Address: ',
newAccount.address,
'Public key',
newAccount.publicKey,
'Private key',
newAccount.secretKey,
);
})();
// print the account address, public and private keys
console.log(

Check warning on line 6 in packages/massa-web3/code-snippets/walletGenerateNewAccount.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement

Check warning on line 6 in packages/massa-web3/code-snippets/walletGenerateNewAccount.ts

View workflow job for this annotation

GitHub Actions / test / build

Unexpected console statement

Check warning on line 6 in packages/massa-web3/code-snippets/walletGenerateNewAccount.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
'Address: ',
newAccount.address,
'Public key',
newAccount.publicKey,
'Private key',
newAccount.secretKey
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {
call,
functionExists,
hasOpKey,
} from '@massalabs/massa-as-sdk';
import { Args } from '@massalabs/as-types';
} from '@massalabs/massa-as-sdk'
import { Args } from '@massalabs/as-types'

const CONSTRUCTOR = 'constructor';
const CONSTRUCTOR = 'constructor'

/**
* This function deploys and calls the constructor function of the deployed smart contract.
Expand All @@ -19,44 +19,44 @@ const CONSTRUCTOR = 'constructor';
* @param _ - not used
*/
export function main(): StaticArray<u8> {
const masterKey = new StaticArray<u8>(1);
masterKey[0] = 0x00;
const masterKey = new StaticArray<u8>(1)
masterKey[0] = 0x00
if (!hasOpKey(masterKey)) {
return [];
return []
}
const nbSCSer = getOpData(masterKey);
const nbSC = new Args(nbSCSer).nextU64().unwrap();
const nbSCSer = getOpData(masterKey)
const nbSC = new Args(nbSCSer).nextU64().unwrap()
for (let i: u64 = 0; i < nbSC; i++) {
const keyBaseArgs = new Args().add(i + 1);
const keyBaseCoins = new Args().add(i + 1);
const key = keyBaseArgs.serialize();
const keyBaseArgs = new Args().add(i + 1)
const keyBaseCoins = new Args().add(i + 1)
const key = keyBaseArgs.serialize()
if (!hasOpKey(key)) {
return [];
return []
}
const SCBytecode = getOpData(key);
const contractAddr = createSC(SCBytecode);
const SCBytecode = getOpData(key)
const contractAddr = createSC(SCBytecode)
if (functionExists(contractAddr, CONSTRUCTOR)) {
const argsIdent = new Uint8Array(1);
argsIdent[0] = 0x00;
const keyArgs = keyBaseArgs.add(argsIdent).serialize();
const coinsIdent = new Uint8Array(1);
coinsIdent[0] = 0x01;
const keyCoins = keyBaseCoins.add(coinsIdent).serialize();
let args: Args;
const argsIdent = new Uint8Array(1)
argsIdent[0] = 0x00
const keyArgs = keyBaseArgs.add(argsIdent).serialize()
const coinsIdent = new Uint8Array(1)
coinsIdent[0] = 0x01
const keyCoins = keyBaseCoins.add(coinsIdent).serialize()
let args: Args
if (hasOpKey(keyArgs)) {
args = new Args(getOpData(keyArgs));
args = new Args(getOpData(keyArgs))
} else {
args = new Args();
args = new Args()
}
let coins: u64;
let coins: u64
if (hasOpKey(keyCoins)) {
coins = new Args(getOpData(keyCoins)).nextU64().unwrap();
coins = new Args(getOpData(keyCoins)).nextU64().unwrap()
} else {
coins = 0;
coins = 0
}
call(contractAddr, CONSTRUCTOR, args, coins);
call(contractAddr, CONSTRUCTOR, args, coins)
}
generateEvent(`Contract deployed at address: ${contractAddr.toString()}`);
generateEvent(`Contract deployed at address: ${contractAddr.toString()}`)
}
return [];
return []
}
108 changes: 53 additions & 55 deletions packages/massa-web3/examples/smartContracts/contracts/sc_code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,64 @@ import {
Context,
callerHasWriteAccess,
Storage,
} from '@massalabs/massa-as-sdk';
import { Args, Result, Serializable, stringToBytes } from '@massalabs/as-types';
} from '@massalabs/massa-as-sdk'
import { Args, Result, Serializable, stringToBytes } from '@massalabs/as-types'

const MUSIC_ALBUM_KEY = 'MUSIC_ALBUM_KEY';
const MUSIC_ALBUM_KEY = 'MUSIC_ALBUM_KEY'

class MusicAlbum implements Serializable {
constructor(
public id: string = '',
public title: string = '',
public artist: string = '',
public album: string = '',
public year: u64 = 0,
public year: u64 = 0
) {}

public serialize(): StaticArray<u8> {
const args = new Args();
args.add(this.id);
args.add(this.title);
args.add(this.artist);
args.add(this.album);
args.add(this.year);
return args.serialize();
const args = new Args()
args.add(this.id)
args.add(this.title)
args.add(this.artist)
args.add(this.album)
args.add(this.year)
return args.serialize()
}

public deserialize(data: StaticArray<u8>, offset: i32 = 0): Result<i32> {
const args = new Args(data, offset);
const args = new Args(data, offset)

const id = args.nextString();
const id = args.nextString()
if (id.isErr()) {
return new Result(0, "Can't deserialize the id");
return new Result(0, "Can't deserialize the id")
}
this.id = id.unwrap();
this.id = id.unwrap()

const title = args.nextString();
const title = args.nextString()
if (title.isErr()) {
return new Result(0, "Can't deserialize the title");
return new Result(0, "Can't deserialize the title")
}
this.title = title.unwrap();
this.title = title.unwrap()

const artist = args.nextString();
const artist = args.nextString()
if (artist.isErr()) {
return new Result(0, "Can't deserialize the artist");
return new Result(0, "Can't deserialize the artist")
}
this.artist = artist.unwrap();
this.artist = artist.unwrap()

const album = args.nextString();
const album = args.nextString()
if (album.isErr()) {
return new Result(0, "Can't deserialize the album");
return new Result(0, "Can't deserialize the album")
}
this.album = album.unwrap();
this.album = album.unwrap()

const year = args.nextU64();
const year = args.nextU64()
if (year.isErr()) {
return new Result(0, "Can't deserialize the year");
return new Result(0, "Can't deserialize the year")
}
this.year = year.unwrap();
this.year = year.unwrap()

return new Result(args.offset);
return new Result(args.offset)
}
}

Expand All @@ -73,73 +73,71 @@ export function constructor(): StaticArray<u8> {
// This line is important. It ensure that this function can't be called in the future.
// If you remove this check someone could call your constructor function and reset your SC.
if (!callerHasWriteAccess()) {
return [];
return []
}
generateEvent(
`Constructor called on contract ${Context.callee().toString()}`,
);
createInitialMusicShop();
return [];
generateEvent(`Constructor called on contract ${Context.callee().toString()}`)
createInitialMusicShop()
return []
}

function createInitialMusicShop(): void {
const musicAlbum = new MusicAlbum('1', 'CD', 'Nirvana', 'Nevermind', 1991);
const musicAlbum = new MusicAlbum('1', 'CD', 'Nirvana', 'Nevermind', 1991)
Storage.set(
stringToBytes(`${MUSIC_ALBUM_KEY}_${musicAlbum.id}`),
musicAlbum.serialize(),
);
musicAlbum.serialize()
)
}

export function addMusicAlbum(binaryArgs: StaticArray<u8>): void {
// convert the binary input to Args
const args: Args = new Args(binaryArgs);
const args: Args = new Args(binaryArgs)

// safely unwrap the request data
const musicAlbum: MusicAlbum = args.nextSerializable<MusicAlbum>().unwrap();
const musicAlbum: MusicAlbum = args.nextSerializable<MusicAlbum>().unwrap()

// assemble the storage key
const storageKey = `${MUSIC_ALBUM_KEY}_${musicAlbum.id}`;
const storageKey = `${MUSIC_ALBUM_KEY}_${musicAlbum.id}`

// check music album does not already exist
const musicAlbumExists = Storage.has(storageKey);
assert(!musicAlbumExists, 'Music Album already exists');
const musicAlbumExists = Storage.has(storageKey)
assert(!musicAlbumExists, 'Music Album already exists')

// save music album to storage
Storage.set(stringToBytes(storageKey), musicAlbum.serialize());
Storage.set(stringToBytes(storageKey), musicAlbum.serialize())
}

export function getMusicAlbum(binaryArgs: StaticArray<u8>): StaticArray<u8> {
// convert the binary input to Args
const args = new Args(binaryArgs);
const args = new Args(binaryArgs)

// safely unwrap the request data
const musicAlbumId = args.nextString().unwrap();
const musicAlbumId = args.nextString().unwrap()

// assemble the storage key
const storageKey = `${MUSIC_ALBUM_KEY}_${musicAlbumId}`;
const storageKey = `${MUSIC_ALBUM_KEY}_${musicAlbumId}`

// check music album must already exist
const musicAlbumExists = Storage.has(storageKey);
assert(musicAlbumExists, 'Music Album does not exist');
const musicAlbumExists = Storage.has(storageKey)
assert(musicAlbumExists, 'Music Album does not exist')

// get the serialized music album info
return Storage.get(stringToBytes(storageKey));
return Storage.get(stringToBytes(storageKey))
}

export function deleteMusicAlbum(binaryArgs: StaticArray<u8>): void {
// convert the binary input to Args
const args = new Args(binaryArgs);
const args = new Args(binaryArgs)

// safely unwrap the request data
const musicAlbumId = args.nextString().unwrap();
const musicAlbumId = args.nextString().unwrap()

// assemble the storage key
const storageKey = `${MUSIC_ALBUM_KEY}_${musicAlbumId}`;
const storageKey = `${MUSIC_ALBUM_KEY}_${musicAlbumId}`

// check music album must already exist
const musicAlbumExists = Storage.has(storageKey);
assert(musicAlbumExists, 'Music Album does not exist');
const musicAlbumExists = Storage.has(storageKey)
assert(musicAlbumExists, 'Music Album does not exist')

// delete the serialized music album info
return Storage.del(stringToBytes(storageKey));
return Storage.del(stringToBytes(storageKey))
}
Loading

0 comments on commit 3325ff5

Please sign in to comment.