Skip to content

Commit

Permalink
Merge pull request #414 from maticnetwork/pos-eth-refuel
Browse files Browse the repository at this point in the history
Pos eth refuel
  • Loading branch information
nitinmittal23 authored Oct 12, 2023
2 parents 481d51a + 0f80f04 commit 194272e
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 4 deletions.
24 changes: 24 additions & 0 deletions examples/pos/erc20/deposit_ether_with_gas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { getPOSClient, from } = require('../../utils_pos');

const execute = async () => {
const client = await getPOSClient();
const result = await client.depositEtherWithGas(
1,
from,
1000000000000000,
"0xd9627aa4000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000038d7ea4c68000000000000000000000000000000000000000000000000000286556c0f059561200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee0000000000000000000000007d1afa7b718fb893db30a3abc0cfc608aacfebb0869584cd000000000000000000000000dea904157bd08dae959a04dc7e5924b6e3cfe450000000000000000000000000000000007551d94e15a6d9373f715de5b9f4080b"
);

const txHash = await result.getTransactionHash();
console.log("txHash", txHash);
const receipt = await result.getReceipt();
console.log("receipt", receipt);

};

execute().then(() => {
}).catch(err => {
console.error("err", err);
}).finally(_ => {
process.exit(0);
})
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@maticnetwork/maticjs",
"version": "3.6.6",
"version": "3.6.7",
"description": "Javascript developer library for interacting with Matic Network",
"main": "dist/npm.export.js",
"types": "dist/ts/index.d.ts",
Expand Down
28 changes: 28 additions & 0 deletions src/pos/erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,34 @@ export class ERC20 extends POSToken {
});
}

private depositEtherWithGas_(amount: TYPE_AMOUNT, userAddress: string, swapEthAmount: TYPE_AMOUNT, swapCallData: string, option: ITransactionOption = {}) {
this.checkForRoot("depositEtherWithGas");

return this.getChainId().then((chainId: number) => {
if (chainId !== 1) {
this.client.logger.error(ERROR_TYPE.AllowedOnMainnet).throw();
}
const amountInABI = this.client.parent.encodeParameters(
[Converter.toHex(amount)],
['uint256'],
);

option.value = Converter.toHex(
Converter.toBN(amount).add(
Converter.toBN(swapEthAmount)
)
);

return this.gasSwapper.depositWithGas(
"0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
amountInABI,
userAddress,
swapCallData,
option
);
});
}

/**
* initiate withdraw by burning provided amount
*
Expand Down
13 changes: 13 additions & 0 deletions src/pos/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ export class POSClient extends BridgeClient<IPOSClientConfig> {
)['depositEther_'](amount, userAddress, option);
}

depositEtherWithGas(
amount: TYPE_AMOUNT,
userAddress: string,
swapEthAmount: TYPE_AMOUNT,
swapCallData: string,
option: ITransactionOption
) {
return new ERC20(
'', true, this.client,
this.getContracts_.bind(this),
)['depositEtherWithGas_'](amount, userAddress, swapEthAmount, swapCallData, option);
}

private getContracts_() {
return {
exitUtil: this.exitUtil,
Expand Down
13 changes: 13 additions & 0 deletions src/utils/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,17 @@ export class Converter {
throw new Error(`Invalid value ${amount}, value is not a number.`);
}
}

static toBN(amount: BaseBigNumber | string | number): BaseBigNumber {
const dataType = typeof amount;
if (dataType === 'string') {
if ((amount as string).slice(0, 2) === '0x') {
amount = parseInt(amount as string, 16);
}
}
if (!utils.BN.isBN(amount)) {
amount = new utils.BN(amount);
}
return amount as BaseBigNumber;
}
}
10 changes: 9 additions & 1 deletion test/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ const execute = async () => {
const goerliERC1155Token = client.erc1155(pos.parent.erc1155, true);
const mumbaiERC1155Token = client.erc1155(pos.child.erc1155);

// const tx = await client.depositEtherWithGas(
// 1, "0xD7Fbe63Db5201f71482Fa47ecC4Be5e5B125eF07",
// 1000000000000000, "0xd9627aa4000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000038d7ea4c68000000000000000000000000000000000000000000000000000286556c0f059561200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee0000000000000000000000007d1afa7b718fb893db30a3abc0cfc608aacfebb0869584cd000000000000000000000000dea904157bd08dae959a04dc7e5924b6e3cfe450000000000000000000000000000000007551d94e15a6d9373f715de5b9f4080b", {
// returnTransaction: true
// }
// );
// console.log(tx)

// setProofApi("https://proof-generator.polygon.technology");

// const tx = await goerliERC20Token.depositWithGas(
Expand Down Expand Up @@ -312,7 +320,7 @@ const executeZkEvm = async () => {
// console.log("receipt", await tx.getReceipt());
}

executeZkEvm().then(_ => {
execute().then(_ => {
process.exit(0)
}).catch(err => {
console.error(err);
Expand Down

0 comments on commit 194272e

Please sign in to comment.