Skip to content

Commit

Permalink
add vault and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hardingjam committed Jan 30, 2025
1 parent a203684 commit 9af1ed8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 24 deletions.
16 changes: 9 additions & 7 deletions crates/js_api/src/subgraph/vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,19 @@ pub async fn get_vault_balance_changes(
/// Returns a string of the calldata
#[wasm_bindgen(js_name = "getVaultDepositCalldata")]
pub async fn get_vault_deposit_calldata(
rpc_url: &str,
order_id: &str,
output_index: u8,
token_address: &str,
vault_id: &str,
deposit_amount: &str,
) -> Result<JsValue, SubgraphError> {
let deposit_amount = validate_amount(deposit_amount)?;
let order = get_sg_order(rpc_url, order_id).await?;
let index = validate_io_index(&order, false, output_index)?;
let token = Address::from_str(token_address)?;
let vault_id = U256::from_str(vault_id)?;

let (deposit_args, _) =
get_deposit_and_transaction_args(rpc_url, &order, index, deposit_amount)?;
let deposit_args = DepositArgs {
token,
amount: deposit_amount,
vault_id,
};

Ok(to_value(&Bytes::copy_from_slice(
&deposit_args.get_deposit_calldata().await?,
Expand Down
19 changes: 2 additions & 17 deletions packages/orderbook/test/js_api/vault.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,26 +238,11 @@ describe('Rain Orderbook JS API Package Bindgen Vault Tests', async function ()
await mockServer.forPost('/sg4').thenReply(200, JSON.stringify({ data: { order } }));

let calldata: string = await getVaultDepositCalldata(
mockServer.url + '/sg4',
'order1',
0,
vault1.token.address,
vault1.vaultId,
'500'
);
assert.equal(calldata.length, 330);

try {
await getVaultDepositCalldata(mockServer.url + '/sg4', 'order1', 99, '500');
assert.fail('expected to reject, but resolved');
} catch (e) {
assert.equal((e as Error).message, 'Invalid output index');
}

try {
await getVaultDepositCalldata(mockServer.url + '/sg4', 'order1', 0, '0');
assert.fail('expected to reject, but resolved');
} catch (error) {
assert.equal((error as Error).message, 'Invalid amount');
}
});

it('should get withdraw calldata for a vault', async () => {
Expand Down

0 comments on commit 9af1ed8

Please sign in to comment.