Skip to content

Commit

Permalink
Fix: allow colony creation with non-colony network tokens
Browse files Browse the repository at this point in the history
Basically, don't presume the token client has the `.locked()`
method available, since the ERC-20 doesn't enforce that.

Just try/catch it, and if not, set the token as 'unlocked'

Fixes: #2264
  • Loading branch information
rdig committed Apr 22, 2024
1 parent cf29884 commit 7e4e5bd
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 14 deletions.
7 changes: 6 additions & 1 deletion amplify/backend/function/createUniqueColony/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,12 @@ exports.handler = async (event) => {
const providerNetwork = await provider.getNetwork();
const chainId = String(providerNetwork.chainId);
const version = await colonyClient.version();
const isTokenLocked = colonyClient.tokenClient.locked();
let isTokenLocked = false;
try {
isTokenLocked = await colonyClient.tokenClient.locked();
} catch (error) {
// token doesn not support the `locked()` method
}

/*
* Create the colony
Expand Down
38 changes: 26 additions & 12 deletions src/redux/sagas/colony/colonyCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,18 @@ import {
ColonyRole,
colonyRoles2Hex,
} from '@colony/colony-js';
import { utils } from 'ethers';
import { utils, providers } from 'ethers';
import { poll } from 'ethers/lib/utils';
import { all, call, put } from 'redux-saga/effects';

import {
ADDRESS_ZERO,
DEFAULT_TOKEN_DECIMALS,
supportedExtensionsConfig,
isDev,
GANACHE_LOCAL_RPC_URL,
} from '~constants/index.ts';
import {
type ColonyManager,
ContextModule,
getContext,
} from '~context/index.ts';
import { ContextModule, getContext } from '~context/index.ts';
import {
CreateColonyEtherealMetadataDocument,
type CreateColonyEtherealMetadataMutation,
Expand Down Expand Up @@ -52,8 +50,8 @@ import {
putError,
takeFrom,
takeLatestCancellable,
getColonyManager,
initiateTransaction,
getNetworkClient,
} from '../utils/index.ts';

function* colonyCreate({
Expand All @@ -74,7 +72,19 @@ function* colonyCreate({
const apolloClient = getContext(ContextModule.ApolloClient);
const wallet = getContext(ContextModule.Wallet);
const walletAddress = utils.getAddress(wallet.address);
const colonyManager: ColonyManager = yield getColonyManager();
// const colonyManager: ColonyManager = yield getColonyManager();

const provider = (() => {
if (isDev) {
return new providers.JsonRpcProvider(GANACHE_LOCAL_RPC_URL);
}
return new providers.Web3Provider(window.ethereum!);
})();

const networkClient = yield call(getNetworkClient, provider);

console.info({ networkClient });

const channelNames: string[] = [];

/*
Expand Down Expand Up @@ -262,21 +272,24 @@ function* colonyCreate({
*/
const colonyClient = yield poll(
async () => {
console.info('attempting to get colony client');
try {
const client = await colonyManager.getClient(
ClientType.ColonyClient,
colonyAddress,
);
const client = await networkClient.getColonyClient(colonyAddress);
console.info(client);
return client;
} catch (err) {
console.error('fetching colony client errored out', err);
return undefined;
}
},
{
timeout: 30000,
retryLimit: 0,
},
);

console.info({ colonyClient });

/*
* Add a colonyAddress identifier to all pending transactions.
*/
Expand Down Expand Up @@ -335,6 +348,7 @@ function* colonyCreate({
},
{
timeout: 30000,
retryLimit: 0,
},
);

Expand Down
1 change: 1 addition & 0 deletions src/redux/sagas/transactions/getTransactionPromise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ async function getTransactionPromise(
}

console.info('TX DEBUG', {
colonyJSClient: client,
client: client?.clientType || 'unknownContractClient',
methodName,
params,
Expand Down
16 changes: 15 additions & 1 deletion src/redux/sagas/wallet/RetryProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type Block } from '@ethersproject/providers';
import { providers } from 'ethers';
import { providers, utils } from 'ethers';
import { backOff } from 'exponential-backoff';

import { GANACHE_LOCAL_RPC_URL, isDev } from '~constants/index.ts';
Expand Down Expand Up @@ -50,6 +50,20 @@ const classFactory = (
// method is the method name (e.g. getBalance) and params is an
// object with normalized values passed in, depending on the method
perform(method, params) {
const bypassRetryMethods = [
utils.id('locked()').slice(0, 10),
utils.id('nonces(address)').slice(0, 10),
utils.id('getMetatransactionNonce(address)').slice(0, 10),
];
console.info({ method, params });
console.info(params?.transaction?.data?.slice(0, 10));
if (
method === 'call' &&
bypassRetryMethods.includes(params?.transaction?.data?.slice(0, 10))
) {
return super.perform(method, params);
}

return backOff(() => super.perform(method, params), {
retry: this.attemptCheck,
startingDelay: this.delay,
Expand Down

0 comments on commit 7e4e5bd

Please sign in to comment.