Skip to content

Commit

Permalink
[breaking] convert coins to mas in read smart contract
Browse files Browse the repository at this point in the history
Breaking change in the read smart contract function: you must now put the value in nanoMASSA.

* convert coins to MAS in readSmartContract

* various improvement, clean code

* improve contributing

* Add unit in comment doc

---------

Co-authored-by: BenRey <[email protected]>
  • Loading branch information
Thykof and Ben-Rey authored Feb 15, 2024
1 parent 68c94b8 commit 9c17cf0
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .lintstagedrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"*.{js,ts,json,cjs,md}": [
"*.{js,ts,json,cjs}": [
"prettier --check",
"eslint"
]
Expand Down
6 changes: 4 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ To start developing with massa-web3, you must install all the necessary dev depe

```sh
npm install
npm run build
```

This will install all the required packages listed in the package.json file, allowing you to update, fix, or improve massa-web3 in any way you see fit.
Expand All @@ -30,7 +31,7 @@ To contribute code, please follow these steps:
4. Push your branch to your fork.
5. Create a pull request from your branch to the develop branch of the massa-web3 repository.

> **NOTE:** When creating a pull request, please include a clear and concise title and description of your changes, as well as any relevant context or background information.
> **NOTE:** When creating a pull request, please include a clear and concise title and description of your changes, as well as any relevant context or background information.
## Contributing Namespaces

Expand Down Expand Up @@ -89,4 +90,5 @@ To generate the documentation for a specific branch, run the following command:
```sh
npm run doc
```
The documentation will be generated inside each of the packages in the `./docs/documentation/html` directory.

The documentation will be generated inside each of the packages in the `./docs/documentation/html` directory.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ Welcome to the Massa Web3 Workspace. This space houses a collection of projects
## Projects within the Workspace

### 1. **Massa-web3**

![check-code-coverage](https://img.shields.io/badge/coverage-94.69%25-green)

**Description:**
Massa-web3 is a TypeScript library that facilitates communication with the Massa blockchain. It's your key to extracting data, interfacing with smart contracts, monitoring blockchain events, and much more.

- [📖 Read the full `Massa-web3` README](./packages/massa-web3/README.md) for detailed installation instructions, prerequisites, and additional resources.

### 2. **Web3-Utils**
### 2. **Web3-Utils**

![check-code-coverage](https://img.shields.io/badge/coverage-93.15%25-green)

Expand All @@ -31,4 +31,3 @@ If you would like to contribute to `massa-web3`, please read the [CONTRIBUTING f
## License

All projects within the Massa Web3 Workspace are released under the [MIT License](LICENSE).

4 changes: 3 additions & 1 deletion packages/massa-web3/src/interfaces/ICallData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ import { Args } from '@massalabs/web3-utils'
*
* @see fee of type `bigint` represents the transaction fee.
* @see maxGas of type `bigint` represents the maximum amount of gas that the execution of the contract is allowed to cost.
* @see coins of type `bigint` represents the extra coins that are spent from the caller's balance and transferred to the target.
* @see coins of type `bigint` represents the extra coins in `nanoMassa` that are spent from the caller's balance and transferred to the target.
* @see targetAddress of type `string` represents the target smart contract address.
* @see targetFunction of type `string` represents the target function name. No function is called if empty.
* @see parameter of type `Array<number>` or an Args represents the parameters to pass to the target function.
*/
export interface ICallData {
/** The fee amount in nanoMassa. */
fee: bigint
maxGas?: bigint
/** The coin amount in nanoMassa. */
coins?: bigint
targetAddress: string
targetFunction: string
Expand Down
4 changes: 3 additions & 1 deletion packages/massa-web3/src/interfaces/IReadData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import { Args } from '@massalabs/web3-utils'
* @see parameter - Parameter to pass to the target function
* @see callerAddress - Caller address
* @see fee of type `bigint` represents the transaction fee.
* @see coins of type `bigint` represents the extra coins that are spent from the caller's balance and transferred to the target.
* @see coins of type `bigint` represents the extra coins in `nanoMassa` that are spent from the caller's balance and transferred to the target.
*/
export interface IReadData {
maxGas?: bigint
targetAddress: string
targetFunction: string
parameter: Array<number> | Args
callerAddress?: string
/** The coin amount in nanoMassa. */
coins?: bigint
/** The fee amount in nanoMassa. */
fee?: bigint
}
18 changes: 10 additions & 8 deletions packages/massa-web3/src/web3/SmartContractsClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
Args,
fromMAS,
MAX_GAS_CALL,
toMAS,
} from '@massalabs/web3-utils'
import { wait } from '../utils/time'

Expand Down Expand Up @@ -95,7 +96,7 @@ export class SmartContractsClient
): Promise<string> {
const sender = executor || this.walletClient.getBaseAccount()
if (!sender) {
throw new Error(`No tx sender available`)
throw new Error('No tx sender available')
}
return await sender.deploySmartContract(contractData)
}
Expand All @@ -118,7 +119,7 @@ export class SmartContractsClient
): Promise<string> {
const sender = executor || this.walletClient.getBaseAccount()
if (!sender) {
throw new Error(`No tx sender available`)
throw new Error('No tx sender available')
}
// check the max. allowed gas
if (callData.maxGas > MAX_GAS_CALL) {
Expand Down Expand Up @@ -147,8 +148,8 @@ export class SmartContractsClient

if (callData.maxGas === null || callData.maxGas === undefined) {
try {
const reponse = await this.readSmartContract(callData)
callData.maxGas = BigInt(reponse.info.gas_cost)
const response = await this.readSmartContract(callData)
callData.maxGas = BigInt(response.info.gas_cost)
} catch (error) {
throw new Error(
`Operation failed: Max gas unspecified and auto-estimation failed. Error details: ${error.message}`
Expand Down Expand Up @@ -195,9 +196,10 @@ export class SmartContractsClient
target_function: readData.targetFunction,
parameter: readData.parameter,
caller_address: readData.callerAddress || baseAccountSignerAddress,
coins: readData.coins?.toString(),
coins: toMAS(readData.coins || BigInt(0)).toString(),
fee: readData.fee?.toString(),
}

// returns operation ids
const jsonRpcRequestMethod = JSON_RPC_REQUEST_METHOD.EXECUTE_READ_ONLY_CALL
let jsonRpcCallResult: Array<IContractReadOperationData> = []
Expand All @@ -213,7 +215,7 @@ export class SmartContractsClient

if (jsonRpcCallResult.length <= 0) {
throw new Error(
`Read operation bad response. No results array in json rpc response. Inspect smart contract`
'Read operation bad response. No results array in json rpc response. Inspect smart contract'
)
}
if (jsonRpcCallResult[0].result.Error) {
Expand Down Expand Up @@ -303,11 +305,11 @@ export class SmartContractsClient
contractData: IContractData
): Promise<IExecuteReadOnlyResponse> {
if (!contractData.contractDataBinary) {
throw new Error(`Expected non-null contract bytecode, but received null.`)
throw new Error('Expected non-null contract bytecode, but received null.')
}

if (!contractData.address) {
throw new Error(`Expected contract address, but received null.`)
throw new Error('Expected contract address, but received null.')
}

const data = {
Expand Down
6 changes: 3 additions & 3 deletions packages/massa-web3/test/web3/smartContractsClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { IBalance } from '../../src/interfaces/IBalance'
import { JSON_RPC_REQUEST_METHOD } from '../../src/interfaces/JsonRpcMethods'
import { EOperationStatus } from '../../src/interfaces/EOperationStatus'
import { fromMAS, MAX_GAS_CALL } from '@massalabs/web3-utils'
import { fromMAS, MAX_GAS_CALL, toMAS } from '@massalabs/web3-utils'
import { PublicApiClient } from '../../src/web3/PublicApiClient'
import { SmartContractsClient } from '../../src/web3/SmartContractsClient'
import { WalletClient } from '../../src/web3/WalletClient'
Expand Down Expand Up @@ -403,7 +403,7 @@ describe('SmartContractsClient', () => {
target_function: mockReadData.targetFunction,
parameter: mockReadData.parameter,
caller_address: mockReadData.callerAddress,
coins: mockReadData.coins?.toString(),
coins: toMAS(mockReadData.coins || 0n).toString(),
fee: mockReadData.fee?.toString(),
},
],
Expand Down Expand Up @@ -476,7 +476,7 @@ describe('SmartContractsClient', () => {
target_function: mockReadData.targetFunction,
parameter: mockReadData.parameter,
caller_address: mockReadData.callerAddress,
coins: mockReadData.coins?.toString(),
coins: toMAS(mockReadData.coins || 0n).toString(),
fee: mockReadData.fee?.toString(),
},
],
Expand Down

0 comments on commit 9c17cf0

Please sign in to comment.