Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: erc20 gasswapper contract for PoS #402

Merged
merged 3 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ https://docs.polygon.technology/docs/develop/ethereum-polygon/matic-js/get-start

## Support

Our [Discord](https://discord.gg/s2NPJNUvyc) is the best way to reach us ✨.
Our [Discord](https://discord.gg/0xPolygonDevs) is the best way to reach us ✨.

## Contributors

Expand Down
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.0",
"version": "3.6.1",
"description": "Javascript developer library for interacting with Matic Network",
"main": "dist/npm.export.js",
"types": "dist/ts/index.d.ts",
Expand Down
3 changes: 2 additions & 1 deletion src/enums/error_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ export enum ERROR_TYPE {
BurnTxNotCheckPointed = "burn_tx_not_checkpointed",
EIP1559NotSupported = "eip-1559_not_supported",
NullSpenderAddress = "null_spender_address",
AllowedOnNonNativeTokens = "allowed_on_non_native_token"
AllowedOnNonNativeTokens = "allowed_on_non_native_token",
AllowedOnMainnet = "allowed_on_mainnet",
}
1 change: 1 addition & 0 deletions src/interfaces/pos_client_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export interface IPOSClientConfig extends IBaseClientConfig {
rootChain?: string;
erc1155?: IPOSERC1155Address;
rootChainDefaultBlock?:string;
gasSwapper?: string;
}
3 changes: 2 additions & 1 deletion src/interfaces/pos_contracts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ExitUtil, RootChainManager } from "../pos";
import { ExitUtil, RootChainManager, GasSwapper } from "../pos";

export interface IPOSContracts {
rootChainManager: RootChainManager;
exitUtil: ExitUtil;
gasSwapper: GasSwapper;
}
34 changes: 34 additions & 0 deletions src/pos/erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,40 @@ export class ERC20 extends POSToken {
);
}

/**
* Deposit given amount of token for user along with ETHER for gas token
*
* @param {TYPE_AMOUNT} amount
* @param {string} userAddress
* @param {ITransactionOption} [option]
* @returns
* @memberof ERC20
*/
depositWithGas(amount: TYPE_AMOUNT, userAddress: string, swapEthAmount: TYPE_AMOUNT, swapCallData: string, option?: ITransactionOption) {
this.checkForRoot("deposit");

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(swapEthAmount);

return this.gasSwapper.depositWithGas(
this.contractParam.address,
amountInABI,
userAddress,
swapCallData,
option
);
});

}

