Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
juanenrisley committed Aug 17, 2023
1 parent a41fb88 commit 0ae39ec
Show file tree
Hide file tree
Showing 12 changed files with 261 additions and 207 deletions.
1 change: 0 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ if (!('crypto' in globalThis)) globalThis.crypto = require('crypto');

module.exports = {
collectCoverage: true,
globalTeardown: '<rootDir>/teardown.js',
// Resolve modules with alias
moduleNameMapper: {
'^~(.*)$': '<rootDir>/src$1',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"docs:serve": "documentation serve --watch ./src/**",
"docs:lint": "documentation lint ./src/**",
"lint": "eslint --ignore-path .gitignore --ignore-pattern lib .",
"test": "jest --runInBand --forceExit",
"test": "jest --runInBand",
"test:watch": "npm run test -- --watch"
},
"devDependencies": {
Expand Down
3 changes: 0 additions & 3 deletions teardown.js

This file was deleted.

8 changes: 0 additions & 8 deletions test/helpers/accounts.js

This file was deleted.

63 changes: 63 additions & 0 deletions test/helpers/addTrust.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { getSafeContract } from '~/common/getContracts';

import getTrustConnection from './getTrustConnection';

export default async function addTrust(
{ account, safeAddress, safeAddressToTrust, limitPercentage },
core,
) {
const {
contracts: { hub },
options: { hubAddress },
safe,
utils,
web3,
} = core;
// Retrieve safe
const safeSdk = await safe.getSafeSdk(account, { safeAddress });

// Prepare transaction to trust a Safe
return (
safeSdk
.createTransaction({
safeTransactionData: {
to: hubAddress,
value: 0,
data: hub.methods
.trust(safeAddressToTrust, limitPercentage)
.encodeABI(),
},
})
.then((safeTx) => safeSdk.signTransaction(safeTx))
// Execute manually the transaction
.then((signedSafeTx) =>
web3.eth.sendTransaction({
from: account.address,
to: safeAddress,
value: 0,
data: getSafeContract(web3, safeAddress)
.methods.execTransaction(
signedSafeTx.data.to,
signedSafeTx.data.value,
signedSafeTx.data.data,
signedSafeTx.data.operation,
signedSafeTx.data.safeTxGas,
signedSafeTx.data.baseGas,
signedSafeTx.data.gasPrice,
signedSafeTx.data.gasToken,
signedSafeTx.data.refundReceiver,
signedSafeTx.encodedSignatures(),
)
.encodeABI(),
}),
)
.then(() =>
utils.loop(
() =>
getTrustConnection(core, account, safeAddress, safeAddressToTrust),
(isReady) => isReady,
{ label: 'Wait for the graph to index newly added trust connection' },
),
)
);
}
4 changes: 1 addition & 3 deletions test/helpers/core.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import CirclesCore from '~';

import web3 from './web3';

export default function createCore(opts) {
export default function createCore(web3, opts) {
return new CirclesCore(web3, {
apiServiceEndpoint: process.env.API_SERVICE_ENDPOINT,
fallbackHandlerAddress: process.env.SAFE_DEFAULT_CALLBACK_HANDLER,
Expand Down
6 changes: 6 additions & 0 deletions test/helpers/getAccounts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import privateKeys from './accounts.json';

const getAccounts = (web3) =>
privateKeys.map((key) => web3.eth.accounts.privateKeyToAccount(key));

export default getAccounts;
85 changes: 85 additions & 0 deletions test/helpers/setupAccount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { ZERO_ADDRESS } from '~/common/constants';
import { getSafeContract } from '~/common/getContracts';

// Set up manually a Safe for being fully usable in Circles
export default async function setupAccount({ account, nonce }, core) {
const {
contracts: { safeMaster, hub, proxyFactory },
options: {
fallbackHandlerAddress,
hubAddress,
proxyFactoryAddress,
safeMasterAddress,
},
safe,
web3,
} = core;
const safeAddress = await safe.predictAddress(account, { nonce });
let safeSdk;

return (
// Deploy manually a Safe
web3.eth
.sendTransaction({
from: account.address,
to: proxyFactoryAddress,
value: 0,
data: proxyFactory.methods
.createProxyWithNonce(
safeMasterAddress,
safeMaster.methods
.setup(
[account.address],
1,
ZERO_ADDRESS,
'0x',
fallbackHandlerAddress,
ZERO_ADDRESS,
0,
ZERO_ADDRESS,
)
.encodeABI(),
nonce,
)
.encodeABI(),
})
// Instantiate deployed Safe
.then(async () => {
safeSdk = await safe.getSafeSdk(account, { safeAddress });
})
// Prepare transaction for Safe to be signed up
.then(() =>
safeSdk.createTransaction({
safeTransactionData: {
to: hubAddress,
value: 0,
data: hub.methods.signup().encodeABI(),
},
}),
)
.then((safeTx) => safeSdk.signTransaction(safeTx))
// Execute manually the transaction
.then((signedSafeTx) =>
web3.eth.sendTransaction({
from: account.address,
to: safeAddress,
value: 0,
data: getSafeContract(web3, safeAddress)
.methods.execTransaction(
signedSafeTx.data.to,
signedSafeTx.data.value,
signedSafeTx.data.data,
signedSafeTx.data.operation,
signedSafeTx.data.safeTxGas,
signedSafeTx.data.baseGas,
signedSafeTx.data.gasPrice,
signedSafeTx.data.gasToken,
signedSafeTx.data.refundReceiver,
signedSafeTx.encodedSignatures(),
)
.encodeABI(),
}),
)
.then(() => safeAddress)
);
}
17 changes: 17 additions & 0 deletions test/helpers/setupWeb3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Web3 from 'web3';
import HDWalletProvider from '@truffle/hdwallet-provider';

import privateKeys from './accounts.json';

export default function setupWeb3() {
const provider = new HDWalletProvider({
privateKeys,
providerOrUrl: process.env.RPC_URL,
});
const web3 = new Web3(provider);

return {
web3,
provider,
};
}
Loading

0 comments on commit 0ae39ec

Please sign in to comment.