Transactions
Send Token
Currently, Ramper SDK supports sendToken
which is a simple way to send a token to another address from the current logged in wallet.
const sendToken = async (params: Partial<{
from: string
to: string
value: string
decimal: number
symbol: string
network: string
theme: string
}>) => Promise<boolean>
Example Usage:
import { sendToken } from '@ramper/ethereum'
await sendToken({
to: '0xa419dfa199Df8651c3f4476546AF5E4CC4E0F73F',
value: '0.00001',
network: 'mainnet'
})
RamperSigner
Ramper SDK allows you to request a signed transaction from the user for subsequent broadcasting to the network. Ramper SDK is built around Ether.js and Alchemy but if you'd like to see Ramper SDK support other use cases, let us know on Discord, or email us at [email protected]. In order to broadcast a transaction, you must first set up an Alchemy Provider.
import { ethers } from 'ethers'
const alchemy = new ethers.providers.AlchemyProvider(80001, 'pEWvHrkSkkyWGZmezdGMk_LjYu8DAx1k')
After the user is logged in through Ramper SDK, you can set up a RamperSigner
object with the alchemy provider that will eventually collect the user's authorization to sign the requested transaction.
import { getRamperSigner } from '@ramper/ethereum'
// User must be logged in
const ramperSigner = await getRamperSigner(alchemy)
Sign Transaction
import { TransactionRequest } from '@ethersproject/abstract-provider'
ramperSigner.signTransaction(
transaction: Deferrable<TransactionRequest>
): Promise<string>
Example usage:
import { getRamperSigner } from '@ramper/ethereum'
import { ethers } from 'ethers'
const alchemy = new ethers.providers.AlchemyProvider(80001, 'pEWvHrkSkkyWGZmezdGMk_LjYu8DAx1k')
const ramperSigner = await getRamperSigner(alchemy)
const value = ethers.utils.parseEther('0.0000001')
const nonce = await alchemy.getTransactionCount('your-wallet-address')
const gasLimit = await alchemy.estimateGas({
to: '0xa419dfa199Df8651c3f4476546AF5E4CC4E0F73F',
value: value,
})
const feeData = await alchemy.getFeeData()
try {
const result = await ramperSigner.signTransaction({
type: 2,
from: 'your-wallet-address',
to: '0xa419dfa199Df8651c3f4476546AF5E4CC4E0F73F',
value: value,
chainId: 80001,
nonce: nonce,
gasLimit: gasLimit,
maxFeePerGas: feeData.maxFeePerGas,
maxPriorityFeePerGas: feeData.maxPriorityFeePerGas,
})
console.log('signTransaction result', result)
} catch (e) {
console.log(e)
}
Send Transaction
import {
TransactionRequest,
TransactionResponse
} from '@ethersproject/abstract-provider'
ramperSigner.sendTransaction(
transaction: Deferrable<TransactionRequest>
): Promise<TransactionResponse>
Example usage:
import { getRamperSigner } from '@ramper/ethereum'
import { ethers } from 'ethers'
const alchemy = new ethers.providers.AlchemyProvider(80001, 'pEWvHrkSkkyWGZmezdGMk_LjYu8DAx1k')
const ramperSigner = await getRamperSigner(alchemy)
const value = ethers.utils.parseEther('0.0000001')
const nonce = await alchemy.getTransactionCount('your-wallet-address')
const gasLimit = await alchemy.estimateGas({
to: '0xa419dfa199Df8651c3f4476546AF5E4CC4E0F73F',
value: value,
})
const feeData = await alchemy.getFeeData()
try {
await ramperSigner.sendTransaction({
type: 2,
from: 'your-wallet-address',
to: '0xa419dfa199Df8651c3f4476546AF5E4CC4E0F73F',
value: value,
chainId: 80001,
nonce: nonce,
gasLimit: gasLimit,
maxFeePerGas: feeData.maxFeePerGas,
maxPriorityFeePerGas: feeData.maxPriorityFeePerGas,
})
console.log('sendTransaction result', result)
} catch (e) {
console.log(e)
}
Sign Message
ramperSigner.signMessage(
message: Bytes | string
): Promise<string>
Example usage:
import { getRamperSigner } from '@ramper/ethereum'
import { ethers } from 'ethers'
const alchemy = new ethers.providers.AlchemyProvider(80001, 'pEWvHrkSkkyWGZmezdGMk_LjYu8DAx1k')
const ramperSigner = await getRamperSigner(alchemy)
try {
const message = 'Ramper sign message test'
const result = await ramperSigner.signMessage(message)
console.log('signMessage result: ', result)
} catch (e) {
console.log(e)
}
Sign TypedData
import { TypedDataField } from '@ethersproject/abstract-signer'
ramperSigner._signTypedData(
domain: TypedDataDomain,
types: Record<string, Array<TypedDataField>>,
value: Record<string, any>,
): Promise<string>
Example usage:
import { getRamperSigner } from '@ramper/ethereum'
import { ethers } from 'ethers'
import { eip712 } from './eip712
const alchemy = new ethers.providers.AlchemyProvider(80001, 'pEWvHrkSkkyWGZmezdGMk_LjYu8DAx1k')
const ramperSigner = await getRamperSigner(alchemy)
try {
const message = 'Ramper signTypedData test'
const result = await ramperSigner.signTypedData(
eip712.example.domain,
eip712.example.types,
eip712.example.value
)
console.log('signTypedData result: ', result)
} catch (e) {
console.log(e)
}
eip712.ts
const example = {
domain: {
name: 'Ether Mail',
version: '1',
chainId: 1,
verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
},
// The named list of all type definitions
types: {
Person: [
{ name: 'name', type: 'string' },
{ name: 'wallet', type: 'address' },
],
Mail: [
{ name: 'from', type: 'Person' },
{ name: 'to', type: 'Person' },
{ name: 'contents', type: 'string' },
],
},
// The data to sign
value: {
from: {
name: 'Cow',
wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
},
to: {
name: 'Bob',
wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
},
contents: 'Hello, Bob!',
},
}
export const eip712 = {
example,
}
Error Handling
Use a try/catch block to detect if the user canceled the transaction or the transaction failed to be broadcasted successfully.
Last updated