private depositEther_(amount: TYPE_AMOUNT, userAddress: string, option: ITransactionOption = {}) {
this.checkForRoot("depositEther");

Expand Down
39 changes: 39 additions & 0 deletions src/pos/gas_swapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { BaseToken, Web3SideChainClient } from "../utils";
import { IPOSClientConfig, ITransactionOption } from "../interfaces";

export class GasSwapper extends BaseToken<IPOSClientConfig> {

constructor(client_: Web3SideChainClient<IPOSClientConfig>, address: string) {
super({
address: address,
name: 'GasSwapper',
bridgeType: 'pos',
isParent: true
}, client_);
}

method(methodName: string, ...args) {
return this.getContract().then(contract => {
return contract.method(methodName, ...args);
});
}

depositWithGas(
tokenAddress: string,
depositAmount: string,
userAddress: string,
swapCallData: string,
option?: ITransactionOption
) {
return this.method(
"swapAndBridge",
tokenAddress,
depositAmount,
userAddress,
swapCallData
).then(method => {
return this.processWrite(method, option);
});
}

}
15 changes: 12 additions & 3 deletions src/pos/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ import { RootChain } from "./root_chain";
import { ERC721 } from "./erc721";
import { TYPE_AMOUNT } from "../types";
import { ERC1155 } from "./erc1155";
import { GasSwapper } from "./gas_swapper";

export * from "./exit_util";
export * from "./root_chain_manager";
export * from "./root_chain";
export * from "./gas_swapper";

export class POSClient extends BridgeClient<IPOSClientConfig> {

rootChainManager: RootChainManager;
gasSwapper: GasSwapper;

init(config: IPOSClientConfig) {
const client = this.client;
Expand All @@ -23,9 +26,9 @@ export class POSClient extends BridgeClient<IPOSClientConfig> {
const mainPOSContracts = client.mainPOSContracts;
client.config = config = Object.assign(
{

rootChainManager: mainPOSContracts.RootChainManagerProxy,
rootChain: client.mainPlasmaContracts.RootChainProxy
rootChain: client.mainPlasmaContracts.RootChainProxy,
gasSwapper: mainPOSContracts.GasSwapper
} as IPOSClientConfig,
config
);
Expand All @@ -45,6 +48,11 @@ export class POSClient extends BridgeClient<IPOSClientConfig> {
rootChain
);

this.gasSwapper = new GasSwapper(
this.client,
config.gasSwapper
);

return this;
});
}
Expand Down Expand Up @@ -86,7 +94,8 @@ export class POSClient extends BridgeClient<IPOSClientConfig> {
private getContracts_() {
return {
exitUtil: this.exitUtil,
rootChainManager: this.rootChainManager
rootChainManager: this.rootChainManager,
gasSwapper: this.gasSwapper
} as IPOSContracts;
}
}
4 changes: 4 additions & 0 deletions src/pos/pos_token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export class POSToken extends BaseToken<IPOSClientConfig> {
return this.getPOSContracts().rootChainManager;
}

protected get gasSwapper() {
return this.getPOSContracts().gasSwapper;
}

protected get exitUtil() {
return this.getPOSContracts().exitUtil;
}
Expand Down
3 changes: 3 additions & 0 deletions src/utils/error_helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ export class ErrorHelper implements IError {
case ERROR_TYPE.AllowedOnRoot:
errMsg = `The action ${info} is allowed only on root token.`;
break;
case ERROR_TYPE.AllowedOnMainnet:
errMsg = `The action is allowed only on mainnet chains.`;
break;
case ERROR_TYPE.ProofAPINotSet:
errMsg = `Proof api is not set, please set it using "setProofApi"`;
break;
Expand Down
62 changes: 37 additions & 25 deletions test/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,34 +51,46 @@ const execute = async () => {

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

var result = await goerliERC1155Token.isWithdrawExited('0xbc48c0ccd9821141779a200586ef52033a3487c4e1419625fe7a0ea984521052', {
returnTransaction: true
});
// const tx = await goerliERC20Token.depositWithGas(
// "9887",
// "0xD7Fbe63Db5201f71482Fa47ecC4Be5e5B125eF07",
// "1000000000000000",
// "0xd9627aa4000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000038d7ea4c68000000000000000000000000000000000000000000000000000261bad812e880a9e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee0000000000000000000000007d1afa7b718fb893db30a3abc0cfc608aacfebb0869584cd000000000000000000000000dea904157bd08dae959a04dc7e5924b6e3cfe45000000000000000000000000000000000000000000000002ecac6fcd564a499d4",
// {
// // maxPriorityFeePerGas: 2000000000,
// returnTransaction: true
// });
// console.log(tx)
// return

// var result = await goerliERC1155Token.isWithdrawExited('0xbc48c0ccd9821141779a200586ef52033a3487c4e1419625fe7a0ea984521052', {
// returnTransaction: true
// });
// var result = await goerliERC20Token.withdrawExit('0x1c20c41b9d97d1026aa456a21f13725df63edec1b1f43aacb180ebcc6340a2d3', {
// returnTransaction: true
// });

return console.log('result', result);
// return console.log('result', result);

// return console.log(await client.isDeposited('0x05b6d0d2280557c04de48d395f1f4ea9deb498fabb9bb09b9aec929db5ce62fa'));


var tx = await mumbaiERC20Token.getAllowance(from);
return console.log('isapp', tx);
var tx = await goerliERC1155Token.deposit({
amount: 10,
tokenId: 123,
userAddress: from
}, {
returnTransaction: true
});
// var tx = await mumbaiERC20Token.getAllowance(from);
// return console.log('isapp', tx);
// var tx = await goerliERC1155Token.deposit({
// amount: 10,
// tokenId: 123,
// userAddress: from
// }, {
// returnTransaction: true
// });

return console.log('tx', tx);
// return console.log('tx', tx);

console.log("hash", await tx.getTransactionHash());
console.log("receipt", await tx.getReceipt());
// console.log("hash", await tx.getTransactionHash());
// console.log("receipt", await tx.getReceipt());

return;
// return;

// const tokens = await goerliERC721Token.getAllTokens(
// from
Expand All @@ -97,9 +109,9 @@ const execute = async () => {
// return console.log('tx', tx);


var tx = await goerliERC20Token.deposit(1000000000, from, {
// returnTransaction: true
});
// var tx = await goerliERC20Token.deposit(1000000000, from, {
// // returnTransaction: true
// });
// var tx = await mumbaiERC20Token.transfer(10,to,{
// // returnTransaction: true
// });
Expand All @@ -114,14 +126,14 @@ const execute = async () => {
// returnTransaction: true
// });

console.log('tx', tx);
// console.log('tx', tx);
// // setProofApi("https://proof-generator.polygon.technology")
// // const tx = await goerliERC20Token.withdrawExit('0xd6f7f4c6052611761946519076de28fbd091693af974e7d4abc1b17fd7926fd7');
console.log("txHash", await tx.getTransactionHash());
console.log("txReceipt", await tx.getReceipt());
// console.log("txHash", await tx.getTransactionHash());
// console.log("txReceipt", await tx.getReceipt());

//txhash to plasma exit - 0x63aa095e0d6ee8698399b871daa202eb5522933e2d94c5929cf0fb86b6b0c628
const tokenId = '60399350241383852757821046101235634991156913804166740995010931519407953501076'
// const tokenId = '60399350241383852757821046101235634991156913804166740995010931519407953501076'

// const tx = await (client['client_']).child.getTransactionCount(from, 'pending');
// console.log("tx", tx);
Expand Down Expand Up @@ -269,7 +281,7 @@ const executeZkEvm = async () => {
// console.log("receipt", await tx.getReceipt());
}

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