Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions simple-storage/web3api-completed/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
w3
build
28 changes: 28 additions & 0 deletions simple-storage/web3api-completed/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# How To Run

## 1. Setup Test Env

```
npx w3 test-env up
```

## 2. Build & Deploy SimpleStorage Contract

```
node ./deploy-contracts.js
```

## 3. Build & Deploy The Web3API

```
npx w3 build \
--ipfs http://localhost:5001 \
--graph simplestorage,http://localhost:8020 \
--test-ens simplestorage.eth
```

## 4. Test The Web3API Using A Query Recipe

```
npx w3 query ./recipes/e2e.json --test-ens
```
25 changes: 25 additions & 0 deletions simple-storage/web3api-completed/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "templates-api-assemblyscript",
"description": "Web3API Assemblyscript Template",
"private": true,
"version": "0.0.1-prealpha.11",
"scripts": {
"build": "yarn build:contract && yarn build:web3api",
"build:web3api": "npx w3 build",
"build:contract": "node ./scripts/build-contract.js",
"test:env:up": "npx w3 test-env up",
"test:env:down": "npx w3 test-env down",
"deploy": "yarn deploy:contract && yarn deploy:web3api",
"deploy:web3api": "npx w3 build --ipfs http://localhost:5001 --test-ens simplestorage.eth",
"deploy:contract": "node ./scripts/deploy-contract.js",
"test": "npx w3 query ./recipes/e2e.json --test-ens"
},
"devDependencies": {
"@web3api/cli": "0.0.1-prealpha.11",
"@web3api/ethereum-plugin-js": "0.0.1-prealpha.11",
"@web3api/wasm-as": "0.0.1-prealpha.11",
"ethers": "5.0.8",
"js-yaml": "3.14.0",
"solc": "0.8.3"
}
}
3 changes: 3 additions & 0 deletions simple-storage/web3api-completed/recipes/constants.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"SimpleStorageAddr": "0x995629b19667Ae71483DC812c1B5a35fCaaAF4B8"
}
45 changes: 45 additions & 0 deletions simple-storage/web3api-completed/recipes/e2e.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[
{
"api": "ens/simplestorage.eth",
"constants": "./constants.json"
},
{
"query": "./get.graphql",
"variables": { "address": "$SimpleStorageAddr" }
},
{
"query": "./set.graphql",
"variables": {
"address": "$SimpleStorageAddr",
"value": 5
}
},
{
"query": "./get.graphql",
"variables": { "address": "$SimpleStorageAddr" }
},
{
"query": "./set.graphql",
"variables": {
"address": "$SimpleStorageAddr",
"value": 569
}
},
{
"query": "./get.graphql",
"variables": { "address": "$SimpleStorageAddr" }
},
{
"query": "./setIpfs.graphql",
"variables": {
"address": "$SimpleStorageAddr",
"data": "Hello from IPFS!"
}
},
{
"query": "./getIpfs.graphql",
"variables": {
"address": "$SimpleStorageAddr"
}
}
]
5 changes: 5 additions & 0 deletions simple-storage/web3api-completed/recipes/get.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
query GetData($address: String!) {
getData(
address: $address
)
}
5 changes: 5 additions & 0 deletions simple-storage/web3api-completed/recipes/getIpfs.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
query {
getIpfsData(
address: $address
)
}
6 changes: 6 additions & 0 deletions simple-storage/web3api-completed/recipes/set.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
mutation SetData($address: String!, $value: Int!) {
setData(
address: $address
value: $value
)
}
11 changes: 11 additions & 0 deletions simple-storage/web3api-completed/recipes/setIpfs.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
mutation {
setIpfsData(
options: {
address: $address
data: $data
}
) {
ipfsHash
txReceipt
}
}
76 changes: 76 additions & 0 deletions simple-storage/web3api-completed/scripts/build-contract.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const solc = require("solc");
const fs = require("fs");

