Skip to content

Instantly share code, notes, and snippets.

@NoahMarconi
Last active June 18, 2018 18:23
Show Gist options
  • Save NoahMarconi/ad18f8db6296272d34599c1c58e8390d to your computer and use it in GitHub Desktop.
Save NoahMarconi/ad18f8db6296272d34599c1c58e8390d to your computer and use it in GitHub Desktop.
pragma solidity ^0.4.24;
pragma experimental ABIEncoderV2;
contract ABIExample {
enum Permission { ReadOnly, Write, Admin }
struct User {
uint256 id;
Permission permission;
}
User[] users;
constructor() public {
users.push(User(uint256(111), Permission.Admin));
users.push(User(uint256(222), Permission.Write));
users.push(User(uint256(333), Permission.ReadOnly));
}
function getUsers() public constant returns (User[]) {
return users;
}
// New method to encode users before returning as bytes.
function getUsersBytes() public constant returns (bytes) { // Change return type to bytes.
return abi.encode(users); // Encode prior to return.
}
}
// Imports
const ethers = require('ethers');
const solc = require('solc');
const ganache = require("ganache-cli");
// Provider / Signer setup.
const providers = ethers.providers;
const web3Provider = new providers.Web3Provider(ganache.provider({ gasLimit: 6e6 }));
const signer = web3Provider.getSigner();
let contractCode = `pragma solidity ^0.4.24;
pragma experimental ABIEncoderV2;
contract ABIExample {
enum Permission { ReadOnly, Write, Admin }
struct User {
uint256 id;
Permission permission;
}
User[] users;
constructor() public {
users.push(User(uint256(111), Permission.Admin));
users.push(User(uint256(222), Permission.Write));
users.push(User(uint256(333), Permission.ReadOnly));
}
function getUsers() public constant returns (User[]) {
return users;
}
// New method to encode users before returning as bytes.
function getUsersBytes() public constant returns (bytes) { // Change return type to bytes.
return abi.encode(users); // Encode prior to return.
}
}`
// Recompile
let compiledContract = solc.compile(contractCode);
// Prepare deployment overwriting interface and bytecode.
let interface = compiledContract.contracts[':ABIExample'].interface;
let bytecode = compiledContract.contracts[':ABIExample'].bytecode;
let deployTransaction = ethers.Contract.getDeployTransaction('0x' + bytecode, interface);
let sendPromise = signer.sendTransaction(Object.assign(deployTransaction, { gas: 6e6 }));
// Deploy contract.
let transactionReceipt;
let userArray;
let userBytesArray;
sendPromise.then(res => {
web3Provider.getTransactionReceipt(res.hash).then(res => {
transactionReceipt = res;
// New JS contract interface.
ABIExample = new ethers.Contract(transactionReceipt.contractAddress, interface, signer);
ABIExample.getUsers().then(res => {
// Log result of calling the contract's `get()` function.
userArray = res;
console.log(`userArray -> `);
console.log(userArray);
});
ABIExample.getUsersBytes().then(res => {
// Log result of calling the contract's `getUsersBytes()` function.
userBytesArray = res;
console.log(`userBytesArray -> `);
console.log(userBytesArray);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment