Nexera-Fi SDKUsage ExamplesSupported Adapters
WrappedNativeToken Adapter Showcase
In this demonstration, we'll highlight the application of the WrappedNativeToken Adapter within the Nexera-Fi SDK for executing a token swap operation, assuming that the strategy Orchestrator instance is already deployed and configured (see Deployment & Configuration Guide)
Specifically, we will swap native token (ETH) for its ERC-20 compliant wrapped token (WETH).
Make sure you've appropriately initialized the NexeraFiSDK, covering configurations essential for further interactions.
Typescript Example
const chainId: ChainId = 1; // Ethereum Mainnet
// Target Assets
// Note: Assets at play should be already whitelisted in the `Orchestrator`
const WETH = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"; // Target asset (WETH)
const ETH = ethers.constants.AddressZero // "0x000...000" is the Sentinel value for native currency (i.e. ETH)
// Target Adapter (e.g. WrappedNativeTokenAdapter)
const WrappedNativeTokenAdapter = nexeraFiSdk.getAdapters(chainId).WrappedNativeTokenAdapter;
// Connect to your Orchestrator instance
// Note: `WrappedNativeTokenAdapter` should be already whitelisted in the `Orchestrator`
const orchestratorAddress = "0x..."; // Replace with the addrress of the instance you own
const Orchestrator = nexeraFiSdk.getOrchestrator(orchestratorAddress, wallet_signer);
// Get nonce for Tx
let nonce_ = await wallet_signer.getTransactionCount();
// Send ETH to Orchestrator
// (Adapters can only manage assets that are allocated to the Orchestrator)
await wallet_signer.sendTransaction({
to: Orchestrator.address,
value: ethers.utils.parseEther("10"), // Send 10 ETH
nonce: nonce_
});
/* ---------------------------- Construct `out` and `expectedIn` token compositions ------------------------ */
// Tokens to send (i.e. 10 ETH)
const out = [
{
token: ETH,
amount: ethers.utils.parseEther("10")
},
];
// Token to receive (i.e. WETH)
// `amount` field is for Minimum amount of tokens expected to be received (set to "0" for no limit indication)
const expectedIn = [
{ token: WETH, amount: "0" }
];
// Use `callStatic` to simulate the Tx and acquire what will be received (asset-wise) by the Orchestrator
const totalReceived = await Orchestrator.callStatic.execute(
[{
adapter: WrappedNativeTokenAdapter.address,
op: Operation.SWAP, // `Operation` enum is exported by the NexeraFi-SDK package
send: out,
minReceive: expectedIn,
extraData: "0x"
}]
);
nonce_ = await wallet_signer.getTransactionCount(); // Get nonce for Tx
// Execute Swap action
await Orchestrator.execute(
[{
adapter: WrappedNativeTokenAdapter.address,
op: Operation.SWAP,
send: out,
minReceive: expectedIn,
extraData: "0x"
}],
{nonce: nonce_}
);
nonce_ = await wallet_signer.getTransactionCount(); // Get nonce for Tx
// Withdraw specified assets from the Orchestrator and transfer them to a designated receiver
await Orchestrator.withdraw(WETH, totalReceived[0][0].amount, wallet_signer.address, {nonce: nonce_})
Python Example
load_dotenv(override=True)
config = dotenv_values(".env")
# --------------- Configurations - Set up
executor_account_address = "0x...." # Replace with the address of the designated executor account
executor_pk = config.get("PRIVATE_KEY_EXECUTOR") # Private key of designated executor account
chain_id = 1 # Ethereum Mainnet
# Target Assets
# Note: Assets at play should be already whitelisted in the `Orchestrator`
WETH = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2" # Target asset (WETH)
ETH = web3.constants.ADDRESS_ZERO # "0x000...000" is the Sentinel value for native currency (i.e. ETH)
# Target Adapter (e.g. WrappedNativeTokenAdapter)
WrappedNativeToken_Adapter = get_adapter_addresses(chain_id).WrappedNativeTokenAdapter
OPERATION = get_operations() # Get Operations enum for Orchestrator
# Get Orchestrator instance
# Note: `WrappedNativeTokenAdapter` should be already whitelisted in the `Orchestrator`
orchestrator_address = "0x..." # Replace with the addrress of the instance you own
Orchestrator = get_orchestrator(orchestrator_address, w3)
"""
At this point, it is assumed that:
- Orchestrator instance holds some ETH balance (i.e. 10 ETH)
"""
# ---------------------------- Construct `out` and `expectedIn` token compositions ------------------------
# Tokens to send (i.e. 10 ETH)
out = [
{
token: ETH,
amount: w3.to_wei(10, 'ether')
},
]
# Token to receive (i.e. WETH)
# `amount` field is for Minimum amount of tokens expected to be received (set to `0` for no limit indication)
expectedIn = [
{ token: WETH, amount: 0 }
]
# Use `call` to simulate the Tx and acquire what will be received (asset-wise) by the Orchestrator
total_received = Orchestrator.functions.execute(
[{
adapter: WrappedNativeToken_Adapter.address,
op: OPERATION.SWAP.value,
send: out,
minReceive: expectedIn,
extraData: bytes()
}]
).call({"from": executor_account_address})
# Get nonce for Tx
nonce_ = w3.eth.get_transaction_count(executor_account_address)
# Execute Swap action ----------------------------
# Build Tx request object
transaction = Orchestrator.functions.execute(
[{
adapter: WrappedNativeToken_Adapter.address,
op: OPERATION.SWAP.value,
send: out,
minReceive: expectedIn,
extraData: bytes()
}]
).build_transaction(
{"chainId": chain_id, "from": executor_account_address, "nonce": nonce_}
)
# Sign Tx request object
signed_tx = w3.eth.account.sign_transaction(transaction, private_key=executor_pk)
# Send the signed Tx request object to the blockchain for resolution
tx_hash = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
# Wait for block confirmations to get the Tx receipt object
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)