async function main() {
// Fetch the contract's source
const contractSource = fs.readFileSync(
`${__dirname}/../src/contracts/SimpleStorage.sol`, 'utf-8'
);

// Compile the SimpleStorage contract using solc (Solidity compiler)
const output = JSON.parse(
solc.compile(
JSON.stringify({
language: "Solidity",
sources: {
"SimpleStorage.sol": {
content: contractSource
}
},
settings: {
outputSelection: {
'*': {
'*': ['*']
}
}
}
})
)
);

if (output.contracts) {
console.log("✔️ Compiled SimpleStorage.sol");
} else {
throw Error(`Error: Failed to compile SimpleStorage.sol.\n${JSON.stringify(output, null, 2)}`);
}

// Fetch the compiled contract's abi & bytecode
const contract = output.contracts["SimpleStorage.sol"]["SimpleStorage"];
const abi = JSON.stringify(contract.abi);
const bytecode = contract.evm.bytecode.object;

// Generate an Assemblyscript file containing the abi + bytecode
fs.writeFileSync(
`${__dirname}/../src/contracts/SimpleStorage.ts`,
`/// NOTE: This file is auto-generate, see deploy-contract.js
export const abi = \`${abi}\`;
export const bytecode = "0x${bytecode}";
`
);

console.log("✔️ Generated SimpleStorage.ts");

// Generate a JSON ABI file
fs.writeFileSync(
`${__dirname}/../src/contracts/SimpleStorage.json`,
JSON.stringify({
abi: contract.abi,
bytecode: `0x${bytecode}`
})
);

console.log("✔️ Generated SimpleStorage.json");
}

if (require.main === module) {
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
} else {
module.exports = {
main
};
}
49 changes: 49 additions & 0 deletions simple-storage/web3api-completed/scripts/deploy-contract.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const { EthereumPlugin } = require("@web3api/ethereum-plugin-js");
const fs = require("fs");
const buildContract = require("./build-contract");

async function main() {
// Ensure the contract is built
await buildContract.main();

// Fetch the contract's ABI
const contractAbi = JSON.parse(
fs.readFileSync(
`${__dirname}/../src/contracts/SimpleStorage.json`, 'utf-8'
)
);

// Deploy the contract to testnet
const eth = new EthereumPlugin({
provider: "http://localhost:8545"
});

const address = await eth.deployContract(
contractAbi.abi, contractAbi.bytecode
);

console.log(`✔️ SimpleStorage live at: ${address}`)

// Store the address in our recipes' constants file
const constants = require(`${__dirname}/../recipes/constants.json`);
constants.SimpleStorageAddr = address;
fs.writeFileSync(
`${__dirname}/../recipes/constants.json`,
JSON.stringify(constants, null, 2)
);

console.log("✔️ Recipe Constants Updated");
}

