Skip to content

Instantly share code, notes, and snippets.

@shrugs
Created July 31, 2018 01:52
Show Gist options
  • Save shrugs/fcddf369145a7140b2762912137d75e6 to your computer and use it in GitHub Desktop.
Save shrugs/fcddf369145a7140b2762912137d75e6 to your computer and use it in GitHub Desktop.
pragma solidity ^0.4.24;
contract Greeter {
string greeting;
function greet() public view returns (string) {
return greeting;
}
function setGreet(string _greeting) public {
greeting = _greeting;
}
}
// some of this code isn't used in the test cases; ignore it until you need it
const RLP = require('rlp')
const b = require('buidler')
const GREETING = 'Hello, buidler!'
// @TODO - if args provided, construct a delegate call to `target.initialize(...args)`
const proxyFor = (target, args) => `0x603160008181600b9039f3600080808080368092803773${target.replace('0x', '')}5af43d828181803e808314602f57f35bfd`
const contractAddress = (sender, nonce) => b.web3.utils.sha3(RLP.encode([ sender, nonce ])).substr(-40)
const AdminUpgradeabilityProxy = b.artifacts.require('AdminUpgradeabilityProxy')
const Greeter = b.artifacts.require('Greeter')
const estimateDeploy = async (artifact, args, owner) => {
const Contract = b.web3.eth.contract(artifact._json.abi)
const data = Contract.new.getData(...args, { data: artifact._json.bytecode })
return b.pweb3.eth.estimateGas({
from: owner,
data,
})
}
const estimateContract = async (data, from) => b.pweb3.eth.estimateGas({
from,
data,
})
const deployContract = async (data, from) => {
const res = await b.pweb3.eth.sendTransaction({ from, data })
const receipt = await b.pweb3.eth.getTransactionReceipt(res)
return receipt.contractAddress
}
contract('test', ([ _, owner, anyone ]) => {
beforeEach(async function () {
this.greeter = await Greeter.new()
})
it('Should return the right greeting', async function () {
const gotGreeting = await this.greeter.greet()
await this.greeter.setGreet(GREETING)
assert.equal(gotGreeting, GREETING)
})
describe('AdminUpgradeabilityProxy', function () {
it('should work', async function () {
const identityImplementation = await AdminUpgradeabilityProxy.new(this.greeter.address, { from: owner })
const identityGreeter = Greeter.at(identityImplementation.address)
await identityGreeter.setGreet(GREETING, { from: anyone })
const gotGreeting = await identityGreeter.greet({ from: anyone })
assert.equal(gotGreeting, GREETING)
})
it('should be able to proxy twice', async function () {
const firstProxy = await AdminUpgradeabilityProxy.new(this.greeter.address, { from: owner })
const secondProxy = await AdminUpgradeabilityProxy.new(firstProxy.address, { from: owner })
const identityGreeter = Greeter.at(secondProxy.address)
await identityGreeter.setGreet(GREETING, { from: anyone }) // revert
const gotGreeting = await identityGreeter.greet({ from: anyone })
assert.equal(gotGreeting, GREETING)
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment