commit | 5db4734f07b8d4fce8e21a8d89d1d438568802c0 | [log] [tgz] |
---|---|---|
author | gopuman <gopuman@node0.gopuman-218604.resilientdb-pg0.utah.cloudlab.us> | Thu Sep 19 18:52:32 2024 -0600 |
committer | gopuman <gopuman@node0.gopuman-218604.resilientdb-pg0.utah.cloudlab.us> | Thu Sep 19 18:52:32 2024 -0600 |
tree | 4e932e791b28f689fdd83e2e65561fdb0b88adf2 | |
parent | a61e2e0c394dbcef403a9b35d14f06e05452f5d7 [diff] |
Code Revamp
This repository provides a GraphQL API that allows users to interact with smart contracts in the ResilientDB ecosystem through a set of GraphQL queries and mutations. The API leverages the rescontract-cli
tool to perform various functions, including creating accounts, compiling contracts, deploying contracts, and executing contract functions.
Before you begin, ensure you have the following prerequisites:
ResilientDB: A running instance of ResilientDB with the smart contracts service running. More information and setup instructions can be found here: ResilientDB
ResContract CLI: Install the rescontract-cli
tool globally. Follow the instructions in the ResContract CLI Repository to install and configure it.
npm install -g rescontract-cli
Node.js (version >= 14): Download and install Node.js
To set up the project, follow these steps:
Clone the repository:
git clone https://github.com/yourusername/smart-contracts-graphql.git cd smart-contracts-graphql
Install dependencies:
npm install
Ensure that the ResContract CLI
is properly configured. You need to set the ResDB_Home
environment variable or provide a config.yaml
file as required by the CLI.
Refer to the ResContract CLI README for detailed instructions.
Start the server using the following command:
npm start
The server will start running on port 4000
. You can access the GraphQL API at http://localhost:4000/graphql
.
The GraphQL API supports the following mutations to interact with smart contracts.
createAccount
Creates a new account using the specified configuration file.
Type: Mutation
Arguments:
config
(String!): Path to the configuration file.Returns: String - Address of the newly created account or an error message.
compileContract
Compiles a smart contract from the specified source path and outputs the compiled contract to the specified output path.
Type: Mutation
Arguments:
sourcePath
(String!): Path to the source file of the smart contract.outputPath
(String!): Path to the output file for the compiled contract.Returns: String - Success message or an error message.
deployContract
Deploys a smart contract using the specified configuration, contract file, contract name, arguments, and owner address.
Type: Mutation
Arguments:
config
(String!): Path to the configuration file.contract
(String!): Path to the contract JSON file.name
(String!): Name of the contract (include the full path and contract name).arguments
(String!): Constructor arguments for the contract (comma-separated).owner
(String!): Owner address.Returns: String - Details of the deployed contract or an error message.
executeContract
Executes a function of a deployed smart contract.
Type: Mutation
Arguments:
config
(String!): Path to the configuration file.sender
(String!): Address of the sender.contract
(String!): Address of the deployed contract.functionName
(String!): Name of the function to execute (include parameter types).arguments
(String!): Arguments for the function (comma-separated).Returns: String - Result of the function execution or an error message.
Here are some sample mutations you can run:
mutation { createAccount(config: "../path/to/service.config") }
Sample Response:
{ "data": { "createAccount": "0x3b706424119e09dcaad4acf21b10af3b33cde350" } }
Explanation: This mutation creates a new account using the specified configuration file. The response contains the address of the newly created account.
mutation { compileContract( sourcePath: "/path/to/token.sol", outputPath: "output.json" ) }
Sample Response:
{ "data": { "compileContract": "Compiled successfully to output.json" } }
Explanation: This mutation compiles the smart contract located at /path/to/token.sol
and outputs the compiled contract to output.json
.
mutation { deployContract( config: "../path/to/service.config", contract: "output.json", name: "token.sol:Token", arguments: "1000", owner: "0x3b706424119e09dcaad4acf21b10af3b33cde350" ) }
Sample Response:
{ "data": { "deployContract": "owner_address: \"0x3b706424119e09dcaad4acf21b10af3b33cde350\"\ncontract_address: \"0xc975ab41e0c2042a0229925a2f4f544747fd66cd\"\ncontract_name: \"token.sol:Token\"" } }
Explanation: This mutation deploys the compiled contract using the specified parameters. The response includes the owner address, contract address, and contract name.
mutation { executeContract( config: "../path/to/service.config", sender: "0x3b706424119e09dcaad4acf21b10af3b33cde350", contract: "0xc975ab41e0c2042a0229925a2f4f544747fd66cd", functionName: "transfer(address,uint256)", arguments: "0x4847155cbb6f2219ba9b7df50be11a1c7f23f829,100" ) }
Sample Response:
{ "data": { "executeContract": "Function executed successfully." } }
Explanation: This mutation executes the transfer
function of the deployed contract, transferring 100
tokens to 0x4847155cbb6f2219ba9b7df50be11a1c7f23f829
.
This project is licensed under the Apache License - see the LICENSE file for details.