Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Converting USD -> L$ at market rates

Angel Fluffy
Very Helpful
Join date: 3 Mar 2006
Posts: 810
11-22-2006 11:52
Hi. I'm looking for a working, open source function that when passed a USD value returns the equivalent value in L$, at current market rates.

Not a whole script, just a function to convert quickly between USD and L$.
I figure since other people already have scripts that do this, I should ask for copies before re-inventing the wheel. :)
_____________________
Volunteer Portal (FAQs!) : https://wiki.secondlife.com/wiki/Volunteer_Portal

JIRA / Issue Tracker : http://jira.secondlife.com (& http://tinyurl.com/2jropp)
Francis Chung
This sentence no verb.
Join date: 22 Sep 2003
Posts: 918
11-22-2006 13:17
I'm not sure this helps you at all, but this is what I use. It's written in C.

Prior to LindeX, Gaming Open Market was the norm, and they calculated rates in USD/L$1000 - that's how we used to think about rates. This also helped me identify exactly how much LL was skimming with the LindeX. (LL's fees are significantly higher than GOM's fees were)


CODE

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

#define BUFSIZE 1024

#define ABS(x) ((x)<0 ? -(x) : (x))

// Rate: Effective market rate, L$/USD
// Commision: Transaction Fee, as a fraction (3.5% on 11/02/05)
// Withdrawl: Withdrawl Fee, a constant in USD (USD1 with paypal 11/23/05)
// Quantity: Quantity of transaction, in thousands of L$
void printRate( long rate_l, double commission, double withdrawl, long quantity) {
double rate = rate_l;
double invertedRate;
double effectiveRate;
double proceeds;
double totalCommission;

// Proceeds in USD from this transaction
proceeds = quantity / rate * 1000 * (1-commission) - withdrawl;
proceeds = rint(proceeds * 100) / 100; // Round to 2 significant digits

// Effective rate in USD$ / Block
effectiveRate = proceeds / (double)quantity;
// Round to 3 significant digits
effectiveRate = rint(effectiveRate*1000) / 1000;

// Effective rate in L$ / USD
invertedRate = 1 / effectiveRate * 1000;
// Round to 1 significant digit
invertedRate = rint(invertedRate*10) / 10;

// Total amount of money that LL chargese
totalCommission = quantity / rate * 1000 - proceeds;

printf( " Rate: USD %.3lf / L$1k (L$%.1lf/USD)\n", effectiveRate, invertedRate );
if ( ABS(totalCommission) > 0.01 ) {
// double commissionPercentage = ABS(totalCommission) / (proceeds+ABS(totalCommission));
double commissionPercentage = ABS(totalCommission) / proceeds;
// Round to 1 significant digit
commissionPercentage = rint( commissionPercentage*1000 ) / 10;
printf( " LindeX Fees: USD %.2lf (%.2lf%%)\n", ABS(totalCommission),
commissionPercentage );
}
printf( " Proceeds: USD %.2lf\n", proceeds );

}

void printMarketRates( long rate_l, long quantity ) {
int i;
int len;
double commissionRate = .035; // 3.5%

len = printf( "Buying L$ %ldk at L$ %ld / USD on LindeX\n", quantity, rate_l );
for ( i=0; i<len-1; i++ )
printf( "=" );

printf( "\n\nMarket Rate: (Before Commission)\n" );
printRate( rate_l, 0, 0, quantity );

printf( "\n\nEffective Rate: (USD .30 Transaction Commission)\n" );
printRate( rate_l, 0, -0.30, quantity );

printf( "\n\n" );

len = printf( "Selling L$ %ldk at L$ %ld / USD on LindeX\n", quantity, rate_l );
for ( i=0; i<len-1; i++ )
printf( "=" );

printf( "\n\nMarket Rate: (Before Commission)\n" );
printRate( rate_l, 0, 0, quantity );

printf( "\nEffective Rate: (%.1lf%% trading commission, no withdrawl)\n",
commissionRate*100 );
printRate( rate_l, commissionRate, 0, quantity );

printf( "\nEffective Rate: (%.1lf%% trading commission, Paypal withdrawl)\n",
commissionRate*100 );
printRate( rate_l, commissionRate, 1, quantity );

printf( "\nEffective Rate: (%.1lf%% trading commission, US Cheque withdrawl)\n",
commissionRate*100 );
printRate( rate_l, commissionRate, 10, quantity );

printf( "\nEffective Rate: (%.1lf%% trading commission, International Cheque withdrawl)\n",
commissionRate*100 );
printRate( rate_l, commissionRate, 15, quantity );

printf( "\n" );
}


// Print usage message
void usage( char *name) {
fprintf( stderr, "Usage: %s [exchange rate L$/USD1] [quantity of L$ in K]\n", name );
fprintf( stderr, " Calculate the effective price of buying/selling on LindeX.\n" );
fprintf( stderr, "\n" );
fprintf( stderr, " Exchange Rate is the sell price as found on LindeX\n" );
fprintf( stderr, " (https://secondlife.com/currency/market.php)\n" );
fprintf( stderr, "Sample Usage:\n" );
fprintf( stderr, " %s 250 10\n", name );
}

int main( int argc, char *argv[] ) {
char buffer[BUFSIZE];
long rate, quantity;

// Validate Arguments
if ( argc != 3 ) {
usage( argv[0] );
return 0;
}

rate = strtol( argv[1], NULL, 10 );
sprintf( buffer, "%ld", rate );
if ( rate <=0 || strcmp(buffer,argv[1]) ) {
usage( argv[0] );
return 1;
}

quantity = strtol( argv[2], NULL, 10 );
sprintf( buffer, "%ld", quantity );
if ( quantity <=0 || strcmp(buffer,argv[2]) ) {
usage( argv[0] );
return 1;
}


// Arguments validated, compute price now
printMarketRates( rate, quantity );

return 0;
}

_____________________
--
~If you lived here, you would be home by now~
Angel Fluffy
Very Helpful
Join date: 3 Mar 2006
Posts: 810
11-22-2006 14:14
Interesting script, thanks for posting it!
It does have some interesting math in it, but it doesn't have a way to figure out what the current exchange rate is, which is really what I am after :)
_____________________
Volunteer Portal (FAQs!) : https://wiki.secondlife.com/wiki/Volunteer_Portal

JIRA / Issue Tracker : http://jira.secondlife.com (& http://tinyurl.com/2jropp)
Jacques Groshomme
Registered User
Join date: 16 Mar 2005
Posts: 355
11-22-2006 14:44
As was mentioned in your linked thread, there is no way for LSL to be inately aware of the exchange rate.

The only solution around is to use XMLRPC or HttpRequest to interact with a script running on a web server somewhere that does a "screen scrape" of secondlife.reuters.com. The caveat to that is if Reuters changes the layout or wording of their page significantly, it may bork the whole deal.
Joannah Cramer
Registered User
Join date: 12 Apr 2006
Posts: 1,539
11-22-2006 14:48
From: Angel Fluffy
it doesn't have a way to figure out what the current exchange rate is, which is really what I am after :)

The way it seems to be done is, you have script pull the current exchange rate from external web site, which in turn pulls it from statistics page on LL page.

Sol Columbia has vendor system written around such setup, you might want to talk with her about providing you with customized module for just readout of the exchange rate, or something to this effect.
Angel Fluffy
Very Helpful
Join date: 3 Mar 2006
Posts: 810
11-25-2006 15:20
Hi. I'm half done with the problem... I now have scripts which fetch the current L$/USD exchange rate from a 3rd party website, adjust it so it fits up against the LindeX rate, and writes the result to a very simple web-page that can be queried by a script.

The LSL-side look-up of this value from the website should be relatively simple. I might post an open-source implementation of this soon, in case there is anyone else that wants to set prices in USD and not have to worry about what the LindeX does.
_____________________
Volunteer Portal (FAQs!) : https://wiki.secondlife.com/wiki/Volunteer_Portal

JIRA / Issue Tracker : http://jira.secondlife.com (& http://tinyurl.com/2jropp)
Angel Fluffy
Very Helpful
Join date: 3 Mar 2006
Posts: 810
11-25-2006 18:17
Ok, I have scripted an orb for parcel tier/rent collection. You basically rez it on a parcel, tell it who the parcel belongs to, and whether they are renting or buying, and how big the parcel is. It will then query a remote perl script on a webserver (which is where you keep your pricing info, so you can update it for all orbs at once), which will calculate the current exchange rate and send the orb back a number of L$ that the orb should charge in tier/rental per month.
People can then pay the orb this amount to extend their time on the parcel. When they are paid up, the orb goes small and its text disappears (but they can touch it to see the details).

When they become overdue it sends them regular reminders until they pay - totally automated... and when they pay it increases their time. If they don't pay, it also IMs the sim owner or rental manager saying who they are, where they are, how big the parcel is and how overdue they are.

I'm thinking about releasing it as open source (if I don't do much more work on it) or as a commercial product (if I work on it a bit more so it's polished and stuff). It has some safeguards built into it (e.g. it does NOT require refund permission... it only accepts payments, it cannot pay other people, hence income splitting and refunds are not possible atm).

But yeah, still working on it.

Thanks for the help guys :)
_____________________
Volunteer Portal (FAQs!) : https://wiki.secondlife.com/wiki/Volunteer_Portal

JIRA / Issue Tracker : http://jira.secondlife.com (& http://tinyurl.com/2jropp)