In all honesty, the existing API already does something like this.
lua Code:
local frame=CreateFrame("Frame");
frame:RegisterEvent("MERCHANT_SHOW");
frame:RegisterEvent("MERCHANT_CLOSED");
local money=0;
frame:SetScript("OnEvent",function(self,event,...)
if event=="MERCHANT_SHOW" then
money=GetMoney();
elseif event=="MERCHANT_CLOSED" then
local diff=GetMoney()-money;
if diff~=0 then
print(diff>0 and "You made" or "You spent",GetCoinTextureString(math.abs(diff)));
end
end
end);
You create a UI object known as a Frame. Through this, you can register for certain event callbacks to be received through the Frame's OnEvent handler.