Skip to content

Commit

Permalink
Replace trust functionality with safe-core-sdk and simplify getNetwor…
Browse files Browse the repository at this point in the history
…k func
  • Loading branch information
juanenrisley committed Aug 22, 2023
1 parent b5206fa commit 7fba17f
Show file tree
Hide file tree
Showing 5 changed files with 298 additions and 356 deletions.
12 changes: 12 additions & 0 deletions src/common/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,15 @@ export const MAX_WEI = '99999999999999999999999';
// Safe versions
export const SAFE_LAST_VERSION = '1.3.0';
export const SAFE_CRC_VERSION = '1.1.1';

// Percentage value that defines when there is no trust at all
export const NO_LIMIT_PERCENTAGE = 0;

// Minimun trusts required
export const DEFAULT_TRUST_LIMIT = 3;

// Default trust percentage for users
export const DEFAULT_USER_LIMIT_PERCENTAGE = 100;

// Default trust percentage for orgs
export const DEFAULT_ORG_LIMIT_PERCENTAGE = 100;
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ export default class CirclesCore {
this.contracts,
this.utils,
);
/** @type {Object} - trust module */
this.trust = createTrustModule(web3, this.contracts, this.utils);
/** @type {Object} - safe module */
this.safe = createSafeModule(this);
/** @type {Object} - trust module */
this.trust = createTrustModule(this);
/** @type {Object} - token module */
this.token = createTokenModule(
web3,
Expand Down
108 changes: 56 additions & 52 deletions src/safe.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import Safe, {
getSafeContract as _getSafeContract,
SafeFactory,
Web3Adapter,
} from '@safe-global/protocol-kit';
import Safe, { SafeFactory, Web3Adapter } from '@safe-global/protocol-kit';

import { SAFE_LAST_VERSION, ZERO_ADDRESS } from '~/common/constants';
import {
DEFAULT_TRUST_LIMIT,
SAFE_LAST_VERSION,
ZERO_ADDRESS,
} from '~/common/constants';
import { SafeAlreadyDeployedError, SafeNotTrustError } from '~/common/error';
import checkAccount from '~/common/checkAccount';
import checkOptions from '~/common/checkOptions';
import { getSafeCRCVersionContract } from '~/common/getContracts';
import loop from '~/common/loop';
import safeContractAbis from '~/common/safeContractAbis';
import { getSafeContract } from '~/common/getContracts';

/**
* Module to manage Gnosis Safes
* Module to manage safes
* @access private
* @param {CirclesCore} context - CirclesCore instance
* @return {Object} - Safe module instance
*/
export default function createSafeModule({
web3,
contracts: { safeMaster, proxyFactory },
trust,
utils,
options: {
proxyFactoryAddress,
Expand Down Expand Up @@ -96,34 +96,32 @@ export default function createSafeModule({
);

/**
* Prepare a Safe transaction to be funded. The transaction is signed and then encoded
* Prepare a transaction to be executed by a Safe while being funded
* @access private
* @param {string} config.safeAddress - Safe address
* @param {Safe} config.safeSdk - Safe instance
* @param {SafeTransaction} config.SafeTx - Params to overwrite the Safe.create method
* @param {string} config.signerAddress - Address of the transactions signer for the adapter
* @return {string} - Encoded signed transaction
* @param {SafeTransaction} config.SafeTx - Safe transaction to prepare
* @return {string} - Signed transaction execution data
*/
const _prepareSafeTransaction = async ({ safeSdk, safeTx, signerAddress }) =>
const _prepareSafeTransaction = async ({ safeAddress, safeSdk, safeTx }) =>
Promise.all([
_getSafeContract({
ethAdapter: _createEthAdapter(signerAddress),
safeVersion: await safeSdk.getContractVersion(),
customContracts: _customContracts,
}),
getSafeContract(web3, safeAddress),
safeSdk.signTransaction(safeTx),
]).then(([safeSingletonContract, signedSafeTx]) =>
safeSingletonContract.encode('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(),
]),
]).then(([safeContract, signedSafeTx]) =>
safeContract.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(),
);

/**
Expand All @@ -135,25 +133,24 @@ export default function createSafeModule({
* @param {string} options.data - Transaction data to be sent
* @return {string} - Encoded and signed transaction data
*/
const _createTransaction = async ({ safeAddress, signerAddress, data }) => {
const _createTransaction = async ({
safeAddress,
signerAddress,
data,
to,
}) => {
const safeSdk = await _getSafeSdk({
safeAddress,
signerAddress,
});

return safeSdk
.createTransaction({
safeTransactionData: {
to: safeAddress,
value: 0,
data,
},
})
.createTransaction({ safeTransactionData: { to, data, value: 0 } })
.then((safeTx) =>
_prepareSafeTransaction({
safeTx,
safeAddress,
safeSdk,
signerAddress,
safeTx,
}),
);
};
Expand Down Expand Up @@ -226,9 +223,9 @@ export default function createSafeModule({
.createAddOwnerTx({ ownerAddress })
.then((safeTx) =>
_prepareSafeTransaction({
safeTx,
safeAddress,
safeSdk,
signerAddress: account.address,
safeTx,
}),
)
.then((data) =>
Expand All @@ -245,24 +242,29 @@ export default function createSafeModule({
* @param {Object} account - web3 account instance
* @param {Object} userOptions - options
* @param {string} userOptions.safeAddress - Safe address
* @param {string} userOptions.to - Target address to send the transaction
* @param {string} userOptions.data - Transaction data to be sent
* @return {string} - Encoded and signed transaction data
*/
const createTransaction = (account, userOptions) => {
checkAccount(web3, account);

const { safeAddress, data } = checkOptions(userOptions, {
const { safeAddress, to, data } = checkOptions(userOptions, {
safeAddress: {
type: web3.utils.checkAddressChecksum,
},
to: {
type: web3.utils.checkAddressChecksum,
},
data: {
type: 'string',
},
});

return _createTransaction({
safeAddress,
signerAddress: account.address,
safeAddress,
to,
data,
});
};
Expand Down Expand Up @@ -295,9 +297,9 @@ export default function createSafeModule({
);
}

const { isTrusted } = await trust.isTrusted(account, {
safeAddress,
});
const isTrusted = await utils
.requestIndexedDB('trust_network', safeAddress.toLowerCase())
.then(({ trusts = [] } = {}) => trusts.length >= DEFAULT_TRUST_LIMIT);

if (!isTrusted) {
throw new SafeNotTrustError(`The Safe has no minimun required trusts.`);
Expand Down Expand Up @@ -481,9 +483,9 @@ export default function createSafeModule({
})
.then((safeTx) =>
_prepareSafeTransaction({
safeTx,
safeAddress,
safeSdk,
signerAddress: account.address,
safeTx,
}),
)
.then((data) =>
Expand Down Expand Up @@ -526,8 +528,9 @@ export default function createSafeModule({
await utils.sendTransaction({
target: safeAddress,
data: await _createTransaction({
safeAddress,
signerAddress: account.address,
safeAddress,
to: safeAddress,
data: safeInstance.methods
.changeMasterCopy(safeMaster.options.address)
.encodeABI(),
Expand All @@ -537,8 +540,9 @@ export default function createSafeModule({
await utils.sendTransaction({
target: safeAddress,
data: await _createTransaction({
safeAddress,
signerAddress: account.address,
safeAddress,
to: safeAddress,
data: safeInstance.methods
.setFallbackHandler(fallbackHandlerAddress)
.encodeABI(),
Expand Down
Loading

0 comments on commit 7fba17f

Please sign in to comment.