Skip to content

Commit

Permalink
added web3 provider example (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
MCarlomagno authored Jan 10, 2024
1 parent e2d9b0f commit c90ebaa
Show file tree
Hide file tree
Showing 5 changed files with 673 additions and 1 deletion.
12 changes: 12 additions & 0 deletions examples/relayer-web3-provider/bytecode.json

Large diffs are not rendered by default.

283 changes: 283 additions & 0 deletions examples/relayer-web3-provider/erc20.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
[
{
"inputs": [
{
"internalType": "uint256",
"name": "initialSupply",
"type": "uint256"
}
],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "owner",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "spender",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "value",
"type": "uint256"
}
],
"name": "Approval",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "from",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "to",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "value",
"type": "uint256"
}
],
"name": "Transfer",
"type": "event"
},
{
"inputs": [
{
"internalType": "address",
"name": "owner",
"type": "address"
},
{
"internalType": "address",
"name": "spender",
"type": "address"
}
],
"name": "allowance",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "spender",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "approve",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "account",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "decimals",
"outputs": [
{
"internalType": "uint8",
"name": "",
"type": "uint8"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "spender",
"type": "address"
},
{
"internalType": "uint256",
"name": "subtractedValue",
"type": "uint256"
}
],
"name": "decreaseAllowance",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "spender",
"type": "address"
},
{
"internalType": "uint256",
"name": "addedValue",
"type": "uint256"
}
],
"name": "increaseAllowance",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "name",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "symbol",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "totalSupply",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "recipient",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "sender",
"type": "address"
},
{
"internalType": "address",
"name": "recipient",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "transferFrom",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
}
]
43 changes: 43 additions & 0 deletions examples/relayer-web3-provider/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require('dotenv').config();

const { DefenderRelayProvider } = require('@openzeppelin/defender-sdk-relay-signer-client/lib/web3');
const { Web3 } = require('web3');

const ERC20Abi = require('./erc20.json');
const ERC20Bytecode = require('./bytecode.json')[0].data.bytecode.object;
const Beneficiary = '0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1';

async function main() {
const validUntil = new Date(Date.now() + 120 * 1000).toISOString();
const provider = new DefenderRelayProvider(
{
apiKey: process.env.RELAYER_API_KEY,
apiSecret: process.env.RELAYER_API_SECRET,
},
{ speed: 'fast', validUntil },
);

const web3 = new Web3(provider);
const [from] = await web3.eth.getAccounts();
const balance = await web3.eth.getBalance(from);
console.log(`Relayer address is ${from} with balance ${balance}`);

console.log(`Deploying ERC20 contract`);
const factory = new web3.eth.Contract(ERC20Abi, null, { data: ERC20Bytecode, from });
const erc20 = await factory.deploy({ arguments: [100] }).send();
console.log(`Contract deployed at address ${erc20.options.address}`);

console.log(`Sending approve transaction for ${Beneficiary} to token ${erc20.options.address}...`);
const tx = await erc20.methods.approve(Beneficiary, (1e17).toString()).send();
console.log(`Transaction sent:`, tx);

const allowance = await erc20.methods.allowance(tx.from, Beneficiary).call();
console.log(`Allowance now is:`, allowance.toString());

const sig = await web3.eth.sign('0xdead', from);
console.log(`Signature is ${sig}`);
}

if (require.main === module) {
main().catch(console.error);
}
16 changes: 16 additions & 0 deletions examples/relayer-web3-provider/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "@openzeppelin/defender-sdk-example-web3-provider",
"version": "1.0.0",
"private": true,
"main": "index.js",
"author": "OpenZeppelin Defender <[email protected]>",
"license": "MIT",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"@openzeppelin/defender-sdk-relay-signer-client": "1.7.0",
"dotenv": "^16.3.1",
"web3": "^4.3.0"
}
}
Loading

0 comments on commit c90ebaa

Please sign in to comment.