Skip to content

Commit

Permalink
Feat : SDK - contract list type
Browse files Browse the repository at this point in the history
  • Loading branch information
SonYoungsung committed Jan 15, 2024
1 parent c1985cf commit ebb035c
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 36 deletions.
2 changes: 1 addition & 1 deletion contracts/data/tosv2/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"TreasuryProxy": "0xFD7C2c54a0A755a46793A91449806A4b14E3eEe8",
"TOSValueCalculator": "0xDF0fCfadAF9F095C509F620A6C2BAFd7B6AD8C22"
},
"1155511": {
"11155111": {
"BondDepository": "0xbBe7881cC14a509FD8F53Ce650fE065C385d98D7",
"BondDepositoryProxy": "0x4d08d2113b75Bfd8B6C5D3Dd956165e1853dC6A4",
"LibStaking": "0x41128bA2b9549eAf8A0D4f6b2C0b514c5b491F39",
Expand Down
4 changes: 2 additions & 2 deletions contracts/tokamak.contractlist.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"55004": {
"L2StandardBridge": "0x4200000000000000000000000000000000000010"
},
"1155511": {
"11155111": {
"BondDepository": "0xbBe7881cC14a509FD8F53Ce650fE065C385d98D7",
"BondDepositoryProxy": "0x4d08d2113b75Bfd8B6C5D3Dd956165e1853dC6A4",
"LibStaking": "0x41128bA2b9549eAf8A0D4f6b2C0b514c5b491F39",
Expand All @@ -67,4 +67,4 @@
"TreasuryProxy": "0xFD7C2c54a0A755a46793A91449806A4b14E3eEe8",
"TOSValueCalculator": "0xDF0fCfadAF9F095C509F620A6C2BAFd7B6AD8C22"
}
}
}
23 changes: 19 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import { Contract } from 'ethers'

import {
ContractsLike,
ERC20ContractsList,
L1ChainId,
L1Contracts,
L2ChainID,
L2Contracts,
NumberLike,
SignerOrProviderLike,
} from './interface/types'
Expand Down Expand Up @@ -37,8 +40,14 @@ export class TitanSDK {
*/
public contracts: ContractsLike

/**
* ERC20 Tokens objects from Tokens folder
*/
public tokens: TokamakTokenListT
public erc20contracts
/**
* ERC20 Tokens contract objects attached to their respective providers and addresses.
*/
public erc20contracts: ERC20ContractsList

/**
* List of custom bridges for the given network.
Expand All @@ -62,7 +71,7 @@ export class TitanSDK {
l1BlockTimeSeconds?: NumberLike
contracts?: DeepPartial<ContractsLike>
// bridges?: BridgeAdapterData
bedrock?: boolean
// bedrock?: boolean
}) {
// this.signerOrProvider = toSignerOrProvider(opts.signerOrProvider)
try {
Expand Down Expand Up @@ -111,6 +120,12 @@ export class TitanSDK {
}
}

public getContract(contractName: keyof L1Contracts | keyof L2Contracts) {
return getContract(contractName, this.chainId, {
signerOrProvider: this.signerOrProvider,
})
}

public getToken(symbol: string) {
const result = this.tokens.filter((token) => token.symbol === symbol)
if (!result || result.length > 1) {
Expand All @@ -132,8 +147,8 @@ export class TitanSDK {
`${symbol} token doesn't exist on this chain(id : ${this.chainId})`
)
} else {
const [test] = Object.values(result)
return test
const [tokenContract] = Object.values(result)
return tokenContract as Contract
}
}
}
10 changes: 4 additions & 6 deletions src/interface/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import {
Provider,
TransactionReceipt,
TransactionResponse,
} from '@ethersproject/abstract-provider'
import { Provider } from '@ethersproject/abstract-provider'
import { Signer } from '@ethersproject/abstract-signer'
import { Contract, BigNumber } from 'ethers'

import TokamakContractList from '../../dist/tokamak.contractlist'
import { TokamakContractList } from '../utils/getList'

/*
* Supported chains for the tokenlist and contractlist
Expand Down Expand Up @@ -65,6 +61,8 @@ export type L2Contracts = {
*/
export type ContractsLike = L1Contracts | L2Contracts

export type ERC20ContractsList = Record<keyof ContractsLike, Contract>

/**
* Stuff that can be coerced into a provider.
*/
Expand Down
12 changes: 11 additions & 1 deletion src/utils/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { DeepPartial } from './type-utils'
import {
AddressLike,
ContractsLike,
ERC20ContractsList,
L1Contracts,
L2Contracts,
} from '../interface/types'
Expand Down Expand Up @@ -88,6 +89,15 @@ export const getContract = (
// iface = getContractInterface(name)
// }

console.log('**')
console.log(
new Contract(
toAddress(opts.address || addresses[contractName] || [contractName]),
iface,
opts.signerOrProvider
)
)

return new Contract(
toAddress(opts.address || addresses[contractName] || [contractName]),
iface,
Expand Down Expand Up @@ -146,7 +156,7 @@ export const getAllERC20Contracts = (
signerOrProvider?: ethers.Signer | ethers.providers.Provider
overrides?: DeepPartial<ContractsLike>
} = {}
): Record<string, Contract> => {
): ERC20ContractsList => {
// Attach all erc20 contracts.
const contracts = {}
for (const [, value] of Object.entries(tokenList)) {
Expand Down
1 change: 1 addition & 0 deletions src/utils/getList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export const TokamakTokenList = titanList.tokens.tokens
export const TokamakContractList = titanList.contracts

export type TokamakTokenListT = typeof TokamakTokenList
export type TokamakContractListT = typeof TokamakContractList
21 changes: 1 addition & 20 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,6 @@ import { ethers } from 'ethers'
import titanGithub from '../dist/index'
import { TitanSDK } from '../src'

const getABI = (fileType) => {
const filePath = path.join('./contracts/data/bridge/abi', `${fileType}.json`)

try {
const fileData = fs.readFileSync(filePath, 'utf8')
const abi = JSON.parse(fileData)
return abi
} catch (error) {
console.error(`Error reading ABI for _${fileType}:`, error)
return null
}
}

const init = async () => {
// console.log(getABI('L1Bridge'))
const sdk = new TitanSDK({ chainId: 55004 })
const contract = sdk.getTokenContract('TON')
// console.log(sdk.signer)
console.log(sdk.getTokenContract('TON'))
}
const init = async () => {}

init()
2 changes: 1 addition & 1 deletion titan.tokenlist.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"layer2",
"infrastructure"
],
"timestamp": "2024-01-15T06:48:21.199Z",
"timestamp": "2024-01-15T07:43:44.480Z",
"tokens": [
{
"chainId": 1,
Expand Down
2 changes: 1 addition & 1 deletion tokamak.contractlist.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"55004": {
"L2StandardBridge": "0x4200000000000000000000000000000000000010"
},
"1155511": {
"11155111": {
"BondDepository": "0xbBe7881cC14a509FD8F53Ce650fE065C385d98D7",
"BondDepositoryProxy": "0x4d08d2113b75Bfd8B6C5D3Dd956165e1853dC6A4",
"LibStaking": "0x41128bA2b9549eAf8A0D4f6b2C0b514c5b491F39",
Expand Down

0 comments on commit ebb035c

Please sign in to comment.