Skip to content

Commit

Permalink
add separate native transfer function
Browse files Browse the repository at this point in the history
  • Loading branch information
josemarinas committed Aug 16, 2023
1 parent 559fb2f commit eb82f04
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 46 deletions.
108 changes: 62 additions & 46 deletions modules/client/src/internal/client/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ export class ClientMethods extends ClientCore implements IClientMethods {
): AsyncGenerator<DaoDepositStepValue> {
switch (params.type) {
case TokenType.NATIVE:
yield* this.depositNative(params);
break;
case TokenType.ERC20:
yield* this.depositErc20(params);
break;
Expand All @@ -302,64 +304,78 @@ export class ClientMethods extends ClientCore implements IClientMethods {
}
}

private async *depositErc20(
params: DepositErc20Params | DepositEthParams,
private async *depositNative(
params: DepositEthParams,
): AsyncGenerator<DaoDepositStepValue> {
const signer = this.web3.getConnectedSigner();
if (
params.type === TokenType.ERC20 && params.tokenAddress &&
params.tokenAddress !== AddressZero
) {
const { tokenAddress, daoAddressOrEns } = params;
// check current allowance
const tokenContract = new Contract(
tokenAddress,
ERC20_ABI,
signer,
);
const currentAllowance = await tokenContract.allowance(
await signer.getAddress(),
daoAddressOrEns,
);
yield {
key: DaoDepositSteps.CHECKED_ALLOWANCE,
allowance: currentAllowance.toBigInt(),
};
// if its lower than the needed, set it to the correct one
if (currentAllowance.lt(params.amount)) {
// If the target is an ERC20 token, ensure that the amount can be transferred
// Relay the yield steps to the caller as they are received
yield* this.setAllowance(
{
amount: params.amount,
spender: params.daoAddressOrEns,
tokenAddress: params.tokenAddress,
},
);
}
const { daoAddressOrEns, amount } = params;
const override: { value?: bigint } = { value: params.amount };
const daoInstance = DAO__factory.connect(daoAddressOrEns, signer);

const tx = await daoInstance.deposit(
AddressZero,
amount,
"",
override,
);
yield { key: DaoDepositSteps.DEPOSITING, txHash: tx.hash };

const cr = await tx.wait();
const log = findLog(cr, daoInstance.interface, "Deposited");
if (!log) {
throw new FailedDepositError();
}
let tokenAddress = AddressZero;
if (params.type === TokenType.ERC20) {
tokenAddress = params.tokenAddress;

const daoInterface = DAO__factory.createInterface();
const parsedLog = daoInterface.parseLog(log);

if (!amount.toString() === parsedLog.args["amount"]) {
throw new AmountMismatchError(
amount,
parsedLog.args["amount"].toBigInt(),
);
}
const {
amount,
yield { key: DaoDepositSteps.DONE, amount: amount };
}

private async *depositErc20(
params: DepositErc20Params,
): AsyncGenerator<DaoDepositStepValue> {
const signer = this.web3.getConnectedSigner();
const { tokenAddress, daoAddressOrEns, amount } = params;
// check current allowance
const tokenContract = new Contract(
tokenAddress,
ERC20_ABI,
signer,
);
const currentAllowance = await tokenContract.allowance(
await signer.getAddress(),
daoAddressOrEns,
} = params;
);
yield {
key: DaoDepositSteps.CHECKED_ALLOWANCE,
allowance: currentAllowance.toBigInt(),
};
// if its lower than the needed, set it to the correct one
if (currentAllowance.lt(params.amount)) {
// If the target is an ERC20 token, ensure that the amount can be transferred
// Relay the yield steps to the caller as they are received
yield* this.setAllowance(
{
amount: params.amount,
spender: params.daoAddressOrEns,
tokenAddress: params.tokenAddress,
},
);
}
// Doing the transfer
const daoInstance = DAO__factory.connect(daoAddressOrEns, signer);
const override: { value?: bigint } = {};

if (tokenAddress === AddressZero) {
// Ether
override.value = params.amount;
}

const tx = await daoInstance.deposit(
tokenAddress,
amount,
"",
override,
);
yield { key: DaoDepositSteps.DEPOSITING, txHash: tx.hash };

Expand Down
39 changes: 39 additions & 0 deletions modules/client/test/integration/client/methods.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,45 @@ describe("Client", () => {
).toBe("7");
});

it("Should allow to deposit native toekn", async () => {
const context = new Context(contextParamsLocalChain);
const client = new Client(context);

const provider = client.web3.getProvider();

const amount = BigInt(7);

const depositParams: DepositParams = {
type: TokenType.NATIVE,
daoAddressOrEns: daoAddress,
amount,
};

// Deposit
for await (const step of client.methods.deposit(depositParams)) {
switch (step.key) {
case DaoDepositSteps.DEPOSITING:
expect(typeof step.txHash).toBe("string");
expect(step.txHash).toMatch(/^0x[A-Fa-f0-9]{64}$/i);
break;
case DaoDepositSteps.DONE:
expect(typeof step.amount).toBe("bigint");
expect(step.amount).toBe(BigInt(7));
break;
default:
throw new Error(
"Unexpected DAO deposit step: " + JSON.stringify(step, null, 2),
);
}
}

const daoBalance = await provider.getBalance(daoAddress);
expect(
daoBalance.toString(),
).toBe(amount.toString());

});

it("Check if dao factory has register dao permission in the dao registry", async () => {
const context = new Context(contextParamsLocalChain);
const client = new Client(context);
Expand Down

0 comments on commit eb82f04

Please sign in to comment.