Skip to content

Commit

Permalink
examples/onchain-signer/test: Add support for Sapphire Testnet/Mainnet
Browse files Browse the repository at this point in the history
  • Loading branch information
matevz authored and CedarMist committed Oct 19, 2023
1 parent 635c763 commit be85b2c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 37 deletions.
22 changes: 10 additions & 12 deletions examples/onchain-signer/contracts/Gasless.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,24 @@
pragma solidity ^0.8.19;

import {EIP155Signer} from "@oasisprotocol/sapphire-contracts/contracts/EIP155Signer.sol";
import {EthereumUtils} from "@oasisprotocol/sapphire-contracts/contracts/EthereumUtils.sol";

struct EthereumKeypair {
address addr;
bytes32 secret;
uint64 nonce;
}

struct EthTx {
uint64 nonce;
uint256 gasPrice;
uint64 gasLimit;
address to;
uint256 value;
bytes data;
uint256 chainId;
}

// Proxy for gasless transaction.
contract Gasless {
error KeyPairNotSet();

EthereumKeypair private kp;

function setKeypair(EthereumKeypair memory keypair) external payable {
kp = keypair;
constructor () payable {
(kp.addr, kp.secret) = EthereumUtils.generateKeypair();

payable(kp.addr).transfer(msg.value);
}

function makeProxyTx(address innercallAddr, bytes memory innercall)
Expand All @@ -33,6 +28,9 @@ contract Gasless {
returns (bytes memory output)
{
bytes memory data = abi.encode(innercallAddr, innercall);
if (kp.secret == 0) {
revert KeyPairNotSet();
}

// Call will invoke proxy().
return
Expand Down
3 changes: 3 additions & 0 deletions examples/onchain-signer/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ const config: HardhatUserConfig = {
},
},
solidity: '0.8.19',
mocha: {
timeout: 120_000_000, // Sapphire Mainnet/Testnet require more time.
},
};

export default config;
51 changes: 26 additions & 25 deletions examples/onchain-signer/test/CommentBox.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from 'chai';
import hre, { config, ethers } from 'hardhat';
import { ethers } from 'hardhat';
import { CommentBox, Gasless } from '../typechain-types';
import { HDAccountsUserConfig } from 'hardhat/types';
import { parseEther } from 'ethers/lib/utils';

describe('CommentBox', function () {
let commentBox: CommentBox;
Expand All @@ -13,51 +13,52 @@ describe('CommentBox', function () {
await commentBox.deployed();

const GaslessFactory = await ethers.getContractFactory('Gasless');
gasless = await GaslessFactory.deploy();
gasless = await GaslessFactory.deploy({value: parseEther('0.1')});
await gasless.deployed();

// Derive the private key of the 1st (counting from 0) builtin hardhat test account.
const accounts = config.networks.hardhat
.accounts as unknown as HDAccountsUserConfig;
const wallet1 = ethers.Wallet.fromMnemonic(
accounts.mnemonic,
accounts.path + `/1`,
);

// Use it as the relayer private key.
await expect(
await gasless.setKeypair({
addr: wallet1.address,
secret: Uint8Array.from(
Buffer.from(wallet1.privateKey.substring(2), 'hex'),
),
nonce: ethers.provider.getTransactionCount(wallet1.address),
}),
).not.to.be.reverted;
});

it('Should comment', async function () {
const prevCommentCount = await commentBox.commentCount();

const tx = await commentBox.comment('Hello, world!');
await tx.wait();

// Sapphire Mainnet/Testnet: Wait a few moments for nodes to catch up.
if (
(await gasless.provider.getNetwork()).chainId == 23294 ||
(await gasless.provider.getNetwork()).chainId == 23295
) {
await new Promise((r) => setTimeout(r, 6_000));
}

expect(await commentBox.commentCount()).eq(prevCommentCount.add(1));
});

it('Should comment gasless', async function () {
// You can set up sapphire-dev image and run the test like this:
// docker run -it -p8545:8545 -p8546:8546 ghcr.io/oasisprotocol/sapphire-dev -to 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
// npx hardhat test --grep proxy --network sapphire-localnet
if ((await ethers.provider.getNetwork()).chainId == 1337) {
if ((await gasless.provider.getNetwork()).chainId == 1337) {
this.skip();
}

const innercall = commentBox.interface.encodeFunctionData('comment', [
'Hello, free world!',
]);

// Sapphire Mainnet/Testnet: Wait a few moments for nodes to catch up.
if (
(await gasless.provider.getNetwork()).chainId == 23294 ||
(await gasless.provider.getNetwork()).chainId == 23295
) {
await new Promise((r) => setTimeout(r, 6_000));
}

const tx = await gasless.makeProxyTx(commentBox.address, innercall);

const plainResp = await gasless.provider.sendTransaction(tx);
const receipt = await ethers.provider.waitForTransaction(plainResp.hash);
// TODO: https://github.com/oasisprotocol/sapphire-paratime/issues/179
const response = await gasless.provider.sendTransaction(tx);
const receipt = await gasless.provider.waitForTransaction(response.hash);
if (!receipt || receipt.status != 1) throw new Error('tx failed');
});
});

0 comments on commit be85b2c

Please sign in to comment.