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

fix: correct withdrawal prompt wait time #3

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/components/Withdrawal/RequestWithdrawalV0.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { isInstanceOfLightGodwokenV0 } from "../../utils/typeAssert";
import { getInputError, isCKBInputValidate, isSudtInputValidate } from "../../utils/inputValidate";
import { parseStringToBI } from "../../utils/numberFormat";
import { handleError } from "./service";
import { getEstimateWaitTime } from "../../utils/dateUtils";

const RequestWithdrawalV0: React.FC = () => {
const [CKBInput, setCKBInput] = useState("");
Expand All @@ -32,6 +33,8 @@ const RequestWithdrawalV0: React.FC = () => {
const CKBBalance = l2CKBBalanceQuery.data;
const erc20BalanceQuery = useERC20Balance();
const tokenList: L1MappedErc20[] | undefined = lightGodwoken?.getBuiltinErc20List();
const withdrawalWaitBlock = lightGodwoken?.getWithdrawalWaitBlock() || 0;
const blockProduceTime = lightGodwoken?.getBlockProduceTime() || 0;

useEffect(() => {
if (!CKBBalance) {
Expand Down Expand Up @@ -149,8 +152,10 @@ const RequestWithdrawalV0: React.FC = () => {
></CurrencyInputPanel>
<SubmitWithdrawal
sendWithdrawal={sendWithdrawal}
blockWait={targetValue === CKB_L1 ? "1000" : "1"}
estimatedTime={targetValue === CKB_L1 ? "5 days" : "a few minutes"}
blockWait={targetValue === CKB_L1 ? String(withdrawalWaitBlock) : "1"}
estimatedTime={
targetValue === CKB_L1 ? getEstimateWaitTime(withdrawalWaitBlock, blockProduceTime) : "a few minutes"
}
loading={loading}
buttonText={inputError}
CKBInput={CKBInput}
Expand Down
7 changes: 5 additions & 2 deletions src/components/Withdrawal/RequestWithdrawalV1.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { getInputError, isCKBInputValidate, isSudtInputValidate } from "../../ut
import { parseStringToBI } from "../../utils/numberFormat";
import { handleError } from "./service";
import { LightGodwokenV1 } from "../../light-godwoken";
import { getEstimateWaitTime } from "../../utils/dateUtils";

const RequestWithdrawalV1: React.FC<{ addTxToHistory: (txHistory: BaseL1TxHistoryInterface) => void }> = ({
addTxToHistory,
Expand All @@ -32,6 +33,8 @@ const RequestWithdrawalV1: React.FC<{ addTxToHistory: (txHistory: BaseL1TxHistor
const l2CKBBalanceQuery = useL2CKBBalance();
const CKBBalance = l2CKBBalanceQuery.data;
const erc20BalanceQuery = useERC20Balance();
const withdrawalWaitBlock = lightGodwoken?.getWithdrawalWaitBlock() || 0;
const blockProduceTime = lightGodwoken?.getBlockProduceTime() || 0;

const tokenList: L1MappedErc20[] | undefined = lightGodwoken?.getBuiltinErc20List();
useEffect(() => {
Expand Down Expand Up @@ -141,8 +144,8 @@ const RequestWithdrawalV1: React.FC<{ addTxToHistory: (txHistory: BaseL1TxHistor
></CurrencyInputPanel>
<SubmitWithdrawal
sendWithdrawal={sendWithdrawal}
blockWait="100"
estimatedTime="50 mins"
blockWait={String(withdrawalWaitBlock)}
estimatedTime={getEstimateWaitTime(withdrawalWaitBlock, blockProduceTime)}
loading={loading}
buttonText={inputError}
CKBInput={CKBInput}
Expand Down
8 changes: 0 additions & 8 deletions src/light-godwoken/LightGodwokenV0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,6 @@ export default class DefaultLightGodwokenV0 extends DefaultLightGodwoken impleme
};
}

getBlockProduceTime(): number {
return 45 * 1000;
}

getWithdrawalWaitBlock(): number {
return 10000;
}

async getL2CkbBalance(payload?: GetL2CkbBalancePayload): Promise<HexNumber> {
const balance = await this.provider.web3.eth.getBalance(payload?.l2Address || this.provider.l2Address);
return "0x" + Number(balance).toString(16);
Expand Down
8 changes: 0 additions & 8 deletions src/light-godwoken/LightGodwokenV1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ export default class DefaultLightGodwokenV1 extends DefaultLightGodwoken impleme
this.updateConfigViaRpc();
}

getWithdrawalWaitBlock(): number {
return this.provider.getConfig().layer2Config.FINALITY_BLOCKS;
}

async getMulticallProvider(): Promise<MulticallProvider | null> {
const multicallContractAddress = this.getConfig().layer2Config.MULTICALL_ADDRESS;

Expand Down Expand Up @@ -106,10 +102,6 @@ export default class DefaultLightGodwokenV1 extends DefaultLightGodwoken impleme
};
}

getBlockProduceTime(): number {
return 30 * 1000;
}

getMinimalDepositCapacity(): BI {
return BI.from(400).mul(100000000);
}
Expand Down
2 changes: 2 additions & 0 deletions src/light-godwoken/__tests__/lightGodwokenConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export const testConfig: LightGodwokenConfigMap = {
SCANNER_API: "",
MIN_CANCEL_DEPOSIT_TIME: 1200,
FINALITY_BLOCKS: 100,
BLOCK_PRODUCE_TIME: 45,
},
},
v1: {
Expand Down Expand Up @@ -116,6 +117,7 @@ export const testConfig: LightGodwokenConfigMap = {
SCANNER_API: "",
MIN_CANCEL_DEPOSIT_TIME: 604800,
FINALITY_BLOCKS: 100,
BLOCK_PRODUCE_TIME: 36,
},
},
};
2 changes: 1 addition & 1 deletion src/light-godwoken/constants/configTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export type Layer2Config = {
SCANNER_API: string;
CHAIN_NAME: string;
FINALITY_BLOCKS: number;
BLOCK_PRODUCE_TIME: number;
MIN_CANCEL_DEPOSIT_TIME: number;

MULTICALL_ADDRESS?: string;
};

Expand Down
170 changes: 91 additions & 79 deletions src/light-godwoken/constants/lightGodwokenConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const layer1ConfigLina: Layer1Config = {
SCANNER_URL: "https://explorer.nervos.org",
};

// https://github.com/nervosnetwork/godwoken-info/blob/69175dff51fb63665abff7cc9640af5bf3409fea/testnet_v0/config/scripts-deploy-result.json
const v0ConfigAggron: LightGodwokenConfig = {
layer1Config: layer1ConfigAggron,
layer2Config: {
Expand Down Expand Up @@ -96,14 +97,16 @@ const v0ConfigAggron: LightGodwokenConfig = {
},
GW_POLYJUICE_RPC_URL: "https://godwoken-testnet-web3-rpc.ckbapp.dev",
SCANNER_URL: "https://aggron.gwscan.com",
SCANNER_API: "https://api.aggron.gwscan.com/api",
SCANNER_API: "https://api.aggron.gwscan.com/api/",
CHAIN_NAME: "Godwoken Testnet v0",
FINALITY_BLOCKS: 10000,
BLOCK_PRODUCE_TIME: 45,
MIN_CANCEL_DEPOSIT_TIME: 1200, // 20 minutes in seconds
MULTICALL_ADDRESS: "0xaf98A74b133CD8373EE055b399b0cE19cF8C4523",
},
};

// https://github.com/nervosnetwork/godwoken-info/blob/69175dff51fb63665abff7cc9640af5bf3409fea/mainnet_v0/config/scripts-result.json
const v0ConfigLina: LightGodwokenConfig = {
layer1Config: layer1ConfigLina,
layer2Config: {
Expand Down Expand Up @@ -145,104 +148,113 @@ const v0ConfigLina: LightGodwokenConfig = {
SCANNER_API: "https://api.gwscan.com/api/",
CHAIN_NAME: "Godwoken v0 mainnet",
FINALITY_BLOCKS: 3600,
BLOCK_PRODUCE_TIME: 45,
MIN_CANCEL_DEPOSIT_TIME: 172800, // two days
},
};

export const predefined_testnet: LightGodwokenConfigMap = {
v0: v0ConfigAggron,
v1: {
layer1Config: layer1ConfigAggron,
layer2Config: {
SCRIPTS: {
deposit_lock: {
script_type_hash: "0x50704b84ecb4c4b12b43c7acb260ddd69171c21b4c0ba15f3c469b7d143f6f18",
cell_dep: {
out_point: {
tx_hash: "0x9caeec735f3cd2a60b9d12be59bb161f7c61ddab1ac22c4383a94c33ba6404a2",
index: "0x0",
},
dep_type: "code",
// https://github.com/nervosnetwork/godwoken-info/blob/69175dff51fb63665abff7cc9640af5bf3409fea/testnet_v1_1/scripts-deploy-result.json
const v1ConfigAggron: LightGodwokenConfig = {
layer1Config: layer1ConfigAggron,
layer2Config: {
SCRIPTS: {
deposit_lock: {
script_type_hash: "0x50704b84ecb4c4b12b43c7acb260ddd69171c21b4c0ba15f3c469b7d143f6f18",
cell_dep: {
out_point: {
tx_hash: "0x9caeec735f3cd2a60b9d12be59bb161f7c61ddab1ac22c4383a94c33ba6404a2",
index: "0x0",
},
dep_type: "code",
},
withdrawal_lock: {
script_type_hash: "0x06ae0706bb2d7997d66224741d3ec7c173dbb2854a6d2cf97088796b677269c6",
cell_dep: {
out_point: {
tx_hash: "0x9c607a9a75ea4699dd01b1c2a478002343998cac8346d2aa582f35b532bd2b93",
index: "0x0",
},
dep_type: "code",
},
withdrawal_lock: {
script_type_hash: "0x06ae0706bb2d7997d66224741d3ec7c173dbb2854a6d2cf97088796b677269c6",
cell_dep: {
out_point: {
tx_hash: "0x9c607a9a75ea4699dd01b1c2a478002343998cac8346d2aa582f35b532bd2b93",
index: "0x0",
},
},
eth_account_lock: {
script_type_hash: "0x07521d0aa8e66ef441ebc31204d86bb23fc83e9edc58c19dbb1b0ebe64336ec0",
dep_type: "code",
},
},
ROLLUP_CONFIG: {
rollup_type_hash: "0x702359ea7f073558921eb50d8c1c77e92f760c8f8656bde4995f26b8963e2dd8",
rollup_type_script: {
code_hash: "0x1e44736436b406f8e48a30dfbddcf044feb0c9eebfe63b0f81cb5bb727d84854",
hash_type: "type",
args: "0x86c7429247beba7ddd6e4361bcdfc0510b0b644131e2afb7e486375249a01802",
},
eth_account_lock: {
script_type_hash: "0x07521d0aa8e66ef441ebc31204d86bb23fc83e9edc58c19dbb1b0ebe64336ec0",
},
},
ROLLUP_CONFIG: {
rollup_type_hash: "0x702359ea7f073558921eb50d8c1c77e92f760c8f8656bde4995f26b8963e2dd8",
rollup_type_script: {
code_hash: "0x1e44736436b406f8e48a30dfbddcf044feb0c9eebfe63b0f81cb5bb727d84854",
hash_type: "type",
args: "0x86c7429247beba7ddd6e4361bcdfc0510b0b644131e2afb7e486375249a01802",
},
GW_POLYJUICE_RPC_URL: "https://godwoken-testnet-v1.ckbapp.dev",
SCANNER_URL: "https://v1.betanet.gwscan.com/",
SCANNER_API: "https://api.v1-betanet.gwscan.com/api/",
CHAIN_NAME: "Godwoken Testnet v1.1",
FINALITY_BLOCKS: 100,
MIN_CANCEL_DEPOSIT_TIME: 604800, // 7 days in seconds

MULTICALL_ADDRESS: "0x65ba0AeE059920dA4D7F8b17B782AF26F463ad5C",
},
GW_POLYJUICE_RPC_URL: "https://godwoken-testnet-v1.ckbapp.dev",
SCANNER_URL: "https://v1.betanet.gwscan.com/",
SCANNER_API: "https://api.v1-betanet.gwscan.com/api/",
CHAIN_NAME: "Godwoken Testnet v1",
FINALITY_BLOCKS: 100,
BLOCK_PRODUCE_TIME: 30,
MIN_CANCEL_DEPOSIT_TIME: 604800, // 7 days in seconds
MULTICALL_ADDRESS: "0x65ba0AeE059920dA4D7F8b17B782AF26F463ad5C",
},
};

export const predefined_mainnet: LightGodwokenConfigMap = {
v0: v0ConfigLina,
v1: {
layer1Config: layer1ConfigLina,
layer2Config: {
SCRIPTS: {
deposit_lock: {
script_type_hash: "0xff602581f07667eef54232cce850cbca2c418b3418611c132fca849d1edcd775",
cell_dep: {
out_point: {
tx_hash: "0x61e576a7e5d2398ecc5b1a969d1af0142c87db0996c2f6fce41bf28f68d805b2",
index: "0x0",
},
dep_type: "code",
// https://github.com/nervosnetwork/godwoken-info/blob/69175dff51fb63665abff7cc9640af5bf3409fea/mainnet_v1/scripts-deploy-result.json
const v1ConfigLina: LightGodwokenConfig = {
layer1Config: layer1ConfigLina,
layer2Config: {
SCRIPTS: {
deposit_lock: {
script_type_hash: "0xff602581f07667eef54232cce850cbca2c418b3418611c132fca849d1edcd775",
cell_dep: {
out_point: {
tx_hash: "0x61e576a7e5d2398ecc5b1a969d1af0142c87db0996c2f6fce41bf28f68d805b2",
index: "0x0",
},
dep_type: "code",
},
withdrawal_lock: {
script_type_hash: "0x3714af858b8b82b2bb8f13d51f3cffede2dd8d352a6938334bb79e6b845e3658",
cell_dep: {
out_point: {
tx_hash: "0xe6389b5cf63eec1e2592e930414bc43f92508e529bdd5f5a07fa1dd140f4f20a",
index: "0x0",
},
dep_type: "code",
},
withdrawal_lock: {
script_type_hash: "0x3714af858b8b82b2bb8f13d51f3cffede2dd8d352a6938334bb79e6b845e3658",
cell_dep: {
out_point: {
tx_hash: "0xe6389b5cf63eec1e2592e930414bc43f92508e529bdd5f5a07fa1dd140f4f20a",
index: "0x0",
},
},
eth_account_lock: {
script_type_hash: "0x096df264f38fff07f3acd318995abc2c71ae0e504036fe32bc38d5b6037364d4",
dep_type: "code",
},
},
ROLLUP_CONFIG: {
rollup_type_hash: "0x1ca35cb5fda4bd542e71d94a6d5f4c0d255d6d6fba73c41cf45d2693e59b3072",
rollup_type_script: {
code_hash: "0xfef1d086d9f74d143c60bf03bd04bab29200dbf484c801c72774f2056d4c6718",
hash_type: "type",
args: "0xab21bfe2bf85927bb42faaf3006a355222e24d5ea1d4dec0e62f53a8e0c04690",
},
eth_account_lock: {
script_type_hash: "0x096df264f38fff07f3acd318995abc2c71ae0e504036fe32bc38d5b6037364d4",
},
GW_POLYJUICE_RPC_URL: "https://v1.mainnet.godwoken.io/rpc",
SCANNER_URL: "https://v1.gwscan.com/",
SCANNER_API: "https://api.v1.gwscan.com/api/",
CHAIN_NAME: "Godwoken Mainet v1",
FINALITY_BLOCKS: 16800,
MIN_CANCEL_DEPOSIT_TIME: 604800,
},
ROLLUP_CONFIG: {
rollup_type_hash: "0x1ca35cb5fda4bd542e71d94a6d5f4c0d255d6d6fba73c41cf45d2693e59b3072",
rollup_type_script: {
code_hash: "0xfef1d086d9f74d143c60bf03bd04bab29200dbf484c801c72774f2056d4c6718",
hash_type: "type",
args: "0xab21bfe2bf85927bb42faaf3006a355222e24d5ea1d4dec0e62f53a8e0c04690",
},
},
GW_POLYJUICE_RPC_URL: "https://v1.mainnet.godwoken.io/rpc",
SCANNER_URL: "https://v1.gwscan.com/",
SCANNER_API: "https://api.v1.gwscan.com/api/",
CHAIN_NAME: "Godwoken Mainet v1",
FINALITY_BLOCKS: 16800,
// Assuming layer 1 block produce time is 12 secondes, layer 2 produces 1 block every 3 layer 1 blocks
BLOCK_PRODUCE_TIME: 12 * 3,
MIN_CANCEL_DEPOSIT_TIME: 604800,
},
};

export const predefined_testnet: LightGodwokenConfigMap = {
v0: v0ConfigAggron,
v1: v1ConfigAggron,
};

export const predefined_mainnet: LightGodwokenConfigMap = {
v0: v0ConfigLina,
v1: v1ConfigLina,
};
6 changes: 5 additions & 1 deletion src/light-godwoken/lightGodwoken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,18 @@ export default abstract class DefaultLightGodwoken implements LightGodwokenBase
abstract getChainId(): string | Promise<string>;
abstract getL2CkbBalance(payload?: GetL2CkbBalancePayload | undefined): Promise<string>;
abstract getErc20Balances(payload: GetErc20Balances): Promise<GetErc20BalancesResult>;
abstract getBlockProduceTime(): number | Promise<number>;
abstract getWithdrawal(txHash: Hash): Promise<unknown>;
abstract getBuiltinErc20List(): ProxyERC20[];
abstract getBuiltinSUDTList(): SUDT[];
// abstract listWithdraw(): Promise<WithdrawResultWithCell[]>;
abstract getVersion(): GodwokenVersion;
abstract withdrawWithEvent(payload: WithdrawalEventEmitterPayload): WithdrawalEventEmitter;

// in milliseconds
getBlockProduceTime(): number {
return this.provider.getConfig().layer2Config.BLOCK_PRODUCE_TIME * 1000;
}

getWithdrawalWaitBlock(): number {
return this.provider.getConfig().layer2Config.FINALITY_BLOCKS;
}
Expand Down
12 changes: 12 additions & 0 deletions src/utils/__tests__/dateUtils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { getEstimateWaitTime } from "../dateUtils";
describe("test date utils", () => {
it("should return expected wait time", async () => {
const waitTime0 = getEstimateWaitTime(100, 30 * 1000);
const waitTime1 = getEstimateWaitTime(10000, 45 * 1000);
const waitTime2 = getEstimateWaitTime(16800, 36 * 1000);

expect(waitTime0).toEqual("50 minutes");
expect(waitTime1).toEqual("5 days");
expect(waitTime2).toEqual("7 days");
});
});
21 changes: 21 additions & 0 deletions src/utils/dateUtils.ts
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
export const DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

/**
* @param waitBlock
* @param blockProduceTime unit is milliseconds
* @returns
*/
export const getEstimateWaitTime = (waitBlock: number, blockProduceTime: number): string => {
const waitSeconds = (waitBlock * blockProduceTime) / 1000;
const secondsPerMinute = 60;
const secondsPerHour = 3600;
const secondsPerday = 86400;
if (waitSeconds < secondsPerHour) {
return `${Math.round(waitSeconds / secondsPerMinute)} minutes`;
} else if (waitSeconds >= secondsPerHour && waitSeconds < secondsPerday) {
return `${Math.round(waitSeconds / secondsPerHour)} hours`;
} else if (waitSeconds >= secondsPerday) {
return `${Math.round(waitSeconds / secondsPerday)} days`;
} else {
return "unkonwn time";
}
};