Skip to content

Commit

Permalink
Feat : SDK - token contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
SonYoungsung committed Jan 15, 2024
1 parent b0e5d98 commit c1985cf
Show file tree
Hide file tree
Showing 8 changed files with 379 additions and 22 deletions.
222 changes: 222 additions & 0 deletions src/constants/abis/erc20.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
[
{
"constant": true,
"inputs": [],
"name": "name",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_spender",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "approve",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "totalSupply",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_from",
"type": "address"
},
{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "transferFrom",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "decimals",
"outputs": [
{
"name": "",
"type": "uint8"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "_owner",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"name": "balance",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "symbol",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "_owner",
"type": "address"
},
{
"name": "_spender",
"type": "address"
}
],
"name": "allowance",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"payable": true,
"stateMutability": "payable",
"type": "fallback"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "owner",
"type": "address"
},
{
"indexed": true,
"name": "spender",
"type": "address"
},
{
"indexed": false,
"name": "value",
"type": "uint256"
}
],
"name": "Approval",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "from",
"type": "address"
},
{
"indexed": true,
"name": "to",
"type": "address"
},
{
"indexed": false,
"name": "value",
"type": "uint256"
}
],
"name": "Transfer",
"type": "event"
}
]
74 changes: 70 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Signer } from '@ethersproject/abstract-signer'
import { Provider } from '@ethersproject/abstract-provider'
import { Contract } from 'ethers'

import {
ContractsLike,
Expand All @@ -10,7 +11,15 @@ import {
} from './interface/types'
import { DeepPartial } from './utils/type-utils'
import { toNumber, toSignerOrProvider } from './utils/coercion'
import { getAllContracts } from './utils/contract'
import {
getAllContracts,
getAllERC20Contracts,
getAllERC20Tokens,
getContract,
} from './utils/contract'
import { getProvider } from './utils/provider'
import TokenList from '../dist/titan.tokenlist'
import { TokamakTokenListT } from './utils/getList'

export class TitanSDK {
/**
Expand All @@ -28,6 +37,9 @@ export class TitanSDK {
*/
public contracts: ContractsLike

public tokens: TokamakTokenListT
public erc20contracts

/**
* List of custom bridges for the given network.
*/
Expand All @@ -44,30 +56,84 @@ export class TitanSDK {
* @param opts.bridges Optional bridge address list.
*/
constructor(opts: {
signerOrProvider: SignerOrProviderLike
signerOrProvider?: SignerOrProviderLike
chainId: NumberLike
depositConfirmationBlocks?: NumberLike
l1BlockTimeSeconds?: NumberLike
contracts?: DeepPartial<ContractsLike>
// bridges?: BridgeAdapterData
bedrock?: boolean
}) {
this.signerOrProvider = toSignerOrProvider(opts.signerOrProvider)

// this.signerOrProvider = toSignerOrProvider(opts.signerOrProvider)
try {
this.chainId = toNumber(opts.chainId)
} catch (err) {
throw new Error(`This chain ID is missing or invalid: ${opts.chainId}`)
}

this.signerOrProvider = opts.signerOrProvider
? toSignerOrProvider(opts.signerOrProvider)
: getProvider(toNumber(opts.chainId))

this.contracts = getAllContracts(this.chainId, {
signerOrProvider: this.signerOrProvider,
overrides: opts.contracts,
})

this.tokens = getAllERC20Tokens(this.chainId)

this.erc20contracts = getAllERC20Contracts(this.tokens, {
signerOrProvider: this.signerOrProvider,
})

// this.bridges = getBridgeAdapters(this.l2ChainId, this, {
// overrides: opts.bridges,
// contracts: opts.contracts,
// })
}

/**
* Signer connected to the chain.
*/
get signer(): Signer {
if (Provider.isProvider(this.signerOrProvider)) {
throw new Error(`messenger has no signer`)
} else {
return this.signerOrProvider
}
}

get provider(): Provider {
if (Provider.isProvider(this.signerOrProvider)) {
return this.signerOrProvider
} else {
return this.signerOrProvider.provider as Provider
}
}

public getToken(symbol: string) {
const result = this.tokens.filter((token) => token.symbol === symbol)
if (!result || result.length > 1) {
throw new Error(
`${symbol} token doesn't exist on this chain(id : ${this.chainId})`
)
} else {
return result[0]
}
}

public getTokenContract(symbol: string): Contract {
const result = Object.fromEntries(
Object.entries(this.erc20contracts).filter(([key]) => key === symbol)
)

if (!result || Object.keys(result).length !== 1) {
throw new Error(
`${symbol} token doesn't exist on this chain(id : ${this.chainId})`
)
} else {
const [test] = Object.values(result)
return test
}
}
}
2 changes: 2 additions & 0 deletions src/interface/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export enum L2ChainID {
TITAN_SEPOLIA = 0,
}

export type SupportedChainID = L1ChainId | L2ChainID

/**
* Convenience type for something that looks like the L1 contract interface but could be
* addresses instead of actual contract objects.
Expand Down
Loading

0 comments on commit c1985cf

Please sign in to comment.