Skip to content

Commit

Permalink
CU-86dud53c9 - BSEthereum - Transfer - Change type 1 transaction to t…
Browse files Browse the repository at this point in the history
…ype 2
  • Loading branch information
raulduartep committed Aug 15, 2024
1 parent b58e8bb commit a62334a
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 85 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@cityofzion/bs-ethereum",
"comment": "Change transaction transfer to type 2",
"type": "patch"
}
],
"packageName": "@cityofzion/bs-ethereum"
}
145 changes: 64 additions & 81 deletions packages/bs-ethereum/src/BSEthereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,56 @@ export class BSEthereum<BSCustomName extends string = string>
this.setNetwork(network)
}

async #buildTransferParams(param: TransferParam) {
const provider = new ethers.providers.JsonRpcProvider(this.network.url)

let ledgerTransport: Transport | undefined

if (param.isLedger) {
if (!this.ledgerService.getLedgerTransport)
throw new Error('You must provide getLedgerTransport function to use Ledger')
ledgerTransport = await this.ledgerService.getLedgerTransport(param.senderAccount)
}

let signer: ethers.Signer
if (ledgerTransport) {
signer = this.ledgerService.getSigner(ledgerTransport, provider)
} else {
signer = new ethers.Wallet(param.senderAccount.key, provider)
}

const decimals = param.intent.tokenDecimals ?? 18
const amount = ethersBigNumber.parseFixed(param.intent.amount, decimals)

const gasPrice = await provider.getGasPrice()

let transactionParams: ethers.utils.Deferrable<ethers.providers.TransactionRequest> = {
type: 2,
}

const isNative =
BSEthereumHelper.normalizeHash(this.feeToken.hash) === BSEthereumHelper.normalizeHash(param.intent.tokenHash)
if (isNative) {
transactionParams.to = param.intent.receiverAddress
transactionParams.value = amount
} else {
const contract = new ethers.Contract(param.intent.tokenHash, [
'function transfer(address to, uint amount) returns (bool)',
])
const populatedTransaction = await contract.populateTransaction.transfer(param.intent.receiverAddress, amount)
transactionParams = {
...populatedTransaction,
...transactionParams,
}
}

return {
transactionParams,
signer,
gasPrice,
}
}

#setTokens(network: Network<BSEthereumNetworkId>) {
const nativeAsset = BSEthereumHelper.getNativeAsset(network)
this.tokens = [nativeAsset]
Expand Down Expand Up @@ -153,95 +203,28 @@ export class BSEthereum<BSCustomName extends string = string>
}

async transfer(param: TransferParam): Promise<string> {
const provider = new ethers.providers.JsonRpcProvider(this.network.url)

let ledgerTransport: Transport | undefined

if (param.isLedger) {
if (!this.ledgerService.getLedgerTransport)
throw new Error('You must provide getLedgerTransport function to use Ledger')
ledgerTransport = await this.ledgerService.getLedgerTransport(param.senderAccount)
}
const { signer, transactionParams, gasPrice } = await this.#buildTransferParams(param)

let signer: ethers.Signer
if (ledgerTransport) {
signer = this.ledgerService.getSigner(ledgerTransport, provider)
} else {
signer = new ethers.Wallet(param.senderAccount.key, provider)
}

const decimals = param.intent.tokenDecimals ?? 18
const amount = ethersBigNumber.parseFixed(param.intent.amount, decimals)

const gasPrice = await provider.getGasPrice()

let transactionParams: ethers.utils.Deferrable<ethers.providers.TransactionRequest> = {
gasPrice,
}

const isNative =
BSEthereumHelper.normalizeHash(this.feeToken.hash) === BSEthereumHelper.normalizeHash(param.intent.tokenHash)
if (isNative) {
transactionParams.to = param.intent.receiverAddress
transactionParams.value = amount
} else {
const contract = new ethers.Contract(param.intent.tokenHash, [
'function transfer(address to, uint amount) returns (bool)',
])
const populatedTransaction = await contract.populateTransaction.transfer(param.intent.receiverAddress, amount)
transactionParams = {
...populatedTransaction,
...transactionParams,
}
let gasLimit: ethers.BigNumberish
try {
gasLimit = await signer.estimateGas(transactionParams)
} catch {
gasLimit = BSEthereumHelper.DEFAULT_GAS_LIMIT
}

const transaction = await signer.sendTransaction(transactionParams)
const transaction = await signer.sendTransaction({
...transactionParams,
gasLimit,
maxPriorityFeePerGas: gasPrice,
maxFeePerGas: gasPrice,
})

return transaction.hash
}

async calculateTransferFee(param: TransferParam): Promise<string> {
const provider = new ethers.providers.JsonRpcProvider(this.network.url)

let ledgerTransport: Transport | undefined

if (param.isLedger) {
if (!this.ledgerService.getLedgerTransport)
throw new Error('You must provide getLedgerTransport function to use Ledger')
ledgerTransport = await this.ledgerService.getLedgerTransport(param.senderAccount)
}

let signer: ethers.Signer
if (ledgerTransport) {
signer = this.ledgerService.getSigner(ledgerTransport, provider)
} else {
signer = new ethers.Wallet(param.senderAccount.key, provider)
}

const gasPrice = await provider.getGasPrice()

let estimated: ethers.BigNumber

const isNative =
BSEthereumHelper.normalizeHash(this.feeToken.hash) === BSEthereumHelper.normalizeHash(param.intent.tokenHash)
const decimals = param.intent.tokenDecimals ?? 18
const amount = ethersBigNumber.parseFixed(param.intent.amount, decimals)

if (!isNative) {
const contract = new ethers.Contract(
param.intent.tokenHash,
['function transfer(address to, uint amount) returns (bool)'],
signer
)

estimated = await contract.estimateGas.transfer(param.intent.receiverAddress, amount)
} else {
estimated = await signer.estimateGas({
to: param.intent.receiverAddress,
value: amount,
})
}

const { signer, transactionParams, gasPrice } = await this.#buildTransferParams(param)
const estimated = await signer.estimateGas(transactionParams)
return ethers.utils.formatEther(gasPrice.mul(estimated))
}

Expand Down
3 changes: 3 additions & 0 deletions packages/bs-ethereum/src/BSEthereumHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export type BSEthereumNetworkId = NetworkId<

export class BSEthereumHelper {
static DEFAULT_DECIMALS = 18

static DEFAULT_GAS_LIMIT = 0x5208

static #NATIVE_ASSET: Token = {
decimals: 18,
hash: '-',
Expand Down
8 changes: 4 additions & 4 deletions packages/bs-ethereum/src/__tests__/BSEthereum.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ describe('BSEthereum', () => {
const transactionHash = await bsEthereum.transfer({
senderAccount: account,
intent: {
amount: '0.00000001',
amount: '0.00001',
receiverAddress: '0xFACf5446B71dB33E920aB1769d9427146183aEcd',
tokenDecimals: 18,
tokenHash: '0xba62bcfcaafc6622853cca2be6ac7d845bc0f2dc',
tokenDecimals: 6,
tokenHash: '0x1291070C5f838DCCDddc56312893d3EfE9B372a8',
},
})

Expand Down Expand Up @@ -197,7 +197,7 @@ describe('BSEthereum', () => {
const transactionHash = await service.transfer({
senderAccount: account,
intent: {
amount: '0.00000001',
amount: '0.0000000000001',
receiverAddress: '0x82B5Cd984880C8A821429cFFf89f36D35BaeBE89',
tokenDecimals: 18,
tokenHash: '-',
Expand Down

0 comments on commit a62334a

Please sign in to comment.