if (require.main === module) {
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
} else {
module.exports = {
main
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"abi":[{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"from","type":"address"},{"indexed":false,"internalType":"uint256","name":"data","type":"uint256"}],"name":"DataSet","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"from","type":"address"},{"indexed":false,"internalType":"string","name":"ipfsHash","type":"string"}],"name":"HashSet","type":"event"},{"inputs":[],"name":"get","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getHash","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"x","type":"uint256"}],"name":"set","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"string","name":"x","type":"string"}],"name":"setHash","outputs":[],"stateMutability":"nonpayable","type":"function"}],"bytecode":"0x608060405234801561001057600080fd5b506105d9806100206000396000f3fe608060405234801561001057600080fd5b506004361061004c5760003560e01c80631ed83fd41461005157806360fe47b11461006d5780636d4ce63c14610089578063d13319c4146100a7575b600080fd5b61006b600480360381019061006691906102f6565b6100c5565b005b6100876004803603810190610082919061033b565b610116565b005b610091610159565b60405161009e9190610465565b60405180910390f35b6100af610162565b6040516100bc9190610443565b60405180910390f35b8181600191906100d69291906101f4565b507f7701f49eb9aabe8890631508a9092eabb511a34566c30f2d94ff4420da1ccb1333838360405161010a939291906103e8565b60405180910390a15050565b806000819055507f7c94a94848d5859b1a30c887dc5740bf8d1cf789779be90adda1d0d34dd25022338260405161014e92919061041a565b60405180910390a150565b60008054905090565b6060600180546101719061051a565b80601f016020809104026020016040519081016040528092919081815260200182805461019d9061051a565b80156101ea5780601f106101bf576101008083540402835291602001916101ea565b820191906000526020600020905b8154815290600101906020018083116101cd57829003601f168201915b5050505050905090565b8280546102009061051a565b90600052602060002090601f0160209004810192826102225760008555610269565b82601f1061023b57803560ff1916838001178555610269565b82800160010185558215610269579182015b8281111561026857823582559160200191906001019061024d565b5b509050610276919061027a565b5090565b5b8082111561029357600081600090555060010161027b565b5090565b60008083601f8401126102a957600080fd5b8235905067ffffffffffffffff8111156102c257600080fd5b6020830191508360018202830111156102da57600080fd5b9250929050565b6000813590506102f08161058c565b92915050565b6000806020838503121561030957600080fd5b600083013567ffffffffffffffff81111561032357600080fd5b61032f85828601610297565b92509250509250929050565b60006020828403121561034d57600080fd5b600061035b848285016102e1565b91505092915050565b61036d8161049c565b82525050565b600061037f838561048b565b935061038c8385846104d8565b6103958361057b565b840190509392505050565b60006103ab82610480565b6103b5818561048b565b93506103c58185602086016104e7565b6103ce8161057b565b840191505092915050565b6103e2816104ce565b82525050565b60006040820190506103fd6000830186610364565b8181036020830152610410818486610373565b9050949350505050565b600060408201905061042f6000830185610364565b61043c60208301846103d9565b9392505050565b6000602082019050818103600083015261045d81846103a0565b905092915050565b600060208201905061047a60008301846103d9565b92915050565b600081519050919050565b600082825260208201905092915050565b60006104a7826104ae565b9050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000819050919050565b82818337600083830152505050565b60005b838110156105055780820151818401526020810190506104ea565b83811115610514576000848401525b50505050565b6000600282049050600182168061053257607f821691505b602082108114156105465761054561054c565b5b50919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b6000601f19601f8301169050919050565b610595816104ce565b81146105a057600080fd5b5056fea26469706673582212204f956d7623b7fe0a5a722d01575cc1d310a1a18086a604796ff457447881740864736f6c63430008030033"}
27 changes: 27 additions & 0 deletions simple-storage/web3api-completed/src/contracts/SimpleStorage.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
pragma solidity 0.8.3;

