-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
97 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import config from '@unstoppabledomains/config'; | ||
|
||
export const getProviderUrl = (chainId: number) => { | ||
const providerUrl = Object.values(config.BLOCKCHAINS).find( | ||
v => v.CHAIN_ID === chainId, | ||
)?.JSON_RPC_API_URL; | ||
if (providerUrl) { | ||
return providerUrl; | ||
} | ||
|
||
// an RPC provider URL is required | ||
throw new Error(`Chain ID not supported: ${chainId}`); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,35 @@ | ||
import {erc20Abi} from 'abitype/abis'; | ||
|
||
import type {CreateTransaction} from '../../types'; | ||
import {getWeb3} from './web3'; | ||
|
||
export const createErc20TransferTx = async ( | ||
chainId: number, | ||
tokenAddress: string, | ||
fromAddress: string, | ||
toAddress: string, | ||
amount: number, | ||
): Promise<CreateTransaction> => { | ||
// retrieve a web3 provider for requested chain ID | ||
const web3 = getWeb3(chainId); | ||
if (!web3) { | ||
throw new Error(`Chain ID not supported: ${chainId}`); | ||
} | ||
import {getContract, getContractDecimals} from './web3'; | ||
|
||
export const createErc20TransferTx = async (opts: { | ||
chainId: number; | ||
providerUrl: string; | ||
tokenAddress: string; | ||
fromAddress: string; | ||
toAddress: string; | ||
amount: number; | ||
}): Promise<CreateTransaction> => { | ||
// ERC-20 contract instance for sending a specific token | ||
const erc20Contract = new web3.eth.Contract(erc20Abi, tokenAddress, { | ||
from: fromAddress, | ||
}); | ||
const erc20Contract = getContract( | ||
opts.providerUrl, | ||
opts.tokenAddress, | ||
opts.fromAddress, | ||
); | ||
|
||
// retrieve the contract decimals to represent the amount | ||
const decimals = await erc20Contract.methods.decimals.call([]).call(); | ||
const normalizedAmt = Math.floor(amount * Math.pow(10, Number(decimals))); | ||
const decimals = await getContractDecimals( | ||
opts.providerUrl, | ||
opts.tokenAddress, | ||
); | ||
const normalizedAmt = Math.floor(opts.amount * Math.pow(10, decimals)); | ||
|
||
// create the transaction that should be signed to execute ERC-20 transfer | ||
return { | ||
chainId, | ||
to: tokenAddress, | ||
data: erc20Contract.methods.transfer(toAddress, normalizedAmt).encodeABI(), | ||
chainId: opts.chainId, | ||
to: opts.tokenAddress, | ||
data: erc20Contract.methods | ||
.transfer(opts.toAddress, normalizedAmt) | ||
.encodeABI(), | ||
value: '0', | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,27 @@ | ||
import {erc20Abi} from 'abitype/abis'; | ||
import {Web3} from 'web3'; | ||
|
||
import config from '@unstoppabledomains/config'; | ||
|
||
export const getWeb3 = (chainId: number): Web3 | undefined => { | ||
const providerUrl = Object.values(config.BLOCKCHAINS).find( | ||
v => v.CHAIN_ID === chainId, | ||
)?.JSON_RPC_API_URL; | ||
if (!providerUrl) { | ||
return undefined; | ||
} | ||
export const getWeb3 = (providerUrl: string): Web3 => { | ||
return new Web3(providerUrl); | ||
}; | ||
|
||
export const getContract = ( | ||
providerUrl: string, | ||
tokenAddress: string, | ||
fromAddress?: string, | ||
) => { | ||
// ERC-20 contract instance for sending a specific token | ||
const web3 = getWeb3(providerUrl); | ||
return new web3.eth.Contract(erc20Abi, tokenAddress, { | ||
from: fromAddress, | ||
}); | ||
}; | ||
|
||
export const getContractDecimals = async ( | ||
providerUrl: string, | ||
address: string, | ||
): Promise<number> => { | ||
const erc20Contract = getContract(providerUrl, address); | ||
const decimals = await erc20Contract.methods.decimals.call([]).call(); | ||
return Number(decimals); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters