Skip to content

Commit

Permalink
add example contract call (#460)
Browse files Browse the repository at this point in the history
* add example contract call

* add deps resolutions to example

* use ethers v6
  • Loading branch information
MCarlomagno authored Jul 15, 2024
1 parent e7f4801 commit 310c6f4
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
52 changes: 52 additions & 0 deletions examples/relayer-call-contract-function/box.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
[
{
"inputs": [
{
"internalType": "uint256",
"name": "value",
"type": "uint256"
}
],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "uint256",
"name": "value",
"type": "uint256"
}
],
"name": "ValueChanged",
"type": "event"
},
{
"inputs": [],
"name": "retrieve",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "value",
"type": "uint256"
}
],
"name": "store",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]
47 changes: 47 additions & 0 deletions examples/relayer-call-contract-function/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require('dotenv').config();

const { ethers, version } = require('ethers');
const BoxAbi = require('./box.json');
const { Defender } = require('@openzeppelin/defender-sdk');

function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}

async function main() {
const creds = {
relayerApiKey: process.env.RELAYER_API_KEY,
relayerApiSecret: process.env.RELAYER_API_SECRET,
};
const validUntil = new Date(Date.now() + 120 * 1000).toISOString();

const client = new Defender(creds);
console.log('using ethers version ', version);

const provider = client.relaySigner.getProvider({ ethersVersion: 'v6' });
const signer = await client.relaySigner.getSigner(provider, { speed: 'fast', validUntil, ethersVersion: 'v6' });

const contractAddress = '0x1B9ec5Cc45977927fe6707f2A02F51e1415f2052';
const contract = new ethers.Contract(contractAddress, BoxAbi, signer);

console.log(`Calling store function`);
const result = await contract.store('10');

console.log(`Transaction sent:`, result.hash);
console.log(`Status:`, result.status);

// polls until the transaction is mined.
let recipt = null;
while (recipt == null) {
recipt = await provider.getTransactionReceipt(result.hash);
console.log('sleep 1s before requesting again');
await sleep(1000);
}
console.log(recipt);
}

if (require.main === module) {
main().catch(console.error);
}
16 changes: 16 additions & 0 deletions examples/relayer-call-contract-function/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "@openzeppelin/defender-sdk-example-relayer-contract-function",
"version": "1.14.1",
"private": true,
"main": "index.js",
"author": "OpenZeppelin Defender <[email protected]>",
"license": "MIT",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"@openzeppelin/defender-sdk": "1.14.1",
"dotenv": "^16.3.1",
"ethers": "^6.9.0"
}
}
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 310c6f4

Please sign in to comment.