Skip to content

Commit

Permalink
Merge pull request #832 from tradingstrategy-ai/usdt-payment-improvem…
Browse files Browse the repository at this point in the history
…ents

USDT payment flow improvements
  • Loading branch information
kenkunz authored Oct 15, 2024
2 parents 95914e1 + 0686789 commit 8cfa41e
Show file tree
Hide file tree
Showing 9 changed files with 279 additions and 148 deletions.
74 changes: 71 additions & 3 deletions src/lib/eth-defi/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,21 +157,89 @@ export async function getDenominationTokenBalance(
type ApproveTokenTransferParams = {
chainId?: number;
address: Address;
sender: Address;
spender: Address;
value: number | bigint;
};

export async function approveTokenTransfer(
config: Config,
{ chainId, address, sender, value }: ApproveTokenTransferParams
{ chainId, address, spender, value }: ApproveTokenTransferParams
) {
const { request } = await simulateContract(config, {
abi: erc20Abi,
chainId,
address,
functionName: 'approve',
args: [sender, BigInt(value)]
args: [spender, BigInt(value)]
});

return writeContract(config, request);
}

type GetTokenAllowanceParams = {
chainId?: number;
address: Address;
owner: Address;
spender: Address;
};

export function getTokenAllowance(config: Config, { chainId, address, owner, spender }: GetTokenAllowanceParams) {
return readContract(config, {
abi: erc20Abi,
chainId,
address,
functionName: 'allowance',
args: [owner, spender]
});
}

/**
* Return expected block time for a given chain. This is used to display a "best guess" progress
* bar for transactions. The times returned are about double the average block times, plus added
* time for HTTP latency. This results in a conservative but reasonable estimate.
*/
export function getExpectedBlockTime(chainId: number) {
// prettier-ignore
switch (chainId) {
case 1 : return 25_000; // Ethereum
case 137 : return 10_000; // Polygon
case 42161 : return 2_500; // Arbitrum
default : return 10_000; // everything else
}
}

export type ErrorInfo = {
name: string;
message: string;
shortMessage: string | undefined;
details: string | undefined;
functionName: string | undefined;
state: string | undefined;
cause: ErrorInfo | unknown | undefined;
};

/**
* Extract an ErrorInfo object from an error. This enables errors to be serialized
* (for example, in $wizard.data). Key properties are extracted that are useful for
* displaying appropriate error messages in the UI. An optional state (of an fsm)
* may also be included.
*
* Extrated data includes common Error properties as well as some custom properties
* available on [viem errors](https://github.com/wevm/viem/blob/main/src/errors/base.ts).
*/
export function extractErrorInfo(error: unknown, state?: string | undefined): ErrorInfo | unknown {
if (!(error instanceof Error)) return error;

const { name, message, shortMessage, details, functionName } = error as any;
const cause = extractErrorInfo(error.cause);
return { name, message, shortMessage, details, functionName, state, cause };
}

/**
* Walk error's causes and return true if any match the provided error name
*/
export function errorCausedBy(error: any, name: string) {
if (error?.name === name) return true;
if (error?.cause) return errorCausedBy(error.cause, name);
return false;
}
21 changes: 21 additions & 0 deletions src/lib/helpers/progressbar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { tweened } from 'svelte/motion';
import { cubicOut } from 'svelte/easing';

/**
* Create a progress bar store with custom reset, start and finish methods.
*
* @param duration - time in ms to go from 0 to 100
*/
export function getProgressBar(initial = 0, duration: number) {
const { set, subscribe } = tweened(initial, { duration: 0, easing: cubicOut });

return {
subscribe,
reset: () => set(initial),
start: (startAt = 0) => {
set(startAt);
set(95, { duration: (1 - startAt / 100) * duration });
},
finish: (duration = 100) => set(100, { duration })
};
}
Loading

0 comments on commit 8cfa41e

Please sign in to comment.