Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
louilinn committed Sep 26, 2023
1 parent b2a8374 commit 9012470
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 47 deletions.
183 changes: 139 additions & 44 deletions src/trust.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,63 +89,158 @@ export default function createTrustModule(web3, contracts, utils) {
},
});

//console.log(
// '----------------------------------------------------------------------',
//);

return utils
.requestIndexedDB('trust_status', safeAddress.toLowerCase())
.then(({ safe } = {}) => {
let result = [];

if (safe) {
//console.log({safe});
const connections = [...safe.incoming, ...safe.outgoing];
// Create first the connections network object with safes we trust and safes that trust us
const network = connections.reduce(
(acc, { canSendToAddress, userAddress }) => {
const checksumSafeAddress = web3.utils.toChecksumAddress(
canSendToAddress || userAddress,
connections.forEach((a) => {
if (a.userAddress) {
//console.log({ userAddress: a.userAddress }, a.user)
}
if (a.canSendToAddress) {
//console.log({ canSendToAddress: a.canSendToAddress }, a.canSendTo)
}
});

const trusters = safe.outgoing;
const trustees = safe.incoming;
//console.log('trustees', trustees, typeof(trustees), trustees.length);
//console.log('trusters', trusters, typeof(trusters), trusters.length);
//console.log('connections', connections, typeof(connections), connections.length);
let network = [];
// 1 Add direct truster connections
trusters &&
trusters.forEach((truster) => {
// outgoing capacity, incoming trust
const trusterAddress = web3.utils.toChecksumAddress(
truster.canSendToAddress,
);
// If the connection already exists in the network, use its values to overwrite new info
const { isIncoming, isOutgoing } =
acc[checksumSafeAddress] || {};

return {
...acc,
[checksumSafeAddress]: {
safeAddress: checksumSafeAddress,
isIncoming: isIncoming || !!userAddress,
isOutgoing: isOutgoing || !!canSendToAddress,
},
const newConnection = {
safeAddress: trusterAddress,
isIncoming: false, // default
isOutgoing: true,
mutualConnections: [], // default
};
},
{},
);

// Select mutual connections between our related safes and safes they trust
connections.forEach(
({ canSendTo, canSendToAddress, user, userAddress }) => {
const safe = canSendTo || user;
const safeAddress = canSendToAddress || userAddress;
const checksumSafeAddress =
web3.utils.toChecksumAddress(safeAddress);

// Calculate mutual connections if they do not exist yet
if (safe && !network[checksumSafeAddress].mutualConnections) {
network[checksumSafeAddress].mutualConnections =
safe.incoming.reduce((acc, curr) => {
const target = web3.utils.toChecksumAddress(
curr.userAddress,
network = [...network, newConnection];
});
// 2 Add direct trustee connections
trustees &&
trustees.forEach((trustee) => {
// incoming capacity, outgoing trust
const trusteeAddress = web3.utils.toChecksumAddress(
trustee.userAddress,
);
const existingConnection = network.find(
(obj) => obj.safeAddress === trusteeAddress,
);
if (existingConnection) {
// trusterOfTrustee is already trusting "me" - update record
existingConnection.isIncoming = true;
} else {
// new record
const newConnection = {
safeAddress: trusteeAddress,
isIncoming: true,
isOutgoing: false, // default
mutualConnections: [], // default
};
network = [...network, newConnection];
}
// 3 Add mutual connections
const trustersOfTrustee = trustee.user.outgoing;
trustersOfTrustee &&
trustersOfTrustee.forEach((trusterOfTrustee) => {
const ttAddress = web3.utils.toChecksumAddress(
trusterOfTrustee.canSendToAddress,
);
if (ttAddress !== trusteeAddress) {
//console.log('3 - user that shares a mutual connection ', ttAddress, trusteeAddress)
const existingConnection = network.find(
(obj) => obj.safeAddress === ttAddress,
);
if (existingConnection) {
// trusterOfTrustee is already trusted or trusting "me" - update record
const previousMutualConnections =
existingConnection.mutualConnections;
existingConnection.mutualConnections = [
...previousMutualConnections,
trusteeAddress,
];
} else {
const newConnection = {
safeAddress: ttAddress,
isIncoming: false,
isOutgoing: false,
mutualConnections: [trusteeAddress],
};
network = [...network, newConnection];
}
}
});
});
//console.log('connectedList or network ', network, network.length);

// If it is a mutual connection and is not self
return network[target] && curr.userAddress !== safeAddress
? [...acc, target]
: acc;
}, []);
}
},
);
// Create first the connections network object with safes we trust and safes that trust us
// const network = connections.reduce(
// (acc, { canSendToAddress, userAddress }) => {
// const checksumSafeAddress = web3.utils.toChecksumAddress(
// canSendToAddress || userAddress,
// );
// // If the connection already exists in the network, use its values to overwrite new info
// const { isIncoming, isOutgoing } =
// acc[checksumSafeAddress] || {};

result = Object.values(network);
}
// return {
// ...acc,
// [checksumSafeAddress]: {
// safeAddress: checksumSafeAddress,
// isIncoming: isIncoming || !!userAddress,
// isOutgoing: isOutgoing || !!canSendToAddress,
// },
// };
// },
// {},
// );

// // console.log('network', network);

// // Select mutual connections between our related safes and safes they trust
// connections.forEach(
// ({ canSendTo, canSendToAddress, user, userAddress }) => {
// const safe = canSendTo || user;
// const safeAddress = canSendToAddress || userAddress;
// const checksumSafeAddress =
// web3.utils.toChecksumAddress(safeAddress);

// // Calculate mutual connections if they do not exist yet
// if (safe && !network[checksumSafeAddress].mutualConnections) {
// network[checksumSafeAddress].mutualConnections =
// safe.incoming.reduce((acc, curr) => {
// const target = web3.utils.toChecksumAddress(
// curr.userAddress,
// );

// // If it is a mutual connection and is not self
// return network[target] && curr.userAddress !== safeAddress
// ? [...acc, target]
// : acc;
// }, []);
// }
// },
// );

//console.log('old ', Object.values(network));
result = network; //Object.values(network) connectedList;
}
// console.log(result, result.length);
return result;
});
},
Expand Down
7 changes: 7 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ function getTrustStatus(
case 'graph':
default:
query = {
// line 368-373 probably not needed, same with 380-382 - let's try removing after all tests pass
query: `{
safe(id: "${safeAddress}") {
outgoing (first: 1000 where: { limitPercentage_not: "${NO_LIMIT_PERCENTAGE}" canSendToAddress_not: "${safeAddress}" }) {
Expand All @@ -368,6 +369,9 @@ function getTrustStatus(
incoming (first: 1000 where: { limitPercentage_not: "${NO_LIMIT_PERCENTAGE}"}) {
userAddress
}
outgoing (first: 1000 where: { limitPercentage_not: "${NO_LIMIT_PERCENTAGE}" }) {
canSendToAddress
}
}
}
incoming (first: 1000 where: { limitPercentage_not: "${NO_LIMIT_PERCENTAGE}" userAddress_not: "${safeAddress}" }) {
Expand All @@ -376,6 +380,9 @@ function getTrustStatus(
incoming (first: 1000 where: { limitPercentage_not: "${NO_LIMIT_PERCENTAGE}"}) {
userAddress
}
outgoing (first: 1000 where: { limitPercentage_not: "${NO_LIMIT_PERCENTAGE}" }) {
canSendToAddress
}
}
}
}
Expand Down
41 changes: 38 additions & 3 deletions test/trust.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@ let accountA;
let accountB;
let accountC;
let accountD;
let accountSW;
let core;
let safeAddressA;
let safeAddressB;
let safeAddressC;
let safeAddressD;
let safeAddressSW;

beforeAll(async () => {
accountA = getAccount();
accountB = getAccount(3);
accountC = getAccount(5);
accountD = getAccount(6);
accountSW = getAccount(7);
core = createCore();
});

Expand All @@ -29,15 +32,24 @@ describe('Trust', () => {
deploySafeAndToken(core, accountB),
deploySafeAndToken(core, accountC),
deploySafeAndToken(core, accountD),
deploySafeAndToken(core, accountSW),
]).then((result) => {
safeAddressA = result[0].safeAddress;
safeAddressB = result[1].safeAddress;
safeAddressC = result[2].safeAddress;
safeAddressD = result[3].safeAddress;
safeAddressSW = result[4].safeAddress;
// console.log({
// safeAddressA,
// safeAddressB,
// safeAddressC,
// safeAddressD,
// safeAddressSW,
// });
}),
);

it('should trust someone', async () => {
xit('should trust someone', async () => {
// A trusts B
const response = await core.trust.addConnection(accountA, {
user: safeAddressB,
Expand Down Expand Up @@ -113,7 +125,7 @@ describe('Trust', () => {
expect(isTrustedLowLimit).toBe(true);
});

it('should untrust someone', async () => {
xit('should untrust someone', async () => {
const response = await core.trust.removeConnection(accountA, {
user: safeAddressB,
canSendTo: safeAddressA,
Expand Down Expand Up @@ -176,6 +188,22 @@ describe('Trust', () => {
user: safeAddressC,
canSendTo: safeAddressD,
});
// // SW trusts B
// await core.trust.addConnection(accountSW, {
// user: safeAddressB,
// canSendTo: safeAddressSW,
// });
// SW trusts A
await core.trust.addConnection(accountSW, {
user: safeAddressA,
canSendTo: safeAddressSW,
});
// SW trusts C
await core.trust.addConnection(accountSW, {
user: safeAddressC,
canSendTo: safeAddressSW,
});
// To represent a SW we have an account that only trusts others but no-one trusts them

await core.utils.loop(
() => getTrustConnection(core, accountB, safeAddressB, safeAddressA),
Expand All @@ -195,7 +223,7 @@ describe('Trust', () => {
core.trust.getNetwork(accountB, {
safeAddress: safeAddressB,
}),
(network) => network.length === 3,
(network) => network.length === 4,
{ label: 'Wait for trust network to be updated' },
);

Expand All @@ -208,6 +236,9 @@ describe('Trust', () => {
const connectionWithD = network.find(
(element) => element.safeAddress === safeAddressD,
);
const connectionWithSW = network.find(
(element) => element.safeAddress === safeAddressSW,
);

// Check outgoing with mutual connections
expect(connectionWithA.isOutgoing).toBe(true);
Expand All @@ -225,5 +256,9 @@ describe('Trust', () => {
expect(connectionWithD.mutualConnections.length).toBe(2);
expect(connectionWithD.mutualConnections).toContain(safeAddressA);
expect(connectionWithD.mutualConnections).toContain(safeAddressC);
// Check outgoing with mutual connections
expect(connectionWithSW.isOutgoing).toBe(false);
expect(connectionWithSW.isIncoming).toBe(false);
expect(connectionWithSW.mutualConnections).toStrictEqual([safeAddressA]);
});
});

0 comments on commit 9012470

Please sign in to comment.