diff --git a/packages/app/src/types/contracts/ExchangeContractAbi.d.ts b/packages/app/src/types/contracts/ExchangeContractAbi.d.ts index 3ec9e34d..0283425c 100644 --- a/packages/app/src/types/contracts/ExchangeContractAbi.d.ts +++ b/packages/app/src/types/contracts/ExchangeContractAbi.d.ts @@ -13,6 +13,16 @@ import type { BN, } from 'fuels'; +export type PreviewAddLiquidityInfoInput = { + token_amount: BigNumberish; + lp_token_received: BigNumberish; +}; + +export type PreviewAddLiquidityInfoOutput = { + token_amount: BN; + lp_token_received: BN; +}; + export type ContractIdInput = { value: string }; export type ContractIdOutput = { value: string }; @@ -45,15 +55,9 @@ export type PositionInfoOutput = { lp_token_supply: BN; }; -export type PreviewAddLiquidityInfoInput = { - token_amount: BigNumberish; - lp_token_received: BigNumberish; -}; +export type PreviewInfoInput = { amount: BigNumberish; has_liquidity: boolean }; -export type PreviewAddLiquidityInfoOutput = { - token_amount: BN; - lp_token_received: BN; -}; +export type PreviewInfoOutput = { amount: BN; has_liquidity: boolean }; export type RemoveLiquidityInfoInput = { eth_amount: BigNumberish; @@ -62,102 +66,98 @@ export type RemoveLiquidityInfoInput = { export type RemoveLiquidityInfoOutput = { eth_amount: BN; token_amount: BN }; -export type PreviewInfoInput = { amount: BigNumberish; has_liquidity: boolean }; - -export type PreviewInfoOutput = { amount: BN; has_liquidity: boolean }; - interface ExchangeContractAbiInterface extends Interface { functions: { + add_liquidity: FunctionFragment; + deposit: FunctionFragment; + get_add_liquidity: FunctionFragment; get_balance: FunctionFragment; get_pool_info: FunctionFragment; get_position: FunctionFragment; - get_add_liquidity: FunctionFragment; - deposit: FunctionFragment; - withdraw: FunctionFragment; - add_liquidity: FunctionFragment; + get_swap_with_maximum: FunctionFragment; + get_swap_with_minimum: FunctionFragment; remove_liquidity: FunctionFragment; - swap_with_minimum: FunctionFragment; swap_with_maximum: FunctionFragment; - get_swap_with_minimum: FunctionFragment; - get_swap_with_maximum: FunctionFragment; + swap_with_minimum: FunctionFragment; + withdraw: FunctionFragment; }; - encodeFunctionData(functionFragment: 'get_balance', values: [ContractIdInput]): Uint8Array; - encodeFunctionData(functionFragment: 'get_pool_info', values?: undefined): Uint8Array; - encodeFunctionData(functionFragment: 'get_position', values: [BigNumberish]): Uint8Array; encodeFunctionData( - functionFragment: 'get_add_liquidity', - values: [BigNumberish, string] + functionFragment: 'add_liquidity', + values: [BigNumberish, BigNumberish] ): Uint8Array; encodeFunctionData(functionFragment: 'deposit', values?: undefined): Uint8Array; encodeFunctionData( - functionFragment: 'withdraw', - values: [BigNumberish, ContractIdInput] - ): Uint8Array; - encodeFunctionData( - functionFragment: 'add_liquidity', - values: [BigNumberish, BigNumberish] + functionFragment: 'get_add_liquidity', + values: [BigNumberish, string] ): Uint8Array; + encodeFunctionData(functionFragment: 'get_balance', values: [ContractIdInput]): Uint8Array; + encodeFunctionData(functionFragment: 'get_pool_info', values?: undefined): Uint8Array; + encodeFunctionData(functionFragment: 'get_position', values: [BigNumberish]): Uint8Array; + encodeFunctionData(functionFragment: 'get_swap_with_maximum', values: [BigNumberish]): Uint8Array; + encodeFunctionData(functionFragment: 'get_swap_with_minimum', values: [BigNumberish]): Uint8Array; encodeFunctionData( functionFragment: 'remove_liquidity', values: [BigNumberish, BigNumberish, BigNumberish] ): Uint8Array; encodeFunctionData( - functionFragment: 'swap_with_minimum', + functionFragment: 'swap_with_maximum', values: [BigNumberish, BigNumberish] ): Uint8Array; encodeFunctionData( - functionFragment: 'swap_with_maximum', + functionFragment: 'swap_with_minimum', values: [BigNumberish, BigNumberish] ): Uint8Array; - encodeFunctionData(functionFragment: 'get_swap_with_minimum', values: [BigNumberish]): Uint8Array; - encodeFunctionData(functionFragment: 'get_swap_with_maximum', values: [BigNumberish]): Uint8Array; + encodeFunctionData( + functionFragment: 'withdraw', + values: [BigNumberish, ContractIdInput] + ): Uint8Array; + decodeFunctionData(functionFragment: 'add_liquidity', data: BytesLike): DecodedValue; + decodeFunctionData(functionFragment: 'deposit', data: BytesLike): DecodedValue; + decodeFunctionData(functionFragment: 'get_add_liquidity', data: BytesLike): DecodedValue; decodeFunctionData(functionFragment: 'get_balance', data: BytesLike): DecodedValue; decodeFunctionData(functionFragment: 'get_pool_info', data: BytesLike): DecodedValue; decodeFunctionData(functionFragment: 'get_position', data: BytesLike): DecodedValue; - decodeFunctionData(functionFragment: 'get_add_liquidity', data: BytesLike): DecodedValue; - decodeFunctionData(functionFragment: 'deposit', data: BytesLike): DecodedValue; - decodeFunctionData(functionFragment: 'withdraw', data: BytesLike): DecodedValue; - decodeFunctionData(functionFragment: 'add_liquidity', data: BytesLike): DecodedValue; + decodeFunctionData(functionFragment: 'get_swap_with_maximum', data: BytesLike): DecodedValue; + decodeFunctionData(functionFragment: 'get_swap_with_minimum', data: BytesLike): DecodedValue; decodeFunctionData(functionFragment: 'remove_liquidity', data: BytesLike): DecodedValue; - decodeFunctionData(functionFragment: 'swap_with_minimum', data: BytesLike): DecodedValue; decodeFunctionData(functionFragment: 'swap_with_maximum', data: BytesLike): DecodedValue; - decodeFunctionData(functionFragment: 'get_swap_with_minimum', data: BytesLike): DecodedValue; - decodeFunctionData(functionFragment: 'get_swap_with_maximum', data: BytesLike): DecodedValue; + decodeFunctionData(functionFragment: 'swap_with_minimum', data: BytesLike): DecodedValue; + decodeFunctionData(functionFragment: 'withdraw', data: BytesLike): DecodedValue; } export class ExchangeContractAbi extends Contract { interface: ExchangeContractAbiInterface; functions: { - get_balance: InvokeFunction<[asset_id: ContractIdInput], BN>; - - get_pool_info: InvokeFunction<[], PoolInfoOutput>; + add_liquidity: InvokeFunction<[min_liquidity: BigNumberish, deadline: BigNumberish], BN>; - get_position: InvokeFunction<[amount: BigNumberish], PositionInfoOutput>; + deposit: InvokeFunction<[], void>; get_add_liquidity: InvokeFunction< [amount: BigNumberish, asset_id: string], PreviewAddLiquidityInfoOutput >; - deposit: InvokeFunction<[], void>; + get_balance: InvokeFunction<[asset_id: ContractIdInput], BN>; - withdraw: InvokeFunction<[amount: BigNumberish, asset_id: ContractIdInput], void>; + get_pool_info: InvokeFunction<[], PoolInfoOutput>; - add_liquidity: InvokeFunction<[min_liquidity: BigNumberish, deadline: BigNumberish], BN>; + get_position: InvokeFunction<[amount: BigNumberish], PositionInfoOutput>; + + get_swap_with_maximum: InvokeFunction<[amount: BigNumberish], PreviewInfoOutput>; + + get_swap_with_minimum: InvokeFunction<[amount: BigNumberish], PreviewInfoOutput>; remove_liquidity: InvokeFunction< [min_eth: BigNumberish, min_tokens: BigNumberish, deadline: BigNumberish], RemoveLiquidityInfoOutput >; - swap_with_minimum: InvokeFunction<[min: BigNumberish, deadline: BigNumberish], BN>; - swap_with_maximum: InvokeFunction<[amount: BigNumberish, deadline: BigNumberish], BN>; - get_swap_with_minimum: InvokeFunction<[amount: BigNumberish], PreviewInfoOutput>; + swap_with_minimum: InvokeFunction<[min: BigNumberish, deadline: BigNumberish], BN>; - get_swap_with_maximum: InvokeFunction<[amount: BigNumberish], PreviewInfoOutput>; + withdraw: InvokeFunction<[amount: BigNumberish, asset_id: ContractIdInput], void>; }; } diff --git a/packages/app/src/types/contracts/TokenContractAbi.d.ts b/packages/app/src/types/contracts/TokenContractAbi.d.ts index 0e1cbf7b..1bc68e21 100644 --- a/packages/app/src/types/contracts/TokenContractAbi.d.ts +++ b/packages/app/src/types/contracts/TokenContractAbi.d.ts @@ -13,35 +13,39 @@ import type { BN, } from 'fuels'; -export type AddressInput = { value: string }; - -export type AddressOutput = { value: string }; - export type ContractIdInput = { value: string }; export type ContractIdOutput = { value: string }; +export type AddressInput = { value: string }; + +export type AddressOutput = { value: string }; + interface TokenContractAbiInterface extends Interface { functions: { + burn_coins: FunctionFragment; + get_balance: FunctionFragment; + get_mint_amount: FunctionFragment; + get_token_balance: FunctionFragment; initialize: FunctionFragment; - set_mint_amount: FunctionFragment; + mint: FunctionFragment; mint_coins: FunctionFragment; - burn_coins: FunctionFragment; + set_mint_amount: FunctionFragment; transfer_coins: FunctionFragment; transfer_token_to_output: FunctionFragment; - mint: FunctionFragment; - get_mint_amount: FunctionFragment; - get_balance: FunctionFragment; - get_token_balance: FunctionFragment; }; + encodeFunctionData(functionFragment: 'burn_coins', values: [BigNumberish]): Uint8Array; + encodeFunctionData(functionFragment: 'get_balance', values?: undefined): Uint8Array; + encodeFunctionData(functionFragment: 'get_mint_amount', values?: undefined): Uint8Array; + encodeFunctionData(functionFragment: 'get_token_balance', values: [ContractIdInput]): Uint8Array; encodeFunctionData( functionFragment: 'initialize', values: [BigNumberish, AddressInput] ): Uint8Array; - encodeFunctionData(functionFragment: 'set_mint_amount', values: [BigNumberish]): Uint8Array; + encodeFunctionData(functionFragment: 'mint', values?: undefined): Uint8Array; encodeFunctionData(functionFragment: 'mint_coins', values: [BigNumberish]): Uint8Array; - encodeFunctionData(functionFragment: 'burn_coins', values: [BigNumberish]): Uint8Array; + encodeFunctionData(functionFragment: 'set_mint_amount', values: [BigNumberish]): Uint8Array; encodeFunctionData( functionFragment: 'transfer_coins', values: [BigNumberish, AddressInput] @@ -50,33 +54,37 @@ interface TokenContractAbiInterface extends Interface { functionFragment: 'transfer_token_to_output', values: [BigNumberish, ContractIdInput, AddressInput] ): Uint8Array; - encodeFunctionData(functionFragment: 'mint', values?: undefined): Uint8Array; - encodeFunctionData(functionFragment: 'get_mint_amount', values?: undefined): Uint8Array; - encodeFunctionData(functionFragment: 'get_balance', values?: undefined): Uint8Array; - encodeFunctionData(functionFragment: 'get_token_balance', values: [ContractIdInput]): Uint8Array; + decodeFunctionData(functionFragment: 'burn_coins', data: BytesLike): DecodedValue; + decodeFunctionData(functionFragment: 'get_balance', data: BytesLike): DecodedValue; + decodeFunctionData(functionFragment: 'get_mint_amount', data: BytesLike): DecodedValue; + decodeFunctionData(functionFragment: 'get_token_balance', data: BytesLike): DecodedValue; decodeFunctionData(functionFragment: 'initialize', data: BytesLike): DecodedValue; - decodeFunctionData(functionFragment: 'set_mint_amount', data: BytesLike): DecodedValue; + decodeFunctionData(functionFragment: 'mint', data: BytesLike): DecodedValue; decodeFunctionData(functionFragment: 'mint_coins', data: BytesLike): DecodedValue; - decodeFunctionData(functionFragment: 'burn_coins', data: BytesLike): DecodedValue; + decodeFunctionData(functionFragment: 'set_mint_amount', data: BytesLike): DecodedValue; decodeFunctionData(functionFragment: 'transfer_coins', data: BytesLike): DecodedValue; decodeFunctionData(functionFragment: 'transfer_token_to_output', data: BytesLike): DecodedValue; - decodeFunctionData(functionFragment: 'mint', data: BytesLike): DecodedValue; - decodeFunctionData(functionFragment: 'get_mint_amount', data: BytesLike): DecodedValue; - decodeFunctionData(functionFragment: 'get_balance', data: BytesLike): DecodedValue; - decodeFunctionData(functionFragment: 'get_token_balance', data: BytesLike): DecodedValue; } export class TokenContractAbi extends Contract { interface: TokenContractAbiInterface; functions: { + burn_coins: InvokeFunction<[burn_amount: BigNumberish], void>; + + get_balance: InvokeFunction<[], BN>; + + get_mint_amount: InvokeFunction<[], BN>; + + get_token_balance: InvokeFunction<[asset_id: ContractIdInput], BN>; + initialize: InvokeFunction<[mint_amount: BigNumberish, address: AddressInput], void>; - set_mint_amount: InvokeFunction<[mint_amount: BigNumberish], void>; + mint: InvokeFunction<[], void>; mint_coins: InvokeFunction<[mint_amount: BigNumberish], void>; - burn_coins: InvokeFunction<[burn_amount: BigNumberish], void>; + set_mint_amount: InvokeFunction<[mint_amount: BigNumberish], void>; transfer_coins: InvokeFunction<[coins: BigNumberish, address: AddressInput], void>; @@ -84,13 +92,5 @@ export class TokenContractAbi extends Contract { [coins: BigNumberish, asset_id: ContractIdInput, address: AddressInput], void >; - - mint: InvokeFunction<[], void>; - - get_mint_amount: InvokeFunction<[], BN>; - - get_balance: InvokeFunction<[], BN>; - - get_token_balance: InvokeFunction<[asset_id: ContractIdInput], BN>; }; } diff --git a/packages/app/src/types/contracts/factories/ExchangeContractAbi__factory.ts b/packages/app/src/types/contracts/factories/ExchangeContractAbi__factory.ts index 1d6d1331..97d83bae 100644 --- a/packages/app/src/types/contracts/factories/ExchangeContractAbi__factory.ts +++ b/packages/app/src/types/contracts/factories/ExchangeContractAbi__factory.ts @@ -6,6 +6,68 @@ import type { Provider, Wallet, AbstractAddress } from 'fuels'; import { Interface, Contract } from 'fuels'; import type { ExchangeContractAbi, ExchangeContractAbiInterface } from '../ExchangeContractAbi'; const _abi = [ + { + type: 'function', + name: 'add_liquidity', + inputs: [ + { + type: 'u64', + name: 'min_liquidity', + }, + { + type: 'u64', + name: 'deadline', + }, + ], + outputs: [ + { + type: 'u64', + name: '', + }, + ], + }, + { + type: 'function', + name: 'deposit', + inputs: [], + outputs: [ + { + type: '()', + name: '', + components: [], + }, + ], + }, + { + type: 'function', + name: 'get_add_liquidity', + inputs: [ + { + type: 'u64', + name: 'amount', + }, + { + type: 'b256', + name: 'asset_id', + }, + ], + outputs: [ + { + type: 'struct PreviewAddLiquidityInfo', + name: '', + components: [ + { + type: 'u64', + name: 'token_amount', + }, + { + type: 'u64', + name: 'lp_token_received', + }, + ], + }, + ], + }, { type: 'function', name: 'get_balance', @@ -93,29 +155,25 @@ const _abi = [ }, { type: 'function', - name: 'get_add_liquidity', + name: 'get_swap_with_maximum', inputs: [ { type: 'u64', name: 'amount', }, - { - type: 'b256', - name: 'asset_id', - }, ], outputs: [ { - type: 'struct PreviewAddLiquidityInfo', + type: 'struct PreviewInfo', name: '', components: [ { type: 'u64', - name: 'token_amount', + name: 'amount', }, { - type: 'u64', - name: 'lp_token_received', + type: 'bool', + name: 'has_liquidity', }, ], }, @@ -123,62 +181,29 @@ const _abi = [ }, { type: 'function', - name: 'deposit', - inputs: [], - outputs: [ - { - type: '()', - name: '', - components: [], - }, - ], - }, - { - type: 'function', - name: 'withdraw', + name: 'get_swap_with_minimum', inputs: [ { type: 'u64', name: 'amount', }, + ], + outputs: [ { - type: 'struct ContractId', - name: 'asset_id', + type: 'struct PreviewInfo', + name: '', components: [ { - type: 'b256', - name: 'value', + type: 'u64', + name: 'amount', + }, + { + type: 'bool', + name: 'has_liquidity', }, ], }, ], - outputs: [ - { - type: '()', - name: '', - components: [], - }, - ], - }, - { - type: 'function', - name: 'add_liquidity', - inputs: [ - { - type: 'u64', - name: 'min_liquidity', - }, - { - type: 'u64', - name: 'deadline', - }, - ], - outputs: [ - { - type: 'u64', - name: '', - }, - ], }, { type: 'function', @@ -216,11 +241,11 @@ const _abi = [ }, { type: 'function', - name: 'swap_with_minimum', + name: 'swap_with_maximum', inputs: [ { type: 'u64', - name: 'min', + name: 'amount', }, { type: 'u64', @@ -236,11 +261,11 @@ const _abi = [ }, { type: 'function', - name: 'swap_with_maximum', + name: 'swap_with_minimum', inputs: [ { type: 'u64', - name: 'amount', + name: 'min', }, { type: 'u64', @@ -256,53 +281,28 @@ const _abi = [ }, { type: 'function', - name: 'get_swap_with_minimum', + name: 'withdraw', inputs: [ { type: 'u64', name: 'amount', }, - ], - outputs: [ { - type: 'struct PreviewInfo', - name: '', + type: 'struct ContractId', + name: 'asset_id', components: [ { - type: 'u64', - name: 'amount', - }, - { - type: 'bool', - name: 'has_liquidity', + type: 'b256', + name: 'value', }, ], }, ], - }, - { - type: 'function', - name: 'get_swap_with_maximum', - inputs: [ - { - type: 'u64', - name: 'amount', - }, - ], outputs: [ { - type: 'struct PreviewInfo', + type: '()', name: '', - components: [ - { - type: 'u64', - name: 'amount', - }, - { - type: 'bool', - name: 'has_liquidity', - }, - ], + components: [], }, ], }, diff --git a/packages/app/src/types/contracts/factories/TokenContractAbi__factory.ts b/packages/app/src/types/contracts/factories/TokenContractAbi__factory.ts index b925c4b1..7cadb259 100644 --- a/packages/app/src/types/contracts/factories/TokenContractAbi__factory.ts +++ b/packages/app/src/types/contracts/factories/TokenContractAbi__factory.ts @@ -8,15 +8,50 @@ import type { TokenContractAbi, TokenContractAbiInterface } from '../TokenContra const _abi = [ { type: 'function', - name: 'initialize', + name: 'burn_coins', inputs: [ { type: 'u64', - name: 'mint_amount', + name: 'burn_amount', }, + ], + outputs: [ { - type: 'struct Address', - name: 'address', + type: '()', + name: '', + components: [], + }, + ], + }, + { + type: 'function', + name: 'get_balance', + inputs: [], + outputs: [ + { + type: 'u64', + name: '', + }, + ], + }, + { + type: 'function', + name: 'get_mint_amount', + inputs: [], + outputs: [ + { + type: 'u64', + name: '', + }, + ], + }, + { + type: 'function', + name: 'get_token_balance', + inputs: [ + { + type: 'struct ContractId', + name: 'asset_id', components: [ { type: 'b256', @@ -27,21 +62,42 @@ const _abi = [ ], outputs: [ { - type: '()', + type: 'u64', name: '', - components: [], }, ], }, { type: 'function', - name: 'set_mint_amount', + name: 'initialize', inputs: [ { type: 'u64', name: 'mint_amount', }, + { + type: 'struct Address', + name: 'address', + components: [ + { + type: 'b256', + name: 'value', + }, + ], + }, + ], + outputs: [ + { + type: '()', + name: '', + components: [], + }, ], + }, + { + type: 'function', + name: 'mint', + inputs: [], outputs: [ { type: '()', @@ -69,11 +125,11 @@ const _abi = [ }, { type: 'function', - name: 'burn_coins', + name: 'set_mint_amount', inputs: [ { type: 'u64', - name: 'burn_amount', + name: 'mint_amount', }, ], outputs: [ @@ -148,62 +204,6 @@ const _abi = [ }, ], }, - { - type: 'function', - name: 'mint', - inputs: [], - outputs: [ - { - type: '()', - name: '', - components: [], - }, - ], - }, - { - type: 'function', - name: 'get_mint_amount', - inputs: [], - outputs: [ - { - type: 'u64', - name: '', - }, - ], - }, - { - type: 'function', - name: 'get_balance', - inputs: [], - outputs: [ - { - type: 'u64', - name: '', - }, - ], - }, - { - type: 'function', - name: 'get_token_balance', - inputs: [ - { - type: 'struct ContractId', - name: 'asset_id', - components: [ - { - type: 'b256', - name: 'value', - }, - ], - }, - ], - outputs: [ - { - type: 'u64', - name: '', - }, - ], - }, ]; export class TokenContractAbi__factory { diff --git a/packages/contracts/exchange_abi/src/main.sw b/packages/contracts/exchange_abi/src/main.sw index e9b79dcb..ec24632a 100644 --- a/packages/contracts/exchange_abi/src/main.sw +++ b/packages/contracts/exchange_abi/src/main.sw @@ -69,9 +69,9 @@ abi Exchange { #[storage(read, write)] fn swap_with_maximum(amount: u64, deadline: u64) -> u64; /// Get the minimum amount of coins that will be received for a swap_with_minimum. - #[storage(read, write)] + #[storage(read)] fn get_swap_with_minimum(amount: u64) -> PreviewInfo; /// Get required amount of coins for a swap_with_maximum. - #[storage(read, write)] + #[storage(read)] fn get_swap_with_maximum(amount: u64) -> PreviewInfo; } diff --git a/packages/contracts/exchange_contract/src/main.sw b/packages/contracts/exchange_contract/src/main.sw index bee76865..e181086b 100644 --- a/packages/contracts/exchange_contract/src/main.sw +++ b/packages/contracts/exchange_contract/src/main.sw @@ -4,8 +4,11 @@ use std::{ address::*, assert::assert, block::*, - chain::auth::*, - context::{*, call_frames::*}, + call_frames::{ + contract_id, + msg_asset_id, + }, + context::msg_amount, contract_id::ContractId, hash::*, result::*, @@ -74,8 +77,8 @@ fn calculate_amount_with_fee(amount: u64) -> u64 { } fn mutiply_div(a: u64, b: u64, c: u64) -> u64 { - let calculation = (~U128::from(0, a) * ~U128::from(0, b)); - let result_wrapped = (calculation / ~U128::from(0, c)).as_u64(); + let calculation = (U128::from((0, a)) * U128::from((0, b))); + let result_wrapped = (calculation / U128::from((0, c))).as_u64(); // TODO remove workaround once https://github.com/FuelLabs/sway/pull/1671 lands. match result_wrapped { @@ -84,8 +87,8 @@ fn mutiply_div(a: u64, b: u64, c: u64) -> u64 { } fn div_mutiply(a: u64, b: u64, c: u64) -> u64 { - let calculation = (~U128::from(0, a) / ~U128::from(0, b)); - let result_wrapped = (calculation * ~U128::from(0, c)).as_u64(); + let calculation = (U128::from((0, a)) / U128::from((0, b))); + let result_wrapped = (calculation * U128::from((0, c))).as_u64(); // TODO remove workaround once https://github.com/FuelLabs/sway/pull/1671 lands. match result_wrapped { @@ -97,8 +100,8 @@ fn div_mutiply(a: u64, b: u64, c: u64) -> u64 { fn get_input_price(input_amount: u64, input_reserve: u64, output_reserve: u64) -> u64 { assert(input_reserve > 0 && output_reserve > 0); let input_amount_with_fee: u64 = calculate_amount_with_fee(input_amount); - let numerator = ~U128::from(0, input_amount_with_fee) * ~U128::from(0, output_reserve); - let denominator = ~U128::from(0, input_reserve) + ~U128::from(0, input_amount_with_fee); + let numerator = U128::from((0, input_amount_with_fee)) * U128::from((0, output_reserve)); + let denominator = U128::from((0, input_reserve)) + U128::from((0, input_amount_with_fee)); let result_wrapped = (numerator / denominator).as_u64(); // TODO remove workaround once https://github.com/FuelLabs/sway/pull/1671 lands. match result_wrapped { @@ -109,12 +112,12 @@ fn get_input_price(input_amount: u64, input_reserve: u64, output_reserve: u64) - /// Pricing function for converting between ETH and Tokens. fn get_output_price(output_amount: u64, input_reserve: u64, output_reserve: u64) -> u64 { assert(input_reserve > 0 && output_reserve > 0); - let numerator = ~U128::from(0, input_reserve) * ~U128::from(0, output_amount); - let denominator = ~U128::from(0, calculate_amount_with_fee(output_reserve - output_amount)); + let numerator = U128::from((0, input_reserve)) * U128::from((0, output_amount)); + let denominator = U128::from((0, calculate_amount_with_fee(output_reserve - output_amount))); let result_wrapped = (numerator / denominator).as_u64(); if denominator > numerator { // Emulate Infinity Value - ~u64::max() + u64::max() } else { // TODO remove workaround once https://github.com/FuelLabs/sway/pull/1671 lands. match result_wrapped { @@ -225,8 +228,8 @@ impl Exchange for Contract { let total_liquidity = storage.lp_token_supply; - let current_eth_amount = storage.deposits.get((sender, ~ContractId::from(ETH_ID))); - let current_token_amount = storage.deposits.get((sender, ~ContractId::from(get::(TOKEN_ID_KEY)))); + let current_eth_amount = storage.deposits.get((sender, ContractId::from(ETH_ID))); + let current_token_amount = storage.deposits.get((sender, ContractId::from(get::(TOKEN_ID_KEY)))); assert(current_eth_amount > 0); @@ -256,13 +259,13 @@ impl Exchange for Contract { // If user sent more than the correct ratio, we deposit back the extra tokens let token_extra = current_token_amount - token_amount; if (token_extra > 0) { - transfer_to_address(token_extra, ~ContractId::from(get::(TOKEN_ID_KEY)), sender); + transfer_to_address(token_extra, ContractId::from(get::(TOKEN_ID_KEY)), sender); } minted = liquidity_minted; } else { - transfer_to_address(current_token_amount, ~ContractId::from(get::(TOKEN_ID_KEY)), sender); - transfer_to_address(current_eth_amount, ~ContractId::from(ETH_ID), sender); + transfer_to_address(current_token_amount, ContractId::from(get::(TOKEN_ID_KEY)), sender); + transfer_to_address(current_eth_amount, ContractId::from(ETH_ID), sender); minted = 0; } } else { @@ -284,8 +287,8 @@ impl Exchange for Contract { }; // Clear user contract balances after finishing add/create liquidity - storage.deposits.insert((sender, ~ContractId::from(get::(TOKEN_ID_KEY))), 0); - storage.deposits.insert((sender, ~ContractId::from(ETH_ID)), 0); + storage.deposits.insert((sender, ContractId::from(get::(TOKEN_ID_KEY))), 0); + storage.deposits.insert((sender, ContractId::from(ETH_ID)), 0); minted } @@ -317,8 +320,8 @@ impl Exchange for Contract { remove_reserve(ETH_ID, eth_amount); // Send tokens back - transfer_to_address(eth_amount, ~ContractId::from(ETH_ID), sender); - transfer_to_address(token_amount, ~ContractId::from(get::(TOKEN_ID_KEY)), sender); + transfer_to_address(eth_amount, ContractId::from(ETH_ID), sender); + transfer_to_address(token_amount, ContractId::from(get::(TOKEN_ID_KEY)), sender); RemoveLiquidityInfo { eth_amount: eth_amount, @@ -344,7 +347,7 @@ impl Exchange for Contract { if (asset_id == ETH_ID) { let tokens_bought = get_input_price(forwarded_amount, eth_reserve, token_reserve); assert(tokens_bought >= min); - transfer_to_address(tokens_bought, ~ContractId::from(get::(TOKEN_ID_KEY)), sender); + transfer_to_address(tokens_bought, ContractId::from(get::(TOKEN_ID_KEY)), sender); bought = tokens_bought; // Update reserve add_reserve(ETH_ID, forwarded_amount); @@ -352,7 +355,7 @@ impl Exchange for Contract { } else { let eth_bought = get_input_price(forwarded_amount, token_reserve, eth_reserve); assert(eth_bought >= min); - transfer_to_address(eth_bought, ~ContractId::from(ETH_ID), sender); + transfer_to_address(eth_bought, ContractId::from(ETH_ID), sender); bought = eth_bought; // Update reserve remove_reserve(ETH_ID, eth_bought); @@ -380,9 +383,9 @@ impl Exchange for Contract { assert(forwarded_amount >= eth_sold); let refund = forwarded_amount - eth_sold; if refund > 0 { - transfer_to_address(refund, ~ContractId::from(ETH_ID), sender); + transfer_to_address(refund, ContractId::from(ETH_ID), sender); }; - transfer_to_address(amount, ~ContractId::from(get::(TOKEN_ID_KEY)), sender); + transfer_to_address(amount, ContractId::from(get::(TOKEN_ID_KEY)), sender); sold = eth_sold; // Update reserve add_reserve(ETH_ID, eth_sold); @@ -392,9 +395,9 @@ impl Exchange for Contract { assert(forwarded_amount >= tokens_sold); let refund = forwarded_amount - tokens_sold; if refund > 0 { - transfer_to_address(refund, ~ContractId::from(get::(TOKEN_ID_KEY)), sender); + transfer_to_address(refund, ContractId::from(get::(TOKEN_ID_KEY)), sender); }; - transfer_to_address(amount, ~ContractId::from(ETH_ID), sender); + transfer_to_address(amount, ContractId::from(ETH_ID), sender); sold = tokens_sold; // Update reserve remove_reserve(ETH_ID, amount); @@ -403,7 +406,7 @@ impl Exchange for Contract { sold } - #[storage(read, write)] + #[storage(read)] fn get_swap_with_minimum(amount: u64) -> PreviewInfo { let eth_reserve = get_current_reserve(ETH_ID); let token_reserve = get_current_reserve(get::(TOKEN_ID_KEY)); @@ -422,7 +425,7 @@ impl Exchange for Contract { } } - #[storage(read, write)] + #[storage(read)] fn get_swap_with_maximum(amount: u64) -> PreviewInfo { let eth_reserve = get_current_reserve(ETH_ID); let token_reserve = get_current_reserve(get::(TOKEN_ID_KEY)); diff --git a/packages/contracts/swayswap_contract/src/main.sw b/packages/contracts/swayswap_contract/src/main.sw index 290f9bc1..d593e5c8 100644 --- a/packages/contracts/swayswap_contract/src/main.sw +++ b/packages/contracts/swayswap_contract/src/main.sw @@ -16,6 +16,6 @@ impl SwapSwap for Contract { store_b256(token_id.into(), exchange_id.into()); } fn get_exchange_contract(token_id: ContractId) -> ContractId { - ~ContractId::from(get_b256(token_id.into())) + ContractId::from(get_b256(token_id.into())) } } diff --git a/packages/contracts/swayswap_helpers/src/main.sw b/packages/contracts/swayswap_helpers/src/main.sw index d42b85e6..ebee95b4 100644 --- a/packages/contracts/swayswap_helpers/src/main.sw +++ b/packages/contracts/swayswap_helpers/src/main.sw @@ -3,26 +3,28 @@ library swayswap_helpers; use std::{ address::*, block::*, - chain::auth::*, - context::{*, call_frames::*}, + auth::{ + AuthError, + msg_sender, + }, result::*, revert::revert, identity::Identity, }; pub fn get_b256(key: b256) -> b256 { - asm(r1: key, r2) { + asm (key, is_set, r2, count: 1) { move r2 sp; cfei i32; - srwq r2 r1; + srwq r2 is_set r1 count; r2: b256 } } // Store b256 values on memory pub fn store_b256(key: b256, value: b256) { - asm(r1: key, r2: value) { - swwq r1 r2; + asm(key, is_set, value, count: 1) { + swwq key is_set value count; }; } diff --git a/packages/contracts/token_contract/src/main.sw b/packages/contracts/token_contract/src/main.sw index 7129fd00..f8646e87 100644 --- a/packages/contracts/token_contract/src/main.sw +++ b/packages/contracts/token_contract/src/main.sw @@ -3,10 +3,13 @@ contract; use std::{ address::*, revert::require, - context::{*, call_frames::*}, contract_id::ContractId, storage::*, token::*, + call_frames::{ + contract_id, + }, + context::balance_of, }; use token_abi::Token;