contract SimpleStorage {
uint256 data;
string ipfsHash;

event DataSet(address from, uint256 data);
event HashSet(address from, string ipfsHash);

function set(uint256 x) public {
data = x;
emit DataSet(msg.sender, x);
}

function get() public view returns (uint256) {
return data;
}

function setHash(string calldata x) public {
ipfsHash = x;
emit HashSet(msg.sender, x);
}

function getHash() public view returns (string memory) {
return ipfsHash;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/// NOTE: This file is auto-generate, see deploy-contract.js
export const abi = `[{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"from","type":"address"},{"indexed":false,"internalType":"uint256","name":"data","type":"uint256"}],"name":"DataSet","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"from","type":"address"},{"indexed":false,"internalType":"string","name":"ipfsHash","type":"string"}],"name":"HashSet","type":"event"},{"inputs":[],"name":"get","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getHash","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"x","type":"uint256"}],"name":"set","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"string","name":"x","type":"string"}],"name":"setHash","outputs":[],"stateMutability":"nonpayable","type":"function"}]`;
export const bytecode = "0x608060405234801561001057600080fd5b506105d9806100206000396000f3fe608060405234801561001057600080fd5b506004361061004c5760003560e01c80631ed83fd41461005157806360fe47b11461006d5780636d4ce63c14610089578063d13319c4146100a7575b600080fd5b61006b600480360381019061006691906102f6565b6100c5565b005b6100876004803603810190610082919061033b565b610116565b005b610091610159565b60405161009e9190610465565b60405180910390f35b6100af610162565b6040516100bc9190610443565b60405180910390f35b8181600191906100d69291906101f4565b507f7701f49eb9aabe8890631508a9092eabb511a34566c30f2d94ff4420da1ccb1333838360405161010a939291906103e8565b60405180910390a15050565b806000819055507f7c94a94848d5859b1a30c887dc5740bf8d1cf789779be90adda1d0d34dd25022338260405161014e92919061041a565b60405180910390a150565b60008054905090565b6060600180546101719061051a565b80601f016020809104026020016040519081016040528092919081815260200182805461019d9061051a565b80156101ea5780601f106101bf576101008083540402835291602001916101ea565b820191906000526020600020905b8154815290600101906020018083116101cd57829003601f168201915b5050505050905090565b8280546102009061051a565b90600052602060002090601f0160209004810192826102225760008555610269565b82601f1061023b57803560ff1916838001178555610269565b82800160010185558215610269579182015b8281111561026857823582559160200191906001019061024d565b5b509050610276919061027a565b5090565b5b8082111561029357600081600090555060010161027b565b5090565b60008083601f8401126102a957600080fd5b8235905067ffffffffffffffff8111156102c257600080fd5b6020830191508360018202830111156102da57600080fd5b9250929050565b6000813590506102f08161058c565b92915050565b6000806020838503121561030957600080fd5b600083013567ffffffffffffffff81111561032357600080fd5b61032f85828601610297565b92509250509250929050565b60006020828403121561034d57600080fd5b600061035b848285016102e1565b91505092915050565b61036d8161049c565b82525050565b600061037f838561048b565b935061038c8385846104d8565b6103958361057b565b840190509392505050565b60006103ab82610480565b6103b5818561048b565b93506103c58185602086016104e7565b6103ce8161057b565b840191505092915050565b6103e2816104ce565b82525050565b60006040820190506103fd6000830186610364565b8181036020830152610410818486610373565b9050949350505050565b600060408201905061042f6000830185610364565b61043c60208301846103d9565b9392505050565b6000602082019050818103600083015261045d81846103a0565b905092915050565b600060208201905061047a60008301846103d9565b92915050565b600081519050919050565b600082825260208201905092915050565b60006104a7826104ae565b9050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000819050919050565b82818337600083830152505050565b60005b838110156105055780820151818401526020810190506104ea565b83811115610514576000848401525b50505050565b6000600282049050600182168061053257607f821691505b602082108114156105465761054561054c565b5b50919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b6000601f19601f8301169050919050565b610595816104ce565b81146105a057600080fd5b5056fea26469706673582212204f956d7623b7fe0a5a722d01575cc1d310a1a18086a604796ff457447881740864736f6c63430008030033";
43 changes: 43 additions & 0 deletions simple-storage/web3api-completed/src/mutation/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {
Ethereum_Mutation,
Ipfs_Mutation,
Input_setData,
Input_setIpfsData,
SetIpfsDataResult,
} from "./w3";
import { abi, bytecode } from "../contracts/SimpleStorage";

export function setData(input: Input_setData): string {
return Ethereum_Mutation.sendTransaction({
address: input.address,
method: "function set(uint256 value)",
args: [input.value.toString()]
});
}

export function deployContract(): string {
return Ethereum_Mutation.deployContract({
abi,
bytecode
});
}

export function setIpfsData(input: Input_setIpfsData): SetIpfsDataResult {
// 1. Upload the data to IPFS
const ipfsHash = Ipfs_Mutation.addFile({
data: String.UTF8.encode(input.options.data),
});

// 2. Add the data's IPFS hash to SimpleStorage using `setHash(...)`
const txReceipt = Ethereum_Mutation.sendTransaction({
address: input.options.address,
method: 'function setHash(string value)',
args: [ipfsHash],
});

// 3. Return the result
return {
ipfsHash,
txReceipt,
};
}
25 changes: 25 additions & 0 deletions simple-storage/web3api-completed/src/mutation/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#import { Mutation } into Ethereum from "w3://ens/ethereum.web3api.eth"
#import { Mutation } into Ipfs from "w3://ens/ipfs.web3api.eth"

type Mutation {
setData(
address: String!
value: UInt32!
): String!

deployContract: String!

setIpfsData(
options: SetIpfsDataOptions!
): SetIpfsDataResult!
}

type SetIpfsDataOptions {
address: String!
data: String!
}

type SetIpfsDataResult {
ipfsHash: String!
txReceipt: String!
}
Loading